Skip to content

Commit 4ddaa3b

Browse files
authored
Merge pull request rust-lang#281 from folkertdev/drop-regex-dependency
remove regex dependency (outside of tests)
2 parents f3a78d1 + 1d1c12a commit 4ddaa3b

File tree

2 files changed

+21
-10
lines changed

2 files changed

+21
-10
lines changed

Cargo.toml

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -107,9 +107,11 @@ llvm-sys-120 = { package = "llvm-sys", version = "120.2", optional = true }
107107
llvm-sys-130 = { package = "llvm-sys", version = "130.0", optional = true }
108108
once_cell = "1.4.1"
109109
parking_lot = "0.11"
110-
regex = "1"
111110
static-alloc = { version = "0.2", optional = true }
112111

112+
[dev-dependencies]
113+
regex = "1"
114+
113115
[badges]
114116
travis-ci = { repository = "TheDan64/inkwell" }
115117
codecov = { repository = "TheDan64/inkwell" }

src/types/int_type.rs

Lines changed: 18 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
use llvm_sys::core::{LLVMConstInt, LLVMConstAllOnes, LLVMGetIntTypeWidth, LLVMConstIntOfStringAndSize, LLVMConstIntOfArbitraryPrecision, LLVMConstArray};
22
use llvm_sys::execution_engine::LLVMCreateGenericValueOfInt;
33
use llvm_sys::prelude::{LLVMTypeRef, LLVMValueRef};
4-
use regex::Regex;
54

65
use crate::AddressSpace;
76
use crate::context::ContextRef;
@@ -43,14 +42,24 @@ impl TryFrom<u8> for StringRadix {
4342
}
4443

4544
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();
4857
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')),
5463
}
5564
}
5665
}
@@ -117,7 +126,7 @@ impl<'ctx> IntType<'ctx> {
117126
/// assert!(i8_val.is_none());
118127
/// ```
119128
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) {
121130
return None
122131
}
123132

0 commit comments

Comments
 (0)