1 package net.mograsim.plugin.asm;
3 import static java.lang.String.format;
5 import java.util.regex.Pattern;
7 public final class AsmNumberUtil
10 private AsmNumberUtil()
15 private static final String sgnPat = "[-+]?";
16 private static final String binPat = "(?:[01]+_)*[01]+";
17 private static final String octPat = "(?:[0-7]+_)*[0-7]+";
18 private static final String decPat = "(?:[0-9]+_)*[0-9]+";
19 private static final String hexPat = "(?:[0-9a-f]+_)*[0-9a-f]+";
20 static final Pattern numberBin = Pattern.compile(format("%1$s0b%2$s|%1$s%2$sb", sgnPat, binPat), Pattern.CASE_INSENSITIVE);
21 static final Pattern numberOct = Pattern.compile(format("%s%sq", sgnPat, octPat), Pattern.CASE_INSENSITIVE);
22 static final Pattern numberDec = Pattern.compile(format("%s%s", sgnPat, decPat));
23 static final Pattern numberHex = Pattern.compile(format("%1$s0x%2$s|%1$s%2$sh", sgnPat, hexPat), Pattern.CASE_INSENSITIVE);
24 static final Pattern numberFloat = Pattern.compile(format("%1$s%2$s(?:\\.%2$s)?(?:e%1$s%2$s)?", sgnPat, decPat),
25 Pattern.CASE_INSENSITIVE);
27 public static boolean isBinary(CharSequence cs)
29 return numberBin.matcher(cs).matches();
32 public static boolean isOctal(CharSequence cs)
34 return numberOct.matcher(cs).matches();
37 public static boolean isDecimal(CharSequence cs)
39 return numberDec.matcher(cs).matches();
42 public static boolean isHexadecimal(CharSequence cs)
44 return numberHex.matcher(cs).matches();
47 public static boolean isFloatingPoint(CharSequence cs)
49 return numberFloat.matcher(cs).matches();
52 public static boolean isNumber(CharSequence cs)
54 return getType(cs) != NumberType.NONE;
57 public static boolean quickCheckForNumber(CharSequence cs)
59 if (cs.length() == 0 || !isStart(cs.charAt(0)))
61 return cs.length() == 1 || isPart(cs.charAt(1));
64 public static boolean isStart(int c)
66 return isDigit(c) || c == '+' || c == '-';
69 public static boolean isDigit(int c)
71 return ('0' <= c && c <= '9') || ('A' <= c && c <= 'F') || ('a' <= c && c <= 'f');
74 public static boolean isPart(int c)
76 return isDigit(c) || isMarker(Character.toLowerCase(c));
79 public static boolean isMarker(int lowerCase)
98 public static NumberType getType(CharSequence cs)
100 if (!quickCheckForNumber(cs))
101 return NumberType.NONE;
103 return NumberType.DECIMAL;
104 if (isHexadecimal(cs))
105 return NumberType.HEXADECIMAL;
107 return NumberType.BINARY;
109 return NumberType.OCTAL;
110 if (isFloatingPoint(cs))
111 return NumberType.FLOATINGPOINT;
112 return NumberType.NONE;
115 public enum NumberType
117 NONE, BINARY, OCTAL, DECIMAL, HEXADECIMAL, FLOATINGPOINT