private static RubyString filePathConvert(ThreadContext context, RubyString path) { if (!Platform.IS_WINDOWS) { Ruby runtime = context.getRuntime(); EncodingService encodingService = runtime.getEncodingService(); Encoding pathEncoding = path.getEncoding(); // If we are not ascii and do not match fs encoding then transcode to fs. if (runtime.getDefaultInternalEncoding() != null && pathEncoding != encodingService.getUSAsciiEncoding() && pathEncoding != encodingService.getAscii8bitEncoding() && pathEncoding != encodingService.getFileSystemEncoding() && !path.isAsciiOnly()) { path = EncodingUtils.strConvEnc(context, path, pathEncoding, encodingService.getFileSystemEncoding()); } } return path; }
private static RubyString filePathConvert(ThreadContext context, RubyString path) { if (!Platform.IS_WINDOWS) { Ruby runtime = context.getRuntime(); EncodingService encodingService = runtime.getEncodingService(); Encoding pathEncoding = path.getEncoding(); // If we are not ascii and do not match fs encoding then transcode to fs. if (runtime.getDefaultInternalEncoding() != null && pathEncoding != encodingService.getUSAsciiEncoding() && pathEncoding != encodingService.getAscii8bitEncoding() && pathEncoding != encodingService.getFileSystemEncoding() && !path.isAsciiOnly()) { path = EncodingUtils.strConvEnc(context, path, pathEncoding, encodingService.getFileSystemEncoding()); } } return path; }
private static RubyString normalizeEnvString(ThreadContext context, RubyString key, RubyString value) { final Ruby runtime = context.runtime; RubyString valueStr; // Ensure PATH is encoded like filesystem if (Platform.IS_WINDOWS ? equalIgnoreCase(context, key, RubyString.newString(context.runtime, PATH_BYTES)) : key.getByteList().equal(PATH_BYTES)) { Encoding enc = runtime.getEncodingService().getFileSystemEncoding(); valueStr = EncodingUtils.strConvEnc(context, value, value.getEncoding(), enc); if (value == valueStr) valueStr = (RubyString) value.dup(); } else { valueStr = RubyString.newString(runtime, value.toString(), runtime.getEncodingService().getLocaleEncoding()); } valueStr.setFrozen(true); return valueStr; }
private static RubyString normalizeEnvString(ThreadContext context, RubyString key, RubyString value) { final Ruby runtime = context.runtime; RubyString valueStr; // Ensure PATH is encoded like filesystem if (Platform.IS_WINDOWS ? equalIgnoreCase(context, key, RubyString.newString(context.runtime, PATH_BYTES)) : key.getByteList().equal(PATH_BYTES)) { Encoding enc = runtime.getEncodingService().getFileSystemEncoding(); valueStr = EncodingUtils.strConvEnc(context, value, value.getEncoding(), enc); if (value == valueStr) valueStr = (RubyString) value.dup(); } else { valueStr = RubyString.newString(runtime, value.toString(), runtime.getEncodingService().getLocaleEncoding()); } valueStr.setFrozen(true); return valueStr; }
/** * Decode a given ByteList to a Java string. * * @param runtime the current runtime * @param value the bytelist * @return a Java String representation of the ByteList */ public static String decodeByteList(Ruby runtime, ByteList value) { byte[] unsafeBytes = value.getUnsafeBytes(); int begin = value.getBegin(); int length = value.length(); Encoding encoding = value.getEncoding(); if (encoding == UTF8Encoding.INSTANCE) { return RubyEncoding.decodeUTF8(unsafeBytes, begin, length); } Charset charset = runtime.getEncodingService().charsetForEncoding(encoding); if (charset == null) { // No JDK Charset available for this encoding; convert to UTF-16 ourselves. Encoding utf16 = EncodingUtils.getUTF16ForPlatform(); return EncodingUtils.strConvEnc(runtime.getCurrentContext(), value, value.getEncoding(), utf16).toString(); } return RubyEncoding.decode(unsafeBytes, begin, length, charset); }
/** * Decode a given ByteList to a Java string. * * @param runtime the current runtime * @param value the bytelist * @return a Java String representation of the ByteList */ public static String decodeByteList(Ruby runtime, ByteList value) { byte[] unsafeBytes = value.getUnsafeBytes(); int begin = value.getBegin(); int length = value.length(); Encoding encoding = value.getEncoding(); if (encoding == UTF8Encoding.INSTANCE) { return RubyEncoding.decodeUTF8(unsafeBytes, begin, length); } Charset charset = runtime.getEncodingService().charsetForEncoding(encoding); if (charset == null) { // No JDK Charset available for this encoding; convert to UTF-16 ourselves. Encoding utf16 = EncodingUtils.getUTF16ForPlatform(); return EncodingUtils.strConvEnc(runtime.getCurrentContext(), value, value.getEncoding(), utf16).toString(); } return RubyEncoding.decode(unsafeBytes, begin, length, charset); }
str = EncodingUtils.strConvEnc(context, str, encStr, enc);
str = EncodingUtils.strConvEnc(context, str, encStr, enc);
@JRubyMethod(name = "ungetc") public IRubyObject ungetc(ThreadContext context, IRubyObject arg) { Encoding enc, enc2; checkModifiable(); checkReadable(); if (arg.isNil()) return arg; if (arg instanceof RubyInteger) { int len, cc = RubyNumeric.num2int(arg); byte[] buf = new byte[16]; enc = getEncoding(); len = enc.codeToMbcLength(cc); if (len <= 0) EncodingUtils.encUintChr(context, cc, enc); enc.codeToMbc(cc, buf, 0); ungetbyteCommon(buf, 0, len); return context.nil; } else { arg = arg.convertToString(); enc = getEncoding(); RubyString argStr = (RubyString) arg; enc2 = argStr.getEncoding(); if (enc != enc2 && enc != ASCIIEncoding.INSTANCE) { argStr = EncodingUtils.strConvEnc(context, argStr, enc2, enc); } ByteList argBytes = argStr.getByteList(); ungetbyteCommon(argBytes.unsafeBytes(), argBytes.begin(), argBytes.realSize()); return context.nil; } }
@JRubyMethod(name = "ungetc") public IRubyObject ungetc(ThreadContext context, IRubyObject arg) { Encoding enc, enc2; checkModifiable(); checkReadable(); if (arg.isNil()) return arg; if (arg instanceof RubyInteger) { int len, cc = RubyNumeric.num2int(arg); byte[] buf = new byte[16]; enc = getEncoding(); len = enc.codeToMbcLength(cc); if (len <= 0) EncodingUtils.encUintChr(context, cc, enc); enc.codeToMbc(cc, buf, 0); ungetbyteCommon(buf, 0, len); return context.nil; } else { arg = arg.convertToString(); enc = getEncoding(); RubyString argStr = (RubyString) arg; enc2 = argStr.getEncoding(); if (enc != enc2 && enc != ASCIIEncoding.INSTANCE) { argStr = EncodingUtils.strConvEnc(context, argStr, enc2, enc); } ByteList argBytes = argStr.getByteList(); ungetbyteCommon(argBytes.unsafeBytes(), argBytes.begin(), argBytes.realSize()); return context.nil; } }