1 package be
.nikiroo
.jvcard
.tui
;
3 import java
.security
.MessageDigest
;
4 import java
.security
.NoSuchAlgorithmException
;
5 import java
.text
.Normalizer
;
6 import java
.text
.Normalizer
.Form
;
7 import java
.text
.ParseException
;
8 import java
.text
.SimpleDateFormat
;
10 import java
.util
.regex
.Pattern
;
12 import com
.googlecode
.lanterna
.gui2
.LinearLayout
.Alignment
;
14 public class StringUtils
{
15 static private Pattern marks
= Pattern
16 .compile("[\\p{InCombiningDiacriticalMarks}\\p{IsLm}\\p{IsSk}]+");
19 * Fix the size of the given {@link String} either with space-padding or by
23 * the {@link String} to fix
25 * the size of the resulting {@link String} if the text fits or
28 * @return the resulting {@link String} of size <i>size</i>
30 static public String
padString(String text
, int width
) {
31 return padString(text
, width
, true, Alignment
.Beginning
);
35 * Fix the size of the given {@link String} either with space-padding or by
36 * optionally shortening it.
39 * the {@link String} to fix
41 * the size of the resulting {@link String} if the text fits or
44 * cut the {@link String} shorter if needed
46 * align the {@link String} in this position if we have enough
49 * @return the resulting {@link String} of size <i>size</i> minimum
51 static public String
padString(String text
, int width
, boolean cut
,
58 int diff
= width
- text
.length();
62 text
= text
.substring(0, width
);
63 } else if (diff
> 0) {
64 if (diff
< 2 && align
!= Alignment
.End
)
65 align
= Alignment
.Beginning
;
69 text
= text
+ new String(new char[diff
]).replace('\0', ' ');
72 text
= new String(new char[diff
]).replace('\0', ' ') + text
;
77 int pad1
= (diff
) / 2;
78 int pad2
= (diff
+ 1) / 2;
79 text
= new String(new char[pad1
]).replace('\0', ' ') + text
80 + new String(new char[pad2
]).replace('\0', ' ');
90 * Sanitise the given input to make it more Terminal-friendly by removing
91 * combining characters.
94 * the input to sanitise
96 * allow Unicode or only allow ASCII Latin characters
98 * @return the sanitised {@link String}
100 static public String
sanitize(String input
, boolean allowUnicode
) {
101 return sanitize(input
, allowUnicode
, !allowUnicode
);
105 * Sanitise the given input to make it more Terminal-friendly by removing
106 * combining characters.
109 * the input to sanitise
110 * @param allowUnicode
111 * allow Unicode or only allow ASCII Latin characters
112 * @param removeAllAccents
113 * TRUE to replace all accentuated characters by their non
114 * accentuated counter-parts
116 * @return the sanitised {@link String}
118 static public String
sanitize(String input
, boolean allowUnicode
,
119 boolean removeAllAccents
) {
121 if (removeAllAccents
) {
122 input
= Normalizer
.normalize(input
, Form
.NFKD
);
123 input
= marks
.matcher(input
).replaceAll("");
126 input
= Normalizer
.normalize(input
, Form
.NFKC
);
129 StringBuilder builder
= new StringBuilder();
130 for (int index
= 0; index
< input
.length(); index
++) {
131 char car
= input
.charAt(index
);
132 // displayable chars in ASCII are in the range 32<->255,
134 if (car
>= 32 && car
<= 255 && car
!= 127) {
138 input
= builder
.toString();
145 * Convert between time in milliseconds to {@link String} in a "static" way
146 * (to exchange data over the wire, for instance).
149 * the time in milliseconds
151 * @return the time as a {@link String}
153 static public String
fromTime(long time
) {
154 SimpleDateFormat sdf
= new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
155 return sdf
.format(new Date(time
));
159 * Convert between time as a {@link String} to milliseconds in a "static"
160 * way (to exchange data over the wire, for instance).
163 * the time as a {@link String}
165 * @return the time in milliseconds
167 static public long toTime(String display
) {
168 SimpleDateFormat sdf
= new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
170 return sdf
.parse(display
).getTime();
171 } catch (ParseException e
) {
177 * Return a hash of the given {@link String}.
184 static public String
getHash(String input
) {
186 MessageDigest md
= MessageDigest
.getInstance("MD5");
187 md
.update(input
.getBytes());
188 byte byteData
[] = md
.digest();
190 StringBuffer hexString
= new StringBuffer();
191 for (int i
= 0; i
< byteData
.length
; i
++) {
192 String hex
= Integer
.toHexString(0xff & byteData
[i
]);
193 if (hex
.length() == 1)
194 hexString
.append('0');
195 hexString
.append(hex
);
198 return hexString
.toString();
199 } catch (NoSuchAlgorithmException e
) {
200 // all JVM most probably have an MD5 implementation, but even if
201 // not, returning the input is "correct", if inefficient and