support input strings longer than u16::MAX chars

This commit is contained in:
missing 2022-07-21 11:23:48 -07:00
parent b4968938d4
commit 7db27d0c2b

View file

@ -13,7 +13,40 @@ fn main() {
);
}
fn string_with_fromcodepoint(s: &str) -> String {
fn string_with_fromcodepoint(mut s: &str) -> String {
const ARG_LIMIT: usize = u16::MAX as usize - 3000; // without this 3000, it sometimes throws a call stack overflow error
if s.is_empty() {
return "[]+[]".to_owned();
}
let mut out = String::new();
loop {
if s.is_empty() {
break;
}
let split_point = if s.len() < ARG_LIMIT {
s.len()
} else {
let mut i = ARG_LIMIT;
loop {
if s.is_char_boundary(i) {
break i;
}
i -= 1;
}
};
out.push_str(&short_string_with_fromcodepoint(&s[..split_point]));
out.push('+');
s = &s[split_point..];
}
out.pop(); // remove the last `+`
out
}
fn short_string_with_fromcodepoint(s: &str) -> String {
let mut out = String::new();
out.push_str(&function_constructor());
out.push('(');