1 package be
.nikiroo
.jvcard
.tui
;
3 import java
.text
.Normalizer
;
4 import java
.text
.Normalizer
.Form
;
5 import java
.util
.regex
.Pattern
;
7 import com
.googlecode
.lanterna
.gui2
.LinearLayout
.Alignment
;
9 public class StringUtils
{
10 static private Pattern marks
= Pattern
11 .compile("[\\p{InCombiningDiacriticalMarks}\\p{IsLm}\\p{IsSk}]+");
12 static private Pattern notAscii
= Pattern
.compile("[^\\p{ASCII}]+");
15 * Fix the size of the given {@link String} either with space-padding or by
19 * the {@link String} to fix
21 * the size of the resulting {@link String} if the text fits or
24 * @return the resulting {@link String} of size <i>size</i>
26 static public String
padString(String text
, int width
) {
27 return padString(text
, width
, true, Alignment
.Beginning
);
31 * Fix the size of the given {@link String} either with space-padding or by
32 * optionally shortening it.
35 * the {@link String} to fix
37 * the size of the resulting {@link String} if the text fits or
40 * cut the {@link String} shorter if needed
42 * align the {@link String} in this position if we have enough
45 * @return the resulting {@link String} of size <i>size</i> minimum
47 static public String
padString(String text
, int width
, boolean cut
,
54 int diff
= width
- text
.length();
58 text
= text
.substring(0, width
);
59 } else if (diff
> 0) {
60 if (diff
< 2 && align
!= Alignment
.End
)
61 align
= Alignment
.Beginning
;
65 text
= text
+ new String(new char[diff
]).replace('\0', ' ');
68 text
= new String(new char[diff
]).replace('\0', ' ') + text
;
73 int pad1
= (diff
) / 2;
74 int pad2
= (diff
+ 1) / 2;
75 text
= new String(new char[pad1
]).replace('\0', ' ') + text
76 + new String(new char[pad2
]).replace('\0', ' ');
86 * Sanitise the given input to make it more Terminal-friendly by removing
87 * combining characters.
90 * the input to sanitise
92 * allow Unicode or only allow ASCII Latin characters
94 * @return the sanitised {@link String}
96 static public String
sanitize(String input
, boolean allowUnicode
) {
97 return sanitize(input
, allowUnicode
, !allowUnicode
);
101 * Sanitise the given input to make it more Terminal-friendly by removing
102 * combining characters.
105 * the input to sanitise
106 * @param allowUnicode
107 * allow Unicode or only allow ASCII Latin characters
108 * @param removeAllAccents
109 * TRUE to replace all accentuated characters by their non
110 * accentuated counter-parts
112 * @return the sanitised {@link String}
114 static public String
sanitize(String input
, boolean allowUnicode
,
115 boolean removeAllAccents
) {
117 if (removeAllAccents
) {
118 input
= Normalizer
.normalize(input
, Form
.NFKD
);
119 input
= marks
.matcher(input
).replaceAll("");
122 input
= Normalizer
.normalize(input
, Form
.NFKC
);
125 input
= notAscii
.matcher(input
).replaceAll("");