Package org.jcodings

Examples of org.jcodings.Encoding


        return s;
    }
   
    private ByteList inspectIntoByteList(boolean ignoreKCode) {
        Ruby runtime = getRuntime();
        Encoding enc = runtime.getKCode().getEncoding();
        final int length = value.length();
        ByteList sb = new ByteList(length + 2 + length / 100);

        sb.append('\"');

        for (int i = 0; i < length; i++) {
            int c = value.get(i) & 0xFF;
           
            if (!ignoreKCode) {
                int seqLength = enc.length((byte)c);
               
                if (seqLength > 1 && (i + seqLength -1 < length)) {
                    // don't escape multi-byte characters, leave them as bytes
                    sb.append(value, i, seqLength);
                    i += seqLength - 1;
View Full Code Here


        byte[]bytes = value.bytes;
        final Matcher matcher = regex.matcher(bytes, begin, range);
       
        boolean lastNull = false;
        RubyArray result = runtime.newArray();
        final Encoding enc = regex.getEncoding();
       
        if (regex.numberOfCaptures() == 0) { // shorter path, no captures defined, no region will be returned
            while ((end = matcher.search(start, range, Option.NONE)) >= 0) {
                if (start == end + begin && matcher.getBegin() == matcher.getEnd()) {
                    if (value.realSize == 0) {
                        result.append(newEmptyString(runtime, getMetaClass()));
                        break;
                    } else if (lastNull) {
                        result.append(substr(runtime, beg, enc.length(bytes, begin + beg, range)));
                        beg = start - begin;
                    } else {
                        if (start == range) {
                            start++;
                        } else {
                            start += enc.length(bytes, start, range);
                        }
                        lastNull = true;
                        continue;
                    }
                } else {
                    result.append(substr(beg, end - beg));
                    beg = matcher.getEnd();
                    start = begin + matcher.getEnd();
                }
                lastNull = false;
                if (limit && lim <= ++i) break;
            }
        } else {
            while ((end = matcher.search(start, range, Option.NONE)) >= 0) {
                final Region region = matcher.getRegion();
                if (start == end + begin && region.beg[0] == region.end[0]) {
                    if (value.realSize == 0) {                       
                        result.append(newEmptyString(runtime, getMetaClass()));
                        break;
                    } else if (lastNull) {
                        result.append(substr(beg, enc.length(bytes, begin + beg, range)));
                        beg = start - begin;
                    } else {
                        if (start == range) {
                            start++;
                        } else {
                            start += enc.length(bytes, start, range);
                        }
                        lastNull = true;
                        continue;
                    }                   
                } else {
View Full Code Here

    private final void rb_reg_expr_str(StringBuilder sb, byte[] s, int start, int len) {
        int p,pend;
        boolean need_escape = false;
        p = start;
        pend = start+len;
        Encoding enc = kcode.getEncoding();
        while (p < pend) {
            if (s[p] == '/' ||
                    (!(' ' == s[p] || (!Character.isWhitespace(s[p]) &&
                                       !Character.isISOControl(s[p]))) &&
                    enc.length(s, p, pend) == 1)) {
                need_escape = true;
                break;
            }
            p += enc.length(s, p, pend);
        }
        if (!need_escape) {
            sb.append(new ByteList(s,start,len,false).toString());
        } else {
            p = 0;
            while (p < pend) {
                if (s[p] == '\\') {
                    int n = enc.length(s, p + 1, pend) + 1;
                    sb.append(new ByteList(s,p,n,false).toString());
                    p += n;
                    continue;
                } else if (s[p] == '/') {
                    sb.append("\\/");
                } else if (enc.length(s, p, pend) != 1) {
                    sb.append(new ByteList(s, p, enc.length(s, p, pend), false).toString());
                    p += enc.length(s, p, pend);
                    continue;
                } else if ((' ' == s[p] || (!Character.isWhitespace(s[p]) &&
                                           !Character.isISOControl(s[p])))) {
                    sb.append((char)(s[p]&0xFF));
                } else if (!Character.isWhitespace((char)(s[p]&0xFF))) {
View Full Code Here

        int no = -1;
        ByteList bs = str.getByteList();
        ByteList srcbs = src.getByteList();
        int e = bs.realSize;
        RubyString val = null;
        Encoding enc = kcode.getEncoding();

        int beg, end;
        while (s < e) {
            int ss = s;
            int c = bs.charAt(s);
            int l = enc.length(bs.bytes, bs.begin + s++, bs.begin + e);
            if (l != 1) {
                s += l - 1;
                continue;
            }
            if (c != '\\' || s == e) continue;
View Full Code Here

    private final void rb_reg_expr_str(RubyString ss, int s, int len) {
        int p = s;
        int pend = p + len;
        boolean need_escape = false;
        Encoding enc = kcode.getEncoding();
        while (p < pend) {
            if (str.bytes[p] == '/' || (!enc.isPrint(str.bytes[p] & 0xff) && enc.length(str.bytes, p, pend) == 1)) {
                need_escape = true;
                break;
            }
            p += enc.length(str.bytes, p, pend);
        }
        if (!need_escape) {
            ss.cat(str.bytes, s, len);
        } else {
            p = s;
            while (p<pend) {
                if (str.bytes[p] == '\\') {
                    int n = enc.length(str.bytes, p + 1, pend) + 1;
                    ss.cat(str.bytes, p, n);
                    p += n;
                    continue;
                } else if (str.bytes[p] == '/') {
                    ss.cat((byte)'\\');
                    ss.cat(str.bytes, p, 1);
                } else if (enc.length(str.bytes, p, pend) != 1) {
                    ss.cat(str.bytes, p, enc.length(str.bytes, p, pend));
                    p += enc.length(str.bytes, p, pend);
                    continue;
                } else if (enc.isPrint(str.bytes[p] & 0xff)) {
                    ss.cat(str.bytes,p,1);
                } else if (!enc.isSpace(str.bytes[p] & 0xff)) {
                    ss.cat(ByteList.create(Integer.toString(str.bytes[p] & 0377, 8)));
                } else {
                    ss.cat(str.bytes, p, 1);
                }
                p++;
View Full Code Here

    public static ByteList quote(ByteList str, KCode kcode) {
        ByteList bs = str;
        int tix = 0;
        int s = bs.begin;
        int send = s + bs.length();
        Encoding enc = kcode.getEncoding();
        meta_found: do {
            for(; s < send; s++) {
                int c = bs.bytes[s] & 0xff;
                int l = enc.length(bs.bytes, s, send);
                if (l != 1) {
                    int n = l;
                    while (n-- > 0 && s < send) {
                        s++;
                    }
                    s--;
                    continue;
                }
                switch (c) {
                case '[': case ']': case '{': case '}':
                case '(': case ')': case '|': case '-':
                case '*': case '.': case '\\':
                case '?': case '+': case '^': case '$':
                case ' ': case '#':
                case '\t': case '\f': case '\n': case '\r':
                    break meta_found;
                }
            }
            return bs;
        } while (false);
        ByteList b1 = new ByteList(send*2);
        System.arraycopy(bs.bytes,bs.begin,b1.bytes,b1.begin,s-bs.begin);
        tix += (s-bs.begin);

        for(; s<send; s++) {
            int c = bs.bytes[s] & 0xff;
            int l = enc.length(bs.bytes, s, send);
            if (l != 1) {
                int n = l;
                while (n-- > 0 && s < send) {
                    b1.bytes[tix++] = bs.bytes[s++];
                }
View Full Code Here

        Ruby runtime = context.getRuntime();
        ByteList value = str.getByteList();
       
        if (pos >= value.realSize) return runtime.getNil();

        Encoding enc = runtime.getKCode().getEncoding();
       
        int len;
        if (enc.isSingleByte()) {
            len = 1;
        } else {
            len = enc.length(value.bytes, value.begin + pos, value.begin + value.realSize);
        }
       
        if (pos + len > value.realSize) len = value.realSize - pos;
        lastPos = pos;
        pos += len;
View Full Code Here

    //   System.out.printf("  * %d\n", pair.bytePos);
    // }
   
    Arrays.sort(pairs);
    Encoding enc = UTF8Encoding.INSTANCE;
    byte[] bytes;
    try {
      bytes = text.getBytes("UTF-8");
    } catch (UnsupportedEncodingException e) {
      e.printStackTrace();
View Full Code Here

    }

    public Encoding loadEncoding(ByteList name) {
        Entry entry = findEncodingOrAliasEntry(name);
        if (entry == null) return null;
        Encoding enc = entry.getEncoding(); // load the encoding
        int index = enc.getIndex();
        if (index >= encodingIndex.length) {
            RubyEncoding tmp[] = new RubyEncoding[index + 4];
            System.arraycopy(encodingIndex, 0, tmp, 0, encodingIndex.length);
            encodingIndex = tmp;
        }
View Full Code Here

        }
    }

    private void defineEncodingConstants(Ruby runtime, RubyEncoding encoding, byte[]name, int p,
            int end) {
        Encoding enc = ASCIIEncoding.INSTANCE;
        int s = p;

        int code = name[s] & 0xff;
        if (enc.isDigit(code)) return;

        boolean hasUpper = false;
        boolean hasLower = false;
        if (enc.isUpper(code)) {
            hasUpper = true;
            while (++s < end && (enc.isAlnum(name[s] & 0xff) || name[s] == (byte)'_')) {
                if (enc.isLower(name[s] & 0xff)) hasLower = true;
            }
        }

        boolean isValid = false;
        if (s >= end) {
            isValid = true;
            defineEncodingConstant(runtime, encoding, name, p, end);
        }

        if (!isValid || hasLower) {
            if (!hasLower || !hasUpper) {
                do {
                    code = name[s] & 0xff;
                    if (enc.isLower(code)) hasLower = true;
                    if (enc.isUpper(code)) hasUpper = true;
                } while (++s < end && (!hasLower || !hasUpper));
            }

            byte[]constName = new byte[end - p];
            System.arraycopy(name, p, constName, 0, end - p);
            s = 0;
            code = constName[s] & 0xff;

            if (!isValid) {
                if (enc.isLower(code)) constName[s] = AsciiTables.ToUpperCaseTable[code];
                for (; s < constName.length; ++s) {
                    if (!enc.isAlnum(constName[s] & 0xff)) constName[s] = (byte)'_';
                }
                if (hasUpper) {
                    defineEncodingConstant(runtime, encoding, constName, 0, constName.length);
                }
            }
            if (hasLower) {
                for (s = 0; s < constName.length; ++s) {
                    code = constName[s] & 0xff;
                    if (enc.isLower(code)) constName[s] = AsciiTables.ToUpperCaseTable[code];
                }
                defineEncodingConstant(runtime, encoding, constName, 0, constName.length);
            }
        }
    }
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.