remove jar files from sources, should not be there
[jvcard.git] / src / be / nikiroo / jvcard / tui / StringUtils.java
... / ...
CommitLineData
1package be.nikiroo.jvcard.tui;
2
3import java.text.Normalizer;
4import java.text.Normalizer.Form;
5import java.util.regex.Pattern;
6
7import com.googlecode.lanterna.gui2.LinearLayout.Alignment;
8
9public 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}]+");
13
14 /**
15 * Fix the size of the given {@link String} either with space-padding or by
16 * shortening it.
17 *
18 * @param text
19 * the {@link String} to fix
20 * @param width
21 * the size of the resulting {@link String} if the text fits or
22 * if cut is TRUE
23 *
24 * @return the resulting {@link String} of size <i>size</i>
25 */
26 static public String padString(String text, int width) {
27 return padString(text, width, true, Alignment.Beginning);
28 }
29
30 /**
31 * Fix the size of the given {@link String} either with space-padding or by
32 * optionally shortening it.
33 *
34 * @param text
35 * the {@link String} to fix
36 * @param width
37 * the size of the resulting {@link String} if the text fits or
38 * if cut is TRUE
39 * @param cut
40 * cut the {@link String} shorter if needed
41 * @param align
42 * align the {@link String} in this position if we have enough
43 * space
44 *
45 * @return the resulting {@link String} of size <i>size</i> minimum
46 */
47 static public String padString(String text, int width, boolean cut,
48 Alignment align) {
49
50 if (width >= 0) {
51 if (text == null)
52 text = "";
53
54 int diff = width - text.length();
55
56 if (diff < 0) {
57 if (cut)
58 text = text.substring(0, width);
59 } else if (diff > 0) {
60 if (diff < 2 && align != Alignment.End)
61 align = Alignment.Beginning;
62
63 switch (align) {
64 case Beginning:
65 text = text + new String(new char[diff]).replace('\0', ' ');
66 break;
67 case End:
68 text = new String(new char[diff]).replace('\0', ' ') + text;
69 break;
70 case Center:
71 case Fill:
72 default:
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', ' ');
77 break;
78 }
79 }
80 }
81
82 return text;
83 }
84
85 /**
86 * Sanitise the given input to make it more Terminal-friendly by removing
87 * combining characters.
88 *
89 * @param input
90 * the input to sanitise
91 * @param allowUnicode
92 * allow Unicode or only allow ASCII Latin characters
93 *
94 * @return the sanitised {@link String}
95 */
96 static public String sanitize(String input, boolean allowUnicode) {
97 return sanitize(input, allowUnicode, !allowUnicode);
98 }
99
100 /**
101 * Sanitise the given input to make it more Terminal-friendly by removing
102 * combining characters.
103 *
104 * @param input
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
111 *
112 * @return the sanitised {@link String}
113 */
114 static public String sanitize(String input, boolean allowUnicode,
115 boolean removeAllAccents) {
116
117 if (removeAllAccents) {
118 input = Normalizer.normalize(input, Form.NFKD);
119 input = marks.matcher(input).replaceAll("");
120 }
121
122 input = Normalizer.normalize(input, Form.NFKC);
123
124 if (!allowUnicode) {
125 input = notAscii.matcher(input).replaceAll("");
126 }
127
128 return input;
129 }
130}