Package org.jcodings

Examples of org.jcodings.Encoding


        }
        return runtime.getNil();
    }
   
    private IRubyObject singleByteStrip19(Ruby runtime) {
        Encoding enc = value.getEncoding();
        int s = value.getBegin();
        int end = s + value.getRealSize();
        byte[]bytes = value.getUnsafeBytes();
        int p = s;
        while (p < end && enc.isSpace(bytes[p] & 0xff)) p++;
        int endp = end - 1;
        while (endp >= p && (bytes[endp] == 0 || enc.isSpace(bytes[endp] & 0xff))) endp--;

        if (p > s || endp < end - 1) {
            view(p - s, endp - p + 1);
            return this;
        }
View Full Code Here


        }
        return runtime.getNil();
    }   

    private IRubyObject multiByteStrip19(Ruby runtime) {
        Encoding enc = value.getEncoding();
        int s = value.getBegin();
        int end = s + value.getRealSize();
        byte[]bytes = value.getUnsafeBytes();       
        int p = s;
        int c;
        while (p < end && enc.isSpace(c = codePoint(runtime, enc, bytes, p, end))) p += codeLength(runtime, enc, c);
       
        int endp = end;
        int prev;
        while ((prev = enc.prevCharHead(bytes, s, endp, end)) != -1) {
            int code = codePoint(runtime, enc, bytes, prev, end);
            if (code != 0 && !enc.isSpace(code)) break;
            endp = prev;
        }
        if (p > s || prev < end) {
            view(p - s, endp - p);
            return this;
View Full Code Here

    public IRubyObject count19(ThreadContext context, IRubyObject arg) {
        Ruby runtime = context.getRuntime();
        if (value.getRealSize() == 0) return RubyFixnum.zero(runtime);

        RubyString otherStr = arg.convertToString();
        Encoding enc = checkEncoding(otherStr);
        final boolean[]table = new boolean[TRANS_SIZE];
        TrTables tables = otherStr.trSetupTable(context.getRuntime(), table, null, true, enc);
        return countCommon19(runtime, table, tables, enc);
    }
View Full Code Here

    public IRubyObject count19(ThreadContext context, IRubyObject[] args) {
        Ruby runtime = context.getRuntime();
        if (value.getRealSize() == 0) return RubyFixnum.zero(runtime);

        RubyString otherStr = args[0].convertToString();
        Encoding enc = checkEncoding(otherStr);
        final boolean[]table = new boolean[TRANS_SIZE];
        TrTables tables = otherStr.trSetupTable(runtime, table, null, true, enc);
        for (int i = 1; i<args.length; i++) {
            otherStr = args[i].convertToString();
            enc = checkEncoding(otherStr);
View Full Code Here

    public IRubyObject delete_bang19(ThreadContext context, IRubyObject arg) {
        Ruby runtime = context.getRuntime();
        if (value.getRealSize() == 0) return runtime.getNil();

        RubyString otherStr = arg.convertToString();
        Encoding enc = checkEncoding(otherStr);
        final boolean[]squeeze = new boolean[TRANS_SIZE];
        TrTables tables = otherStr.trSetupTable(runtime, squeeze, null, true, enc);
        return delete_bangCommon19(runtime, squeeze, tables, enc);
    }
View Full Code Here

    public IRubyObject delete_bang19(ThreadContext context, IRubyObject[] args) {
        Ruby runtime = context.getRuntime();
        if (value.getRealSize() == 0) return runtime.getNil();

        RubyString otherStr = args[0].convertToString();
        Encoding enc = checkEncoding(otherStr);
        boolean[]squeeze = new boolean[TRANS_SIZE];
        TrTables tables = otherStr.trSetupTable(runtime, squeeze, null, true, enc);
        for (int i=1; i<args.length; i++) {
            otherStr = args[i].convertToString();
            enc = checkEncoding(otherStr);
View Full Code Here

            modifyCheck();
            return runtime.getNil();
        }

        RubyString otherStr = args[0].convertToString();
        Encoding enc = checkEncoding(otherStr);
        final boolean squeeze[] = new boolean[TRANS_SIZE];
        TrTables tables = otherStr.trSetupTable(runtime, squeeze, null, true, enc);

        boolean singlebyte = singleByteOptimizable() && otherStr.singleByteOptimizable();
        for (int i=1; i<args.length; i++) {
View Full Code Here

        ByteList replList = replStr.value;
        if (replList.getRealSize() == 0) return delete_bang19(context, src);

        RubyString srcStr = src.convertToString();
        ByteList srcList = srcStr.value;
        Encoding e1 = checkEncoding(srcStr);
        Encoding e2 = checkEncoding(replStr);
        Encoding enc = e1 == e2 ? e1 : srcStr.checkEncoding(replStr);

        int cr = getCodeRange();

        final TR trSrc = new TR(srcList);
        boolean cflag = false;
        if (value.getRealSize() > 1) {
            if (enc.isAsciiCompatible()) {
                if (trSrc.buf.length > 0 && (trSrc.buf[trSrc.p] & 0xff) == '^' && trSrc.p + 1 < trSrc.pend) {
                    cflag = true;
                    trSrc.p++;
                }
            } else {
                int cl = StringSupport.preciseLength(enc, trSrc.buf, trSrc.p, trSrc.pend);
                if (enc.mbcToCode(trSrc.buf, trSrc.p, trSrc.pend) == '^' && trSrc.p + cl < trSrc.pend) {
                    cflag = true;
                    trSrc.p += cl;
                }
            }           
        }

        boolean singlebyte = true;
        int c;
        final int[]trans = new int[TRANS_SIZE];
        IntHash<Integer> hash = null;
        final TR trRepl = new TR(replList);

        if (cflag) {
            for (int i=0; i<TRANS_SIZE; i++) trans[i] = 1;
           
            while ((c = trNext(trSrc, runtime, enc)) >= 0) {
                if (c < TRANS_SIZE) {
                    trans[c & 0xff] = -1;
                } else {
                    if (hash == null) hash = new IntHash<Integer>();
                    hash.put(c, 1); // QTRUE
                }
            }
            while ((c = trNext(trRepl, runtime, enc)) >= 0) {}  /* retrieve last replacer */
            int last = trRepl.now;
            for (int i=0; i<TRANS_SIZE; i++) {
                if (trans[i] >= 0) trans[i] = last;
            }
        } else {
            for (int i=0; i<TRANS_SIZE; i++) trans[i] = -1;
           
            while ((c = trNext(trSrc, runtime, enc)) >= 0) {
                int r = trNext(trRepl, runtime, enc);
                if (r == -1) r = trRepl.now;
                if (c < TRANS_SIZE) {
                    trans[c & 0xff] = r;
                    if (r > TRANS_SIZE - 1) singlebyte = false;
                } else {
                    if (hash == null) hash = new IntHash<Integer>();
                    hash.put(c, r);
                }
            }
        }

        if (cr == CR_VALID) cr = CR_7BIT;
        modifyAndKeepCodeRange();
        int s = value.getBegin();
        int send = s + value.getRealSize();
        byte sbytes[] = value.getUnsafeBytes();
        int max = value.getRealSize();
        boolean modify = false;

        int last = -1;
        int clen, tlen, c0;

        if (sflag) {
            int save = -1;
            byte[]buf = new byte[max];
            int t = 0;
            while (s < send) {
                boolean mayModify = false;
                c0 = c = codePoint(runtime, e1, sbytes, s, send);
                clen = codeLength(runtime, e1, c);
                tlen = enc == e1 ? clen : codeLength(runtime, enc, c);
                s += clen;
                c = trCode(c, trans, hash, cflag, last);

                if (c != -1) {
                    if (save == c) {
                        if (cr == CR_7BIT && !Encoding.isAscii(c)) cr = CR_VALID;
                        continue;
                    }
                    save = c;
                    tlen = codeLength(runtime, enc, c);
                    modify = true;
                } else {
                    save = -1;
                    c = c0;
                    if (enc != e1) mayModify = true;
                }

                while (t + tlen >= max) {
                    max <<= 1;
                    byte[]tbuf = new byte[max];
                    System.arraycopy(buf, 0, tbuf, 0, buf.length);
                    buf = tbuf;
                }
                enc.codeToMbc(c, buf, t);
                if (mayModify && (tlen == 1 ? sbytes[s] != buf[t] : ByteList.memcmp(sbytes, s, buf, t, tlen) != 0)) modify = true;
                if (cr == CR_7BIT && !Encoding.isAscii(c)) cr = CR_VALID;
                t += tlen;
            }
            value.setUnsafeBytes(buf);
            value.setRealSize(t);
        } else if (enc.isSingleByte() || (singlebyte && hash == null)) {
            while (s < send) {
                c = sbytes[s] & 0xff;
                if (trans[c] != -1) {
                    if (!cflag) {
                        c = trans[c];
                        sbytes[s] = (byte)c;
                    } else {
                        sbytes[s] = (byte)last;
                    }
                    modify = true;
                }
                if (cr == CR_7BIT && !Encoding.isAscii(c)) cr = CR_VALID;
                s++;
            }
        } else {
            max += max >> 1;
            byte[]buf = new byte[max];
            int t = 0;

            while (s < send) {
                boolean mayModify = false;
                c0 = c = codePoint(runtime, e1, sbytes, s, send);
                clen = codeLength(runtime, e1, c);
                tlen = enc == e1 ? clen : codeLength(runtime, enc, c);

                c = trCode(c, trans, hash, cflag, last);

                if (c != -1) {
                    tlen = codeLength(runtime, enc, c);
                    modify = true;
                } else {
                    c = c0;
                    if (enc != e1) mayModify = true;
                }
                while (t + tlen >= max) {
                    max <<= 1;
                    byte[]tbuf = new byte[max];
                    System.arraycopy(buf, 0, tbuf, 0, buf.length);
                    buf = tbuf;
                }

                enc.codeToMbc(c, buf, t);

                if (mayModify && (tlen == 1 ? sbytes[s] != buf[t] : ByteList.memcmp(sbytes, s, buf, t, tlen) != 0)) modify = true;
                if (cr == CR_7BIT && !Encoding.isAscii(c)) cr = CR_VALID;
                s += clen;
                t += tlen;
View Full Code Here

        int s = p;
        int len = val.getRealSize();
        int end = p + len;
        byte[]bytes = val.getUnsafeBytes();

        final Encoding enc;
        RubyString sepStr = sep.convertToString();
        if (sepStr == runtime.getGlobalVariables().getDefaultSeparator()) {
            enc = val.getEncoding();
            while (p < end) {
                if (bytes[p] == (byte)'\n') {
                    int p0 = enc.leftAdjustCharHead(bytes, s, p, end);
                    if (enc.isNewLine(bytes, p0, end)) {
                        p = p0 + StringSupport.length(enc, bytes, p0, end);
                        block.yield(context, makeShared19(runtime, val, s, p - s).infectBy(this));
                        s = p;
                        continue;
                    }
View Full Code Here

        byte bytes[] = value.getUnsafeBytes();
        int p = value.getBegin();
        int end = p + value.getRealSize();

        Ruby runtime = context.getRuntime();
        Encoding enc = runtime.getKCode().getEncoding();
        ByteList val = value.shallowDup();
        while (p < end) {
            int n = StringSupport.length(enc, bytes, p, end);
            block.yield(context, makeShared19(runtime, val, p-val.getBegin(), n));
            p += n;
View Full Code Here

TOP

Related Classes of org.jcodings.Encoding

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.