Skip to content

Commit

Permalink
replace spec indices search with regex
Browse files Browse the repository at this point in the history
  • Loading branch information
kossnikita committed Oct 5, 2023
1 parent 9ab01ef commit eaa93d2
Showing 1 changed file with 10 additions and 13 deletions.
23 changes: 10 additions & 13 deletions src/patch/mod.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
pub mod patch_cli;

use globset::Glob;
use regex::Regex;
use std::fs::File;
use std::io::{Read, Write};
use std::path::{Path, PathBuf};
Expand Down Expand Up @@ -571,19 +572,15 @@ fn make_cpu(cmod: &Hash) -> Result<CpuBuilder> {
/// Find left and right indices of enumeration token in specification string
fn spec_ind(spec: &str) -> (usize, usize) {
let spec = spec.split(',').next().unwrap_or(spec);
let li = spec
.bytes()
.position(|b| b == b'*')
.or_else(|| spec.bytes().position(|b| b == b'?'))
.or_else(|| spec.bytes().position(|b| b == b'['))
.unwrap();
let ri = spec
.bytes()
.rev()
.position(|b| b == b'*')
.or_else(|| spec.bytes().rev().position(|b| b == b'?'))
.or_else(|| spec.bytes().rev().position(|b| b == b']'))
.unwrap();
let re = Regex::new(
r"^\w*((?:[\?*]|\[\d+(?:-\d+)?\]|\[[a-zA-Z]+(?:-[a-zA-Z]+)?\])+)\w*$",
)
.unwrap();
let caps = re.captures(spec).unwrap();
let spec = caps.get(0).unwrap();
let token = caps.get(1).unwrap();
let li = token.start();
let ri = spec.end() - token.end();
(li, ri)
}

Expand Down

0 comments on commit eaa93d2

Please sign in to comment.