diff --git a/Cargo.toml b/Cargo.toml index 0b454d6a5d278..22bb91f66b29f 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -107,9 +107,11 @@ llvm-sys-120 = { package = "llvm-sys", version = "120.2", optional = true } llvm-sys-130 = { package = "llvm-sys", version = "130.0", optional = true } once_cell = "1.4.1" parking_lot = "0.11" -regex = "1" static-alloc = { version = "0.2", optional = true } +[dev-dependencies] +regex = "1" + [badges] travis-ci = { repository = "TheDan64/inkwell" } codecov = { repository = "TheDan64/inkwell" } diff --git a/src/types/int_type.rs b/src/types/int_type.rs index e86e99faa9b43..0404c4ef851d4 100644 --- a/src/types/int_type.rs +++ b/src/types/int_type.rs @@ -1,7 +1,6 @@ use llvm_sys::core::{LLVMConstInt, LLVMConstAllOnes, LLVMGetIntTypeWidth, LLVMConstIntOfStringAndSize, LLVMConstIntOfArbitraryPrecision, LLVMConstArray}; use llvm_sys::execution_engine::LLVMCreateGenericValueOfInt; use llvm_sys::prelude::{LLVMTypeRef, LLVMValueRef}; -use regex::Regex; use crate::AddressSpace; use crate::context::ContextRef; @@ -43,14 +42,24 @@ impl TryFrom for StringRadix { } impl StringRadix { - /// Create a Regex that matches valid strings for the given radix. - pub fn to_regex(&self) -> Regex { + /// Is the string valid for the given radix? + pub fn matches_str(&self, slice: &str) -> bool { + // drop 1 optional + or - + let slice = slice.strip_prefix(|c| c == '+' || c == '-').unwrap_or(slice); + + // there must be at least 1 actual digit + if slice.is_empty() { + return false; + } + + // and all digits must be in the radix' character set + let mut it = slice.chars(); match self { - StringRadix::Binary => Regex::new(r"^[-+]?[01]+$").unwrap(), - StringRadix::Octal => Regex::new(r"^[-+]?[0-7]+$").unwrap(), - StringRadix::Decimal => Regex::new(r"^[-+]?[0-9]+$").unwrap(), - StringRadix::Hexadecimal => Regex::new(r"^[-+]?[0-9abcdefABCDEF]+$").unwrap(), - StringRadix::Alphanumeric => Regex::new(r"^[-+]?[0-9[:alpha:]]+$").unwrap(), + StringRadix::Binary => it.all(|c| matches!(c, '0'..='1')), + StringRadix::Octal => it.all(|c| matches!(c, '0'..='7')), + StringRadix::Decimal => it.all(|c| matches!(c, '0'..='9')), + StringRadix::Hexadecimal => it.all(|c| matches!(c, '0'..='9' | 'a'..='f' | 'A'..='F')), + StringRadix::Alphanumeric => it.all(|c| matches!(c, '0'..='9' | 'a'..='z' | 'A'..='Z')), } } } @@ -117,7 +126,7 @@ impl<'ctx> IntType<'ctx> { /// assert!(i8_val.is_none()); /// ``` pub fn const_int_from_string(self, slice: &str, radix: StringRadix) -> Option> { - if !radix.to_regex().is_match(slice) { + if !radix.matches_str(slice) { return None }