1
1
package com .bitwig .extensions .controllers .mcu ;
2
2
3
3
public class StringUtil {
4
-
4
+
5
5
private static final int PAN_RANGE = 50 ;
6
- private static final char [] SPECIALS = {'ä' , 'ü' , 'ö' , 'Ä' , 'Ü' , 'Ö' , 'ß' , 'é' , 'è' , 'ê' , 'â' , 'á' , 'à' , //
7
- 'û' , 'ú' , 'ù' , 'ô' , 'ó' , 'ò' };
8
- private static final String [] REPLACE = {"a" , "u" , "o" , "A" , "U" , "O" , "ss" , "e" , "e" , "e" , "a" , "a" , "a" , //
9
- "u" , "u" , "u" , "o" , "o" , "o" };
10
-
6
+ private static final char [] SPECIALS = {
7
+ 'ä' , 'ü' , 'ö' , 'Ä' , 'Ü' , 'Ö' , 'ß' , 'é' , 'è' , 'ê' , 'â' , 'á' , 'à' , //
8
+ 'û' , 'ú' , 'ù' , 'ô' , 'ó' , 'ò'
9
+ };
10
+ private static final String [] REPLACE = {
11
+ "a" , "u" , "o" , "A" , "U" , "O" , "ss" , "e" , "e" , "e" , "a" , "a" , "a" , //
12
+ "u" , "u" , "u" , "o" , "o" , "o"
13
+ };
14
+
11
15
private StringUtil () {
12
16
}
13
-
17
+
14
18
public static String toBarBeats (final double value ) {
15
19
final int bars = (int ) Math .floor (value );
16
20
final int beats = (int ) Math .floor ((value - bars ) * 4 );
17
21
return String .format ("%02d:%02d" , bars , beats );
18
22
}
19
-
23
+
20
24
public static String panToString (final double v ) {
21
25
final int intv = (int ) (v * PAN_RANGE * 2 );
22
26
if (intv == PAN_RANGE ) {
@@ -26,7 +30,7 @@ public static String panToString(final double v) {
26
30
}
27
31
return " " + (intv - PAN_RANGE ) + "R" ;
28
32
}
29
-
33
+
30
34
/**
31
35
* Tailored to condense Volume value strings. Removes leading + and spaces.
32
36
*
@@ -45,27 +49,27 @@ public static String condenseVolumeValue(final String valueText, final int maxLe
45
49
}
46
50
return sb .toString ();
47
51
}
48
-
52
+
49
53
public static String toTwoCharVal (final int value ) {
50
54
if (value < 10 ) {
51
55
return " " + value ;
52
56
}
53
57
return Integer .toString (value );
54
58
}
55
-
59
+
56
60
public static String toDisplayName (final String text ) {
57
61
if (text .length () < 2 ) {
58
62
return text ;
59
63
}
60
64
return text .charAt (0 ) + text .substring (1 , Math .min (6 , text .length ())).toLowerCase ();
61
65
}
62
-
63
-
66
+
67
+
64
68
public static String padString (final String text , final int pad ) {
65
69
return " " .repeat (Math .max (0 , pad )) + text ;
66
70
}
67
-
68
- public static String padEnd (final String text , int paddingLength ) {
71
+
72
+ public static String padEnd (final String text , final int paddingLength ) {
69
73
if (text .length () == paddingLength ) {
70
74
return text ;
71
75
}
@@ -74,19 +78,35 @@ public static String padEnd(final String text, int paddingLength) {
74
78
}
75
79
return text + " " .repeat (paddingLength - text .length ());
76
80
}
77
-
81
+
78
82
public static String limit (final String value , final int max ) {
79
83
return value .substring (0 , Math .min (max , value .length ()));
80
84
}
81
-
85
+
82
86
public static String reduceAscii (final String name , final int maxLen ) {
83
- String result = toAsciiDisplay (name , maxLen + 10 );
87
+ final String result = toAsciiDisplay (name , maxLen + 10 );
84
88
if (result .length () <= maxLen ) {
85
89
return result ;
86
90
}
87
91
return result .replace (" " , "" );
88
92
}
89
-
93
+
94
+ public static String toAscii (final String value ) {
95
+ final StringBuilder b = new StringBuilder ();
96
+ for (int i = 0 ; i < value .length (); i ++) {
97
+ final char c = value .charAt (i );
98
+ if (c < 128 ) {
99
+ b .append (c );
100
+ } else {
101
+ final int replacement = getReplace (c );
102
+ if (replacement >= 0 ) {
103
+ b .append (REPLACE [replacement ]);
104
+ }
105
+ }
106
+ }
107
+ return b .toString ();
108
+ }
109
+
90
110
public static String toAsciiDisplay (final String name , final int maxLen ) {
91
111
final StringBuilder b = new StringBuilder ();
92
112
for (int i = 0 ; i < name .length () && b .length () < maxLen ; i ++) {
@@ -105,7 +125,7 @@ public static String toAsciiDisplay(final String name, final int maxLen) {
105
125
}
106
126
return b .toString ();
107
127
}
108
-
128
+
109
129
private static int getReplace (final char c ) {
110
130
for (int i = 0 ; i < SPECIALS .length ; i ++) {
111
131
if (c == SPECIALS [i ]) {
@@ -114,5 +134,5 @@ private static int getReplace(final char c) {
114
134
}
115
135
return -1 ;
116
136
}
117
-
137
+
118
138
}
0 commit comments