[Jython-checkins] jython: Implemented bytearray.capitalize, lower, upper, swapcase, title.
frank.wierzbicki
jython-checkins at python.org
Wed Jun 13 20:44:04 CEST 2012
http://hg.python.org/jython/rev/1b43f3c6e324
changeset: 6707:1b43f3c6e324
user: Jeff Allen <ja...py at farowl.co.uk>
date: Mon Jun 11 08:22:04 2012 +0100
summary:
Implemented bytearray.capitalize, lower, upper, swapcase, title.
Now scoring 1 failure and 8 errors in test_bytes.py
files:
src/org/python/core/BaseBytes.java | 195 +++++++++++++++
src/org/python/core/PyByteArray.java | 31 ++-
2 files changed, 225 insertions(+), 1 deletions(-)
diff --git a/src/org/python/core/BaseBytes.java b/src/org/python/core/BaseBytes.java
--- a/src/org/python/core/BaseBytes.java
+++ b/src/org/python/core/BaseBytes.java
@@ -3733,6 +3733,201 @@
return hasCased;
}
+ //
+ // Case transformations
+ //
+
+ /**
+ * Java API equivalent of Python <code>capitalize()</code>. This method treats the bytes as
+ * Unicode pont codes and is consistent with Java's {@link Character#toUpperCase(char)} and
+ * {@link Character#toLowerCase(char)}.
+ *
+ * @return a copy of the array with its first character capitalized and the rest lowercased.
+ */
+ public BaseBytes capitalize() {
+ return basebytes_capitalize();
+ }
+
+ /**
+ * Ready-to-expose implementation of Python <code>capitalize()</code>.
+ *
+ * @return a copy of the array with its first character capitalized and the rest lowercased.
+ */
+ final BaseBytes basebytes_capitalize() {
+
+ Builder builder = getBuilder(size);
+
+ if (size > 0) {
+ // Treat first character
+ char c = charAt(0);
+ if (Character.isLowerCase(c)) {
+ c = Character.toUpperCase(c);
+ }
+ // Put the adjusted character in the output as a byte
+ builder.append((byte)c);
+
+ // Treat the rest
+ for (int i = 1; i < size; i++) {
+ c = charAt(i);
+ if (Character.isUpperCase(c)) {
+ c = Character.toLowerCase(c);
+ }
+ // Put the adjusted character in the output as a byte
+ builder.append((byte)c);
+ }
+ }
+
+ return builder.getResult();
+ }
+
+ /**
+ * Java API equivalent of Python <code>lower()</code>. This method treats the bytes as Unicode
+ * pont codes and is consistent with Java's {@link Character#toLowerCase(char)}.
+ *
+ * @return a copy of the array with all the cased characters converted to lowercase.
+ */
+ public BaseBytes lower() {
+ return basebytes_lower();
+ }
+
+ /**
+ * Ready-to-expose implementation of Python <code>lower()</code>.
+ *
+ * @return a copy of the array with all the cased characters converted to lowercase.
+ */
+ final BaseBytes basebytes_lower() {
+
+ Builder builder = getBuilder(size);
+
+ for (int i = 0; i < size; i++) {
+ char c = charAt(i);
+ if (Character.isUpperCase(c)) {
+ c = Character.toLowerCase(c);
+ }
+ // Put the adjusted character in the output as a byte
+ builder.append((byte)c);
+ }
+
+ return builder.getResult();
+ }
+
+ /**
+ * Java API equivalent of Python <code>swapcase()</code>. This method treats the bytes as
+ * Unicode pont codes and is consistent with Java's {@link Character#toUpperCase(char)} and
+ * {@link Character#toLowerCase(char)}.
+ *
+ * @return a copy of the array with uppercase characters converted to lowercase and vice versa.
+ */
+ public BaseBytes swapcase() {
+ return basebytes_swapcase();
+ }
+
+ /**
+ * Ready-to-expose implementation of Python <code>swapcase()</code>.
+ *
+ * @return a copy of the array with uppercase characters converted to lowercase and vice versa.
+ */
+ final BaseBytes basebytes_swapcase() {
+
+ Builder builder = getBuilder(size);
+
+ for (int i = 0; i < size; i++) {
+ char c = charAt(i);
+ if (Character.isUpperCase(c)) {
+ c = Character.toLowerCase(c);
+ } else if (Character.isLowerCase(c)) {
+ c = Character.toUpperCase(c);
+ }
+ // Put the adjusted character in the output as a byte
+ builder.append((byte)c);
+ }
+
+ return builder.getResult();
+ }
+
+ /**
+ * Java API equivalent of Python <code>title()</code>. The algorithm uses a simple
+ * language-independent definition of a word as groups of consecutive letters. The definition
+ * works in many contexts but it means that apostrophes in contractions and possessives form
+ * word boundaries, which may not be the desired result.
+ *
+ * @return a titlecased version of the array where words start with an uppercase character and
+ * the remaining characters are lowercase.
+ */
+ public BaseBytes title() {
+ return basebytes_title();
+ }
+
+ /**
+ * Ready-to-expose implementation of Python <code>title()</code>.
+ *
+ * @return a titlecased version of the array where words start with an uppercase character and
+ * the remaining characters are lowercase.
+ */
+ final BaseBytes basebytes_title() {
+
+ Builder builder = getBuilder(size);
+ boolean inWord = false; // We begin, not in a word (sequence of cased characters)
+
+ for (int i = 0; i < size; i++) {
+ char c = charAt(i);
+
+ if (!inWord) {
+ // When we are not in a word ...
+ if (Character.isLowerCase(c)) {
+ c = Character.toUpperCase(c); // ... a lowercase letter must be upcased
+ inWord = true; // and it starts a word.
+ } else if (Character.isUpperCase(c)) {
+ inWord = true; // ... an uppercase letter just starts the word
+ }
+
+ } else {
+ // When we are in a word ...
+ if (Character.isUpperCase(c)) {
+ c = Character.toLowerCase(c); // ... an uppercase letter must be downcased
+ } else if (!Character.isLowerCase(c)) {
+ inWord = false; // ... and a non-letter ends the word
+ }
+ }
+ // Put the adjusted character in the output as a byte
+ builder.append((byte)c);
+ }
+ return builder.getResult();
+ }
+
+ /**
+ * Java API equivalent of Python <code>upper()</code>. Note that
+ * <code>x.upper().isupper()</code> might be <code>false</code> if the array contains uncased
+ * characters.
+ *
+ *
+ * @return a copy of the array with all the cased characters converted to uppercase.
+ */
+ public BaseBytes upper() {
+ return basebytes_upper();
+ }
+
+ /**
+ * Ready-to-expose implementation of Python <code>upper()</code>.
+ *
+ * @return a copy of the array with all the cased characters converted to uppercase.
+ */
+ final BaseBytes basebytes_upper() {
+
+ Builder builder = getBuilder(size);
+
+ for (int i = 0; i < size; i++) {
+ char c = charAt(i);
+ if (Character.isLowerCase(c)) {
+ c = Character.toUpperCase(c);
+ }
+ // Put the adjusted character in the output as a byte
+ builder.append((byte)c);
+ }
+
+ return builder.getResult();
+ }
+
/*
* ============================================================================================
* Java API for access as byte[]
diff --git a/src/org/python/core/PyByteArray.java b/src/org/python/core/PyByteArray.java
--- a/src/org/python/core/PyByteArray.java
+++ b/src/org/python/core/PyByteArray.java
@@ -1309,7 +1309,36 @@
return basebytes_isupper();
}
- /**
+ //
+ // Case transformations
+ //
+
+ @ExposedMethod(doc = BuiltinDocs.bytearray_capitalize_doc)
+ final PyByteArray bytearray_capitalize() {
+ return (PyByteArray)basebytes_capitalize();
+ }
+
+ @ExposedMethod(doc = BuiltinDocs.bytearray_lower_doc)
+ final PyByteArray bytearray_lower() {
+ return (PyByteArray)basebytes_lower();
+ }
+
+ @ExposedMethod(doc = BuiltinDocs.bytearray_swapcase_doc)
+ final PyByteArray bytearray_swapcase() {
+ return (PyByteArray)basebytes_swapcase();
+ }
+
+ @ExposedMethod(doc = BuiltinDocs.bytearray_title_doc)
+ final PyByteArray bytearray_title() {
+ return (PyByteArray)basebytes_title();
+ }
+
+ @ExposedMethod(doc = BuiltinDocs.bytearray_upper_doc)
+ final PyByteArray bytearray_upper() {
+ return (PyByteArray)basebytes_upper();
+ }
+
+ /**
* Implementation of Python <code>join(iterable)</code>. Return a bytearray which is the
* concatenation of the byte arrays in the iterable <code>iterable</code>. The separator between
* elements is the byte array providing this method.
--
Repository URL: http://hg.python.org/jython
More information about the Jython-checkins
mailing list