コード例 #1
0
ファイル: RubyFloat.java プロジェクト: komax/jruby
  /** flo_to_s */
  @JRubyMethod(name = "to_s")
  @Override
  public IRubyObject to_s() {
    Ruby runtime = getRuntime();
    if (Double.isInfinite(value))
      return RubyString.newString(runtime, value < 0 ? "-Infinity" : "Infinity");
    if (Double.isNaN(value)) return RubyString.newString(runtime, "NaN");

    ByteList buf = new ByteList();
    // Under 1.9, use full-precision float formatting (JRUBY-4846).
    // Double-precision can represent around 16 decimal digits;
    // we use 20 to ensure full representation.
    Sprintf.sprintf(buf, Locale.US, "%#.20g", this);
    int e = buf.indexOf('e');
    if (e == -1) e = buf.getRealSize();
    ASCIIEncoding ascii = ASCIIEncoding.INSTANCE;

    if (!ascii.isDigit(buf.get(e - 1))) {
      buf.setRealSize(0);
      Sprintf.sprintf(buf, Locale.US, "%#.14e", this);
      e = buf.indexOf('e');
      if (e == -1) e = buf.getRealSize();
    }

    int p = e;
    while (buf.get(p - 1) == '0' && ascii.isDigit(buf.get(p - 2))) p--;
    System.arraycopy(buf.getUnsafeBytes(), e, buf.getUnsafeBytes(), p, buf.getRealSize() - e);
    buf.setRealSize(p + buf.getRealSize() - e);

    buf.setEncoding(USASCIIEncoding.INSTANCE);

    return runtime.newString(buf);
  }
コード例 #2
0
ファイル: RubyStringIO.java プロジェクト: ribrdb/jruby
  private IRubyObject internalGets(ThreadContext context, IRubyObject[] args) {
    Ruby runtime = context.getRuntime();

    if (pos < internal.getByteList().realSize && !eof) {
      boolean isParagraph = false;

      ByteList sep;
      if (args.length > 0) {
        if (args[0].isNil()) {
          ByteList buf =
              internal
                  .getByteList()
                  .makeShared((int) pos, internal.getByteList().realSize - (int) pos);
          pos += buf.realSize;
          return RubyString.newString(runtime, buf);
        }
        sep = args[0].convertToString().getByteList();
        if (sep.realSize == 0) {
          isParagraph = true;
          sep = Stream.PARAGRAPH_SEPARATOR;
        }
      } else {
        sep = ((RubyString) runtime.getGlobalVariables().get("$/")).getByteList();
      }

      ByteList ss = internal.getByteList();

      if (isParagraph) {
        swallowLF(ss);
        if (pos == ss.realSize) {
          return runtime.getNil();
        }
      }

      int ix = ss.indexOf(sep, (int) pos);

      ByteList add;
      if (-1 == ix) {
        ix = internal.getByteList().realSize;
        add = ByteList.EMPTY_BYTELIST;
      } else {
        add = isParagraph ? NEWLINE : sep;
      }

      ByteList line = new ByteList(ix - (int) pos + add.length());
      line.append(internal.getByteList(), (int) pos, ix - (int) pos);
      line.append(add);
      pos = ix + add.realSize;
      lineno++;

      return RubyString.newString(runtime, line);
    }
    return runtime.getNil();
  }
コード例 #3
0
ファイル: RubyZlib.java プロジェクト: headius/jruby-cdc
 private IRubyObject internalSepGets(ByteList sep) throws IOException {
   ByteList result = new ByteList();
   int ce = io.read();
   while (ce != -1 && sep.indexOf(ce) == -1) {
     result.append((byte) ce);
     ce = io.read();
   }
   // io.available() only returns 0 after EOF is encountered
   // so we need to differentiate between the empty string and EOF
   if (0 == result.length() && -1 == ce) {
     return getRuntime().getNil();
   }
   line++;
   result.append(sep);
   return RubyString.newString(getRuntime(), result);
 }
コード例 #4
0
  private IRubyObject internalGets(ThreadContext context, IRubyObject[] args) {
    Ruby runtime = context.runtime;

    if (!isEndOfString() && !data.eof) {
      boolean isParagraph = false;
      boolean is19 = runtime.is1_9();
      ByteList sep = ((RubyString) runtime.getGlobalVariables().get("$/")).getByteList();
      IRubyObject sepArg;
      int limit = -1;

      if (is19) {
        IRubyObject limitArg =
            (args.length > 1
                ? args[1]
                : (args.length > 0 && args[0] instanceof RubyFixnum ? args[0] : null));
        if (limitArg != null) {
          limit = RubyNumeric.fix2int(limitArg);
        }

        sepArg = (args.length > 0 && !(args[0] instanceof RubyFixnum) ? args[0] : null);
      } else {
        sepArg = (args.length > 0 ? args[0] : null);
      }

      if (sepArg != null) {
        if (sepArg.isNil()) {
          int bytesAvailable = data.internal.getByteList().getRealSize() - (int) data.pos;
          int bytesToUse = (limit < 0 || limit >= bytesAvailable ? bytesAvailable : limit);
          ByteList buf = data.internal.getByteList().makeShared((int) data.pos, bytesToUse);
          data.pos += buf.getRealSize();
          return makeString(runtime, buf);
        }

        sep = sepArg.convertToString().getByteList();
        if (sep.getRealSize() == 0) {
          isParagraph = true;
          sep = Stream.PARAGRAPH_SEPARATOR;
        }
      }

      ByteList ss = data.internal.getByteList();

      if (isParagraph) {
        swallowLF(ss);
        if (data.pos == ss.getRealSize()) {
          return runtime.getNil();
        }
      }

      int ix = ss.indexOf(sep, (int) data.pos);

      ByteList add;
      if (-1 == ix) {
        ix = data.internal.getByteList().getRealSize();
        add = ByteList.EMPTY_BYTELIST;
      } else {
        add = sep;
      }

      int bytes = ix - (int) data.pos;
      int bytesToUse = (limit < 0 || limit >= bytes ? bytes : limit);

      int bytesWithSep = ix - (int) data.pos + add.getRealSize();
      int bytesToUseWithSep = (limit < 0 || limit >= bytesWithSep ? bytesWithSep : limit);

      ByteList line = new ByteList(bytesToUseWithSep);
      if (is19) line.setEncoding(data.internal.getByteList().getEncoding());
      line.append(data.internal.getByteList(), (int) data.pos, bytesToUse);
      data.pos += bytesToUse;

      int sepBytesToUse = bytesToUseWithSep - bytesToUse;
      line.append(add, 0, sepBytesToUse);
      data.pos += sepBytesToUse;

      if (sepBytesToUse >= add.getRealSize()) {
        data.lineno++;
      }

      return makeString(runtime, line);
    }
    return runtime.getNil();
  }