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

garydgregory pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/commons-lang.git


The following commit(s) were added to refs/heads/master by this push:
     new 7e29cde71 fix StringUtils.getDigits dropping supplementary digits 
(#1729)
7e29cde71 is described below

commit 7e29cde71c6c1673c2dd1d7f88f58416582d7158
Author: alhuda <[email protected]>
AuthorDate: Fri Jun 26 00:52:21 2026 +0530

    fix StringUtils.getDigits dropping supplementary digits (#1729)
    
    Scan by code point so a supplementary digit such as U+1D7CF is kept instead 
of dropped when neither surrogate half is itself a digit.
---
 src/main/java/org/apache/commons/lang3/StringUtils.java     | 9 +++++----
 src/test/java/org/apache/commons/lang3/StringUtilsTest.java | 8 ++++++++
 2 files changed, 13 insertions(+), 4 deletions(-)

diff --git a/src/main/java/org/apache/commons/lang3/StringUtils.java 
b/src/main/java/org/apache/commons/lang3/StringUtils.java
index bbbcc52cc..4d7150fca 100644
--- a/src/main/java/org/apache/commons/lang3/StringUtils.java
+++ b/src/main/java/org/apache/commons/lang3/StringUtils.java
@@ -2076,11 +2076,12 @@ public static String getDigits(final String str) {
         final char[] buffer = new char[len];
         int count = 0;
 
-        for (int i = 0; i < len; i++) {
-            final char tempChar = str.charAt(i);
-            if (Character.isDigit(tempChar)) {
-                buffer[count++] = tempChar;
+        for (int i = 0; i < len;) {
+            final int codePoint = str.codePointAt(i);
+            if (Character.isDigit(codePoint)) {
+                count += Character.toChars(codePoint, buffer, count);
             }
+            i += Character.charCount(codePoint);
         }
         return new String(buffer, 0, count);
     }
diff --git a/src/test/java/org/apache/commons/lang3/StringUtilsTest.java 
b/src/test/java/org/apache/commons/lang3/StringUtilsTest.java
index 7d60d0f9e..fa34c839d 100644
--- a/src/test/java/org/apache/commons/lang3/StringUtilsTest.java
+++ b/src/test/java/org/apache/commons/lang3/StringUtilsTest.java
@@ -718,6 +718,14 @@ void testGetDigitsKeycaps() {
         assertEquals("0123456789", 
StringUtils.getDigits("0️⃣1️⃣2️⃣3️⃣4️⃣5️⃣6️⃣7️⃣8️⃣9️⃣#️⃣"));
     }
 
+    @Test
+    void testGetDigitsSupplementary() {
+        // U+1D7CF MATHEMATICAL BOLD DIGIT ONE: Character.isDigit is true but 
it is a surrogate pair
+        final String mathOne = new String(Character.toChars(0x1D7CF));
+        assertEquals(mathOne, StringUtils.getDigits(mathOne));
+        assertEquals(mathOne + "9", StringUtils.getDigits("a" + mathOne + 
"9"));
+    }
+
     @Test
     void testGetFuzzyDistance() {
         assertEquals(0, StringUtils.getFuzzyDistance("", "", Locale.ENGLISH));

Reply via email to