sis-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From desruisse...@apache.org
Subject svn commit: r1652447 - in /sis/branches/JDK8/core/sis-utility/src: main/java/org/apache/sis/util/StringBuilders.java test/java/org/apache/sis/util/StringBuildersTest.java
Date Fri, 16 Jan 2015 16:27:34 GMT
Author: desruisseaux
Date: Fri Jan 16 16:27:33 2015
New Revision: 1652447

URL: http://svn.apache.org/r1652447
Log:
toASCII(...) now replace also various kind of space characters.

Modified:
    sis/branches/JDK8/core/sis-utility/src/main/java/org/apache/sis/util/StringBuilders.java
    sis/branches/JDK8/core/sis-utility/src/test/java/org/apache/sis/util/StringBuildersTest.java

Modified: sis/branches/JDK8/core/sis-utility/src/main/java/org/apache/sis/util/StringBuilders.java
URL: http://svn.apache.org/viewvc/sis/branches/JDK8/core/sis-utility/src/main/java/org/apache/sis/util/StringBuilders.java?rev=1652447&r1=1652446&r2=1652447&view=diff
==============================================================================
--- sis/branches/JDK8/core/sis-utility/src/main/java/org/apache/sis/util/StringBuilders.java
[UTF-8] (original)
+++ sis/branches/JDK8/core/sis-utility/src/main/java/org/apache/sis/util/StringBuilders.java
[UTF-8] Fri Jan 16 16:27:33 2015
@@ -212,21 +212,38 @@ public final class StringBuilders extend
      */
     static CharSequence toASCII(CharSequence text, StringBuilder buffer) {
         if (text != null) {
-            final int length = text.length();
-            for (int i=0; i<length;) {
-                final int c = codePointAt(text, i);
+            /*
+             * Scan the buffer in reverse order because we may suppress some characters.
+             */
+            int i = text.length();
+            while (i > 0) {
+                final int c = codePointBefore(text, i);
+                final int n = charCount(c);
                 final int r = c - 0xC0;
-                if (r >= 0 && r<ASCII.length()) {
-                    final char ac = ASCII.charAt(r);
+                i -= n; // After this line, 'i' is the index of character 'c'.
+                if (r >= 0) {
+                    final char cr; // The character replacement.
+                    if (r < ASCII.length()) {
+                        cr = ASCII.charAt(r);
+                    } else {
+                        switch (getType(c)) {
+                            case SPACE_SEPARATOR: cr = ' '; break;
+                            case PARAGRAPH_SEPARATOR: // Fall through
+                            case LINE_SEPARATOR: cr = '\n'; break;
+                            default: continue;
+                        }
+                    }
                     if (buffer == null) {
                         buffer = new StringBuilder(text.length()).append(text);
                         text = buffer;
                     }
-                    // Nothing special do to about codepoint here, since 'c' is
-                    // in the basic plane (verified by the r<ASCII.length() check).
-                    buffer.setCharAt(i, ac);
+                    if (n == 2) {
+                        buffer.deleteCharAt(i + 1); // Remove the low surrogate of a surrogate
pair.
+                    }
+                    // Nothing special to do about codepoint here, since 'c' is in
+                    // the basic plane (verified by the r < ASCII.length() check).
+                    buffer.setCharAt(i, cr);
                 }
-                i += charCount(c);
             }
         }
         return text;

Modified: sis/branches/JDK8/core/sis-utility/src/test/java/org/apache/sis/util/StringBuildersTest.java
URL: http://svn.apache.org/viewvc/sis/branches/JDK8/core/sis-utility/src/test/java/org/apache/sis/util/StringBuildersTest.java?rev=1652447&r1=1652446&r2=1652447&view=diff
==============================================================================
--- sis/branches/JDK8/core/sis-utility/src/test/java/org/apache/sis/util/StringBuildersTest.java
[UTF-8] (original)
+++ sis/branches/JDK8/core/sis-utility/src/test/java/org/apache/sis/util/StringBuildersTest.java
[UTF-8] Fri Jan 16 16:27:33 2015
@@ -97,8 +97,8 @@ public final strictfp class StringBuilde
      */
     @Test
     public void testToASCII() {
-        final StringBuilder metre = new StringBuilder("mètre");
+        final StringBuilder metre = new StringBuilder("mètres" + Characters.PARAGRAPH_SEPARATOR);
         toASCII(metre);
-        assertEquals("metre", metre.toString());
+        assertEquals("metres\n", metre.toString());
     }
 }



Mime
View raw message