1 package io.jawk.ext;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25 import java.io.File;
26 import java.math.BigInteger;
27
28 import java.util.ArrayList;
29 import java.util.Arrays;
30 import java.util.Calendar;
31 import java.util.Collection;
32 import java.util.Collections;
33 import java.util.Comparator;
34 import java.util.Date;
35 import java.util.GregorianCalendar;
36 import java.util.HashMap;
37 import java.util.HashSet;
38 import java.util.List;
39 import java.util.Map;
40 import java.util.Set;
41 import java.util.TimeZone;
42 import java.util.regex.Matcher;
43 import java.util.regex.Pattern;
44
45 import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
46 import io.jawk.backend.AVM;
47 import io.jawk.ext.annotations.JawkAssocArray;
48 import io.jawk.ext.annotations.JawkBeforeStart;
49 import io.jawk.ext.annotations.JawkFunction;
50 import io.jawk.ext.annotations.JawkOptional;
51 import io.jawk.ext.annotations.JawkRawValue;
52 import io.jawk.ext.annotations.JawkRegexp;
53 import io.jawk.intermediate.UninitializedObject;
54 import io.jawk.intermediate.UntypedObject;
55 import io.jawk.jrt.IllegalAwkArgumentException;
56 import io.jawk.jrt.JRT;
57 import io.jawk.jrt.StrNum;
58
59
60
61
62 public class GawkExtension extends AbstractExtension implements JawkExtension {
63
64 private static final String VAL_TYPE_ASC = "@val_type_asc";
65
66
67 private static final String DEFAULT_STRFTIME_FORMAT = "%a %b %e %H:%M:%S %Z %Y";
68
69
70 private static final String DEFAULT_FPAT = "[^\\s]+";
71
72
73 private static final String DEFAULT_TEXTDOMAIN = "messages";
74
75
76
77
78
79
80
81
82 private static final String DEFAULT_LOCALE_DIRECTORY = "/usr/share/locale";
83
84
85 private static final Set<String> LOCALE_CATEGORIES = Collections.unmodifiableSet(
86 new HashSet<String>(
87 Arrays.asList("LC_ALL", "LC_COLLATE", "LC_CTYPE", "LC_MESSAGES", "LC_MONETARY", "LC_NUMERIC", "LC_TIME")));
88
89
90 private AVM avm;
91
92
93 private Set<String> warnedComparators;
94
95
96 private Map<String, String> textdomainBindings;
97
98
99
100
101
102
103
104
105
106 public GawkExtension() {
107
108 }
109
110 private static final class SortEntry {
111 private final Object index;
112 private final Object value;
113
114 private SortEntry(Object indexParam, Object valueParam) {
115 this.index = indexParam;
116 this.value = valueParam;
117 }
118 }
119
120
121 @Override
122 public String getExtensionName() {
123 return "GawkExtension";
124 }
125
126
127
128
129
130
131
132
133
134 @JawkBeforeStart
135 @SuppressFBWarnings(value = "EI_EXPOSE_REP2", justification = "The extension is a per-engine instance deliberately bound to its interpreter")
136 public void initializeGawkVariables(AVM avmParam, JRT jrt) {
137 this.avm = avmParam;
138 avm.setForInKeyOrder(this::orderForInKeys);
139 }
140
141
142
143
144
145
146
147
148
149 @JawkFunction("asort")
150 public Long asort(
151 @JawkAssocArray Map<Object, Object> source,
152 @JawkOptional @JawkAssocArray Map<Object, Object> dest,
153 @JawkOptional Object how) {
154 return sort(source, dest, how, false);
155 }
156
157
158
159
160
161
162
163
164
165 @JawkFunction("asorti")
166 public Long asorti(
167 @JawkAssocArray Map<Object, Object> source,
168 @JawkOptional @JawkAssocArray Map<Object, Object> dest,
169 @JawkOptional Object how) {
170 return sort(source, dest, how, true);
171 }
172
173
174
175
176
177
178
179
180 @JawkFunction("typeof")
181 public String typeof(@JawkRawValue Object value, @JawkOptional @JawkAssocArray Map<Object, Object> meta) {
182 if (meta != null) {
183 meta.clear();
184 if (value instanceof Map) {
185 meta.put("array_type", arrayType((Map<?, ?>) value));
186 }
187 }
188 return typeOf(value);
189 }
190
191
192
193
194
195
196
197 @JawkFunction("isarray")
198 public Long isarray(@JawkRawValue Object value) {
199 return value instanceof Map ? Long.valueOf(1L) : Long.valueOf(0L);
200 }
201
202
203
204
205
206
207
208 @JawkFunction("mkbool")
209 public GawkBool mkbool(Object value) {
210
211
212 return new GawkBool(getJrt().toBoolean(value));
213 }
214
215
216
217
218
219
220
221
222
223
224 @JawkFunction("gensub")
225 public String gensub(@JawkRegexp Object regexp, Object replacement, Object how, @JawkOptional Object target) {
226 Pattern pattern = regexp instanceof Pattern ? (Pattern) regexp : Pattern.compile(toAwkString(regexp));
227
228 pattern = getJrt().caseAwarePattern(pattern);
229 Object targetValue = target == null ? getJrt().getInputLine() : target;
230 Matcher matcher = pattern.matcher(toAwkString(targetValue));
231 String repl = JRT.prepareGensubReplacement(toAwkString(replacement), pattern.matcher("").groupCount());
232 String selector = toAwkString(how);
233
234 if (!selector.isEmpty() && (selector.charAt(0) == 'g' || selector.charAt(0) == 'G')) {
235 return matcher.replaceAll(repl);
236 }
237
238
239
240 double selected = JRT.toDouble(how);
241 if (selected < 1.0D) {
242 warnAtCurrentLine("gensub: third argument `%s' treated as 1", selector);
243 selected = 1.0D;
244 }
245 int occurrence = (int) selected;
246 if (occurrence == 1) {
247 return matcher.replaceFirst(repl);
248 }
249 StringBuffer result = new StringBuffer();
250 int seen = 0;
251 while (matcher.find()) {
252 seen++;
253 if (seen == occurrence) {
254 matcher.appendReplacement(result, repl);
255 break;
256 }
257 }
258 matcher.appendTail(result);
259 return result.toString();
260 }
261
262
263
264
265
266
267 @JawkFunction("systime")
268 public Long systime() {
269 return Long.valueOf(System.currentTimeMillis() / 1000L);
270 }
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286 @JawkFunction("mktime")
287 public Long mktime(Object datespec, @JawkOptional Object utcFlag) {
288 String[] fields = toAwkString(datespec).trim().split("\\s+");
289 if (fields.length < 6 || fields.length > 7) {
290 return Long.valueOf(-1L);
291 }
292 int[] values = new int[fields.length];
293 for (int i = 0; i < fields.length; i++) {
294 try {
295 values[i] = Integer.parseInt(fields[i]);
296 } catch (NumberFormatException e) {
297 return Long.valueOf(-1L);
298 }
299 }
300 boolean utc = utcFlag != null && getJrt().toBoolean(utcFlag);
301 TimeZone timeZone = utc ? TimeZone.getTimeZone("UTC") : localTimeZone();
302 GregorianCalendar calendar = new GregorianCalendar(timeZone);
303
304 calendar.setGregorianChange(new Date(Long.MIN_VALUE));
305 calendar.setLenient(true);
306 calendar.clear();
307 calendar.set(values[0], values[1] - 1, values[2], values[3], values[4], values[5]);
308 if (fields.length == 7 && !utc && values[6] >= 0) {
309
310
311 calendar.set(Calendar.DST_OFFSET, values[6] > 0 ? timeZone.getDSTSavings() : 0);
312 }
313 return Long.valueOf(Math.floorDiv(calendar.getTimeInMillis(), 1000L));
314 }
315
316
317
318
319
320
321
322
323
324
325 @JawkFunction("strftime")
326 public String strftime(@JawkOptional Object format, @JawkOptional Object timestamp, @JawkOptional Object utcFlag) {
327 String formatString = format == null ? defaultStrftimeFormat() : toAwkString(format);
328 long seconds = timestamp == null ? System.currentTimeMillis() / 1000L : (long) JRT.toDouble(timestamp);
329 boolean utc = utcFlag != null && getJrt().toBoolean(utcFlag);
330 TimeZone timeZone = utc ? TimeZone.getTimeZone("UTC") : localTimeZone();
331 return Strftime.format(formatString, seconds, timeZone);
332 }
333
334
335
336
337
338
339
340
341
342
343
344 private TimeZone localTimeZone() {
345 Object environ = getVm().getVariable("ENVIRON");
346 if (environ instanceof Map) {
347 @SuppressWarnings("unchecked")
348 Map<Object, Object> environMap = (Map<Object, Object>) environ;
349 String tz = getJrt().getAwkStringEntry(environMap, "TZ");
350 if (tz != null) {
351 if (tz.startsWith(":")) {
352
353
354 tz = tz.substring(1);
355 }
356
357 return TimeZone.getTimeZone(tz.isEmpty() ? "UTC" : tz);
358 }
359 }
360 return TimeZone.getDefault();
361 }
362
363
364 private String defaultStrftimeFormat() {
365 Object procinfo = getVm().getVariable("PROCINFO");
366 if (procinfo instanceof Map) {
367 @SuppressWarnings("unchecked")
368 Map<Object, Object> procinfoMap = (Map<Object, Object>) procinfo;
369 String format = getJrt().getAwkStringEntry(procinfoMap, "strftime");
370 if (format != null) {
371 return format;
372 }
373 }
374 return DEFAULT_STRFTIME_FORMAT;
375 }
376
377
378
379
380
381
382
383
384
385 @JawkFunction("strtonum")
386 public Number strtonum(@JawkRawValue Object value) {
387 if (value instanceof Number) {
388 return (Number) value;
389 }
390 if (value instanceof StrNum && ((StrNum) value).isNumber()) {
391
392
393 return Double.valueOf(((StrNum) value).doubleValue());
394 }
395 String text = toAwkString(value);
396 switch (numberBase(text)) {
397 case 16:
398 return parseNonDecimal(text, 2, 16);
399 case 8:
400 return parseNonDecimal(text, 1, 8);
401 default:
402 return Double.valueOf(JRT.toDouble(text));
403 }
404 }
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421 private static int numberBase(String text) {
422 if (text.length() < 2 || text.charAt(0) != '0') {
423 return 10;
424 }
425 char second = text.charAt(1);
426 if (second == 'x' || second == 'X') {
427 return 16;
428 }
429 for (int i = 1; i < text.length(); i++) {
430 char c = text.charAt(i);
431 if (c == '.' || c == 'e' || c == 'E') {
432 return 10;
433 }
434 if (c < '0' || c > '9') {
435 break;
436 }
437 if (c > '7') {
438 return 10;
439 }
440 }
441 return 8;
442 }
443
444
445
446
447
448
449
450 private static Number parseNonDecimal(String text, int offset, int base) {
451 int end = offset;
452 while (end < text.length() && Character.digit(text.charAt(end), base) >= 0) {
453 end++;
454 }
455 if (end == offset) {
456 return Long.valueOf(0L);
457 }
458 String digits = text.substring(offset, end);
459 try {
460 return Long.valueOf(Long.parseLong(digits, base));
461 } catch (NumberFormatException overflow) {
462 return Double.valueOf(new BigInteger(digits, base).doubleValue());
463 }
464 }
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479 @JawkFunction("patsplit")
480 public Long patsplit(
481 Object source,
482 @JawkAssocArray Map<Object, Object> array,
483 @JawkOptional @JawkRegexp Object fieldpat,
484 @JawkOptional @JawkAssocArray Map<Object, Object> seps) {
485 if (array == seps) {
486 throw new IllegalAwkArgumentException("patsplit: cannot use the same array for second and fourth args");
487 }
488 String str = toAwkString(source);
489 Pattern pattern = fieldPattern(fieldpat);
490 array.clear();
491 if (seps != null) {
492 seps.clear();
493 }
494 if (str.isEmpty()) {
495 return Long.valueOf(0L);
496 }
497
498
499
500
501
502
503
504
505
506 Matcher matcher = pattern.matcher(str);
507 int length = str.length();
508 int pos = 0;
509 int previousEnd = 0;
510 long fieldCount = 0L;
511 boolean lastMatchNonEmpty = false;
512 while (pos <= length) {
513 matcher.region(pos, length);
514 if (!matcher.find()) {
515 break;
516 }
517 int start = matcher.start();
518 int end = matcher.end();
519 if (end > start) {
520 lastMatchNonEmpty = true;
521 putSeparator(seps, fieldCount, str.substring(previousEnd, start));
522 array.put(Long.valueOf(++fieldCount), getJrt().toInputScalar(str.substring(start, end)));
523 previousEnd = end;
524 pos = end;
525 if (pos >= length) {
526 break;
527 }
528 } else if (lastMatchNonEmpty) {
529 lastMatchNonEmpty = false;
530 pos = str.offsetByCodePoints(pos, 1);
531 } else {
532 putSeparator(seps, fieldCount, str.substring(previousEnd, start));
533 array.put(Long.valueOf(++fieldCount), getJrt().toInputScalar(""));
534 previousEnd = start;
535 if (start >= length) {
536
537 break;
538 }
539 pos = str.offsetByCodePoints(start, 1);
540 }
541 }
542
543
544 putSeparator(seps, fieldCount, str.substring(previousEnd));
545 return Long.valueOf(fieldCount);
546 }
547
548
549 private void putSeparator(Map<Object, Object> separators, long index, String value) {
550 if (separators != null) {
551 separators.put(Long.valueOf(index), getJrt().toInputScalar(value));
552 }
553 }
554
555
556
557
558
559
560
561 private Pattern fieldPattern(Object fieldpat) {
562 if (fieldpat instanceof Pattern) {
563 Pattern pattern = (Pattern) fieldpat;
564 requireNonEmptyFieldPattern(pattern.pattern());
565 return getJrt().caseAwarePattern(pattern);
566 }
567 String expression;
568 if (fieldpat != null) {
569 expression = toAwkString(fieldpat);
570 } else {
571 Object fpat = getVm().getVariable("FPAT");
572 if (fpat == null || fpat instanceof UninitializedObject) {
573 return getJrt().dynamicPattern(DEFAULT_FPAT);
574 }
575 expression = toAwkString(fpat);
576 }
577 requireNonEmptyFieldPattern(expression);
578 return getJrt().dynamicPattern(expression);
579 }
580
581
582 private static void requireNonEmptyFieldPattern(String expression) {
583 if (expression.isEmpty()) {
584 throw new IllegalAwkArgumentException("patsplit: field pattern must be non-null");
585 }
586 }
587
588
589
590
591
592
593
594
595
596
597
598 @JawkFunction("dcgettext")
599 public String dcgettext(Object string, @JawkOptional Object domain, @JawkOptional Object category) {
600 checkLocaleCategory(category);
601
602
603 return toAwkString(string);
604 }
605
606
607
608
609
610
611
612
613
614
615
616
617
618 @JawkFunction("dcngettext")
619 public String dcngettext(
620 Object singular,
621 Object plural,
622 Object number,
623 @JawkOptional Object domain,
624 @JawkOptional Object category) {
625 checkLocaleCategory(category);
626 return (long) JRT.toDouble(number) == 1L ? toAwkString(singular) : toAwkString(plural);
627 }
628
629
630
631
632
633 private void checkLocaleCategory(Object category) {
634 if (category == null) {
635 return;
636 }
637 String name = toAwkString(category);
638 if (!LOCALE_CATEGORIES.contains(name)) {
639 throw new IllegalAwkArgumentException("dcgettext: `" + name + "' is not a valid locale category");
640 }
641 }
642
643
644
645
646
647
648
649
650
651
652 @JawkFunction("bindtextdomain")
653 public String bindtextdomain(Object directory, @JawkOptional Object domain) {
654 String domainName = domain == null ? currentTextdomain() : toAwkString(domain);
655 if (domainName.isEmpty()) {
656
657
658 return "";
659 }
660 String directoryName = toAwkString(directory);
661 if (textdomainBindings == null) {
662 textdomainBindings = new HashMap<String, String>();
663 }
664 if (!directoryName.isEmpty()) {
665 textdomainBindings.put(domainName, directoryName);
666 }
667 String bound = textdomainBindings.get(domainName);
668 return bound == null ? DEFAULT_LOCALE_DIRECTORY : bound;
669 }
670
671
672 private String currentTextdomain() {
673 Object textdomain = getVm().getVariable("TEXTDOMAIN");
674 String name = textdomain == null ? "" : toAwkString(textdomain);
675 return name.isEmpty() ? DEFAULT_TEXTDOMAIN : name;
676 }
677
678
679
680
681
682 private void warnAtCurrentLine(String format, Object... args) {
683 String source = avm == null ? null : avm.getSourceDescription();
684 String basename = source == null ? "" : new File(source).getName();
685 getJrt().printWarning(
686 String.format(
687 "gawk: %s:%d: warning: %s",
688 basename,
689 avm == null ? 0 : avm.getCurrentLineNumber(),
690 String.format(format, args)));
691 }
692
693
694
695
696
697
698 private Collection<Object> orderForInKeys(Map<Object, Object> map) {
699 String mode = currentSortedIn();
700 if (mode == null || mode.isEmpty() || "@unsorted".equals(mode)) {
701 return map.keySet();
702 }
703 return sortedKeys(map, effectiveSortMode(mode, VAL_TYPE_ASC), getJrt(), currentIgnoreCase());
704 }
705
706 private String currentSortedIn() {
707 Object procinfo = getVm().getVariable("PROCINFO");
708 if (!(procinfo instanceof Map)) {
709 return null;
710 }
711 @SuppressWarnings("unchecked")
712 Map<Object, Object> procinfoMap = (Map<Object, Object>) procinfo;
713 return getJrt().getAwkStringEntry(procinfoMap, "sorted_in");
714 }
715
716
717
718
719
720
721 private String effectiveSortMode(String mode, String defaultMode) {
722 if (mode.isEmpty()) {
723
724 return defaultMode;
725 }
726 if (mode.charAt(0) != '@') {
727 warnUnsupportedComparator(mode);
728 return defaultMode;
729 }
730 return mode;
731 }
732
733 private void warnUnsupportedComparator(String name) {
734 if (warnedComparators == null) {
735 warnedComparators = new HashSet<String>();
736 }
737 if (warnedComparators.add(name)) {
738 warnAtCurrentLine("sort comparison function `%s' is not supported; using default ordering", name);
739 }
740 }
741
742 private Long sort(Map<Object, Object> source, Map<Object, Object> dest, Object how, boolean indicesAsValues) {
743 Map<Object, Object> destination = dest == null ? source : dest;
744
745
746 String defaultMode = indicesAsValues ? "@ind_str_asc" : VAL_TYPE_ASC;
747 String mode = how == null ? defaultMode : effectiveSortMode(toAwkString(how), defaultMode);
748 List<SortEntry> entries = entries(source);
749
750 if (!"@unsorted".equals(mode)) {
751 Collections.sort(entries, comparator(mode, getJrt(), currentIgnoreCase()));
752 }
753 destination.clear();
754 long idx = 1L;
755 for (SortEntry entry : entries) {
756
757
758 Object value = indicesAsValues ? getJrt().toAwkString(entry.index) : entry.value;
759 destination.put(Long.valueOf(idx++), value);
760 }
761 return Long.valueOf(entries.size());
762 }
763
764
765
766
767
768
769
770
771
772
773 private static List<Object> sortedKeys(Map<Object, Object> map, String mode, JRT jrt, boolean ignoreCase) {
774 List<SortEntry> entries = entries(map);
775 Collections.sort(entries, comparator(mode, jrt, ignoreCase));
776 List<Object> keys = new ArrayList<Object>(entries.size());
777 for (SortEntry entry : entries) {
778 keys.add(entry.index);
779 }
780 return keys;
781 }
782
783 private static List<SortEntry> entries(Map<Object, Object> map) {
784 List<SortEntry> entries = new ArrayList<SortEntry>(map.size());
785 for (Map.Entry<Object, Object> entry : map.entrySet()) {
786 entries.add(new SortEntry(entry.getKey(), entry.getValue()));
787 }
788 return entries;
789 }
790
791 private boolean currentIgnoreCase() {
792 return getJrt().isIgnoreCase();
793 }
794
795
796
797
798
799 private static Comparator<SortEntry> comparator(String effectiveMode, JRT jrt, boolean ignoreCase) {
800 boolean desc = effectiveMode.endsWith("_desc");
801 Comparator<SortEntry> comparator;
802
803
804
805
806
807
808
809 switch (effectiveMode) {
810 case "@ind_num_asc":
811 case "@ind_num_desc":
812 comparator = (left, right) -> compareNumericThenText(left.index, right.index, jrt, ignoreCase);
813 break;
814 case "@ind_str_asc":
815 case "@ind_str_desc":
816 comparator = (left, right) -> compareStrings(left.index, right.index, jrt, ignoreCase);
817 break;
818 case "@ind_type_asc":
819 case "@ind_type_desc":
820 comparator = (left, right) -> compareByTypeThenValue(left.index, right.index, jrt, ignoreCase);
821 break;
822 case "@val_num_asc":
823 case "@val_num_desc":
824 comparator = (left, right) -> compareNumericThenText(left.value, right.value, jrt, ignoreCase);
825 break;
826 case "@val_str_asc":
827 case "@val_str_desc":
828 comparator = (left, right) -> compareStrings(left.value, right.value, jrt, ignoreCase);
829 break;
830 case "@val_type_asc":
831 case "@val_type_desc":
832 comparator = (left, right) -> compareByTypeThenValue(left.value, right.value, jrt, ignoreCase);
833 break;
834 default:
835 throw new IllegalAwkArgumentException("Invalid sort comparison mode '" + effectiveMode + "'");
836 }
837 return desc ? comparator.reversed() : comparator;
838 }
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856 private static int compareByTypeThenValue(Object left, Object right, JRT jrt, boolean ignoreCase) {
857 int leftRank = typeRank(left);
858 int rightRank = typeRank(right);
859 if (leftRank != rightRank) {
860 return Integer.compare(leftRank, rightRank);
861 }
862 if (left instanceof Map && right instanceof Map) {
863 return 0;
864 }
865 if (leftRank == 0) {
866 return compareNumbers(left, right);
867 }
868 return compareStrings(left, right, jrt, ignoreCase);
869 }
870
871
872 private static int typeRank(Object value) {
873 if (value instanceof Number || isStrnum(value)) {
874 return 0;
875 }
876 if (value instanceof Map) {
877 return 2;
878 }
879 return 1;
880 }
881
882
883 private static int compareNumbers(Object left, Object right) {
884 return Double.compare(numericSortValue(left), numericSortValue(right));
885 }
886
887 private static double numericSortValue(Object value) {
888 return value instanceof Map ? 0.0D : JRT.toDouble(value);
889 }
890
891
892
893
894
895
896 private static int compareNumericThenText(Object left, Object right, JRT jrt, boolean ignoreCase) {
897 if (left instanceof Map || right instanceof Map) {
898 if (left instanceof Map && right instanceof Map) {
899 return 0;
900 }
901 return left instanceof Map ? 1 : -1;
902 }
903 int numeric = compareNumbers(left, right);
904 if (numeric != 0) {
905 return numeric;
906 }
907 return compareStrings(left, right, jrt, ignoreCase);
908 }
909
910
911
912
913
914 private static int compareStrings(Object left, Object right, JRT jrt, boolean ignoreCase) {
915 if (left instanceof Map || right instanceof Map) {
916 if (left instanceof Map && right instanceof Map) {
917 return 0;
918 }
919 return left instanceof Map ? 1 : -1;
920 }
921 String leftString = jrt.toAwkString(left);
922 String rightString = jrt.toAwkString(right);
923
924
925 return ignoreCase ? leftString.compareToIgnoreCase(rightString) : leftString.compareTo(rightString);
926 }
927
928 private static String typeOf(Object value) {
929 if (value == null || value instanceof UntypedObject) {
930 return "untyped";
931 }
932 if (value instanceof Map) {
933 return "array";
934 }
935 if (value instanceof GawkBool) {
936 return "number|bool";
937 }
938 if (value instanceof Number) {
939 return "number";
940 }
941 if (value instanceof Pattern) {
942 return "regexp";
943 }
944 if (value instanceof UninitializedObject) {
945 return "unassigned";
946 }
947 return isStrnum(value) ? "strnum" : "string";
948 }
949
950 private static boolean isStrnum(Object value) {
951 return value instanceof StrNum && ((StrNum) value).isNumber();
952 }
953
954 private static String arrayType(Map<?, ?> map) {
955 if (map.isEmpty()) {
956 return "null";
957 }
958 boolean allNonNegativeIntegral = true;
959 for (Object key : map.keySet()) {
960 if (!(key instanceof Number)) {
961 return "str";
962 }
963 long longValue = ((Number) key).longValue();
964 double doubleValue = ((Number) key).doubleValue();
965 if (Double.compare(doubleValue, (double) longValue) != 0) {
966 return "str";
967 }
968 if (longValue < 0) {
969 allNonNegativeIntegral = false;
970 }
971 }
972 return allNonNegativeIntegral ? "cint" : "int";
973 }
974 }