Package org.jcodings

Examples of org.jcodings.Encoding


    static Regex preparePattern(Ruby runtime, Regex pattern, RubyString str) {
        if (str.scanForCodeRange() == StringSupport.CR_BROKEN) {
            throw runtime.newArgumentError("invalid byte sequence in " + str.getEncoding());
        }
        Encoding enc = str.getEncoding();
        if (!enc.isAsciiCompatible()) {
            if (enc != pattern.getEncoding()) encodingMatchError(runtime, pattern, enc);
        }
        // TODO: check for isKCodeDefault() somehow
//        if (warn && isEncodingNone() && enc != ASCIIEncoding.INSTANCE && str.scanForCodeRange() != StringSupport.CR_7BIT) {
//            getRuntime().getWarnings().warn(ID.REGEXP_MATCH_AGAINST_STRING, "regexp match /.../n against to " + enc + " string");
View Full Code Here


    static ByteList quote19(ByteList bs, boolean asciiOnly) {
        int p = bs.getBegin();
        int end = p + bs.getRealSize();
        byte[]bytes = bs.getUnsafeBytes();
        Encoding enc = bs.getEncoding();

        metaFound: do {
            while (p < end) {
                final int c;
                final int cl;
                if (enc.isAsciiCompatible()) {
                    cl = 1;
                    c = bytes[p] & 0xff;
                } else {
                    cl = StringSupport.preciseLength(enc, bytes, p, end);
                    c = enc.mbcToCode(bytes, p, end);
                }

                if (!Encoding.isAscii(c)) {
                    p += StringSupport.length(enc, bytes, p, end);
                    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 metaFound;
                }
                p += cl;
            }
            if (asciiOnly) {
                ByteList tmp = bs.shallowDup();
                tmp.setEncoding(USASCIIEncoding.INSTANCE);
                return tmp;
            }
            return bs;
        } while (false);

        ByteList result = new ByteList(end * 2);
        result.setEncoding(asciiOnly ? USASCIIEncoding.INSTANCE : bs.getEncoding());
        byte[]obytes = result.getUnsafeBytes();
        int op = p - bs.getBegin();
        System.arraycopy(bytes, bs.getBegin(), obytes, 0, op);

        while (p < end) {
            final int c;
            final int cl;
            if (enc.isAsciiCompatible()) {
                cl = 1;
                c = bytes[p] & 0xff;
            } else {
                cl = StringSupport.preciseLength(enc, bytes, p, end);
                c = enc.mbcToCode(bytes, p, end);
            }

            if (!Encoding.isAscii(c)) {
                int n = StringSupport.length(enc, bytes, p, end);
                while (n-- > 0) obytes[op++] = bytes[p++];
                continue;
            }
            p += cl;
            switch (c) {
            case '[': case ']': case '{': case '}':
            case '(': case ')': case '|': case '-':
            case '*': case '.': case '\\':
            case '?': case '+': case '^': case '$':
            case '#':
                op += enc.codeToMbc('\\', obytes, op);
                break;
            case ' ':
                op += enc.codeToMbc('\\', obytes, op);
                op += enc.codeToMbc(' ', obytes, op);
                continue;
            case '\t':
                op += enc.codeToMbc('\\', obytes, op);
                op += enc.codeToMbc('t', obytes, op);
                continue;
            case '\n':
                op += enc.codeToMbc('\\', obytes, op);
                op += enc.codeToMbc('n', obytes, op);
                continue;
            case '\r':
                op += enc.codeToMbc('\\', obytes, op);
                op += enc.codeToMbc('r', obytes, op);
                continue;
            case '\f':
                op += enc.codeToMbc('\\', obytes, op);
                op += enc.codeToMbc('f', obytes, op);
                continue;
            }
            op += enc.codeToMbc(c, obytes, op);
        }

        result.setRealSize(op);
        return result;
    }
View Full Code Here

            }
        }

        boolean hasAsciiOnly = false;
        RubyString source = runtime.newString();
        Encoding hasAsciiCompatFixed = null;
        Encoding hasAsciiIncompat = null;       

        for (int i = 0; i < realArgs.length; i++) {
            if (0 < i) source.cat((byte)'|');
           
            Encoding enc;
            IRubyObject v = TypeConverter.convertToTypeWithCheck(realArgs[i], runtime.getRegexp(), "to_regexp");
            if (!v.isNil()) {
                RubyRegexp regex = (RubyRegexp) v;
                enc = regex.getEncoding();

                if (!enc.isAsciiCompatible()) {
                    if (hasAsciiIncompat == null) { // First regexp of union sets kcode.
                        hasAsciiIncompat = enc;
                    } else if (hasAsciiIncompat != enc) { // n kcode doesn't match first one
                        throw runtime.newArgumentError("incompatible encodings: " + hasAsciiIncompat + " and " + enc);
                    }
                } else if (regex.getOptions().isFixed()) {
                    if (hasAsciiCompatFixed == null) { // First regexp of union sets kcode.
                        hasAsciiCompatFixed = enc;
                    } else if (hasAsciiCompatFixed != enc) { // n kcode doesn't match first one
                        throw runtime.newArgumentError("incompatible encodings: " + hasAsciiCompatFixed + " and " + enc);
                    }
                } else {
                    hasAsciiOnly = true;
                }
                v = regex.to_s();
            } else {
                RubyString str = realArgs[i].convertToString();
                enc = str.getEncoding();
               
                if (!enc.isAsciiCompatible()) {
                    if (hasAsciiIncompat == null) { // First regexp of union sets kcode.
                        hasAsciiIncompat = enc;
                    } else if (hasAsciiIncompat != enc) { // n kcode doesn't match first one
                        throw runtime.newArgumentError("incompatible encodings: " + hasAsciiIncompat + " and " + enc);
                    }
View Full Code Here

    }

    // rb_reg_initialize_str
    private RubyRegexp initializeCommon19(RubyString str, RegexpOptions options) {
        ByteList bytes = str.getByteList();
        Encoding enc = bytes.getEncoding();
        if (options.isEncodingNone()) {
            if (enc != ASCIIEncoding.INSTANCE) {
                if (str.scanForCodeRange() != StringSupport.CR_7BIT) {
                    raiseRegexpError19(getRuntime(), bytes, enc, options, "/.../n has a non escaped non ASCII character in non ASCII-8BIT script");
                }
View Full Code Here

        ByteList bs = str.getByteList();
        int p = bs.getBegin();
        int s = p;
        int end = p + bs.getRealSize();
        byte[]bytes = bs.getUnsafeBytes();
        Encoding strEnc = bs.getEncoding();

        ByteList srcbs = src.getByteList();
        Encoding srcEnc = srcbs.getEncoding();

        RubyString val = null;

        while (s < end) {
            int c, cl;
View Full Code Here

    private boolean isPrintable() {
        Ruby runtime = getRuntime();
        int p = symbolBytes.getBegin();
        int end = p + symbolBytes.getRealSize();
        byte[]bytes = symbolBytes.getUnsafeBytes();
        Encoding enc = symbolBytes.getEncoding();

        while (p < end) {
            int c = codePoint(runtime, enc, bytes, p, end);
            if (!enc.isPrint(c)) return false;
            p += codeLength(runtime, enc, c);
        }
        return true;
    }
View Full Code Here

        }
    }

    private static Map getAsMapOfRubyStrings(Ruby runtime, Set<Map.Entry<Object, Object>> entrySet) {
        Map envs = new HashMap();
        Encoding encoding = runtime.getEncodingService().getLocaleEncoding();

        // On Windows, entrySet doesn't have corresponding keys for these
        if (Platform.IS_WINDOWS) {
            addRubyKeyValuePair(runtime, envs, "HOME", System.getProperty("user.home"), encoding);
            addRubyKeyValuePair(runtime, envs, "USER", System.getProperty("user.name"), encoding);
View Full Code Here

        ByteList value = str.getByteList();

        if (pos >= value.getRealSize()) return runtime.getNil();
        int len;
        if (is1_9) {
            Encoding enc = str.getEncoding();
            len = enc.isSingleByte() ? 1 : StringSupport.length(enc, value.getUnsafeBytes(), value.getBegin() + pos, value.getBegin() + value.getRealSize());
        } else {
            Encoding enc = runtime.getKCode().getEncoding();
            len = enc.isSingleByte() ? 1 : enc.length(value.getUnsafeBytes(), value.getBegin() + pos, value.getBegin() + value.getRealSize());
        }

        if (pos + len > value.getRealSize()) len = value.getRealSize() - pos;
        lastPos = pos;
        pos += len;
View Full Code Here

     *           </tr>
     *         </table>
     *
     **/
    public static RubyArray unpack(Ruby runtime, ByteList encodedString, ByteList formatString) {
        Encoding encoding = encodedString.getEncoding();
        RubyArray result = runtime.newArray();
        // FIXME: potentially could just use ByteList here?
        ByteBuffer format = ByteBuffer.wrap(formatString.getUnsafeBytes(), formatString.begin(), formatString.length());
        ByteBuffer encode = ByteBuffer.wrap(encodedString.getUnsafeBytes(), encodedString.begin(), encodedString.length());
        int type = 0;
View Full Code Here

        ByteList r = new ByteList(arr, start, buf.limit() - start);

        EncodingDB.Entry entry = EncodingDB.getEncodings().get(displayName.getBytes());
        if (entry != null) {
            Encoding charset = entry.getEncoding();
            r.setEncoding(charset);
        }

        return getRuntime().newString(r);
    }
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.