From: <pj...@us...> - 2008-11-07 08:20:28
|
Revision: 5555 http://jython.svn.sourceforge.net/jython/?rev=5555&view=rev Author: pjenvey Date: 2008-11-07 08:20:22 +0000 (Fri, 07 Nov 2008) Log Message: ----------- add a simple unicodeobject::PyUnicode_EncodeDecimal equivalent for unicode number conversion. fixes test_unicode.test_codecs_errors Modified Paths: -------------- trunk/jython/Lib/test/test_unicode_jy.py trunk/jython/src/org/python/core/PyUnicode.java Modified: trunk/jython/Lib/test/test_unicode_jy.py =================================================================== --- trunk/jython/Lib/test/test_unicode_jy.py 2008-11-07 05:27:30 UTC (rev 5554) +++ trunk/jython/Lib/test/test_unicode_jy.py 2008-11-07 08:20:22 UTC (rev 5555) @@ -50,7 +50,15 @@ self.assertEqual(ord(bar[2]), 92) self.assertEqual(ord(bar[3]), 110) + def test_encode_decimal(self): + self.assertEqual(int(u'\u0039\u0032'), 92) + self.assertEqual(int(u'\u0660'), 0) + self.assertEqual(int(u' \u001F\u0966\u096F\u0039'), 99) + self.assertEqual(long(u'\u0663'), 3) + self.assertEqual(float(u'\u0663.\u0661'), 3.1) + self.assertEqual(complex(u'\u0663.\u0661'), 3.1+0j) + def test_main(): test_support.run_unittest(UnicodeTestCase) Modified: trunk/jython/src/org/python/core/PyUnicode.java =================================================================== --- trunk/jython/src/org/python/core/PyUnicode.java 2008-11-07 05:27:30 UTC (rev 5554) +++ trunk/jython/src/org/python/core/PyUnicode.java 2008-11-07 08:20:22 UTC (rev 5555) @@ -1311,6 +1311,93 @@ return newSubsequenceIterator(); } + @Override + public PyComplex __complex__() { + return new PyString(encodeDecimal()).__complex__(); + } + + @Override + public int atoi(int base) { + return new PyString(encodeDecimal()).atoi(base); + } + + @Override + public PyLong atol(int base) { + return new PyString(encodeDecimal()).atol(base); + } + + @Override + public double atof() { + return new PyString(encodeDecimal()).atof(); + } + + /** + * Encode unicode into a valid decimal String. Throws a UnicodeEncodeError on invalid + * characters. + * + * @return a valid decimal as an encoded String + */ + private String encodeDecimal() { + if (isBasicPlane()) { + return encodeDecimalBasic(); + } + + int digit; + StringBuilder sb = new StringBuilder(); + int i = 0; + for (Iterator<Integer> iter = newSubsequenceIterator(); iter.hasNext(); i++) { + int codePoint = iter.next(); + if (Character.isWhitespace(codePoint)) { + sb.append(' '); + continue; + } + digit = Character.digit(codePoint, 10); + if (digit >= 0) { + sb.append(digit); + continue; + } + if (0 < codePoint && codePoint < 256) { + sb.appendCodePoint(codePoint); + continue; + } + // All other characters are considered unencodable + codecs.encoding_error("strict", "decimal", string, i, i + 1, + "invalid decimal Unicode string"); + } + return sb.toString(); + } + + /** + * Encode unicode in the basic plane into a valid decimal String. Throws a + * UnicodeEncodeError on invalid characters. + * + * @return a valid decimal as an encoded String + */ + private String encodeDecimalBasic() { + int digit; + StringBuilder sb = new StringBuilder(); + for (int i = 0; i < string.length(); i++) { + char ch = string.charAt(i); + if (Character.isWhitespace(ch)) { + sb.append(' '); + continue; + } + digit = Character.digit(ch, 10); + if (digit >= 0) { + sb.append(digit); + continue; + } + if (0 < ch && ch < 256) { + sb.append(ch); + continue; + } + // All other characters are considered unencodable + codecs.encoding_error("strict", "decimal", string, i, i + 1, + "invalid decimal Unicode string"); + } + return sb.toString(); + } + @ExposedMethod final String unicode_toString() { return toString(); This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |