Merge branch 'development' of https://gitlab.lrz.de/lrr-tum/students/eragp-misim...
[Mograsim.git] / net.mograsim.plugin.core / src / net / mograsim / plugin / asm / AsmNumberUtil.java
1 package net.mograsim.plugin.asm;
2
3 import static java.lang.String.format;
4
5 import java.util.regex.Pattern;
6
7 public final class AsmNumberUtil
8 {
9
10         private AsmNumberUtil()
11         {
12
13         }
14
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);
26
27         public static boolean isBinary(CharSequence cs)
28         {
29                 return numberBin.matcher(cs).matches();
30         }
31
32         public static boolean isOctal(CharSequence cs)
33         {
34                 return numberOct.matcher(cs).matches();
35         }
36
37         public static boolean isDecimal(CharSequence cs)
38         {
39                 return numberDec.matcher(cs).matches();
40         }
41
42         public static boolean isHexadecimal(CharSequence cs)
43         {
44                 return numberHex.matcher(cs).matches();
45         }
46
47         public static boolean isFloatingPoint(CharSequence cs)
48         {
49                 return numberFloat.matcher(cs).matches();
50         }
51
52         public static boolean isNumber(CharSequence cs)
53         {
54                 return getType(cs) != NumberType.NONE;
55         }
56
57         public static boolean quickCheckForNumber(CharSequence cs)
58         {
59                 if (cs.length() == 0 || !isStart(cs.charAt(0)))
60                         return false;
61                 return cs.length() == 1 || isPart(cs.charAt(1));
62         }
63
64         public static boolean isStart(int c)
65         {
66                 return isDigit(c) || c == '+' || c == '-';
67         }
68
69         public static boolean isDigit(int c)
70         {
71                 return ('0' <= c && c <= '9') || ('A' <= c && c <= 'F') || ('a' <= c && c <= 'f');
72         }
73
74         public static boolean isPart(int c)
75         {
76                 return isDigit(c) || isMarker(Character.toLowerCase(c));
77         }
78
79         public static boolean isMarker(int lowerCase)
80         {
81                 switch (lowerCase)
82                 {
83                 case '.':
84                 case '_':
85                 case '+':
86                 case '-':
87                 case 'e':
88                 case 'x':
89                 case 'b':
90                 case 'q':
91                 case 'h':
92                         return true;
93                 default:
94                         return false;
95                 }
96         }
97
98         public static NumberType getType(CharSequence cs)
99         {
100                 if (!quickCheckForNumber(cs))
101                         return NumberType.NONE;
102                 if (isDecimal(cs))
103                         return NumberType.DECIMAL;
104                 if (isHexadecimal(cs))
105                         return NumberType.HEXADECIMAL;
106                 if (isBinary(cs))
107                         return NumberType.BINARY;
108                 if (isOctal(cs))
109                         return NumberType.OCTAL;
110                 if (isFloatingPoint(cs))
111                         return NumberType.FLOATINGPOINT;
112                 return NumberType.NONE;
113         }
114
115         public enum NumberType
116         {
117                 NONE, BINARY, OCTAL, DECIMAL, HEXADECIMAL, FLOATINGPOINT
118         }
119 }