From 73ea3e333f30bc2c86de46ef44b65009ad1e453b Mon Sep 17 00:00:00 2001 From: Nikita Koss Date: Thu, 5 Oct 2023 14:56:13 +0300 Subject: [PATCH] replace spec indices search with regex --- src/patch/mod.rs | 21 ++++++++------------- 1 file changed, 8 insertions(+), 13 deletions(-) diff --git a/src/patch/mod.rs b/src/patch/mod.rs index 738521b9..572513d5 100644 --- a/src/patch/mod.rs +++ b/src/patch/mod.rs @@ -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}; @@ -571,19 +572,13 @@ fn make_cpu(cmod: &Hash) -> Result { /// 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) }