new function for splitting game data into segments
This commit is contained in:
parent
e8effb3579
commit
408e46ef7e
43
src/lib.rs
43
src/lib.rs
|
@ -108,9 +108,34 @@ fn transform_line_endings(input: String, mode: TransformMode) -> String {
|
|||
String::from_utf8(output.into_inner()).unwrap()
|
||||
}
|
||||
|
||||
fn segments_from_string(string: String) -> Vec<String> {
|
||||
let mut output:Vec<String> = Vec::new();
|
||||
// are we inside `"""\n...\n"""`? if so, ignore empty lines
|
||||
let mut inside_escaped_block = false;
|
||||
let mut current_segment : Vec<String> = Vec::new();
|
||||
let lines: Vec<&str> = string.lines().collect();
|
||||
|
||||
for line in lines {
|
||||
if line == "\"\"\"" {
|
||||
inside_escaped_block = ! inside_escaped_block;
|
||||
}
|
||||
|
||||
if line == "" && !inside_escaped_block {
|
||||
output.push(current_segment.join("\n"));
|
||||
current_segment = Vec::new();
|
||||
} else {
|
||||
current_segment.push(line.to_string());
|
||||
}
|
||||
}
|
||||
|
||||
output.push(current_segment.join("\n"));
|
||||
|
||||
output
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod test {
|
||||
use crate::{from_base36, ToBase36, optional_data_line, mock};
|
||||
use crate::{from_base36, ToBase36, optional_data_line, mock, segments_from_string};
|
||||
|
||||
#[test]
|
||||
fn test_from_base36() {
|
||||
|
@ -129,4 +154,20 @@ mod test {
|
|||
let output = optional_data_line("NAME", mock::item().name);
|
||||
assert_eq!(output, "\nNAME door".to_string());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_string_to_segments() {
|
||||
let output = segments_from_string(
|
||||
include_str!("./test-resources/segments").to_string()
|
||||
);
|
||||
|
||||
let expected = vec![
|
||||
"\"\"\"\nthe first segment is a long bit of text\n\n\nit contains empty lines\n\n\"\"\"".to_string(),
|
||||
"this is a new segment\nthis is still the second segment\nblah\nblah".to_string(),
|
||||
"DLG SEGMENT_3\n\"\"\"\nthis is a short \"long\" bit of text\n\"\"\"".to_string(),
|
||||
"this is the last segment".to_string(),
|
||||
];
|
||||
|
||||
assert_eq!(output, expected);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -0,0 +1,19 @@
|
|||
"""
|
||||
the first segment is a long bit of text
|
||||
|
||||
|
||||
it contains empty lines
|
||||
|
||||
"""
|
||||
|
||||
this is a new segment
|
||||
this is still the second segment
|
||||
blah
|
||||
blah
|
||||
|
||||
DLG SEGMENT_3
|
||||
"""
|
||||
this is a short "long" bit of text
|
||||
"""
|
||||
|
||||
this is the last segment
|
Loading…
Reference in New Issue