-
Notifications
You must be signed in to change notification settings - Fork 18
Expand file tree
/
Copy pathPython.java
More file actions
360 lines (278 loc) · 7.15 KB
/
Python.java
File metadata and controls
360 lines (278 loc) · 7.15 KB
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
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
package org.docopt;
import java.math.BigDecimal;
import java.math.BigInteger;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.concurrent.atomic.AtomicLong;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
final class Python {
static final class Re {
public static final int IGNORECASE = Pattern.CASE_INSENSITIVE;
public static final int MULTILINE = Pattern.MULTILINE
| Pattern.UNIX_LINES;
public static List<String> findAll(final String pattern,
final String string, final int flags) {
return findAll(Pattern.compile(pattern, flags), string);
}
public static List<String> findAll(final Pattern pattern,
final String string) {
final Matcher matcher = pattern.matcher(string);
final List<String> result = list();
while (matcher.find()) {
if (matcher.groupCount() == 0) {
result.add(matcher.group());
}
else {
for (int i = 0; i < matcher.groupCount(); i++) {
final String match = matcher.group(i + 1);
if (match != null) {
result.add(match);
}
}
}
}
return result;
}
/**
* Determines if {@code pattern} contains at least one capturing group.
*/
private static boolean hasGrouping(final String pattern) {
int i = -1;
// Find the potential beginning of a group by looking for a left
// parenthesis character.
while ((i = pattern.indexOf('(', i + 1)) != -1) {
int c = 0;
// Count the number of escape characters immediately preceding
// the
// left parenthesis character.
for (int j = i - 1; j > -1; j--) {
if (pattern.charAt(j) != '\\') {
break;
}
c++;
}
// If there is an even number of consecutive escape characters,
// the character is not escaped and begins a group.
if (c % 2 == 0) {
return true;
}
}
return false;
}
public static List<String> split(final String pattern,
final String string) {
if (!hasGrouping(pattern)) {
return list(string.split(pattern));
}
final Matcher matcher = Pattern.compile(pattern, 0).matcher(string);
final List<String> matches = list();
int start = 0;
while (matcher.find()) {
matches.add(string.substring(start, matcher.start()));
for (int i = 0; i < matcher.groupCount(); i++) {
matches.add(matcher.group(i + 1));
}
start = matcher.end();
}
matches.add(string.substring(start));
return matches;
}
public static String sub(final String pattern, final String repl,
final String string) {
return Pattern.compile(pattern, 0).matcher(string).replaceAll(repl);
}
private Re() {
// Prevent instantiation.
}
}
/**
* http://docs.python.org/2/library/#truth-value-testing
*/
public static boolean bool(final Object o) {
// None
if (o == null) {
return false;
}
// False
if (o instanceof Boolean) {
return ((Boolean) o).booleanValue();
}
// zero of any numeric type, for example, 0, 0L, 0.0, 0j
if (o instanceof Number) {
if (o instanceof Integer) {
return !((Integer) o).equals(0);
}
if (o instanceof Long) {
return !((Long) o).equals(0L);
}
if (o instanceof Double) {
return !((Double) o).equals(0.0);
}
if (o instanceof Float) {
return !((Float) o).equals(0.0F);
}
if (o instanceof Byte) {
return !((Byte) o).equals((byte) 0);
}
if (o instanceof Short) {
return !((Short) o).equals((short) 0);
}
if (o instanceof AtomicInteger) {
return bool(((AtomicInteger) o).get());
}
if (o instanceof AtomicLong) {
return bool(((AtomicLong) o).get());
}
if (o instanceof BigDecimal) {
return !BigDecimal.ZERO.equals(o);
}
if (o instanceof BigInteger) {
return !BigInteger.ZERO.equals(o);
}
throw new IllegalArgumentException("unknown numeric type: "
+ o.getClass());
}
// any empty sequence, for example, '', (), []
{
if (o instanceof String) {
return !"".equals(o);
}
if (o instanceof Object[]) {
return ((Object[]) o).length != 0;
}
if (o instanceof Collection) {
return !((Collection<?>) o).isEmpty();
}
}
// any empty mapping, for example, {}
if (o instanceof Map) {
return !((Map<?, ?>) o).isEmpty();
}
// All other values are considered true - so objects of many types are
// always true.
return true;
}
public static <T> boolean in(final T left, final T... right) {
for (final Object o : right) {
if (left != null) {
if (left.equals(o)) {
return true;
}
}
else {
if (o == null) {
return true;
}
}
}
return false;
}
// Plus operator
public static <T> List<T> plus(final List<T> a, final List<T> b) {
final List<T> c = new ArrayList<T>(a.size() + b.size());
c.addAll(a);
c.addAll(b);
return c;
}
public static String repr(final Object o) {
if (o == null) {
return "null";
}
if (o instanceof String) {
return "\"" + o + "\"";
}
if (o instanceof Object[]) {
return Arrays.toString((Object[]) o);
}
return o.toString();
}
public static <T> List<T> list(final Iterable<? extends T> elements) {
final List<T> list = list();
for (final T element : elements) {
list.add(element);
}
return list;
}
public static <T> List<T> list(final T[] elements) {
final List<T> list = list();
for (final T element : elements) {
list.add(element);
}
return list;
}
public static <T> List<T> list(final T element) {
final List<T> list = list();
list.add(element);
return list;
}
public static <T> List<T> list() {
return new ArrayList<T>();
}
public static <T> int count(final List<T> self, final T obj) {
int count = 0;
for (final T element : self) {
if (element.equals(obj)) {
count++;
}
}
return count;
}
public static <T> Set<T> set(final Iterable<T> elements) {
final Set<T> set = new HashSet<T>();
for (final T element : elements) {
set.add(element);
}
return set;
}
public static String join(final String self, final Iterable<?> iterable) {
final Iterator<?> i = iterable.iterator();
if (!i.hasNext()) {
return "";
}
final StringBuilder sb = new StringBuilder();
while (i.hasNext()) {
sb.append(i.next());
sb.append(self);
}
sb.setLength(sb.length() - self.length());
return sb.toString();
}
public static String[] partition(final String self, final String sep) {
final int i = self.indexOf(sep);
if (i == -1) {
return new String[] { self, "", "" };
}
// Always <= s.length
final int j = i + sep.length();
return new String[] { self.substring(0, i), sep,
(j < self.length()) ? self.substring(j) : "" };
}
public static boolean isUpper(final String self) {
boolean result = false;
for (final char c : self.toCharArray()) {
if (Character.isLetter(c)) {
if (Character.isUpperCase(c)) {
result = true;
}
else {
return false;
}
}
}
return result;
}
public static List<String> split(final String self) {
return list(self.trim().split("\\s+"));
}
private Python() {
// Prevent instantiation.
}
}