case BYTES: Utils.validateStringEncoding(context, fieldDescriptor.getType(), value); val = ByteString.copyFrom(((RubyString) value).getBytes()); break; case STRING: Utils.validateStringEncoding(context, fieldDescriptor.getType(), value); val = ((RubyString) value).asJavaString(); break; case MESSAGE:
/** * A type specific conversion routine. * * @param rbObject object to convert * @return analogous Pig type */ public static String rubyToPig(RubyString rbObject) { return rbObject.toString(); }
/** inspect_hash * */ private IRubyObject inspectHash(final ThreadContext context) { final RubyString str = RubyString.newStringLight(context.runtime, DEFAULT_INSPECT_STR_SIZE, USASCIIEncoding.INSTANCE); str.infectBy(this); str.cat((byte)'{'); visitAll(context, InspectVisitor, str); str.cat((byte)'}'); return str; }
@JRubyMethod(name = "method_missing", rest = true) public IRubyObject methodMissing(ThreadContext context, IRubyObject[] args) { if (args.length == 1) { RubyDescriptor rubyDescriptor = (RubyDescriptor) getDescriptor(context, metaClass); IRubyObject oneofDescriptor = rubyDescriptor.lookupOneof(context, args[0]); if (oneofDescriptor.isNil()) { if (!hasField(args[0])) { return Helpers.invokeSuper(context, this, metaClass, "method_missing", args, Block.NULL_BLOCK); } return index(context, args[0]); } RubyOneofDescriptor rubyOneofDescriptor = (RubyOneofDescriptor) oneofDescriptor; Descriptors.FieldDescriptor fieldDescriptor = oneofCases.get(rubyOneofDescriptor.getOneofDescriptor()); if (fieldDescriptor == null) return context.runtime.getNil(); return context.runtime.newSymbol(fieldDescriptor.getName()); } else { // fieldName is RubySymbol RubyString field = args[0].asString(); RubyString equalSign = context.runtime.newString(Utils.EQUAL_SIGN); if (field.end_with_p(context, equalSign).isTrue()) { field.chomp_bang(context, equalSign); } if (!hasField(field)) { return Helpers.invokeSuper(context, this, metaClass, "method_missing", args, Block.NULL_BLOCK); } return indexSet(context, field, args[1]); } }
private IRubyObject inspect2() { final Ruby runtime = getRuntime(); if (pos >= str.getByteList().getRealSize()) return RubyString.newEmptyString(runtime); int len = str.getByteList().getRealSize() - pos; if (len > INSPECT_LENGTH) { return ((RubyString) str.substr(runtime, pos, INSPECT_LENGTH)).cat(DOT_BYTES).inspect(); } return str.substr(runtime, pos, len).inspect(); }
@JRubyMethod(name = "bubblebabble", required = 1, optional = 1, meta = true) public static IRubyObject bubblebabble(ThreadContext context, IRubyObject recv, IRubyObject[] args, Block unusedBlock) { byte[] digest = recv.callMethod(context, "digest", args, Block.NULL_BLOCK).convertToString().getBytes(); return RubyString.newString(recv.getRuntime(), BubbleBabble.bubblebabble(digest, 0, digest.length)); }
@JRubyMethod(name = "rest") public IRubyObject rest(ThreadContext context) { check(); ByteList value = str.getByteList(); if (pos >= value.getRealSize()) { return RubyString.newEmptyString(context.runtime).infectBy(str); } return extractRange(context.runtime, pos, value.getRealSize()); }
private String gemClasspath() { RubyString gemLocationRubyObject = (RubyString) evaler.eval(runtime, "Gem.loaded_specs['asciidoctor'].full_gem_path"); String gemLocation = gemLocationRubyObject.asJavaString(); return gemLocation.substring(gemLocation.indexOf("gems"), gemLocation.length()); } }
/** nurat_to_s * */ @JRubyMethod(name = "to_s") public IRubyObject to_s(ThreadContext context) { RubyString str = RubyString.newEmptyString(context.getRuntime()); str.append(f_to_s(context, num)); str.cat((byte)'/'); str.append(f_to_s(context, den)); return str; }
@JRubyMethod(meta = true) public static RubyBigDecimal _load(ThreadContext context, IRubyObject recv, IRubyObject from) { String precisionAndValue = from.convertToString().asJavaString(); String value = precisionAndValue.substring(precisionAndValue.indexOf(':') + 1); return newInstance(context, recv, RubyString.newString(context.runtime, value)); }
private RubyString inspectRecurse(final Ruby runtime) { RubyString str = RubyString.newStringLight(runtime, 20, USASCIIEncoding.INSTANCE); inspectPrefix(str, getMetaClass()); str.cat('{').cat(RECURSIVE_BYTES).cat('}').cat('>'); // "#<Set: {...}>" return str; }
@JRubyMethod(meta = true) public static IRubyObject decode(ThreadContext context, IRubyObject recv, IRubyObject data) { byte[] bin = data.convertToString().getBytes(); RubyMessage ret = (RubyMessage) ((RubyClass) recv).newInstance(context, Block.NULL_BLOCK); try { ret.builder.mergeFrom(bin); } catch (InvalidProtocolBufferException e) { throw context.runtime.newRuntimeError(e.getMessage()); } return ret; }
private IRubyObject inspect1() { final Ruby runtime = getRuntime(); if (pos == 0) return RubyString.newEmptyString(runtime); if (pos > INSPECT_LENGTH) { return RubyString.newStringNoCopy(runtime, DOT_BYTES). append(str.substr(runtime, pos - INSPECT_LENGTH, INSPECT_LENGTH)).inspect(); } return str.substr(runtime, 0, pos).inspect(); }
@Override @JRubyMethod(name = "to_s", optional = 1) public IRubyObject to_s(ThreadContext context, IRubyObject[] args) { return RubyString.newString(context.runtime, name); }
@JRubyMethod(name = {"succ", "next"}) public IRubyObject succ(ThreadContext context) { Ruby runtime = context.runtime; final RubyString str; if (value.getRealSize() > 0) { str = new RubyString(runtime, getMetaClass(), StringSupport.succCommon(runtime, value)); // TODO: rescan code range ? } else { str = newEmptyString(runtime, getType(), value.getEncoding()); } return str.infectBy(this); }
@JRubyMethod(name = "chop") public IRubyObject chop(ThreadContext context) { Ruby runtime = context.runtime; if (value.getRealSize() == 0) return newEmptyString(runtime, getMetaClass(), value.getEncoding()).infectBy(this); return makeShared(runtime, 0, StringSupport.choppedLength(this)); }
@Deprecated public static Object coerceStringToType(RubyString string, Class target) { try { ByteList bytes = string.getByteList(); // 1.9 support for encodings // TODO: Fix charset use for JRUBY-4553 return new String(bytes.getUnsafeBytes(), bytes.begin(), bytes.length(), string.getEncoding().toString()); } catch (UnsupportedEncodingException uee) { return string.toString(); } }
public IRubyObject parse(ThreadContext context, final String format, final RubyString text) { final List<StrptimeToken> compiledPattern = context.runtime.getCachedStrptimePattern(format); final StrptimeParser.FormatBag bag = new StrptimeParser().parse(compiledPattern, text.asJavaString()); return bag == null ? context.nil : convertFormatBagToHash(context, bag, text.getEncoding(), text.isTaint()); }