📄 pystring.java
字号:
case '\'': v.append('\''); break; case '\"': v.append('\"'); break; case 'b': v.append('\b'); break; case 'f': v.append('\014'); break; /* FF */ case 't': v.append('\t'); break; case 'n': v.append('\n'); break; case 'r': v.append('\r'); break; case 'v': v.append('\013'); break; /* VT */ case 'a': v.append('\007'); break; /* BEL, not classic C */ /* \OOO (octal) escapes */ case '0': case '1': case '2': case '3': case '4': case '5': case '6': case '7': int x = Character.digit(ch, 8); for (int j = 0; j < 2 && s < end; j++, s++) { ch = str.charAt(s); if (ch < '0' || ch > '7') break; x = (x<<3) + Character.digit(ch, 8); } v.append((char) x); break; case 'x': int i; for (x = 0, i = 0; i < 2 && s < end; i++) { ch = str.charAt(s + i); int d = Character.digit(ch, 16); if (d == -1) { codecs.decoding_error("unicode escape", v, errors, "truncated \\xXX"); i++; break; } x = ((x<<4) & ~0xF) + d; } s += i; v.append((char) x); break; /* \ uXXXX with 4 hex digits */ case 'u': if (!unicode) { v.append('\\'); v.append('u'); break; } if (s+4 > end) { codecs.decoding_error("unicode escape", v, errors, "truncated \\uXXXX"); break; } for (x = 0, i = 0; i < 4; i++) { ch = str.charAt(s + i); int d = Character.digit(ch, 16); if (d == -1) { codecs.decoding_error("unicode escape", v, errors, "truncated \\uXXXX"); break; } x = ((x<<4) & ~0xF) + d; } s += i; v.append((char) x); break; case 'N': if (!unicode) { v.append('\\'); v.append('N'); break; } /* Ok, we need to deal with Unicode Character Names now, * make sure we've imported the hash table data... */ if (pucnHash == null) { PyObject mod = imp.importName("ucnhash", true); mod = mod.__call__(); pucnHash = (ucnhashAPI) mod.__tojava__(Object.class); if (pucnHash.getCchMax() < 0) codecs.decoding_error("unicode escape", v, errors, "Unicode names not loaded"); } if (str.charAt(s) == '{') { int startName = s + 1; int endBrace = startName; /* look for either the closing brace, or we * exceed the maximum length of the unicode * character names */ int maxLen = pucnHash.getCchMax(); while (endBrace < end && str.charAt(endBrace) != '}' && (endBrace - startName) <= maxLen) { endBrace++; } if (endBrace != end && str.charAt(endBrace) == '}') { int value = pucnHash.getValue(str, startName, endBrace); if (value < 0) { codecs.decoding_error("unicode escape", v, errors, "Invalid Unicode Character Name"); v.append('\\'); v.append(str.charAt(s-1)); break; } if (value < 1<<16) { /* In UCS-2 range, easy solution.. */ v.append((char) value); } else { /* Oops, its in UCS-4 space, */ /* compute and append the two surrogates: */ /* translate from 10000..10FFFF to 0..FFFFF */ value -= 0x10000; /* high surrogate = top 10 bits added to D800 */ v.append((char) (0xD800 + (value >> 10))); /* low surrogate = bottom 10 bits added to DC00*/ v.append((char) (0xDC00 + (value & ~0xFC00))); } s = endBrace + 1; } else { codecs.decoding_error("unicode escape", v, errors, "Unicode name missing closing brace"); v.append('\\'); v.append(str.charAt(s-1)); break; } break; } codecs.decoding_error("unicode escape", v, errors, "Missing opening brace for Unicode " + "Character Name escape"); /* fall through on purpose */ default: v.append('\\'); v.append(str.charAt(s-1)); break; } } return v.toString(); } public boolean equals(Object other) { if (!(other instanceof PyString)) return false; PyString o = (PyString)other; if (interned && o.interned) return string == o.string; return string.equals(o.string); } public int __cmp__(PyObject other) { if (!(other instanceof PyString)) return -2; int c = string.compareTo(((PyString)other).string); return c < 0 ? -1 : c > 0 ? 1 : 0; } public PyObject __eq__(PyObject other) { String s = coerce(other); if (s == null) return null; return string.equals(s) ? Py.One : Py.Zero; } public PyObject __ne__(PyObject other) { String s = coerce(other); if (s == null) return null; return string.equals(s) ? Py.Zero : Py.One; } public PyObject __lt__(PyObject other) { String s = coerce(other); if (s == null) return null; return string.compareTo(s) < 0 ? Py.One : Py.Zero; } public PyObject __le__(PyObject other) { String s = coerce(other); if (s == null) return null; return string.compareTo(s) <= 0 ? Py.One : Py.Zero; } public PyObject __gt__(PyObject other) { String s = coerce(other); if (s == null) return null; return string.compareTo(s) > 0 ? Py.One : Py.Zero; } public PyObject __ge__(PyObject other) { String s = coerce(other); if (s == null) return null; return string.compareTo(s) >= 0 ? Py.One : Py.Zero; } private static String coerce(PyObject o) { if (o instanceof PyString) return o.toString(); return null; } public int hashCode() { if (cached_hashcode == 0) cached_hashcode = string.hashCode(); return cached_hashcode; } private byte[] getBytes() { byte[] buf = new byte[string.length()]; string.getBytes(0, string.length(), buf, 0); return buf; } public Object __tojava__(Class c) { //This is a hack to make almost all Java calls happy if (c == String.class || c == Object.class || c == Serializable.class) return string; if (c == Character.TYPE || c == Character.class) if (string.length() == 1) return new Character(string.charAt(0)); if (c.isArray()) { if (c.getComponentType() == Byte.TYPE) return getBytes(); if (c.getComponentType() == Character.TYPE) return string.toCharArray(); } if (c.isInstance(this)) return this; return Py.NoConversion; } protected PyObject get(int i) { return Py.newString(string.charAt(i)); } protected PyObject getslice(int start, int stop, int step) { if (step > 0 && stop < start) stop = start; if (step == 1) return new PyString(string.substring(start, stop)); else { int n = sliceLength(start, stop, step); char new_chars[] = new char[n]; int j = 0; for (int i=start; j<n; i+=step) new_chars[j++] = string.charAt(i); return new PyString(new String(new_chars)); } } public boolean __contains__(PyObject o) { if (!(o instanceof PyString) || o.__len__() != 1) throw Py.TypeError("string member test needs char left operand"); PyString other = (PyString) o; return string.indexOf(other.string) >= 0; } protected PyObject repeat(int count) { if (count < 0) count = 0; int s = string.length(); char new_chars[] = new char[s*count]; for (int i=0; i<count; i++) { string.getChars(0, s, new_chars, i*s); } return new PyString(new String(new_chars)); } public PyObject __add__(PyObject generic_other) { if (generic_other instanceof PyString) { PyString other = (PyString)generic_other; return new PyString(string.concat(other.string)); } else return null; } public PyObject __mod__(PyObject other) { StringFormatter fmt = new StringFormatter(string); return new PyString(fmt.format(other)); } public PyInteger __int__() { return Py.newInteger(atoi(10)); } public PyLong __long__() { return atol(10); } public PyFloat __float__() { return new PyFloat(atof()); } public PyComplex __complex__() { boolean got_re = false; boolean got_im = false; boolean done = false; boolean sw_error = false; int s = 0; int n = string.length(); while (s < n && Character.isSpaceChar(string.charAt(s))) s++; if (s == n) { throw Py.ValueError("empty string for complex()"); } double z = -1.0; double x = 0.0; double y = 0.0; int sign = 1; do { char c = string.charAt(s); switch (c) { case '-': sign = -1; /* Fallthrough */ case '+': if (done || s+1 == n) { sw_error = true; break; } // a character is guaranteed, but it better be a digit // or J or j c = string.charAt(++s); // eat the sign character // and check the next if (!Character.isDigit(c) && c!='J' && c!='j') sw_error = true; break; case 'J': case 'j': if (got_im || done) { sw_error = true; break; } if (z < 0.0) { y = sign; } else { y = sign * z; } got_im = true; done = got_re; sign = 1; s++; // eat the J or j break; case ' ': while (s < n && Character.isSpaceChar(string.charAt(s))) s++; if (s != n) sw_error = true; break; default: boolean digit_or_dot = (c == '.' || Character.isDigit(c)); if (!digit_or_dot) { sw_error = true; break; } int end = endDouble(string, s); z = Double.valueOf(string.substring(s, end)).doubleValue(); s=end; if (s < n) { c = string.charAt(s); if (c == 'J' || c == 'j') { break; } } if (got_re) { sw_error = true; break; } /* accept a real part */ x = sign * z; got_re = true; done = got_im; z = -1.0; sign = 1; break; } /* end of switch */ } while (s < n && !sw_error); if (sw_error) { throw Py.ValueError("malformed string for complex() " + string.substring(s)); } return new PyComplex(x,y); } private int endDouble(String string, int s) { int n = string.length(); while (s < n) { char c = string.charAt(s++); if (Character.isDigit(c)) continue; if (c == '.') continue; if (c == 'e' || c == 'E') { if (s < n) { c = string.charAt(s); if (c == '+' || c == '-') s++; continue; } } return s-1;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -