1 package be
.nikiroo
.jvcard
.tui
;
3 import java
.text
.Normalizer
;
4 import java
.text
.Normalizer
.Form
;
5 import java
.text
.ParseException
;
6 import java
.text
.SimpleDateFormat
;
8 import java
.util
.regex
.Pattern
;
10 import com
.googlecode
.lanterna
.gui2
.LinearLayout
.Alignment
;
12 public class StringUtils
{
13 static private Pattern marks
= Pattern
14 .compile("[\\p{InCombiningDiacriticalMarks}\\p{IsLm}\\p{IsSk}]+");
17 * Fix the size of the given {@link String} either with space-padding or by
21 * the {@link String} to fix
23 * the size of the resulting {@link String} if the text fits or
26 * @return the resulting {@link String} of size <i>size</i>
28 static public String
padString(String text
, int width
) {
29 return padString(text
, width
, true, Alignment
.Beginning
);
33 * Fix the size of the given {@link String} either with space-padding or by
34 * optionally shortening it.
37 * the {@link String} to fix
39 * the size of the resulting {@link String} if the text fits or
42 * cut the {@link String} shorter if needed
44 * align the {@link String} in this position if we have enough
47 * @return the resulting {@link String} of size <i>size</i> minimum
49 static public String
padString(String text
, int width
, boolean cut
,
56 int diff
= width
- text
.length();
60 text
= text
.substring(0, width
);
61 } else if (diff
> 0) {
62 if (diff
< 2 && align
!= Alignment
.End
)
63 align
= Alignment
.Beginning
;
67 text
= text
+ new String(new char[diff
]).replace('\0', ' ');
70 text
= new String(new char[diff
]).replace('\0', ' ') + text
;
75 int pad1
= (diff
) / 2;
76 int pad2
= (diff
+ 1) / 2;
77 text
= new String(new char[pad1
]).replace('\0', ' ') + text
78 + new String(new char[pad2
]).replace('\0', ' ');
88 * Sanitise the given input to make it more Terminal-friendly by removing
89 * combining characters.
92 * the input to sanitise
94 * allow Unicode or only allow ASCII Latin characters
96 * @return the sanitised {@link String}
98 static public String
sanitize(String input
, boolean allowUnicode
) {
99 return sanitize(input
, allowUnicode
, !allowUnicode
);
103 * Sanitise the given input to make it more Terminal-friendly by removing
104 * combining characters.
107 * the input to sanitise
108 * @param allowUnicode
109 * allow Unicode or only allow ASCII Latin characters
110 * @param removeAllAccents
111 * TRUE to replace all accentuated characters by their non
112 * accentuated counter-parts
114 * @return the sanitised {@link String}
116 static public String
sanitize(String input
, boolean allowUnicode
,
117 boolean removeAllAccents
) {
119 if (removeAllAccents
) {
120 input
= Normalizer
.normalize(input
, Form
.NFKD
);
121 input
= marks
.matcher(input
).replaceAll("");
124 input
= Normalizer
.normalize(input
, Form
.NFKC
);
127 StringBuilder builder
= new StringBuilder();
128 for (int index
= 0; index
< input
.length(); index
++) {
129 char car
= input
.charAt(index
);
130 // displayable chars in ASCII are in the range 32<->255,
132 if (car
>= 32 && car
<= 255 && car
!= 127) {
136 input
= builder
.toString();
143 * Convert between time in milliseconds to {@link String} in a "static" way
144 * (to exchange data over the wire, for instance).
147 * the time in milliseconds
149 * @return the time as a {@link String}
151 static public String
fromTime(long time
) {
152 SimpleDateFormat sdf
= new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
153 return sdf
.format(new Date(time
));
157 * Convert between time as a {@link String} to milliseconds in a "static"
158 * way (to exchange data over the wire, for instance).
161 * the time as a {@link String}
163 * @return the time in milliseconds
165 static public long toTime(String display
) {
166 SimpleDateFormat sdf
= new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
168 return sdf
.parse(display
).getTime();
169 } catch (ParseException e
) {