📄 pystring.java
字号:
char ch = string.charAt(i); if (!Character.isDigit(ch)) return false; } return true; } public boolean isnumeric() { int n = string.length(); /* Shortcut for single character strings */ if (n == 1) return _isnumeric(string.charAt(0)); if (n == 0) return false; for (int i = 0; i < n; i++) { char ch = string.charAt(i); if (!_isnumeric(ch)) return false; } return true; } private boolean _isnumeric(char ch) { int type = Character.getType(ch); return type == Character.DECIMAL_DIGIT_NUMBER || type == Character.LETTER_NUMBER || type == Character.OTHER_NUMBER; } public boolean istitle() { int n = string.length(); /* Shortcut for single character strings */ if (n == 1) return Character.isTitleCase(string.charAt(0)) || Character.isUpperCase(string.charAt(0)); boolean cased = false; boolean previous_is_cased = false; for (int i = 0; i < n; i++) { char ch = string.charAt(i); if (Character.isUpperCase(ch) || Character.isTitleCase(ch)) { if (previous_is_cased) return false; previous_is_cased = true; cased = true; } else if (Character.isLowerCase(ch)) { if (!previous_is_cased) return false; previous_is_cased = true; cased = true; } else previous_is_cased = false; } return cased; } public boolean isspace() { int n = string.length(); /* Shortcut for single character strings */ if (n == 1) return Character.isWhitespace(string.charAt(0)); if (n == 0) return false; for (int i = 0; i < n; i++) { char ch = string.charAt(i); if (!Character.isWhitespace(ch)) return false; } return true; } public boolean isunicode() { int n = string.length(); for (int i = 0; i < n; i++) { char ch = string.charAt(i); if (ch > 255) return true; } return false; } public PyString encode() { return encode(null, null); } public PyString encode(String encoding) { return encode(encoding, null); } public PyString encode(String encoding, String errors) { return codecs.encode(this, encoding, errors); } public PyString decode() { return encode(null, null); } public PyString decode(String encoding) { return decode(encoding, null); } public PyString decode(String encoding, String errors) { return codecs.decode(this, encoding, errors); }}final class StringFormatter{ int index; String format; StringBuffer buffer; boolean negative; int precision; int argIndex; PyObject args; final char pop() { try { return format.charAt(index++); } catch (StringIndexOutOfBoundsException e) { throw Py.ValueError("incomplete format"); } } final char peek() { return format.charAt(index); } final void push() { index--; } public StringFormatter(String format) { index = 0; this.format = format; buffer = new StringBuffer(format.length()+100); } PyObject getarg() { PyObject ret = null; switch(argIndex) { // special index indicating a mapping case -3: return args; // special index indicating a single item that has already been // used case -2: break; // special index indicating a single item that has not yet been // used case -1: argIndex=-2; return args; default: ret = args.__finditem__(argIndex++); break; } if (ret == null) throw Py.TypeError("not enough arguments for format string"); return ret; } int getNumber() { char c = pop(); if (c == '*') { PyObject o = getarg(); if (o instanceof PyInteger) return ((PyInteger)o).getValue(); throw Py.TypeError("* wants int"); } else { if (Character.isDigit(c)) { int numStart = index-1; while (Character.isDigit(c = pop())) ; index -= 1; Integer i = Integer.valueOf( format.substring(numStart, index)); return i.intValue(); } index -= 1; return 0; } } public String formatLong(PyString arg, char type, boolean altFlag) { String s = arg.toString(); int end = s.length(); int ptr = 0; int numnondigits = 0; if (type == 'x' || type == 'X') numnondigits = 2; if (s.endsWith("L")) end--; negative = s.charAt(0) == '-'; if (negative) { ptr++; } int numdigits = end - numnondigits - ptr; if (!altFlag) { switch (type) { case 'o' : if (numdigits > 1) { ++ptr; --numdigits; } break; case 'x' : case 'X' : ptr += 2; numnondigits -= 2; break; } } if (precision > numdigits) { StringBuffer buf = new StringBuffer(); for (int i = 0; i < numnondigits; ++i) buf.append(s.charAt(ptr++)); for (int i = 0; i < precision - numdigits; i++) buf.append('0'); for (int i = 0; i < numdigits; i++) buf.append(s.charAt(ptr++)); s = buf.toString(); } else if (end < s.length() || ptr > 0) s = s.substring(ptr, end); switch (type) { case 'x' : s = s.toLowerCase(); break; } return s; } public String formatInteger(PyObject arg, int radix, boolean unsigned) { return formatInteger(arg.__int__().getValue(), radix, unsigned); } public String formatInteger(long v, int radix, boolean unsigned) { if (unsigned) { if (v < 0) v = 0x100000000l + v; } else { if (v < 0) { negative = true; v = -v; } } String s = Long.toString(v, radix); while (s.length() < precision) { s = "0"+s; } return s; } public String formatFloatDecimal(PyObject arg, boolean truncate) { return formatFloatDecimal(arg.__float__().getValue(), truncate); } public String formatFloatDecimal(double v, boolean truncate) { java.text.NumberFormat format = java.text.NumberFormat.getInstance( java.util.Locale.US); int prec = precision; if (prec == -1) prec = 6; if (v < 0) { v = -v; negative = true; } format.setMaximumFractionDigits(prec); format.setMinimumFractionDigits(truncate ? 0 : prec); format.setGroupingUsed(false); String ret = format.format(v);// System.err.println("formatFloat: "+v+", prec="+prec+", ret="+ret);// if (ret.indexOf('.') == -1) {// return ret+'.';// } return ret; } public String formatFloatExponential(PyObject arg, char e, boolean truncate) { StringBuffer buf = new StringBuffer(); double v = arg.__float__().getValue(); boolean isNegative = false; if (v < 0) { v = -v; isNegative = true; } double power = 0.0; if (v > 0) power = Math.floor(Math.log(v)/Math.log(10)); //System.err.println("formatExp: "+v+", "+power); int savePrecision = precision; if (truncate) precision = -1; else precision = 3; String exp = formatInteger((long)power, 10, false); if (negative) { negative = false; exp = '-'+exp; } else { if (!truncate) exp = '+'+exp; } precision = savePrecision; double base = v/Math.pow(10, power); buf.append(formatFloatDecimal(base, truncate)); buf.append(e); buf.append(exp); negative = isNegative; return buf.toString(); } public String format(PyObject args) { PyObject dict = null; this.args = args; if (args instanceof PyTuple) { argIndex = 0; } else { // special index indicating a single item rather than a tuple argIndex = -1; if (args instanceof PyDictionary || args instanceof PyStringMap || (!(args instanceof PySequence) && args.__findattr__("__getitem__") != null)) { dict = args; argIndex = -3; } } while (index < format.length()) { boolean ljustFlag=false; boolean signFlag=false; boolean blankFlag=false; boolean altFlag=false; boolean zeroFlag=false; int width = -1; precision = -1; char c = pop(); if (c != '%') { buffer.append(c); continue; } c = pop(); if (c == '(') { //System.out.println("( found"); if (dict == null) throw Py.TypeError("format requires a mapping"); int parens = 1; int keyStart = index; while (parens > 0) { c = pop(); if (c == ')') parens--; else if (c == '(') parens++; } String tmp = format.substring(keyStart, index-1); this.args = dict.__getitem__(new PyString(tmp)); //System.out.println("args: "+args+", "+argIndex); } else { push(); } while (true) { switch (c = pop()) { case '-': ljustFlag=true; continue; case '+': signFlag=true; continue; case ' ': blankFlag=true; continue; case '#': altFlag=true; continue; case '0': zeroFlag=true; continue; } break; } push(); width = getNumber(); if (width < 0) { width = -width; ljustFlag = true; } c = pop(); if (c == '.') { precision = getNumber(); if (precision < -1) precision = 0; if (precision > 250) { // A magic number. Larger than in CPython. throw Py.OverflowError( "formatted float is too long (precision too long?)"); } c = pop(); } if (c == 'h' || c == 'l' || c == 'L') { c = pop(); } if (c == '%') { buffer.append(c); continue; } PyObject ar
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -