382922605260260c8f4998da741757d277ba9f4d
[jvcard.git] / src / be / nikiroo / jvcard / tui / StringUtils.java
1 package be.nikiroo.jvcard.tui;
2
3 import java.text.Normalizer;
4 import java.text.Normalizer.Form;
5 import java.util.regex.Pattern;
6
7 import com.googlecode.lanterna.gui2.LinearLayout.Alignment;
8
9 public class StringUtils {
10 static private Pattern marks = Pattern
11 .compile("[\\p{InCombiningDiacriticalMarks}\\p{IsLm}\\p{IsSk}]+");
12
13 /**
14 * Fix the size of the given {@link String} either with space-padding or by
15 * shortening it.
16 *
17 * @param text
18 * the {@link String} to fix
19 * @param width
20 * the size of the resulting {@link String} if the text fits or
21 * if cut is TRUE
22 *
23 * @return the resulting {@link String} of size <i>size</i>
24 */
25 static public String padString(String text, int width) {
26 return padString(text, width, true, Alignment.Beginning);
27 }
28
29 /**
30 * Fix the size of the given {@link String} either with space-padding or by
31 * optionally shortening it.
32 *
33 * @param text
34 * the {@link String} to fix
35 * @param width
36 * the size of the resulting {@link String} if the text fits or
37 * if cut is TRUE
38 * @param cut
39 * cut the {@link String} shorter if needed
40 * @param align
41 * align the {@link String} in this position if we have enough
42 * space
43 *
44 * @return the resulting {@link String} of size <i>size</i> minimum
45 */
46 static public String padString(String text, int width, boolean cut,
47 Alignment align) {
48
49 if (width >= 0) {
50 if (text == null)
51 text = "";
52
53 int diff = width - text.length();
54
55 if (diff < 0) {
56 if (cut)
57 text = text.substring(0, width);
58 } else if (diff > 0) {
59 if (diff < 2 && align != Alignment.End)
60 align = Alignment.Beginning;
61
62 switch (align) {
63 case Beginning:
64 text = text + new String(new char[diff]).replace('\0', ' ');
65 break;
66 case End:
67 text = new String(new char[diff]).replace('\0', ' ') + text;
68 break;
69 case Center:
70 case Fill:
71 default:
72 int pad1 = (diff) / 2;
73 int pad2 = (diff + 1) / 2;
74 text = new String(new char[pad1]).replace('\0', ' ') + text
75 + new String(new char[pad2]).replace('\0', ' ');
76 break;
77 }
78 }
79 }
80
81 return text;
82 }
83
84 /**
85 * Sanitise the given input to make it more Terminal-friendly by removing
86 * combining characters.
87 *
88 * @param input
89 * the input to sanitise
90 * @param allowUnicode
91 * allow Unicode or only allow ASCII Latin characters
92 *
93 * @return the sanitised {@link String}
94 */
95 static public String sanitize(String input, boolean allowUnicode) {
96 return sanitize(input, allowUnicode, !allowUnicode);
97 }
98
99 /**
100 * Sanitise the given input to make it more Terminal-friendly by removing
101 * combining characters.
102 *
103 * @param input
104 * the input to sanitise
105 * @param allowUnicode
106 * allow Unicode or only allow ASCII Latin characters
107 * @param removeAllAccents
108 * TRUE to replace all accentuated characters by their non
109 * accentuated counter-parts
110 *
111 * @return the sanitised {@link String}
112 */
113 static public String sanitize(String input, boolean allowUnicode,
114 boolean removeAllAccents) {
115
116 if (removeAllAccents) {
117 input = Normalizer.normalize(input, Form.NFKD);
118 input = marks.matcher(input).replaceAll("");
119 }
120
121 input = Normalizer.normalize(input, Form.NFKC);
122
123 if (!allowUnicode) {
124 StringBuilder builder = new StringBuilder();
125 for (int index = 0; index < input.length(); index++) {
126 char car = input.charAt(index);
127 // displayable chars in ASCII are in the range 32<->255,
128 // except DEL (127)
129 if (car >= 32 && car <= 255 && car != 127) {
130 builder.append(car);
131 }
132 }
133 input = builder.toString();
134 }
135
136 return input;
137 }
138 }