From 7db27d0c2b298062bb84a3b65fabc29dd9417fd2 Mon Sep 17 00:00:00 2001 From: missing Date: Thu, 21 Jul 2022 11:23:48 -0700 Subject: [PATCH] support input strings longer than u16::MAX chars --- src/main.rs | 35 ++++++++++++++++++++++++++++++++++- 1 file changed, 34 insertions(+), 1 deletion(-) diff --git a/src/main.rs b/src/main.rs index ad9b20c..d8c96d4 100644 --- a/src/main.rs +++ b/src/main.rs @@ -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('(');