Completely changed the structure and switched to Eclipse Plugin.
[Mograsim.git] / net.mograsim.plugin.core / src / net / mograsim / plugin / asm / AsmNumberUtil.java
1 package net.mograsim.plugin.asm;\r
2 \r
3 import static java.lang.String.format;\r
4 \r
5 import java.util.regex.Pattern;\r
6 \r
7 public final class AsmNumberUtil\r
8 {\r
9 \r
10         private AsmNumberUtil()\r
11         {\r
12 \r
13         }\r
14 \r
15         private static final String sgnPat = "[-+]?";\r
16         private static final String binPat = "(?:[01]+_)*[01]+";\r
17         private static final String octPat = "(?:[0-7]+_)*[0-7]+";\r
18         private static final String decPat = "(?:[0-9]+_)*[0-9]+";\r
19         private static final String hexPat = "(?:[0-9a-f]+_)*[0-9a-f]+";\r
20         static final Pattern numberBin = Pattern.compile(format("%1$s0b%2$s|%1$s%2$sb", sgnPat, binPat), Pattern.CASE_INSENSITIVE);\r
21         static final Pattern numberOct = Pattern.compile(format("%s%sq", sgnPat, octPat), Pattern.CASE_INSENSITIVE);\r
22         static final Pattern numberDec = Pattern.compile(format("%s%s", sgnPat, decPat));\r
23         static final Pattern numberHex = Pattern.compile(format("%1$s0x%2$s|%1$s%2$sh", sgnPat, hexPat), Pattern.CASE_INSENSITIVE);\r
24         static final Pattern numberFloat = Pattern.compile(format("%1$s%2$s(?:\\.%2$s)?(?:e%1$s%2$s)?", sgnPat, decPat),\r
25                         Pattern.CASE_INSENSITIVE);\r
26 \r
27         public static boolean isBinary(CharSequence cs)\r
28         {\r
29                 return numberBin.matcher(cs).matches();\r
30         }\r
31 \r
32         public static boolean isOctal(CharSequence cs)\r
33         {\r
34                 return numberOct.matcher(cs).matches();\r
35         }\r
36 \r
37         public static boolean isDecimal(CharSequence cs)\r
38         {\r
39                 return numberDec.matcher(cs).matches();\r
40         }\r
41 \r
42         public static boolean isHexadecimal(CharSequence cs)\r
43         {\r
44                 return numberHex.matcher(cs).matches();\r
45         }\r
46 \r
47         public static boolean isFloatingPoint(CharSequence cs)\r
48         {\r
49                 return numberFloat.matcher(cs).matches();\r
50         }\r
51 \r
52         public static boolean isNumber(CharSequence cs)\r
53         {\r
54                 return getType(cs) != NumberType.NONE;\r
55         }\r
56 \r
57         public static boolean quickCheckForNumber(CharSequence cs)\r
58         {\r
59                 if (cs.length() == 0 || !isStart(cs.charAt(0)))\r
60                         return false;\r
61                 return cs.length() == 1 || isPart(cs.charAt(1));\r
62         }\r
63 \r
64         public static boolean isStart(int c)\r
65         {\r
66                 return isDigit(c) || c == '+' || c == '-';\r
67         }\r
68 \r
69         public static boolean isDigit(int c)\r
70         {\r
71                 return ('0' <= c && c <= '9') || ('A' <= c && c <= 'F') || ('a' <= c && c <= 'f');\r
72         }\r
73 \r
74         public static boolean isPart(int c)\r
75         {\r
76                 return isDigit(c) || isMarker(Character.toLowerCase(c));\r
77         }\r
78 \r
79         public static boolean isMarker(int lowerCase)\r
80         {\r
81                 switch (lowerCase)\r
82                 {\r
83                 case '.':\r
84                 case '_':\r
85                 case '+':\r
86                 case '-':\r
87                 case 'e':\r
88                 case 'x':\r
89                 case 'b':\r
90                 case 'q':\r
91                 case 'h':\r
92                         return true;\r
93                 default:\r
94                         return false;\r
95                 }\r
96         }\r
97 \r
98         public static NumberType getType(CharSequence cs)\r
99         {\r
100                 if (!quickCheckForNumber(cs))\r
101                         return NumberType.NONE;\r
102                 if (isDecimal(cs))\r
103                         return NumberType.DECIMAL;\r
104                 if (isHexadecimal(cs))\r
105                         return NumberType.HEXADECIMAL;\r
106                 if (isBinary(cs))\r
107                         return NumberType.BINARY;\r
108                 if (isOctal(cs))\r
109                         return NumberType.OCTAL;\r
110                 if (isFloatingPoint(cs))\r
111                         return NumberType.FLOATINGPOINT;\r
112                 return NumberType.NONE;\r
113         }\r
114 \r
115         public enum NumberType\r
116         {\r
117                 NONE, BINARY, OCTAL, DECIMAL, HEXADECIMAL, FLOATINGPOINT\r
118         }\r
119 }\r