change single split char to list of chars

This commit is contained in:
mStar aka a person 2024-03-09 22:36:43 +01:00
parent 6760e81151
commit 09f8df41a6
2 changed files with 10 additions and 6 deletions

View file

@ -1,5 +1,9 @@
use crate::utils::str_safe_split;
// Takes space normalised source code and adds semicolons where needed // Takes space normalised source code and adds semicolons where needed
// May not work properly if string isn't space normalised beforehand // May not work properly if string isn't space normalised beforehand
pub fn insert_semicolons(raw: &str) -> String { pub fn insert_semicolons(raw: &str) -> String {
let _splits = str_safe_split(raw, vec![' ','\t']);
"".to_owned() "".to_owned()
} }

View file

@ -1,6 +1,6 @@
/// String safe splitting /// String safe splitting
/// Strings in this case being literal strings in JavaScript source code /// Strings in this case being literal strings in JavaScript source code
pub fn str_safe_split(to_split: &str, split_char: char) -> Vec<String> { pub fn str_safe_split(to_split: &str, split_chars: Vec<char>) -> Vec<String> {
let mut current_str = String::new(); let mut current_str = String::new();
let mut matches = vec![]; let mut matches = vec![];
@ -52,7 +52,7 @@ pub fn str_safe_split(to_split: &str, split_char: char) -> Vec<String> {
if in_string { if in_string {
current_str.push(c.clone()); current_str.push(c.clone());
} else { } else {
if c == split_char { if split_chars.contains(&c) {
matches.push(current_str); matches.push(current_str);
current_str = String::new(); current_str = String::new();
} else { } else {
@ -67,24 +67,24 @@ pub fn str_safe_split(to_split: &str, split_char: char) -> Vec<String> {
#[test] #[test]
fn test_str_safe_split_1() { fn test_str_safe_split_1() {
let res = str_safe_split("this is a test", ' '); let res = str_safe_split("this is a test", vec![' ']);
assert_eq!(res, vec!["this","is","a","test"]); assert_eq!(res, vec!["this","is","a","test"]);
} }
#[test] #[test]
fn test_str_safe_split_2() { fn test_str_safe_split_2() {
let res = str_safe_split("Quote \"test one\"", ' '); let res = str_safe_split("Quote \"test one\"", vec![' ']);
assert_eq!(res, vec!["Quote", "\"test one\""]); assert_eq!(res, vec!["Quote", "\"test one\""]);
} }
#[test] #[test]
fn test_str_safe_split_3() { fn test_str_safe_split_3() {
let res = str_safe_split("Quote \"test one\" with 'more quotes' and '`even quotes` in quotes'", ' '); let res = str_safe_split("Quote \"test one\" with 'more quotes' and '`even quotes` in quotes'", vec![' ']);
assert_eq!(res, vec!["Quote", "\"test one\"", "with", "'more quotes'", "and", "'`even quotes` in quotes'"]); assert_eq!(res, vec!["Quote", "\"test one\"", "with", "'more quotes'", "and", "'`even quotes` in quotes'"]);
} }
#[test] #[test]
fn test_str_safe_split_4() { fn test_str_safe_split_4() {
let res = str_safe_split("Unfinished 'quote", ' '); let res = str_safe_split("Unfinished 'quote", vec![' ']);
assert_eq!(res, vec!["Unfinished", "'quote"]); assert_eq!(res, vec!["Unfinished", "'quote"]);
} }