1 package be
.nikiroo
.jvcard
.resources
;
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} or -1 for a noop
27 * @return the resulting {@link String} of size <i>size</i>
29 static public String
padString(String text
, int width
) {
30 return padString(text
, width
, true, Alignment
.Beginning
);
34 * Fix the size of the given {@link String} either with space-padding or by
35 * optionally shortening it.
38 * the {@link String} to fix
40 * the size of the resulting {@link String} if the text fits or
41 * if cut is TRUE or -1 for a noop
43 * cut the {@link String} shorter if needed
45 * align the {@link String} in this position if we have enough
48 * @return the resulting {@link String} of size <i>size</i> minimum
50 static public String
padString(String text
, int width
, boolean cut
,
57 int diff
= width
- text
.length();
61 text
= text
.substring(0, width
);
62 } else if (diff
> 0) {
63 if (diff
< 2 && align
!= Alignment
.End
)
64 align
= Alignment
.Beginning
;
68 text
= text
+ new String(new char[diff
]).replace('\0', ' ');
71 text
= new String(new char[diff
]).replace('\0', ' ') + text
;
76 int pad1
= (diff
) / 2;
77 int pad2
= (diff
+ 1) / 2;
78 text
= new String(new char[pad1
]).replace('\0', ' ') + text
79 + new String(new char[pad2
]).replace('\0', ' ');
89 * Sanitise the given input to make it more Terminal-friendly by removing
90 * combining characters.
93 * the input to sanitise
95 * allow Unicode or only allow ASCII Latin characters
97 * @return the sanitised {@link String}
99 static public String
sanitize(String input
, boolean allowUnicode
) {
100 return sanitize(input
, allowUnicode
, !allowUnicode
);
104 * Sanitise the given input to make it more Terminal-friendly by removing
105 * combining characters.
108 * the input to sanitise
109 * @param allowUnicode
110 * allow Unicode or only allow ASCII Latin characters
111 * @param removeAllAccents
112 * TRUE to replace all accentuated characters by their non
113 * accentuated counter-parts
115 * @return the sanitised {@link String}
117 static public String
sanitize(String input
, boolean allowUnicode
,
118 boolean removeAllAccents
) {
120 if (removeAllAccents
) {
121 input
= Normalizer
.normalize(input
, Form
.NFKD
);
122 input
= marks
.matcher(input
).replaceAll("");
125 input
= Normalizer
.normalize(input
, Form
.NFKC
);
128 StringBuilder builder
= new StringBuilder();
129 for (int index
= 0; index
< input
.length(); index
++) {
130 char car
= input
.charAt(index
);
131 // displayable chars in ASCII are in the range 32<->255,
133 if (car
>= 32 && car
<= 255 && car
!= 127) {
137 input
= builder
.toString();
144 * Convert between time in milliseconds to {@link String} in a "static" way
145 * (to exchange data over the wire, for instance).
148 * the time in milliseconds
150 * @return the time as a {@link String}
152 static public String
fromTime(long time
) {
153 SimpleDateFormat sdf
= new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
154 return sdf
.format(new Date(time
));
158 * Convert between time as a {@link String} to milliseconds in a "static"
159 * way (to exchange data over the wire, for instance).
162 * the time as a {@link String}
164 * @return the time in milliseconds
166 static public long toTime(String display
) {
167 SimpleDateFormat sdf
= new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
169 return sdf
.parse(display
).getTime();
170 } catch (ParseException e
) {
176 * Return a hash of the given {@link String}.
183 static public String
getHash(String input
) {
185 MessageDigest md
= MessageDigest
.getInstance("MD5");
186 md
.update(input
.getBytes());
187 byte byteData
[] = md
.digest();
189 StringBuffer hexString
= new StringBuffer();
190 for (int i
= 0; i
< byteData
.length
; i
++) {
191 String hex
= Integer
.toHexString(0xff & byteData
[i
]);
192 if (hex
.length() == 1)
193 hexString
.append('0');
194 hexString
.append(hex
);
197 return hexString
.toString();
198 } catch (NoSuchAlgorithmException e
) {
199 // all JVM most probably have an MD5 implementation, but even if
200 // not, returning the input is "correct", if inefficient and