Skip to content

Commit

Permalink
Merge pull request rust-lang#281 from folkertdev/drop-regex-dependency
Browse files Browse the repository at this point in the history
remove regex dependency (outside of tests)
  • Loading branch information
TheDan64 authored Nov 18, 2021
2 parents f3a78d1 + 1d1c12a commit 4ddaa3b
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 10 deletions.
4 changes: 3 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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" }
27 changes: 18 additions & 9 deletions src/types/int_type.rs
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -43,14 +42,24 @@ impl TryFrom<u8> 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')),
}
}
}
Expand Down Expand Up @@ -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<IntValue<'ctx>> {
if !radix.to_regex().is_match(slice) {
if !radix.matches_str(slice) {
return None
}

Expand Down

0 comments on commit 4ddaa3b

Please sign in to comment.