From ad86f6cd6452f8ad2a64da97f69b9d7a7d84627a Mon Sep 17 00:00:00 2001 From: n08i40k Date: Fri, 10 Oct 2025 01:39:54 +0400 Subject: [PATCH] feat(parser): limit names regex to maximum 2 elements This allows us to not worry about subgroups array index overflows, and we can make better non-standard case solving. --- .../src/parser/mod.rs | 24 ++++++++++++------- 1 file changed, 15 insertions(+), 9 deletions(-) diff --git a/providers/provider-engels-polytechnic/src/parser/mod.rs b/providers/provider-engels-polytechnic/src/parser/mod.rs index e096891..a41720e 100644 --- a/providers/provider-engels-polytechnic/src/parser/mod.rs +++ b/providers/provider-engels-polytechnic/src/parser/mod.rs @@ -398,10 +398,10 @@ fn parse_name_and_subgroups(text: &str, row: u32, column: u32) -> Result = None; let mut extra: Option<&str> = None; - let mut shared_subgroup = false; + let mut shared_subgroup = true; let mut subgroups: [Option; 2] = [None, None]; - for capture in NAME_RE.captures_iter(&text) { + for capture in NAME_RE.captures_iter(&text).take(2) { let capture = capture.unwrap(); if lesson_name.is_none() { @@ -442,17 +442,23 @@ fn parse_name_and_subgroups(text: &str, row: u32, column: u32) -> Result { - subgroups[0] = subgroup; - subgroups[1] = None; - shared_subgroup = true; - break; + // we have only 2 matches max so more than 2 subgroups we cant have 100% + *subgroups.iter_mut().find(|x| x.is_none()).unwrap() = subgroup; } Some(num) => { + // bc we have indexed subgroup + shared_subgroup = false; + // 1 - 1 = 0 | 2 - 1 = 1 | 3 - 1 = 2 (schedule index to array index) // 0 % 2 = 0 | 1 % 2 = 1 | 2 % 2 = 0 (clamp) - let normalised = (num - 1) % 2; + let subgroup_index = ((num - 1) % 2) as usize; - subgroups[normalised as usize] = subgroup; + // if we have subgroup in that index (probably non-indexed, we change it index to free) + if subgroups[subgroup_index].is_some() { + subgroups.swap(0, 1); + } + + subgroups[subgroup_index] = subgroup; } } } @@ -460,7 +466,7 @@ fn parse_name_and_subgroups(text: &str, row: u32, column: u32) -> Result