parse all strings and strip comments whether file or not

This commit is contained in:
2026-03-12 15:42:30 -06:00
parent a3bb1cf11b
commit 31faeb3c1b
3 changed files with 47 additions and 8 deletions

View File

@@ -12,16 +12,31 @@ fn from_string(s: String) -> String {
.read_to_string(&mut content) .read_to_string(&mut content)
.expect(format!("failed to read {}", s).as_ref()); .expect(format!("failed to read {}", s).as_ref());
content content
.split("\n")
.map(|x| x.split_whitespace().collect::<Vec<_>>().join(" "))
.collect::<Vec<_>>()
.join("\n")
} }
Err(_) => s, Err(_) => s,
} }
} }
fn parse(s: String) -> Vec<String> { fn parse(s: String) -> Vec<String> {
let s = s
.split("\n")
.filter(|x: &&str| !x // doesnt start with #
.split_whitespace()
.collect::<Vec<_>>()
.join("")
.starts_with("#")
)
.map(|x| x
.split("#")
.take(1) // drop after #
.collect::<Vec<_>>()
.join("")
.split_whitespace()
.collect::<Vec<_>>()
.join(" ")
)
.collect::<Vec<_>>()
.join("\n");
let mut channels = vec![]; let mut channels = vec![];
let lines = s.split("\n").collect::<Vec<_>>(); let lines = s.split("\n").collect::<Vec<_>>();
let mut j = 0; let mut j = 0;
@@ -76,4 +91,20 @@ mod test {
"2a 2b 2c 2d 2e 2f".to_string() "2a 2b 2c 2d 2e 2f".to_string()
); );
} }
#[test]
fn drop_comment_lines() {
assert_eq!(
super::new("# hello\n # world\na b c")[0],
"a b c".to_string()
);
}
#[test]
fn drop_comment_trailer() {
assert_eq!(
super::new("a b c # hello world")[0],
"a b c".to_string()
);
}
} }

9
src/testdata/chord_progressions.txt vendored Normal file
View File

@@ -0,0 +1,9 @@
CM0 CM3 CM4 CM0
.
CM1 CM4 CM0
.
CM0 CM4 CM5 CM3

View File

@@ -1,5 +1,4 @@
. a . b . c CM0 CM3 CM4 CM0
2a2 2b2 2c2
. d . e . f #CM1 CM4 CM0
2d2 2e2 2f2 #CM0 CM4 CM5 CM3