Exemple #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);
 }
Exemple #2
0
  @JRubyMethod
  public IRubyObject set_encoding(ThreadContext context, IRubyObject ext_enc) {
    Encoding enc;

    if (ext_enc.isNil()) {
      enc = EncodingUtils.defaultExternalEncoding(context.runtime);
    } else {
      enc = EncodingUtils.rbToEncoding(context, ext_enc);
    }
    if (ptr.string.getEncoding() != enc) {
      ptr.string.modify();
      ptr.string.setEncoding(enc);
    }
    return this;
  }
Exemple #3
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);
  }
  @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;
  }
Exemple #5
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();
  }