Add function to uplc::parser for string escape

Was originally written as a way to fix a failing property test on the
  program_builder; but the program builder is now gone. This function
  is still useful to have around.
This commit is contained in:
KtorZ 2023-04-05 14:25:19 +02:00
parent cdff149b5e
commit 057102c491
No known key found for this signature in database
GPG Key ID: 33173CB6F77F4277
1 changed files with 15 additions and 0 deletions

View File

@ -55,6 +55,21 @@ fn pair_sub_type(type_info: Option<&Type>) -> Option<(&Type, &Type)> {
}
}
pub fn escape(string: &str) -> String {
string
.chars()
.flat_map(|c| match c {
'\n' => vec!['\\', c],
'\r' => vec!['\\', c],
'\t' => vec!['\\', c],
'\'' => vec!['\\', c],
'\\' => vec!['\\', c],
'"' => vec!['\\', c],
_ => vec![c],
})
.collect::<String>()
}
peg::parser! {
grammar uplc() for str {
pub rule program() -> Program<Name>