@@ -9,39 +9,53 @@ public final class SmallDigits {
99 @ SuppressWarnings ("UnnecessaryUnicodeEscape" )
1010 private static final int SMALL_UP_NUMBER_BASE = '\u2080' ;
1111 private static final int NUMBER_BASE = '0' ;
12+ @ SuppressWarnings ("UnnecessaryUnicodeEscape" )
13+ private static final int FORMATTING_SYMBOL = '\u00a7' ;
1214
1315 private SmallDigits () {}
1416
15- @ NotNull
16- public static String toSmallUpNumbers (String string ) {
17+ public static @ NotNull String toSmallUpNumbers (String string ) {
1718 return convert (string , SMALL_UP_NUMBER_BASE );
1819 }
1920
20- @ NotNull
21- public static String toSmallDownNumbers (String string ) {
21+ public static @ NotNull String toSmallDownNumbers (String string ) {
2222 return convert (string , SMALL_DOWN_NUMBER_BASE );
2323 }
2424
25- @ NotNull
26- private static String convert (@ NotNull String string , int base ) {
27- boolean hasPrecedingDash = false ;
28- char [] charArray = string .toCharArray ();
29- for (int i = 0 ; i < charArray .length ; i ++) {
30- char c = charArray [i ];
31- boolean isDash = c == '-' ;
32- if (isDash ) hasPrecedingDash = true ;
33-
34- int relativeIndex = c - NUMBER_BASE ;
35- if (relativeIndex >= 0 && relativeIndex <= 9 ) {
36- if (!hasPrecedingDash ) {
37- // no preceding dash, so convert the char
38- charArray [i ] = (char ) (base + relativeIndex );
25+ private static @ NotNull String convert (@ NotNull String string , int base ) {
26+ char [] chars = string .toCharArray ();
27+ boolean hasPrefix = false ;
28+ boolean hasFormat = false ;
29+ for (int i = 0 ; i < chars .length ; i ++) {
30+ if (hasFormat ) {
31+ // skip over the next character once the format flag is set
32+ hasFormat = false ;
33+ continue ;
34+ }
35+
36+ char c = chars [i ];
37+ if (c == '-' ) {
38+ hasPrefix = true ;
39+ continue ;
40+ }
41+ if (c == FORMATTING_SYMBOL ) {
42+ hasFormat = true ;
43+ continue ;
44+ }
45+
46+ if (Character .isDigit (c )) {
47+ if (!hasPrefix ) {
48+ chars [i ] = convertToBase (c , base );
3949 }
40- } else if (! isDash && hasPrecedingDash ) {
41- // was a non-number, so invalidate the previously seen dash
42- hasPrecedingDash = false ;
50+ } else {
51+ // reset prefix as soon as a non-digit is encountered
52+ hasPrefix = false ;
4353 }
4454 }
45- return new String (charArray );
55+ return new String (chars );
56+ }
57+
58+ private static char convertToBase (char c , int base ) {
59+ return (char ) (base + (c - NUMBER_BASE ));
4660 }
4761}
0 commit comments