MCPcopy Create free account
hub / github.com/Glyphack/enderpy / extract_string_inside

Function extract_string_inside

parser/src/parser/mod.rs:31–55  ·  view source on GitHub ↗

TODO: performance

(val: String)

Source from the content-addressed store, hash-verified

29
30// TODO: performance
31pub fn extract_string_inside(val: String) -> String {
32 let delimiters = vec!["\"\"\"", "\"", "'''", "'"];
33 let mut result = String::new();
34 let is_raw = val.starts_with('r') || val.starts_with('R');
35 // drop first char if raw and put in val
36 let val = if is_raw {
37 val.chars().skip(1).collect::<String>()
38 } else {
39 val
40 };
41
42 for delimiter in delimiters {
43 if let Some(val) = val.strip_prefix(delimiter) {
44 let message = format!("String must be enclosed with {}", delimiter);
45 result = val.strip_suffix(delimiter).expect(&message).to_string();
46 break;
47 }
48 }
49 // add r back if raw ternary
50 if is_raw {
51 result = format!("r\"{}\"", result);
52 }
53
54 result
55}
56
57pub fn concat_string_exprs(lhs: Expression, rhs: Expression) -> Result<Expression, ParsingError> {
58 use crate::parser::ast::{Constant, ConstantValue};

Callers 1

parse_atomMethod · 0.85

Calls 2

to_stringMethod · 0.80
expectMethod · 0.80

Tested by

no test coverage detected