Skip to content

Commit

Permalink
Compact yaml-string-quoting code a bit
Browse files Browse the repository at this point in the history
In the hopes that this is also faster.
  • Loading branch information
grandchild committed May 6, 2023
1 parent 01385d3 commit 61af986
Showing 1 changed file with 3 additions and 3 deletions.
6 changes: 3 additions & 3 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ fn quote_yaml_string_if_needed(string: &str) -> String {
let mut is_number = true;
let mut is_hex_number = false;
let mut no_number_separator_yet = true;
let starts_with_0x = string.starts_with("0x");
for c in string.chars() {
match c {
'0'..='9' => (),
Expand All @@ -61,7 +62,7 @@ fn quote_yaml_string_if_needed(string: &str) -> String {
no_number_separator_yet = false;
is_hex_number = false;
}
'x' if string.starts_with("0x") && no_number_separator_yet => {
'x' if starts_with_0x && no_number_separator_yet => {
no_number_separator_yet = false;
is_hex_number = true;
}
Expand All @@ -78,8 +79,7 @@ fn quote_yaml_string_if_needed(string: &str) -> String {
if is_number
|| string.starts_with(special_start_chars)
|| string.contains(": ")
|| string.contains(|c: char| matches!(c, '\0'..='\x1f'))
|| string.contains('"')
|| string.contains(|c: char| matches!(c, '\0'..='\x1f' | '"'))
|| string.ends_with([' ', ':'])
|| ["yes", "no", "true", "false", "on", "off", "null", "~"]
.contains(&string.to_lowercase().as_str())
Expand Down

0 comments on commit 61af986

Please sign in to comment.