📄 pystring.java
字号:
if (base == 0) base = 10; String s = string; if (b > 0 || e < string.length()) s = string.substring(b, e); try { long result = Long.parseLong(s, base); if (result < 0 && !(sign == '-' && result == -result)) throw Py.ValueError("invalid literal for __int__: "+string); if (sign == '-') result = - result; if (result < Integer.MIN_VALUE || result > Integer.MAX_VALUE) throw Py.ValueError("invalid literal for __int__: "+string); return (int) result; } catch (NumberFormatException exc) { throw Py.ValueError("invalid literal for __int__: "+string); } catch (StringIndexOutOfBoundsException exc) { throw Py.ValueError("invalid literal for __int__: "+string); } } public PyLong atol() { return atol(10); } public PyLong atol(int base) { String str = string; int b = 0; int e = str.length(); while (b < e && Character.isWhitespace(str.charAt(b))) b++; while (e > b && Character.isWhitespace(str.charAt(e-1))) e--; if (e > b && (str.charAt(e-1) == 'L' || str.charAt(e-1) == 'l')) e--; char sign = 0; if (b < e) { sign = string.charAt(b); if (sign == '-' || sign == '+') { b++; while (b < e && Character.isWhitespace(str.charAt(b))) b++; } if (base == 0 || base == 16) { if (string.charAt(b) == '0') { if (b < e-1 && Character.toUpperCase(string.charAt(b+1)) == 'X') { base = 16; b += 2; } else { if (base == 0) base = 8; } } } } if (base == 0) base = 10; if (base < 2 || base > 36) throw Py.ValueError("invalid base for long literal:" + base); if (b > 0 || e < str.length()) str = str.substring(b, e); try { java.math.BigInteger bi = null; if (sign == '-') bi = new java.math.BigInteger("-" + str, base); else bi = new java.math.BigInteger(str, base); return new PyLong(bi); } catch (NumberFormatException exc) { throw Py.ValueError("invalid literal for __long__: "+str); } catch (StringIndexOutOfBoundsException exc) { throw Py.ValueError("invalid literal for __long__: "+str); } } private static String spaces(int n) { char[] chars = new char[n]; for (int i=0; i<n; i++) chars[i] = ' '; return new String(chars); } public String ljust(int width) { int n = width-string.length(); if (n <= 0) return string; return string+spaces(n); } public String rjust(int width) { int n = width-string.length(); if (n <= 0) return string; return spaces(n)+string; } public String center(int width) { int n = width-string.length(); if (n <= 0) return string; int half = n/2; if (n%2 > 0 && width%2 > 0) half += 1; return spaces(half)+string+spaces(n-half); } public String zfill(int width) { String s = string; int n = s.length(); if (n >= width) return s; char[] chars = new char[width]; int nzeros = width-n; int i=0; int sStart=0; if (n > 0) { char start = s.charAt(0); if (start == '+' || start == '-') { chars[0] = start; i += 1; nzeros++; sStart=1; } } for(;i<nzeros; i++) { chars[i] = '0'; } s.getChars(sStart, s.length(), chars, i); return new String(chars); } public String expandtabs() { return expandtabs(8); } public String expandtabs(int tabsize) { String s = string; StringBuffer buf = new StringBuffer((int)(s.length()*1.5)); char[] chars = s.toCharArray(); int n = chars.length; int position = 0; for(int i=0; i<n; i++) { char c = chars[i]; if (c == '\t') { int spaces = tabsize-position%tabsize; position += spaces; while (spaces-- > 0) { buf.append(' '); } continue; } if (c == '\n' || c == '\r') { position = -1; } buf.append(c); position++; } return buf.toString(); } public String capitalize() { if (string.length() == 0) return string; String first = string.substring(0,1).toUpperCase(); return first.concat(string.substring(1).toLowerCase()); } public String replace(String oldPiece, String newPiece) { return replace(oldPiece, newPiece, string.length()); } public String replace(String oldPiece, String newPiece, int maxsplit) { PyString newstr = new PyString(newPiece); return newstr.join(split(oldPiece, maxsplit)); } public String join(PyObject seq) { StringBuffer buf = new StringBuffer(); PyObject iter = seq.__iter__(); PyObject obj = null; for (int i = 0; (obj = iter.__iternext__()) != null; i++) { if (!(obj instanceof PyString)) throw Py.TypeError( "sequence item " + i + ": expected string, " + obj.safeRepr() + " found"); if (i > 0) buf.append(string); buf.append(obj.__str__()); } return buf.toString(); } public boolean startswith(String prefix) { return string.startsWith(prefix); } public boolean startswith(String prefix, int offset) { return string.startsWith(prefix, offset); } public boolean startswith(String prefix, int start, int end) { if (start < 0 || start + prefix.length() > string.length()) return false; if (end > string.length()) end = string.length(); String substr = string.substring(start, end); return substr.startsWith(prefix); } public boolean endswith(String suffix) { return string.endsWith(suffix); } public boolean endswith(String suffix, int start) { return endswith(suffix, start, string.length()); } public boolean endswith(String suffix, int start, int end) { int len = string.length(); if (start < 0 || start > len || suffix.length() > len) return false; end = (end <= len ? end : len); if (end < start) return false; String substr = string.substring(start, end); return substr.endsWith(suffix); } //public static String zfill(PyObject o, int width) { // return zfill(o.toString(), width); //} public String translate(String table) { return translate(table, null); } public String translate(String table, String deletechars) { if (table.length() != 256) throw Py.ValueError( "translation table must be 256 characters long"); StringBuffer buf = new StringBuffer(string.length()); for (int i=0; i < string.length(); i++) { char c = string.charAt(i); if (deletechars != null && deletechars.indexOf(c) >= 0) continue; try { buf.append(table.charAt(c)); } catch (IndexOutOfBoundsException e) { throw Py.TypeError( "translate() only works for 8-bit character strings"); } } return buf.toString(); } public String translate(PyObject table) { StringBuffer v = new StringBuffer(string.length()); for (int i=0; i < string.length(); i++) { char ch = string.charAt(i); PyObject w = Py.newInteger(ch); PyObject x = table.__finditem__(w); if (x == null) { /* No mapping found: default to 1-1 mapping */ v.append(ch); continue; } /* Apply mapping */ if (x instanceof PyInteger) { int value = ((PyInteger) x).getValue(); v.append((char) value); } else if (x == Py.None) { ; } else if (x instanceof PyString) { if (x.__len__() != 1) { /* 1-n mapping */ throw new PyException(Py.NotImplementedError, "1-n mappings are currently not implemented"); } v.append(x.toString()); } else { /* wrong return value */ throw Py.TypeError( "character mapping must return integer, " + "None or unicode"); } } return v.toString(); } public boolean islower() { int n = string.length(); /* Shortcut for single character strings */ if (n == 1) return Character.isLowerCase(string.charAt(0)); boolean cased = false; for (int i = 0; i < n; i++) { char ch = string.charAt(i); if (Character.isUpperCase(ch) || Character.isTitleCase(ch)) return false; else if (!cased && Character.isLowerCase(ch)) cased = true; } return cased; } public boolean isupper() { int n = string.length(); /* Shortcut for single character strings */ if (n == 1) return Character.isUpperCase(string.charAt(0)); boolean cased = false; for (int i = 0; i < n; i++) { char ch = string.charAt(i); if (Character.isLowerCase(ch) || Character.isTitleCase(ch)) return false; else if (!cased && Character.isUpperCase(ch)) cased = true; } return cased; } public boolean isalpha() { int n = string.length(); /* Shortcut for single character strings */ if (n == 1) return Character.isLetter(string.charAt(0)); if (n == 0) return false; for (int i = 0; i < n; i++) { char ch = string.charAt(i); if (!Character.isLetter(ch)) return false; } return true; } public boolean isalnum() { int n = string.length(); /* Shortcut for single character strings */ if (n == 1) return _isalnum(string.charAt(0)); if (n == 0) return false; for (int i = 0; i < n; i++) { char ch = string.charAt(i); if (!_isalnum(ch)) return false; } return true; } private boolean _isalnum(char ch) { // This can ever be entirely compatible with CPython. In CPython // The type is not used, the numeric property is determined from // the presense of digit, decimal or numeric fields. These fields // are not available in exactly the same way in java. return Character.isLetterOrDigit(ch) || Character.getType(ch) == Character.LETTER_NUMBER; } public boolean isdecimal() { int n = string.length(); /* Shortcut for single character strings */ if (n == 1) { char ch = string.charAt(0); return _isdecimal(ch); } if (n == 0) return false; for (int i = 0; i < n; i++) { char ch = string.charAt(i); if (!_isdecimal(ch)) return false; } return true; } private boolean _isdecimal(char ch) { // See the comment in _isalnum. Here it is even worse. return Character.getType(ch) == Character.DECIMAL_DIGIT_NUMBER; } public boolean isdigit() { int n = string.length(); /* Shortcut for single character strings */ if (n == 1) return Character.isDigit(string.charAt(0)); if (n == 0) return false; for (int i = 0; i < n; i++) {
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -