示例#1
0
  @SuppressWarnings("unchecked")
  @JRubyMethod(name = "names")
  public IRubyObject names(ThreadContext context) {
    Ruby runtime = context.runtime;
    EncodingService service = runtime.getEncodingService();
    Entry entry = service.findEncodingOrAliasEntry(name);

    RubyArray result = runtime.newArray();
    HashEntryIterator i;
    i = service.getEncodings().entryIterator();
    while (i.hasNext()) {
      CaseInsensitiveBytesHash.CaseInsensitiveBytesHashEntry<Entry> e =
          ((CaseInsensitiveBytesHash.CaseInsensitiveBytesHashEntry<Entry>) i.next());
      if (e.value == entry) {
        result.append(
            RubyString.newUsAsciiStringShared(runtime, e.bytes, e.p, e.end - e.p).freeze(context));
      }
    }
    i = service.getAliases().entryIterator();
    while (i.hasNext()) {
      CaseInsensitiveBytesHash.CaseInsensitiveBytesHashEntry<Entry> e =
          ((CaseInsensitiveBytesHash.CaseInsensitiveBytesHashEntry<Entry>) i.next());
      if (e.value == entry) {
        result.append(
            RubyString.newUsAsciiStringShared(runtime, e.bytes, e.p, e.end - e.p).freeze(context));
      }
    }
    result.append(runtime.newString(EXTERNAL));
    result.append(runtime.newString(LOCALE));

    return result;
  }
示例#2
0
  @SuppressWarnings("unchecked")
  @JRubyMethod(name = "aliases", meta = true)
  public static IRubyObject aliases(ThreadContext context, IRubyObject recv) {
    Ruby runtime = context.runtime;
    EncodingService service = runtime.getEncodingService();

    IRubyObject list[] = service.getEncodingList();
    HashEntryIterator i = service.getAliases().entryIterator();
    RubyHash result = RubyHash.newHash(runtime);

    while (i.hasNext()) {
      CaseInsensitiveBytesHash.CaseInsensitiveBytesHashEntry<Entry> e =
          ((CaseInsensitiveBytesHash.CaseInsensitiveBytesHashEntry<Entry>) i.next());
      IRubyObject alias =
          RubyString.newUsAsciiStringShared(runtime, e.bytes, e.p, e.end - e.p).freeze(context);
      IRubyObject name =
          RubyString.newUsAsciiStringShared(runtime, ((RubyEncoding) list[e.value.getIndex()]).name)
              .freeze(context);
      result.fastASet(alias, name);
    }

    result.fastASet(
        runtime.newString(EXTERNAL),
        runtime.newString(new ByteList(runtime.getDefaultExternalEncoding().getName())));
    result.fastASet(
        runtime.newString(LOCALE),
        runtime.newString(new ByteList(service.getLocaleEncoding().getName())));

    return result;
  }
示例#3
0
  @SuppressWarnings("unchecked")
  @JRubyMethod(name = "name_list", meta = true)
  public static IRubyObject name_list(ThreadContext context, IRubyObject recv) {
    Ruby runtime = context.runtime;
    EncodingService service = runtime.getEncodingService();

    RubyArray result =
        runtime.newArray(service.getEncodings().size() + service.getAliases().size());
    HashEntryIterator i;
    i = service.getEncodings().entryIterator();
    while (i.hasNext()) {
      CaseInsensitiveBytesHash.CaseInsensitiveBytesHashEntry<Entry> e =
          ((CaseInsensitiveBytesHash.CaseInsensitiveBytesHashEntry<Entry>) i.next());
      result.append(
          RubyString.newUsAsciiStringShared(runtime, e.bytes, e.p, e.end - e.p).freeze(context));
    }
    i = service.getAliases().entryIterator();
    while (i.hasNext()) {
      CaseInsensitiveBytesHash.CaseInsensitiveBytesHashEntry<Entry> e =
          ((CaseInsensitiveBytesHash.CaseInsensitiveBytesHashEntry<Entry>) i.next());
      result.append(
          RubyString.newUsAsciiStringShared(runtime, e.bytes, e.p, e.end - e.p).freeze(context));
    }

    result.append(runtime.newString(EXTERNAL));
    result.append(runtime.newString(LOCALE));

    return result;
  }
示例#4
0
  @JRubyMethod(name = "locale_charmap", meta = true)
  public static IRubyObject locale_charmap(ThreadContext context, IRubyObject recv) {
    Ruby runtime = context.runtime;
    EncodingService service = runtime.getEncodingService();
    ByteList name = new ByteList(service.getLocaleEncoding().getName());

    return RubyString.newUsAsciiStringNoCopy(runtime, name);
  }
