Skip to content

Commit

Permalink
debug and test split_pattern
Browse files Browse the repository at this point in the history
  • Loading branch information
cm-ayf committed Jan 17, 2024
1 parent 0897e45 commit ec5b6d0
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 10 deletions.
11 changes: 5 additions & 6 deletions crates/jlabel-question/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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('*') {
Expand All @@ -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..]))
}
Expand Down
12 changes: 8 additions & 4 deletions crates/jlabel-question/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down

0 comments on commit ec5b6d0

Please sign in to comment.