private static RubyString concatStrings(final Ruby runtime, String s1, CharSequence s2, String s3, Encoding enc) { StringBuilder str = new StringBuilder(s1.length() + s2.length() + s3.length()).append(s1).append(s2).append(s3); return new RubyString(runtime, runtime.getString(), str, enc); }
public static RubyString newStringShared(Ruby runtime, RubyString orig) { orig.shareLevel = SHARE_LEVEL_BYTELIST; RubyString str = new RubyString(runtime, runtime.getString(), orig.value); str.shareLevel = SHARE_LEVEL_BYTELIST; return str; }
public static RubyString newStringShared(Ruby runtime, ByteList bytes, int codeRange) { RubyString str = new RubyString(runtime, runtime.getString(), bytes, codeRange); str.shareLevel = SHARE_LEVEL_BYTELIST; return str; }
private static RubyString newStringShared(Ruby runtime, ByteList bytes, Encoding encoding, int cr) { RubyString str = newStringShared(runtime, runtime.getString(), bytes, encoding); str.flags |= cr; return str; }
public static RubyString newStringShared(Ruby runtime, byte[] bytes, int start, int length, Encoding encoding) { ByteList byteList = new ByteList(bytes, start, length, encoding, false); RubyString str = new RubyString(runtime, runtime.getString(), byteList); str.shareLevel = SHARE_LEVEL_BUFFER; return str; }
/** * Tries to convert this object to a Ruby String using the * "to_str" method. */ public RubyString convertToString() { return (RubyString) TypeConverter.convertToType(this, getRuntime().getString(), "to_str"); }
@JRubyMethod(name = "String", required = 1, module = true, visibility = PRIVATE) public static IRubyObject new_string(ThreadContext context, IRubyObject recv, IRubyObject object) { Ruby runtime = context.runtime; KernelSites sites = sites(context); IRubyObject tmp = TypeConverter.checkStringType(context, sites.to_str_checked, object, runtime.getString()); if (tmp == context.nil) { tmp = TypeConverter.convertToType(context, object, runtime.getString(), sites(context).to_s_checked); } return tmp; }
DefaultComparator(ThreadContext context, final boolean honorOverride) { this.context = context; if ( honorOverride && context != null ) { this.fixnumBypass = !honorOverride || context.runtime.getFixnum().isMethodBuiltin("<=>"); this.stringBypass = !honorOverride || context.runtime.getString().isMethodBuiltin("<=>"); } else { // no-opt this.fixnumBypass = false; this.stringBypass = false; } }
private static RubyString doJoin(ThreadContext context, IRubyObject recv, IRubyObject[] args) { final Ruby runtime = context.runtime; final String separator = runtime.getFile().getConstant("SEPARATOR").toString(); final RubyArray argsAry = RubyArray.newArrayMayCopy(runtime, args); final StringBuilder buffer = new StringBuilder(24); boolean isTainted = joinImpl(buffer, separator, context, recv, argsAry); RubyString fixedStr = new RubyString(runtime, runtime.getString(), buffer); fixedStr.setTaint(isTainted); return fixedStr; }
@JRubyMethod public static RubyFixnum unseeded_hash(ThreadContext context, IRubyObject recv) { final Ruby runtime = context.runtime; if (!(recv instanceof RubyString)) { throw runtime.newTypeError(recv, runtime.getString()); } return runtime.newFixnum(((RubyString) recv).unseededStrHashCode(runtime)); }
/** * Tries to convert this object to a Ruby String using the "to_str" method. * @return a string representation of this */ @Override public RubyString convertToString() { Ruby runtime = getRuntime(); ThreadContext context = runtime.getCurrentContext(); BasicObjectSites sites = sites(context); return (RubyString) TypeConverter.convertToType(context, this, runtime.getString(), sites.to_str_checked); }
private static void printExceptionSummary(Ruby runtime, RubyException rEx) { RubyStackTraceElement[] elements = rEx.getBacktraceElements(); RubyStackTraceElement firstElement = elements.length > 0 ? elements[0] : new RubyStackTraceElement("", "", "(empty)", 0, false); String msg = String.format("Exception `%s' at %s:%s - %s\n", rEx.getMetaClass(), firstElement.getFileName(), firstElement.getLineNumber(), TypeConverter.convertToType(rEx, runtime.getString(), "to_s")); runtime.getErrorStream().print(msg); }
@JRubyMethod public static IRubyObject unseeded_hash(ThreadContext context, IRubyObject recv) { Ruby runtime = context.runtime; if (!(recv instanceof RubyString)) { throw runtime.newTypeError(recv, runtime.getString()); } return runtime.newFixnum(((RubyString)recv).unseededStrHashCode(runtime)); } }
public static IRubyObject checkStringType(IRubyObject self) { IRubyObject str = TypeConverter.convertToTypeWithCheck(self, getRuntime(self).getString(), "to_str"); if(!str.isNil() && !(str instanceof RubyString)) { str = RubyString.newEmptyString(getRuntime(self)); } return str; }
@JRubyMethod(module = true, visibility = Visibility.PRIVATE) public static IRubyObject ruby_string_to_bytes(IRubyObject recv, IRubyObject string) { Ruby runtime = recv.getRuntime(); if (!(string instanceof RubyString)) { throw runtime.newTypeError(string, runtime.getString()); } return JavaUtil.convertJavaToUsableRubyObject(runtime, ((RubyString)string).getBytes()); }
/** short circuit for String key comparison * */ @Override public final boolean eql(IRubyObject other) { RubyClass metaclass = getMetaClass(); Ruby runtime = metaclass.getClassRuntime(); if (metaclass != runtime.getString() || metaclass != other.getMetaClass()) return super.eql(other); return eql19(other); }
@JRubyMethod public static IRubyObject warn(ThreadContext context, IRubyObject recv, IRubyObject arg) { Ruby runtime = context.runtime; if (!runtime.warningsEnabled()) return context.nil; TypeConverter.checkType(context, arg, runtime.getString()); RubyString str = (RubyString) arg; if (!str.getEncoding().isAsciiCompatible()) { throw runtime.newEncodingCompatibilityError("ASCII incompatible encoding: " + str.getEncoding()); } writeWarningToError(runtime.getCurrentContext(), str); return context.nil; }