From 445c106a46cc2d7244442335f2aa05b25b9de9a8 Mon Sep 17 00:00:00 2001 From: Folkert Date: Wed, 10 Nov 2021 13:52:21 +0100 Subject: [PATCH 1/2] move regex to dev-dependencies --- Cargo.toml | 4 +++- src/types/int_type.rs | 27 ++++++++++++++++++--------- 2 files changed, 21 insertions(+), 10 deletions(-) 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..f25822e425705 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 == '-')); + + // 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 } From 1d1c12a93fd9403b316698f6cc65e936e4be1f49 Mon Sep 17 00:00:00 2001 From: Folkert Date: Wed, 10 Nov 2021 14:04:19 +0100 Subject: [PATCH 2/2] fix copy-paste mistake --- src/types/int_type.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/types/int_type.rs b/src/types/int_type.rs index f25822e425705..0404c4ef851d4 100644 --- a/src/types/int_type.rs +++ b/src/types/int_type.rs @@ -45,7 +45,7 @@ impl StringRadix { /// 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 == '-')); + let slice = slice.strip_prefix(|c| c == '+' || c == '-').unwrap_or(slice); // there must be at least 1 actual digit if slice.is_empty() {