Skip to content

Commit

Permalink
Merge pull request #69 from zkemail/fix/extract-idxes
Browse files Browse the repository at this point in the history
fix: Patch Extract idxes
  • Loading branch information
Bisht13 authored Aug 30, 2024
2 parents c9a89c3 + 653a492 commit 9195cb0
Showing 1 changed file with 25 additions and 22 deletions.
47 changes: 25 additions & 22 deletions packages/apis/src/extract_substrs.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
use fancy_regex::Regex;
use js_sys::Array;
use serde::{Deserialize, Serialize};
use thiserror::Error;

Expand All @@ -16,10 +15,6 @@ pub struct RegexPartConfig {
pub is_public: bool,
/// A regex string.
pub regex_def: String,
// Maximum byte size of the substring in this part.
// pub max_size: usize,
// (Optional) A solidity type of the substring in this part, e.g., "String", "Int", "Decimal".
// pub solidity: Option<SoldityType>,
}

/// Error definitions of the compiler.
Expand All @@ -39,29 +34,37 @@ pub fn extract_substr_idxes(
input_str: &str,
regex_config: &DecomposedRegexConfig,
) -> Result<Vec<(usize, usize)>, ExtractSubstrssError> {
// Construct the full regex pattern with groups for each part
let mut entire_regex_str = String::new();
for part in regex_config.parts.iter() {
entire_regex_str += part.regex_def.as_str();
for (_, part) in regex_config.parts.iter().enumerate() {
let adjusted_regex_def = part.regex_def.replace("(", "(?:");
entire_regex_str += &format!("({})", adjusted_regex_def); // Wrap each part in a group
}

// Compile the entire regex
let entire_regex = Regex::new(&entire_regex_str)?;
let entire_found = entire_regex.find(input_str)?.ok_or_else(|| {
ExtractSubstrssError::SubstringOfEntireNotFound(entire_regex, input_str.to_string())
})?;
let mut start = entire_found.start();
// let entire_end: usize = entire_found.end();

// Find the match for the entire regex
let entire_captures = entire_regex
.captures(input_str)
.map_err(|_| {
ExtractSubstrssError::SubstringOfEntireNotFound(
entire_regex.clone(),
input_str.to_string(),
)
})?
.expect("Expected a match, but none was found");

let mut public_idxes = vec![];
for part_idx in 0..regex_config.parts.len() {
let regex_def = regex_config.parts[part_idx].regex_def.as_str();
let regex = Regex::new(&regex_def)?;
let end = match regex.find_from_pos(&input_str, start)? {
Some(found) => found.end(),
None => start,
};
if regex_config.parts[part_idx].is_public {
public_idxes.push((start, end));

// Iterate over each part to extract the relevant indices
for (i, part) in regex_config.parts.iter().enumerate() {
if part.is_public {
if let Some(matched) = entire_captures.get(i + 1) {
// Capture group indices are 1-based
public_idxes.push((matched.start(), matched.end()));
}
}
start = end;
}

Ok(public_idxes)
Expand Down

0 comments on commit 9195cb0

Please sign in to comment.