From ec5b6d044d010c323e844fde6ff1704f7d87c7b7 Mon Sep 17 00:00:00 2001 From: cm-ayf Date: Wed, 17 Jan 2024 13:29:46 +0900 Subject: [PATCH] debug and test split_pattern --- crates/jlabel-question/src/lib.rs | 11 +++++------ crates/jlabel-question/src/tests.rs | 12 ++++++++---- 2 files changed, 13 insertions(+), 10 deletions(-) diff --git a/crates/jlabel-question/src/lib.rs b/crates/jlabel-question/src/lib.rs index 4307f81..46ba956 100644 --- a/crates/jlabel-question/src/lib.rs +++ b/crates/jlabel-question/src/lib.rs @@ -30,10 +30,6 @@ pub enum ParseError { } fn split_pattern(pattern: &str) -> Option<(&str, &str, &str)> { - if !pattern.len() < 4 { - return None; - } - let start = if pattern.starts_with("*/") { 4 } else if pattern.starts_with('*') { @@ -42,12 +38,15 @@ fn split_pattern(pattern: &str) -> Option<(&str, &str, &str)> { 0 }; let end = if pattern.ends_with(":*") { - pattern.len() - 4 + pattern.len().checked_sub(4)? } else if pattern.ends_with('*') { - pattern.len() - 2 + pattern.len().checked_sub(2)? } else { pattern.len() }; + if start > end { + return None; + } Some((&pattern[..start], &pattern[start..end], &pattern[end..])) } diff --git a/crates/jlabel-question/src/tests.rs b/crates/jlabel-question/src/tests.rs index 6289984..0540e19 100644 --- a/crates/jlabel-question/src/tests.rs +++ b/crates/jlabel-question/src/tests.rs @@ -3,10 +3,14 @@ use jlabel::{Label, Mora, Phoneme, Utterance, Word}; #[test] fn splitter() { - assert_eq!(split_pattern("a^*"), Some(("", "a", "^*"))); - assert_eq!(split_pattern("*/A:-??+*"), Some(("*/A:", "-??", "+*"))); - assert_eq!(split_pattern("*|?+*"), Some(("*|", "?", "+*"))); - assert_eq!(split_pattern("*-1"), Some(("*-", "1", ""))); + assert_eq!(split_pattern("a^*").unwrap(), ("", "a", "^*")); + assert_eq!(split_pattern("*/A:-??+*").unwrap(), ("*/A:", "-??", "+*")); + assert_eq!(split_pattern("*|?+*").unwrap(), ("*|", "?", "+*")); + assert_eq!(split_pattern("*-1").unwrap(), ("*-", "1", "")); + + assert!(split_pattern("*").is_none()); + assert!(split_pattern(":*").is_none()); + assert!(split_pattern("*/A:*").is_none()); } #[test]