示例#5
0
 @JRubyMethod(name = "default_internal=", required = 1, meta = true, compat = RUBY1_9)
 public static IRubyObject setDefaultInternal(IRubyObject recv, IRubyObject encoding) {
   Ruby runtime = recv.getRuntime();
   EncodingService service = runtime.getEncodingService();
   if (encoding.isNil()) {
     recv.getRuntime().newArgumentError("default_internal can not be nil");
   }
   recv.getRuntime().setDefaultInternalEncoding(service.getEncodingFromObject(encoding));
   return encoding;
 }
示例#6
0
 @JRubyMethod
 public IRubyObject convpath(ThreadContext context) {
   Ruby runtime = context.runtime;
   EncodingService encodingService = runtime.getEncodingService();
   // we always pass through UTF-16
   IRubyObject utf16Encoding = encodingService.getEncodingList()[UTF16.getIndex()];
   return RubyArray.newArray(
       runtime,
       RubyArray.newArray(runtime, source_encoding(context), utf16Encoding),
       RubyArray.newArray(runtime, utf16Encoding, destination_encoding(context)));
 }
示例#7
0
  @JRubyMethod(visibility = PRIVATE)
  public IRubyObject initialize(
      ThreadContext context, IRubyObject src, IRubyObject dest, IRubyObject _opt) {
    Ruby runtime = context.runtime;
    EncodingService encodingService = runtime.getEncodingService();

    // both may be null
    Encoding srcEncoding = encodingService.getEncodingFromObjectNoError(src);
    Encoding destEncoding = encodingService.getEncodingFromObjectNoError(dest);

    int flags = 0;
    IRubyObject replace = context.nil;

    if (srcEncoding == destEncoding && srcEncoding != null) {
      throw runtime.newConverterNotFoundError(
          "code converter not found (" + srcEncoding + " to " + destEncoding + ")");
    }

    // Ensure we'll be able to get charsets fo these encodings
    try {
      if (srcEncoding != destEncoding) {
        if (srcEncoding != null) encodingService.charsetForEncoding(srcEncoding);
        if (destEncoding != null) encodingService.charsetForEncoding(destEncoding);
      }
    } catch (RaiseException e) {
      if (e.getException().getMetaClass().getBaseName().equals("CompatibilityError")) {
        throw runtime.newConverterNotFoundError(
            "code converter not found (" + srcEncoding + " to " + destEncoding + ")");
      } else {
        throw e;
      }
    }

    if (!_opt.isNil()) {
      if (_opt instanceof RubyHash) {
        RubyHash opt = (RubyHash) _opt;
        flags |= EncodingUtils.econvPrepareOpts(context, opt, new IRubyObject[] {opt});

        IRubyObject value = opt.fastARef(runtime.newSymbol("replace"));
        if (value != null) {
          replace = value;
        }
      } else {
        flags = (int) _opt.convertToInteger().getLongValue();
        replace = context.nil;
      }
    }

    transcoder = new CharsetTranscoder(context, destEncoding, srcEncoding, flags, replace);

    return context.runtime.getNil();
  }
示例#8
0
 public Encoding toEncoding(Ruby runtime) {
   EncodingService service = runtime.getEncodingService();
   switch (this) {
     case LOCALE:
       return service.getLocaleEncoding();
     case EXTERNAL:
       return runtime.getDefaultExternalEncoding();
     case INTERNAL:
       return runtime.getDefaultInternalEncoding();
     case FILESYSTEM:
       // This needs to do something different on Windows. See encoding.c,
       // in the enc_set_filesystem_encoding function.
       return runtime.getDefaultExternalEncoding();
     default:
       throw new RuntimeException("invalid SpecialEncoding: " + this);
   }
 }
示例#9
0
  @JRubyMethod(compat = RUBY1_9, meta = true)
  public static IRubyObject asciicompat_encoding(
      ThreadContext context, IRubyObject self, IRubyObject strOrEnc) {
    Ruby runtime = context.runtime;
    EncodingService encodingService = runtime.getEncodingService();

    Encoding encoding = encodingService.getEncodingFromObjectNoError(strOrEnc);

    if (encoding == null) {
      return context.nil;
    }

    if (encoding.isAsciiCompatible()) {
      return context.nil;
    }

    Encoding asciiCompat = NONASCII_TO_ASCII.get(encoding);

    if (asciiCompat == null) {
      throw runtime.newConverterNotFoundError("no ASCII compatible encoding found for " + strOrEnc);
    }

    return encodingService.convertEncodingToRubyEncoding(asciiCompat);
  }