|
1 | 1 | use llvm_sys::core::{LLVMConstInt, LLVMConstAllOnes, LLVMGetIntTypeWidth, LLVMConstIntOfStringAndSize, LLVMConstIntOfArbitraryPrecision, LLVMConstArray};
|
2 | 2 | use llvm_sys::execution_engine::LLVMCreateGenericValueOfInt;
|
3 | 3 | use llvm_sys::prelude::{LLVMTypeRef, LLVMValueRef};
|
4 |
| -use regex::Regex; |
5 | 4 |
|
6 | 5 | use crate::AddressSpace;
|
7 | 6 | use crate::context::ContextRef;
|
@@ -43,14 +42,24 @@ impl TryFrom<u8> for StringRadix {
|
43 | 42 | }
|
44 | 43 |
|
45 | 44 | impl StringRadix {
|
46 |
| - /// Create a Regex that matches valid strings for the given radix. |
47 |
| - pub fn to_regex(&self) -> Regex { |
| 45 | + /// Is the string valid for the given radix? |
| 46 | + pub fn matches_str(&self, slice: &str) -> bool { |
| 47 | + // drop 1 optional + or - |
| 48 | + let slice = slice.strip_prefix(|c| c == '+' || c == '-').unwrap_or(slice); |
| 49 | + |
| 50 | + // there must be at least 1 actual digit |
| 51 | + if slice.is_empty() { |
| 52 | + return false; |
| 53 | + } |
| 54 | + |
| 55 | + // and all digits must be in the radix' character set |
| 56 | + let mut it = slice.chars(); |
48 | 57 | match self {
|
49 |
| - StringRadix::Binary => Regex::new(r"^[-+]?[01]+$").unwrap(), |
50 |
| - StringRadix::Octal => Regex::new(r"^[-+]?[0-7]+$").unwrap(), |
51 |
| - StringRadix::Decimal => Regex::new(r"^[-+]?[0-9]+$").unwrap(), |
52 |
| - StringRadix::Hexadecimal => Regex::new(r"^[-+]?[0-9abcdefABCDEF]+$").unwrap(), |
53 |
| - StringRadix::Alphanumeric => Regex::new(r"^[-+]?[0-9[:alpha:]]+$").unwrap(), |
| 58 | + StringRadix::Binary => it.all(|c| matches!(c, '0'..='1')), |
| 59 | + StringRadix::Octal => it.all(|c| matches!(c, '0'..='7')), |
| 60 | + StringRadix::Decimal => it.all(|c| matches!(c, '0'..='9')), |
| 61 | + StringRadix::Hexadecimal => it.all(|c| matches!(c, '0'..='9' | 'a'..='f' | 'A'..='F')), |
| 62 | + StringRadix::Alphanumeric => it.all(|c| matches!(c, '0'..='9' | 'a'..='z' | 'A'..='Z')), |
54 | 63 | }
|
55 | 64 | }
|
56 | 65 | }
|
@@ -117,7 +126,7 @@ impl<'ctx> IntType<'ctx> {
|
117 | 126 | /// assert!(i8_val.is_none());
|
118 | 127 | /// ```
|
119 | 128 | pub fn const_int_from_string(self, slice: &str, radix: StringRadix) -> Option<IntValue<'ctx>> {
|
120 |
| - if !radix.to_regex().is_match(slice) { |
| 129 | + if !radix.matches_str(slice) { |
121 | 130 | return None
|
122 | 131 | }
|
123 | 132 |
|
|
0 commit comments