1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
|
package bjc.utils.ioutils;
import java.util.HashMap;
import java.util.IllegalFormatConversionException;
import java.util.Map;
import java.util.UnknownFormatConversionException;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import bjc.utils.PropertyDB;
import bjc.utils.esodata.Tape;
import bjc.utils.esodata.SingleTape;
import static bjc.utils.PropertyDB.applyFormat;
import static bjc.utils.PropertyDB.getCompiledRegex;
import static bjc.utils.PropertyDB.getRegex;
public class CLFormatter {
@FunctionalInterface
public interface Directive {
/*
* @TODO fill in parameters
*/
public void format();
}
private static final String prefixParam = getRegex("clFormatPrefix");
private static final Pattern pPrefixParam = Pattern.compile(prefixParam);
private static final String formatMod = getRegex("clFormatModifier");
private static final String prefixList = applyFormat("delimSeparatedList", prefixParam, ",");
private static final String directiveName = getRegex("clFormatName");
private static final String formatDirective = applyFormat("clFormatDirective", prefixList, formatMod, directiveName);
private static final Pattern pFormatDirective = Pattern.compile(formatDirective);
private Map<String, Directive> extraDirectives;
public CLFormatter() {
extraDirectives = new HashMap<>();
}
private void checkItem(Object itm, char directive) {
if(itm == null)
throw new IllegalArgumentException(String.format("No argument provided for %c directive", directive));
}
public String formatString(String format, Object... params) {
StringBuffer sb = new StringBuffer();
Matcher dirMatcher = pFormatDirective.matcher(format);
/*
* Put the parameters where we can easily handle them.
*/
Tape<Object> tParams = new SingleTape(params);
while(dirMatcher.find()) {
dirMatcher.appendReplacement(sb, "");
String dirName = dirMatcher.group("name");
String dirFunc = dirMatcher.group("funcname");
String dirMods = dirMatcher.group("modifiers");
String dirParams = dirMatcher.group("params");
CLParameters arrParams = CLParameters.fromDirective(dirParams.split("(?<!'),"), tParams);
boolean atMod = false;
boolean colonMod = false;
if(dirMods != null) {
atMod = dirMods.contains("@");
colonMod = dirMods.contains(":");
}
Object item = tParams.item();
if(dirName == null && dirFunc != null) {
/*
* @TODO implement user-called functions.
*/
continue;
}
switch(dirName) {
case "A":
checkItem(item, 'A');
handleAestheticDirective(sb, item, atMod, colonMod, arrParams);
tParams.right();
break;
case "B":
checkItem(item, 'B');
handleNumberDirective(sb, atMod, colonMod, arrParams, -1, item, 2);
tParams.right();
break;
case "C":
checkItem(item, 'C');
handleCDirective(sb, item, atMod, colonMod);
tParams.right();
break;
case "D":
checkItem(item, 'D');
handleNumberDirective(sb, atMod, colonMod, arrParams, -1, item, 10);
tParams.right();
break;
case "O":
checkItem(item, 'O');
handleNumberDirective(sb, atMod, colonMod, arrParams, -1, item, 8);
tParams.right();
break;
case "R":
if(item == null)
throw new IllegalArgumentException("No argument provided for C directive");
handleRadixDirective(sb, atMod, colonMod, arrParams, item);
tParams.right();
break;
case "X":
checkItem(item, 'X');
handleNumberDirective(sb, atMod, colonMod, arrParams, -1, item, 16);
tParams.right();
break;
case "&":
handleFreshlineDirective(sb, arrParams);
break;
case "%":
handleLiteralDirective(sb, arrParams, "\n", '%');
break;
case "|":
handleLiteralDirective(sb, arrParams, "\f", '|');
break;
case "~":
handleLiteralDirective(sb, arrParams, "~", '~');
break;
case "F":
case "E":
case "G":
case "$":
/* @TODO implement floating point directives. */
throw new IllegalArgumentException("Floating-point directives aren't implemented yet.");
case "S":
case "W":
throw new IllegalArgumentException("S and W aren't implemented. Use A instead");
default:
String msg = String.format("Unknown format directive '%s'", dirName);
throw new UnknownFormatConversionException(msg);
}
}
dirMatcher.appendTail(sb);
return sb.toString();
}
private void handleCDirective(StringBuffer buff, Object parm, boolean atMod, boolean colonMod) {
if(!(parm instanceof Character)) {
throw new IllegalFormatConversionException('C', parm.getClass());
}
char ch = (Character) parm;
int codepoint = (int) ch;
if(colonMod) {
/*
* Colon mod means print Unicode character name.
*/
buff.append(Character.getName(codepoint));
} else {
buff.append(ch);
}
}
private void handleFreshlineDirective(StringBuffer buff, CLParameters params) {
int nTimes = 1;
if(params.length() > 1) {
nTimes = params.getInt(0, "occurance count", '&');
}
if(buff.charAt(buff.length() - 1) == '\n') nTimes -= 1;
for(int i = 0; i < nTimes; i++) {
buff.append("\n");
}
}
private void handleLiteralDirective(StringBuffer buff, CLParameters params, String lit, char directive) {
int nTimes = 1;
if(params.length() > 1) {
nTimes = params.getInt(0, "occurance count", directive);
}
for(int i = 0; i < nTimes; i++) {
buff.append(lit);
}
}
private void handleNumberDirective(StringBuffer buff, boolean atMod, boolean colonMod, CLParameters params, int argidx, long val, int radix) {
/*
* Initialize the two padding related parameters, and
* then fill them in from the directive parameters if
* they are present.
*/
int mincol = 0;
char padchar = ' ';
if(params.length() > (argidx + 2)) {
mincol = params.getIntDefault(argidx + 1, "minimum column count", 'R', 0);
}
if(params.length() > (argidx + 3)) {
padchar = params.getCharDefault(argidx + 2, "padding character", 'R', ' ');
}
if(colonMod) {
/*
* We're doing commas, so check if the two
* comma-related parameters were supplied.
*/
int commaInterval = 0;
char commaChar = ',';
if(params.length() > (argidx + 3)) {
commaChar = params.getCharDefault((argidx + 3), "comma character", 'R', ' ');
}
if(params.length() > (argidx + 4)) {
commaInterval = params.getIntDefault((argidx + 4), "comma interval", 'R', 0);
}
NumberUtils.toCommaString(val, mincol, padchar, commaInterval, commaChar, atMod, radix);
} else {
NumberUtils.toNormalString(val, mincol, padchar, atMod, radix);
}
}
private void handleRadixDirective(StringBuffer buff, boolean atMod, boolean colonMod, CLParameters params, Object arg) {
if(!(arg instanceof Number)) {
throw new IllegalFormatConversionException('R', arg.getClass());
}
/*
* @TODO see if this is the way we want to do this.
*/
long val = ((Number)arg).longValue();
if(params.length() == 0) {
if(atMod) {
buff.append(NumberUtils.toRoman((Long)val, colonMod));
} else if(colonMod) {
buff.append(NumberUtils.toOrdinal(val));
} else {
buff.append(NumberUtils.toCardinal(val));
}
} else {
if(params.length() < 1)
throw new IllegalArgumentException("R directive requires at least one parameter, the radix");
int radix = params.getInt(0, "radix", 'R');
handleNumberDirective(buff, atMod, colonMod, params, 0, val, radix);
}
}
private void handleAestheticDirective(StringBuffer buff, Object item, boolean atMod, boolean colonMod, CLParameters arrParams) {
int mincol = 0, colinc = 1, minpad = 0;
char padchar = ' ';
if(params.length() > 1) {
mincol = params.getIntDefault(0, "minimum column count", 'A', 0);
}
if(params.length() < 4) {
throw new IllegalArgumentException("Must provide either zero, one or four arguments to A directive");
}
colinc = params.getIntDefault(1, "padding increment", 'A', 1);
minpad = params.getIntDefault(2, "minimum amount of padding", 'A', 0);
padchar = params.getCharDefault(3, "padding character", 'A', ' ');
StringBuilder work = new StringBuilder();
if(atMod) {
for(int i = 0; i < minpad; i++) {
work.append(padchar);
}
for(int i = work.length(); i < mincol; i++) {
for(int k = 0; k < colinc; k++) {
work.append(padchar);
}
}
}
work.append(item.toString());
if(!atMod) {
for(int i = 0; i < minpad; i++) {
work.append(padchar);
}
for(int i = work.length(); i < mincol; i++) {
for(int k = 0; k < colinc; k++) {
work.append(padchar);
}
}
}
}
}
|