示例#1
0
 public void testStrConvEncThatGrows() throws Exception {
   String javaStr = "--- こんにちは!";
   RubyString rubyStr = RubyString.newString(runtime, javaStr);
   rubyStr =
       EncodingUtils.strConvEnc(
           runtime.getCurrentContext(), rubyStr, rubyStr.getEncoding(), SJISEncoding.INSTANCE);
   assertEquals(rubyStr.getEncoding(), SJISEncoding.INSTANCE);
   rubyStr =
       EncodingUtils.strConvEnc(
           runtime.getCurrentContext(), rubyStr, SJISEncoding.INSTANCE, UTF8Encoding.INSTANCE);
   assertEquals(rubyStr.getEncoding(), UTF8Encoding.INSTANCE);
 }
示例#2
0
 @JRubyMethod(name = "ruby=")
 public IRubyObject ruby_set(final ThreadContext ctx, IRubyObject arg) {
   RubyString string;
   try {
     string = arg.convertToString();
   } catch (RaiseException re) {
     throw newTypeError(ctx.runtime, arg.getMetaClass(), "String");
   }
   if (string.getEncoding() == UTF8Encoding.INSTANCE
       || string.getEncoding() == USASCIIEncoding.INSTANCE) {
     ByteList bytes = string.getByteList();
     value.set(bytes.getUnsafeBytes(), bytes.getBegin(), bytes.getRealSize());
   } else {
     value.set(string.toString());
   }
   return arg;
 }
示例#3
0
  private RubyString strioSubstr(Ruby runtime, int pos, int len) {
    RubyString str = ptr.string;
    ByteList strByteList = str.getByteList();
    byte[] strBytes = strByteList.getUnsafeBytes();
    Encoding enc = str.getEncoding();
    int rlen = str.size() - pos;

    if (len > rlen) len = rlen;
    if (len < 0) len = 0;

    if (len == 0) return RubyString.newEmptyString(runtime);
    return RubyString.newStringShared(runtime, strBytes, strByteList.getBegin() + pos, len, enc);
  }
示例#4
0
  @JRubyMethod(
      name = {"write"},
      required = 1)
  public IRubyObject write(ThreadContext context, IRubyObject arg) {
    checkWritable();

    Ruby runtime = context.runtime;

    RubyString str = arg.asString();
    int len, olen;
    Encoding enc, enc2;

    enc = ptr.string.getEncoding();
    enc2 = str.getEncoding();
    if (enc != enc2
        && enc != EncodingUtils.ascii8bitEncoding(runtime)
        // this is a hack because we don't seem to handle incoming ASCII-8BIT properly in transcoder
        && enc2 != ASCIIEncoding.INSTANCE) {
      str = runtime.newString(Transcoder.strConvEnc(context, str.getByteList(), enc2, enc));
    }
    len = str.size();
    if (len == 0) return RubyFixnum.zero(runtime);
    checkModifiable();
    olen = ptr.string.size();
    if ((ptr.flags & OpenFile.APPEND) != 0) {
      ptr.pos = olen;
    }
    if (ptr.pos == olen
        // this is a hack because we don't seem to handle incoming ASCII-8BIT properly in transcoder
        && enc2 != ASCIIEncoding.INSTANCE) {
      EncodingUtils.encStrBufCat(runtime, ptr.string, str.getByteList(), enc);
    } else {
      strioExtend(ptr.pos, len);
      ByteList ptrByteList = ptr.string.getByteList();
      System.arraycopy(
          str.getByteList().getUnsafeBytes(),
          str.getByteList().getBegin(),
          ptrByteList.getUnsafeBytes(),
          ptrByteList.begin + ptr.pos,
          len);
      ptr.string.infectBy(str);
    }
    ptr.string.infectBy(this);
    ptr.pos += len;
    return RubyFixnum.newFixnum(runtime, len);
  }
  @JRubyMethod(name = "ungetc", compat = CompatVersion.RUBY1_9)
  @Override
  public IRubyObject ungetc19(ThreadContext context, IRubyObject arg) {
    checkReadable();

    if (!arg.isNil()) {
      int c;
      if (arg instanceof RubyFixnum) {
        c = RubyNumeric.fix2int(arg);
      } else {
        RubyString str = arg.convertToString();
        c = str.getEncoding().mbcToCode(str.getBytes(), 0, 1);
      }

      ungetcCommon(c);
    }

    return getRuntime().getNil();
  }
  @Override
  protected IRubyObject inspectAry(ThreadContext context) {
    if (!packed()) return super.inspectAry(context);

    final Ruby runtime = context.runtime;
    RubyString str =
        RubyString.newStringLight(runtime, DEFAULT_INSPECT_STR_SIZE, USASCIIEncoding.INSTANCE);
    EncodingUtils.strBufCat(runtime, str, OPEN_BRACKET);
    boolean tainted = isTaint();

    RubyString s = inspect(context, value);
    if (s.isTaint()) tainted = true;
    else str.setEncoding(s.getEncoding());
    str.cat19(s);

    EncodingUtils.strBufCat(runtime, str, CLOSE_BRACKET);

    if (tainted) str.setTaint(true);

    return str;
  }