new function for splitting game data into segments

This commit is contained in:
Max Bradbury 2020-04-24 18:52:27 +01:00
parent e8effb3579
commit 408e46ef7e
2 changed files with 61 additions and 1 deletions

View File

@ -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);
}
}

View File

@ -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