Removed prints

This commit is contained in:
mStar aka a person 2024-03-09 20:28:48 +01:00
parent ade4a32b3e
commit 14e2a12dec

View file

@ -1,4 +1,4 @@
use std::{fmt::Write, str::Chars}; use std::str::Chars;
#[derive(PartialEq)] #[derive(PartialEq)]
enum QuoteMethod { enum QuoteMethod {
@ -8,6 +8,7 @@ enum QuoteMethod {
} }
// Normalised empty space in codeblocks, ensuring that all whitespace in codeblocks is one character wide // Normalised empty space in codeblocks, ensuring that all whitespace in codeblocks is one character wide
#[allow(unused)] // reason: Will be used later
pub fn clear_spaces(raw: &str) -> String { pub fn clear_spaces(raw: &str) -> String {
clear_in_code(&mut raw.chars()) clear_in_code(&mut raw.chars())
} }
@ -19,35 +20,30 @@ fn clear_in_code<'a, 'b>(raw: &mut Chars<'a>) -> String {
match &c { match &c {
'"' => { '"' => {
cleared.push('"'); cleared.push('"');
println!("Entering double quoted with current \"{}\"", &cleared);
let res = clear_in_quotes(raw, QuoteMethod::Double); let res = clear_in_quotes(raw, QuoteMethod::Double);
cleared.push_str(&res); cleared.push_str(&res);
last_char = '"'; last_char = '"';
} }
'\'' => { '\'' => {
cleared.push('\''); cleared.push('\'');
println!("Entering single quoted with current \"{}\"", &cleared);
let res = clear_in_quotes(raw, QuoteMethod::Single); let res = clear_in_quotes(raw, QuoteMethod::Single);
cleared.push_str(&res); cleared.push_str(&res);
last_char = '\''; last_char = '\'';
} }
'`' => { '`' => {
cleared.push('`'); cleared.push('`');
println!("Entering backtick quoted with current \"{}\"", &cleared);
let res = clear_in_quotes(raw, QuoteMethod::Backtick); let res = clear_in_quotes(raw, QuoteMethod::Backtick);
cleared.push_str(&res); cleared.push_str(&res);
last_char = '`'; last_char = '`';
} }
'{' => { '{' => {
cleared.push('{'); cleared.push('{');
println!("Entering codeblock with current \"{}\"", &cleared);
let res = clear_in_code(raw); let res = clear_in_code(raw);
cleared.push_str(&res); cleared.push_str(&res);
last_char = '}'; last_char = '}';
} }
'}' => { '}' => {
cleared.push('}'); cleared.push('}');
println!("Exiting codeblock with current \"{}\"", &cleared);
return cleared return cleared
} }
x => { x => {
@ -72,7 +68,6 @@ fn clear_in_quotes<'a, 'b>(raw: &mut Chars<'a>, method: QuoteMethod) -> String {
'"' => { '"' => {
cleared.push('"'); cleared.push('"');
if method == QuoteMethod::Double && last_char != '\\' { if method == QuoteMethod::Double && last_char != '\\' {
println!("Exiting double quoted with current \"{}\"", &cleared);
return cleared return cleared
} }
last_char = '"'; last_char = '"';
@ -80,7 +75,6 @@ fn clear_in_quotes<'a, 'b>(raw: &mut Chars<'a>, method: QuoteMethod) -> String {
'\'' => { '\'' => {
cleared.push('\''); cleared.push('\'');
if method == QuoteMethod::Single && last_char != '\\' { if method == QuoteMethod::Single && last_char != '\\' {
println!("Exiting single quoted with current \"{}\"", &cleared);
return cleared return cleared
} }
last_char = '\''; last_char = '\'';
@ -88,7 +82,6 @@ fn clear_in_quotes<'a, 'b>(raw: &mut Chars<'a>, method: QuoteMethod) -> String {
'`' => { '`' => {
cleared.push('`'); cleared.push('`');
if method == QuoteMethod::Backtick && last_char != '\\' { if method == QuoteMethod::Backtick && last_char != '\\' {
println!("Exiting backtick quoted with current \"{}\"", &cleared);
return cleared return cleared
} }
last_char = '`'; last_char = '`';
@ -97,7 +90,6 @@ fn clear_in_quotes<'a, 'b>(raw: &mut Chars<'a>, method: QuoteMethod) -> String {
cleared.push('{'); cleared.push('{');
// Code blocks are only run inside backtick strings and with a $ before the curly braces // Code blocks are only run inside backtick strings and with a $ before the curly braces
if method == QuoteMethod::Backtick && last_char == '$' { if method == QuoteMethod::Backtick && last_char == '$' {
println!("Entering codeblock with current \"{}\"", &cleared);
let res = clear_in_code(raw); let res = clear_in_code(raw);
cleared.push_str(&res); cleared.push_str(&res);
last_char = '}'; last_char = '}';
@ -114,71 +106,6 @@ fn clear_in_quotes<'a, 'b>(raw: &mut Chars<'a>, method: QuoteMethod) -> String {
cleared cleared
} }
fn old_clear_spaces(raw: &str) -> String {
let mut cleared = String::new();
let mut currently_quoted = false;
let mut quote_is_single = false;
let mut inside_quoted_codeblock = false;
let mut previous_char = 'a';
for c in raw.chars() {
if currently_quoted {
cleared.write_char(c.clone()).expect("Failed to write character to out string during whitespace normalisation");
previous_char = c;
} else {
match c {
'"' => {
if currently_quoted && !quote_is_single && !inside_quoted_codeblock {
currently_quoted = false;
} else if !currently_quoted {
currently_quoted = true;
quote_is_single = false;
// Just overwrite it here already in case of some weird stuff
// Assume that only ever one codeblock will be nested in a string
// If there's more, this code will explode
// Not really, but spaces might get messed up
inside_quoted_codeblock = false;
}
cleared.write_char(c.clone()).expect("Failed to write character to out string during whitespace normalisation");
}
'\'' => {
if currently_quoted && quote_is_single {
currently_quoted = false;
} else {
currently_quoted = true;
quote_is_single = true;
}
cleared.write_char(c.clone()).expect("Failed to write character to out string during whitespace normalisation");
}
'{' => {
if currently_quoted && !inside_quoted_codeblock && !quote_is_single {
inside_quoted_codeblock = true;
}
cleared.write_char(c.clone()).expect("Failed to write character to out string during whitespace normalisation");
}
'}' => {
if currently_quoted && !inside_quoted_codeblock && !quote_is_single {
inside_quoted_codeblock = false;
}
cleared.write_char(c.clone()).expect("Failed to write character to out string during whitespace normalisation");
}
x => {
if !(previous_char.is_whitespace() && x.is_whitespace()) {
cleared.write_char(x.clone()).expect("Failed to write character to out string during whitespace normalisation");
} else {
}
previous_char = x;
}
}
}
}
cleared
}
// Test to make sure that multiple spaces get grouped into one // Test to make sure that multiple spaces get grouped into one
#[test] #[test]
fn test_clear_spaces1() { fn test_clear_spaces1() {