Package org.jcodings

Examples of org.jcodings.Encoding


    }

    @JRubyMethod(name = "capitalize!", compat = RUBY1_9)
    public IRubyObject capitalize_bang19(ThreadContext context) {
        Ruby runtime = context.getRuntime();
        Encoding enc = checkDummyEncoding();

        if (value.getRealSize() == 0) {
            modifyCheck();
            return runtime.getNil();
        }

        modifyAndKeepCodeRange();

        int s = value.getBegin();
        int end = s + value.getRealSize();
        byte[]bytes = value.getUnsafeBytes();
        boolean modify = false;

        int c = codePoint(runtime, enc, bytes, s, end);
        if (enc.isLower(c)) {
            enc.codeToMbc(toUpper(enc, c), bytes, s);
            modify = true;
        }

        s += codeLength(runtime, enc, c);
        while (s < end) {
            c = codePoint(runtime, enc, bytes, s, end);
            if (enc.isUpper(c)) {
                enc.codeToMbc(toLower(enc, c), bytes, s);
                modify = true;
            }
            s += codeLength(runtime, enc, c);
        }
View Full Code Here


    }

    @JRubyMethod(name = "upcase!", compat = RUBY1_9)
    public IRubyObject upcase_bang19(ThreadContext context) {
        Ruby runtime = context.getRuntime();
        Encoding enc = checkDummyEncoding();

        if (value.getRealSize() == 0) {
            modifyCheck();
            return runtime.getNil();
        }
View Full Code Here

    }

    @JRubyMethod(name = "downcase!", compat = RUBY1_9)
    public IRubyObject downcase_bang19(ThreadContext context) {
        Ruby runtime = context.getRuntime();
        Encoding enc = checkDummyEncoding();

        if (value.getRealSize() == 0) {
            modifyCheck();
            return runtime.getNil();
        }
View Full Code Here

    }

    @JRubyMethod(name = "swapcase!", compat = RUBY1_9)
    public IRubyObject swapcase_bang19(ThreadContext context) {
        Ruby runtime = context.getRuntime();
        Encoding enc = checkDummyEncoding();
        if (value.getRealSize() == 0) {
            modifyCheck();
            return runtime.getNil();       
        }
        modifyAndKeepCodeRange();
View Full Code Here

    }

    private IRubyObject dumpCommon(boolean is1_9) {
        Ruby runtime = getRuntime();
        ByteList buf = null;
        Encoding enc = value.getEncoding();

        int p = value.getBegin();
        int end = p + value.getRealSize();
        byte[]bytes = value.getUnsafeBytes();
       
        int len = 2;
        while (p < end) {
            int c = bytes[p++] & 0xff;

            switch (c) {
            case '"':case '\\':case '\n':case '\r':case '\t':case '\f':
            case '\013': case '\010': case '\007': case '\033':
                len += 2;
                break;
            case '#':
                len += isEVStr(bytes, p, end) ? 2 : 1;
                break;
            default:
                if (ASCII.isPrint(c)) {
                    len++;
                } else {
                    if (is1_9 && enc instanceof UTF8Encoding) {
                        int n = StringSupport.preciseLength(enc, bytes, p - 1, end) - 1;
                        if (n > 0) {
                            if (buf == null) buf = new ByteList();
                            int cc = codePoint(runtime, enc, bytes, p - 1, end);
                            Sprintf.sprintf(runtime, buf, "%x", cc);
                            len += buf.getRealSize() + 4;
                            buf.setRealSize(0);
                            p += n;
                            break;
                        }
                    }
                    len += 4;
                }
                break;
            }
        }

        if (is1_9 && !enc.isAsciiCompatible()) {
            len += ".force_encoding(\"".length() + enc.getName().length + "\")".length();
        }

        ByteList outBytes = new ByteList(len);
        byte out[] = outBytes.getUnsafeBytes();
        int q = 0;
        p = value.getBegin();
        end = p + value.getRealSize();

        out[q++] = '"';
        while (p < end) {
            int c = bytes[p++] & 0xff;
            if (c == '"' || c == '\\') {
                out[q++] = '\\';
                out[q++] = (byte)c;
            } else if (c == '#') {
                if (isEVStr(bytes, p, end)) out[q++] = '\\';
                out[q++] = '#';
            } else if (!is1_9 && ASCII.isPrint(c)) {
                out[q++] = (byte)c;
            } else if (c == '\n') {
                out[q++] = '\\';
                out[q++] = 'n';
            } else if (c == '\r') {
                out[q++] = '\\';
                out[q++] = 'r';
            } else if (c == '\t') {
                out[q++] = '\\';
                out[q++] = 't';
            } else if (c == '\f') {
                out[q++] = '\\';
                out[q++] = 'f';
            } else if (c == '\013') {
                out[q++] = '\\';
                out[q++] = 'v';
            } else if (c == '\010') {
                out[q++] = '\\';
                out[q++] = 'b';
            } else if (c == '\007') {
                out[q++] = '\\';
                out[q++] = 'a';
            } else if (c == '\033') {
                out[q++] = '\\';
                out[q++] = 'e';
            } else if (is1_9 && ASCII.isPrint(c)) {
                out[q++] = (byte)c;
            } else {
                out[q++] = '\\';
                if (is1_9) {
                    if (enc instanceof UTF8Encoding) {
                        int n = StringSupport.preciseLength(enc, bytes, p - 1, end) - 1;
                        if (n > 0) {
                            int cc = codePoint(runtime, enc, bytes, p - 1, end);
                            p += n;
                            outBytes.setRealSize(q);
                            Sprintf.sprintf(runtime, outBytes, "u{%x}", cc);
                            q = outBytes.getRealSize();
                            continue;
                        }
                    }
                    outBytes.setRealSize(q);
                    Sprintf.sprintf(runtime, outBytes, "x%02X", c);
                    q = outBytes.getRealSize();
                } else {
                    outBytes.setRealSize(q);
                    Sprintf.sprintf(runtime, outBytes, "%03o", c);
                    q = outBytes.getRealSize();
                }
            }
        }
        out[q++] = '"';
        outBytes.setRealSize(q);
        assert out == outBytes.getUnsafeBytes(); // must not reallocate

        final RubyString result = new RubyString(runtime, getMetaClass(), outBytes);
        if (is1_9) {
            if (!enc.isAsciiCompatible()) {
                result.cat(".force_encoding(\"".getBytes());
                result.cat(enc.getName());
                result.cat((byte)'"').cat((byte)')');
                enc = ASCII;
            }
            result.associateEncoding(enc);
            result.setCodeRange(CR_7BIT);
View Full Code Here

        byte bytes[] = value.getUnsafeBytes();
        int p = value.getBegin();
        int end = p + value.getRealSize();
        RubyString result = new RubyString(runtime, runtime.getString(), new ByteList(end - p));

        Encoding enc;
        if (is1_9) {
            enc = value.getEncoding();
            if (enc != runtime.getKCode().getEncoding()) {
                enc = runtime.getKCode().getEncoding();
            }
            if (!enc.isAsciiCompatible()) {
                enc = USASCIIEncoding.INSTANCE;
            }
            result.associateEncoding(enc);
        } else {
            enc = runtime.getKCode().getEncoding();
        }

        result.cat('"');
        while (p < end) {
            int c, n;

            if (is1_9) {
                n = StringSupport.preciseLength(enc, bytes, p, end);
                if (n <= 0) { // Illegal combination
                    p++;
                    n = 1;
                    result.escapeCodePointCat(runtime, bytes, p, n);
                    continue;
                }
                c = codePoint(runtime, enc, bytes, p, end);
                n = codeLength(runtime, enc, c);
                p += n;
            } else {
                c = bytes[p++] & 0xff;
                n = enc.length((byte)c);
            }

            if (!is1_9 && n > 1 && p - 1 <= end - n) {
                try {
                    result.cat(bytes, p - 1, n);
                } catch (ArrayIndexOutOfBoundsException x) {
                    System.out.println("begin = " + (p - 1));
                    System.out.println("len = " + n);
                    System.out.println("bytes = " + Arrays.toString(bytes));
                    throw x;
                }
                p += n - 1;
                continue;
            } else if (c == '"'|| c == '\\') {
                result.prefixEscapeCat(c);
                continue;
            } else if (c == '#') {
                if (is1_9) {
                    int cc;
                    if (p < end && StringSupport.preciseLength(enc, bytes, p, end) > 0 &&
                            isEVStr(cc = codePoint(runtime, enc, bytes, p, end))) {
                        if ("$@{".indexOf(cc) != -1) {
                            cc = '#';
                        }
                        result.prefixEscapeCat(cc);
                        continue;
                    }
                } else {
                    if (isEVStr(bytes, p, end)) {
                        result.prefixEscapeCat(c);
                        continue;
                    }
                }
            }

            if (!is1_9 && ASCII.isPrint(c)) {
                result.cat(c);
            } else if (c == '\n') {
                result.prefixEscapeCat('n');
            } else if (c == '\r') {
                result.prefixEscapeCat('r');
            } else if (c == '\t') {
                result.prefixEscapeCat('t');
            } else if (c == '\f') {
                result.prefixEscapeCat('f');
            } else if (c == '\013') {
                result.prefixEscapeCat('v');
            } else if (c == '\010') {
                result.prefixEscapeCat('b');
            } else if (c == '\007') {
                result.prefixEscapeCat('a');
            } else if (c == '\033') {
                result.prefixEscapeCat('e');
            } else if (is1_9 && enc.isPrint(c)) {
                result.cat(bytes, p - n, n, enc);
            } else {
                if (!is1_9) {
                    Sprintf.sprintf(runtime, result.value, "\\%03o", c & 0377);
                } else {
View Full Code Here

        if (value.getRealSize() == 0) {
            modifyCheck();
            return runtime.getNil();
        }

        Encoding enc = value.getEncoding();
        int s = value.getBegin();
        int end = s + value.getRealSize();
        byte[]bytes = value.getUnsafeBytes();

        final IRubyObject result;
View Full Code Here

    // In 1.9 we strip any combination of \0 and \s
    private IRubyObject singleByteRStrip19(Ruby runtime) {
        byte[] bytes = value.getUnsafeBytes();
        int start = value.getBegin();
        int end = start + value.getRealSize();
        Encoding enc = value.getEncoding();
        int endp = end - 1;
        while (endp >= start && (bytes[endp] == 0 ||
                enc.isSpace(bytes[endp] & 0xff))) endp--;

        if (endp < end - 1) {
            view(0, endp - start + 1);
            return this;
        }
View Full Code Here

    // In 1.9 we strip any combination of \0 and \s
    private IRubyObject multiByteRStrip19(Ruby runtime) {
        byte[] bytes = value.getUnsafeBytes();
        int start = value.getBegin();
        int end = start + value.getRealSize();
        Encoding enc = value.getEncoding();
        int endp = end;
        int prev;
        while ((prev = enc.prevCharHead(bytes, start, endp, end)) != -1) {
            int point = codePoint(runtime, enc, bytes, prev, end);
            if (point != 0 && !enc.isSpace(point)) break;
            endp = prev;
        }

        if (endp < end) {
            view(0, endp - start + 1);
View Full Code Here

    @JRubyMethod(name = "strip!", compat = RUBY1_9)
    public IRubyObject strip_bang19(ThreadContext context) {
        Ruby runtime = context.getRuntime();
        modifyCheck();

        Encoding enc = value.getEncoding();

        IRubyObject result = singleByteOptimizable(enc) ?
                singleByteStrip19(runtime) : multiByteStrip19(runtime);
        keepCodeRange();
        return result;
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.