Exemple #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;
  }
Exemple #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;
  }
Exemple #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;
  }
Exemple #4
0
  @JRubyMethod(name = "zone")
  public RubyString zone() {
    Ruby runtime = getRuntime();
    String envTZ = getEnvTimeZone(runtime).toString();
    // see declaration of SHORT_TZNAME
    if (SHORT_STD_TZNAME.containsKey(envTZ)
        && !dt.getZone().toTimeZone().inDaylightTime(dt.toDate())) {
      return runtime.newString(SHORT_STD_TZNAME.get(envTZ));
    }

    if (SHORT_DL_TZNAME.containsKey(envTZ)
        && dt.getZone().toTimeZone().inDaylightTime(dt.toDate())) {
      return runtime.newString(SHORT_DL_TZNAME.get(envTZ));
    }

    String zone = dt.getZone().getShortName(dt.getMillis());

    Matcher offsetMatcher = TIME_OFFSET_PATTERN.matcher(zone);

    if (offsetMatcher.matches()) {
      boolean minus_p = offsetMatcher.group(1).toString().equals("-");
      int hourOffset = Integer.valueOf(offsetMatcher.group(2));

      if (zone.equals("+00:00")) {
        zone = "GMT";
      } else {
        // try non-localized time zone name
        zone = dt.getZone().getNameKey(dt.getMillis());
        if (zone == null) {
          char sign = minus_p ? '+' : '-';
          zone = "GMT" + sign + hourOffset;
        }
      }
    }

    return runtime.newString(zone);
  }
Exemple #5
0
  @JRubyMethod(name = "inspect")
  @Override
  public IRubyObject inspect() {
    if (str == null) return anyToString();

    Ruby runtime = getRuntime();
    RubyString result = runtime.newString();
    result.cat((byte) '#').cat((byte) '<');
    result.append(getMetaClass().getRealClass().to_s());

    NameEntry[] names = new NameEntry[regs == null ? 1 : regs.numRegs];

    if (pattern.numberOfNames() > 0) {
      for (Iterator<NameEntry> i = pattern.namedBackrefIterator(); i.hasNext(); ) {
        NameEntry e = i.next();
        for (int num : e.getBackRefs()) names[num] = e;
      }
    }

    for (int i = 0; i < names.length; i++) {
      result.cat((byte) ' ');
      if (i > 0) {
        NameEntry e = names[i];
        if (e != null) {
          result.cat(e.name, e.nameP, e.nameEnd - e.nameP);
        } else {
          result.cat((byte) ('0' + i));
        }
        result.cat((byte) ':');
      }
      IRubyObject v = RubyRegexp.nth_match(i, this);
      if (v.isNil()) {
        result.cat("nil".getBytes());
      } else {
        result.append(((RubyString) v).inspectCommon(runtime.is1_9()));
      }
    }

    return result.cat((byte) '>');
  }
Exemple #6
0
 private static IRubyObject getEnvTimeZone(Ruby runtime) {
   RubyString tzVar = runtime.newString(TZ_STRING);
   RubyHash h = ((RubyHash) runtime.getObject().getConstant("ENV"));
   IRubyObject tz = h.op_aref(runtime.getCurrentContext(), tzVar);
   return tz;
 }