This is an automated email from the ASF dual-hosted git repository.

sunlan pushed a commit to branch GROOVY_4_0_X
in repository https://gitbox.apache.org/repos/asf/groovy.git


The following commit(s) were added to refs/heads/GROOVY_4_0_X by this push:
     new acd4b51e4a GROOVY-11436: trivial refactor and refine test
acd4b51e4a is described below

commit acd4b51e4a1fa7f4a1644d1319bcc1a845546da7
Author: Daniel Sun <sun...@apache.org>
AuthorDate: Sun Jul 7 10:24:51 2024 +0000

    GROOVY-11436: trivial refactor and refine test
    
    (cherry picked from commit c773e04ce402816b8f92c0318d08bde32e723eed)
---
 .../collection/runtime/AsciiTableMaker.groovy      | 54 ++++++++++++----------
 .../collection/runtime/AsciiTableMakerTest.groovy  | 18 ++++----
 2 files changed, 39 insertions(+), 33 deletions(-)

diff --git 
a/subprojects/groovy-ginq/src/main/groovy/org/apache/groovy/ginq/provider/collection/runtime/AsciiTableMaker.groovy
 
b/subprojects/groovy-ginq/src/main/groovy/org/apache/groovy/ginq/provider/collection/runtime/AsciiTableMaker.groovy
index e9fd6fbd45..a50cf8e9f7 100644
--- 
a/subprojects/groovy-ginq/src/main/groovy/org/apache/groovy/ginq/provider/collection/runtime/AsciiTableMaker.groovy
+++ 
b/subprojects/groovy-ginq/src/main/groovy/org/apache/groovy/ginq/provider/collection/runtime/AsciiTableMaker.groovy
@@ -28,7 +28,9 @@ import groovy.transform.PackageScope
 @CompileStatic
 class AsciiTableMaker {
     private static final String[] EMPTY_STRING_ARRAY = new String[0]
-    private static final int PADDING = 1 // Padding space for each cell
+    private static final int PADDING = 1
+    private static final String SPACE = " "
+    // Padding space for each cell
 
     /**
      * Makes ASCII table for list whose elements are of type {@link 
NamedRecord}.
@@ -62,7 +64,7 @@ class AsciiTableMaker {
         return tableData.toString()
     }
 
-    private static String buildTable(String[] headers, List<String[]> data, 
boolean[] alignLeft) throws UnsupportedEncodingException {
+    private static String buildTable(String[] headers, List<String[]> data, 
boolean[] alignLeft) {
         int[] columnWidths = calculateColumnWidths(headers, data)
 
         def headerCnt = headers ? headers.length : 0
@@ -92,7 +94,7 @@ class AsciiTableMaker {
         return data.size() * data[0].length
     }
 
-    private static int[] calculateColumnWidths(String[] headers, 
List<String[]> data) throws UnsupportedEncodingException {
+    private static int[] calculateColumnWidths(String[] headers, 
List<String[]> data) {
         int[] columnWidths = new int[headers.length]
 
         for (int i = 0; i < headers.length; i++) {
@@ -100,7 +102,7 @@ class AsciiTableMaker {
         }
 
         for (String[] row : data) {
-            for (int i = 0; i < row.length; i++) {
+            for (int i = 0, n = row.length; i < n; i++) {
                 int displayWidth = getDisplayWidth(row[i])
                 if (displayWidth > columnWidths[i]) {
                     columnWidths[i] = displayWidth
@@ -108,7 +110,7 @@ class AsciiTableMaker {
             }
         }
 
-        for (int i = 0; i < columnWidths.length; i++) {
+        for (int i = 0, n = columnWidths.length; i < n; i++) {
             columnWidths[i] += PADDING * 2 // Add padding
         }
 
@@ -145,13 +147,13 @@ class AsciiTableMaker {
         int paddingRight = width - displayWidth - PADDING
 
         if (alignLeft) {
-            padded.append(" " * PADDING) // Left padding
+            padded.append(SPACE * PADDING) // Left padding
             padded.append(str ?: '')
-            padded.append(" " * paddingRight) // Right padding
+            padded.append(SPACE * paddingRight) // Right padding
         } else {
-            padded.append(" " * paddingRight) // Left padding
+            padded.append(SPACE * paddingRight) // Left padding
             padded.append(str ?: '')
-            padded.append(" " * PADDING) // Right padding
+            padded.append(SPACE * PADDING) // Right padding
         }
 
         return padded.toString()
@@ -159,30 +161,32 @@ class AsciiTableMaker {
 
     private static int getDisplayWidth(String str) {
         if (!str) return 0
-
         int width = 0
         for (char c : str.toCharArray()) {
-            if (isFullWidth(c)) {
-                width += 2
-            } else {
-                width += 1
-            }
+            width += isFullWidth(c) ? 2 : 1
         }
         return width
     }
 
     private static boolean isFullWidth(char c) {
+        Character.UnicodeBlock block
+
+        try {
+            block = Character.UnicodeBlock.of(c)
+        } catch (IllegalArgumentException ignored) {
+            return false
+        }
+
         // Unicode block check for full-width characters
-        Character.UnicodeBlock block = Character.UnicodeBlock.of(c)
-        return block == Character.UnicodeBlock.CJK_UNIFIED_IDEOGRAPHS ||
-                block == Character.UnicodeBlock.CJK_COMPATIBILITY_IDEOGRAPHS ||
-                block == 
Character.UnicodeBlock.CJK_UNIFIED_IDEOGRAPHS_EXTENSION_A ||
-                block == Character.UnicodeBlock.GENERAL_PUNCTUATION ||
-                block == Character.UnicodeBlock.CJK_SYMBOLS_AND_PUNCTUATION ||
-                block == Character.UnicodeBlock.HALFWIDTH_AND_FULLWIDTH_FORMS 
||
-                block == Character.UnicodeBlock.HIRAGANA ||
-                block == Character.UnicodeBlock.KATAKANA ||
-                block == Character.UnicodeBlock.HANGUL_SYLLABLES
+        return block === Character.UnicodeBlock.CJK_UNIFIED_IDEOGRAPHS ||
+                block === Character.UnicodeBlock.CJK_COMPATIBILITY_IDEOGRAPHS 
||
+                block === 
Character.UnicodeBlock.CJK_UNIFIED_IDEOGRAPHS_EXTENSION_A ||
+                block === Character.UnicodeBlock.GENERAL_PUNCTUATION ||
+                block === Character.UnicodeBlock.CJK_SYMBOLS_AND_PUNCTUATION ||
+                block === Character.UnicodeBlock.HALFWIDTH_AND_FULLWIDTH_FORMS 
||
+                block === Character.UnicodeBlock.HIRAGANA ||
+                block === Character.UnicodeBlock.KATAKANA ||
+                block === Character.UnicodeBlock.HANGUL_SYLLABLES
     }
 
     private AsciiTableMaker() {}
diff --git 
a/subprojects/groovy-ginq/src/test/groovy/org/apache/groovy/ginq/provider/collection/runtime/AsciiTableMakerTest.groovy
 
b/subprojects/groovy-ginq/src/test/groovy/org/apache/groovy/ginq/provider/collection/runtime/AsciiTableMakerTest.groovy
index b877df0492..6db1303b84 100644
--- 
a/subprojects/groovy-ginq/src/test/groovy/org/apache/groovy/ginq/provider/collection/runtime/AsciiTableMakerTest.groovy
+++ 
b/subprojects/groovy-ginq/src/test/groovy/org/apache/groovy/ginq/provider/collection/runtime/AsciiTableMakerTest.groovy
@@ -28,18 +28,20 @@ class AsciiTableMakerTest {
             from r in [
                 [name: 'Daniel', age: 39, location: 'Shanghai'],
                 [name: '山风小子', age: 40, location: '上海'],
-                [name: 'Candy', age: 36, location: 'Shanghai']
+                [name: 'Candy', age: 36, location: 'Shanghai'],
+                [name: '中文,English, 123, abc,ひらがな,カタカナ', age: 1, location: 
'未知']
             ]
             select  r.name, r.age, r.location
         }
         def tableStr = AsciiTableMaker.makeAsciiTable(result)
         assert tableStr == '\n' +
-            '+----------+-----+----------+\n' +
-            '| name     | age | location |\n' +
-            '+----------+-----+----------+\n' +
-            '| Daniel   | 39  | Shanghai |\n' +
-            '| 山风小子 | 40  | 上海     |\n' +
-            '| Candy    | 36  | Shanghai |\n' +
-            '+----------+-----+----------+\n'
+            
'+------------------------------------------------+-----+----------+\n' +
+            '| name                                           | age | location 
|\n' +
+            
'+------------------------------------------------+-----+----------+\n' +
+            '| Daniel                                         | 39  | Shanghai 
|\n' +
+            '| 山风小子                                       | 40  | 上海     |\n' +
+            '| Candy                                          | 36  | Shanghai 
|\n' +
+            '| 中文,English, 123, abc,ひらがな,カタカナ | 1   | 未知     |\n' +
+            
'+------------------------------------------------+-----+----------+\n'
     }
 }

Reply via email to