toon_format/utils/
string.rs1use crate::{
2 types::Delimiter,
3 utils::literal,
4};
5
6pub fn escape_string(s: &str) -> String {
8 let mut result = String::with_capacity(s.len());
9
10 for ch in s.chars() {
11 match ch {
12 '\n' => result.push_str("\\n"),
13 '\r' => result.push_str("\\r"),
14 '\t' => result.push_str("\\t"),
15 '"' => result.push_str("\\\""),
16 '\\' => result.push_str("\\\\"),
17 _ => result.push(ch),
18 }
19 }
20
21 result
22}
23
24pub fn unescape_string(s: &str) -> String {
26 let mut result = String::with_capacity(s.len());
27 let mut chars = s.chars();
28
29 while let Some(ch) = chars.next() {
30 if ch == '\\' {
31 if let Some(next) = chars.next() {
32 match next {
33 'n' => result.push('\n'),
34 'r' => result.push('\r'),
35 't' => result.push('\t'),
36 '"' => result.push('"'),
37 '\\' => result.push('\\'),
38 _ => {
39 result.push('\\');
40 result.push(next);
41 }
42 }
43 } else {
44 result.push('\\');
45 }
46 } else {
47 result.push(ch);
48 }
49 }
50
51 result
52}
53
54pub fn is_valid_unquoted_key(key: &str) -> bool {
57 if key.is_empty() {
58 return false;
59 }
60
61 let mut chars = key.chars();
62 let first = match chars.next() {
63 Some(c) => c,
64 None => return false,
65 };
66
67 if !first.is_alphabetic() && first != '_' {
68 return false;
69 }
70
71 chars.all(|c| c.is_alphanumeric() || c == '_' || c == '.')
72}
73
74pub fn needs_quoting(s: &str, delimiter: char) -> bool {
76 if s.is_empty() {
77 return true;
78 }
79
80 if literal::is_literal_like(s) {
81 return true;
82 }
83
84 if s.chars().any(literal::is_structural_char) {
85 return true;
86 }
87
88 if s.contains('\\') || s.contains('"') {
89 return true;
90 }
91
92 if s.contains(delimiter) {
93 return true;
94 }
95
96 if s.contains('\n') || s.contains('\r') || s.contains('\t') {
97 return true;
98 }
99
100 if s.starts_with(char::is_whitespace) || s.ends_with(char::is_whitespace) {
101 return true;
102 }
103
104 if s.starts_with("-") {
105 return true;
106 }
107
108 false
109}
110
111pub fn quote_string(s: &str) -> String {
113 format!("\"{}\"", escape_string(s))
114}
115
116pub fn split_by_delimiter(s: &str, delimiter: Delimiter) -> Vec<String> {
117 let mut result = Vec::new();
118 let mut current = String::new();
119 let mut in_quotes = false;
120 let chars = s.chars().peekable();
121 let delim_char = delimiter.as_char();
122
123 for ch in chars {
124 if ch == '"' && (current.is_empty() || !current.ends_with('\\')) {
125 in_quotes = !in_quotes;
126 current.push(ch);
127 } else if ch == delim_char && !in_quotes {
128 result.push(current.trim().to_string());
129 current.clear();
130 } else {
131 current.push(ch);
132 }
133 }
134
135 if !current.is_empty() {
136 result.push(current.trim().to_string());
137 }
138
139 result
140}
141
142#[cfg(test)]
143mod tests {
144 use super::*;
145
146 #[test]
147 fn test_escape_string() {
148 assert_eq!(escape_string("hello"), "hello");
149 assert_eq!(escape_string("hello\nworld"), "hello\\nworld");
150 assert_eq!(escape_string("say \"hi\""), "say \\\"hi\\\"");
151 assert_eq!(escape_string("back\\slash"), "back\\\\slash");
152 }
153
154 #[test]
155 fn test_unescape_string() {
156 assert_eq!(unescape_string("hello"), "hello");
157 assert_eq!(unescape_string("hello\\nworld"), "hello\nworld");
158 assert_eq!(unescape_string("say \\\"hi\\\""), "say \"hi\"");
159 assert_eq!(unescape_string("back\\\\slash"), "back\\slash");
160 }
161
162 #[test]
163 fn test_needs_quoting() {
164 let comma = Delimiter::Comma.as_char();
165
166 assert!(needs_quoting("", comma));
167
168 assert!(needs_quoting("true", comma));
169 assert!(needs_quoting("false", comma));
170 assert!(needs_quoting("null", comma));
171 assert!(needs_quoting("123", comma));
172
173 assert!(needs_quoting("hello[world]", comma));
174 assert!(needs_quoting("key:value", comma));
175
176 assert!(needs_quoting("a,b", comma));
177 assert!(!needs_quoting("a,b", Delimiter::Pipe.as_char()));
178
179 assert!(!needs_quoting("hello world", comma));
180 assert!(needs_quoting(" hello", comma));
181 assert!(needs_quoting("hello ", comma));
182
183 assert!(!needs_quoting("hello", comma));
184 assert!(!needs_quoting("world", comma));
185 assert!(!needs_quoting("helloworld", comma));
186 }
187
188 #[test]
189 fn test_quote_string() {
190 assert_eq!(quote_string("hello"), "\"hello\"");
191 assert_eq!(quote_string("hello\nworld"), "\"hello\\nworld\"");
192 }
193
194 #[test]
195 fn test_split_by_delimiter() {
196 let comma = Delimiter::Comma;
197
198 assert_eq!(split_by_delimiter("a,b,c", comma), vec!["a", "b", "c"]);
199
200 assert_eq!(split_by_delimiter("a, b, c", comma), vec!["a", "b", "c"]);
201
202 assert_eq!(split_by_delimiter("\"a,b\",c", comma), vec!["\"a,b\"", "c"]);
203 }
204
205 #[test]
206 fn test_is_valid_unquoted_key() {
207 assert!(is_valid_unquoted_key("normal_key"));
209 assert!(is_valid_unquoted_key("key123"));
210 assert!(is_valid_unquoted_key("key.value"));
211 assert!(is_valid_unquoted_key("_private"));
212 assert!(is_valid_unquoted_key("KeyName"));
213 assert!(is_valid_unquoted_key("key_name"));
214 assert!(is_valid_unquoted_key("key.name.sub"));
215 assert!(is_valid_unquoted_key("a"));
216 assert!(is_valid_unquoted_key("_"));
217 assert!(is_valid_unquoted_key("key_123.value"));
218
219 assert!(!is_valid_unquoted_key(""));
220 assert!(!is_valid_unquoted_key("123"));
221 assert!(!is_valid_unquoted_key("key:value"));
222 assert!(!is_valid_unquoted_key("key-value"));
223 assert!(!is_valid_unquoted_key("key value"));
224 assert!(!is_valid_unquoted_key(".key"));
225 assert!(is_valid_unquoted_key("key.value.sub."));
226 assert!(is_valid_unquoted_key("key."));
227 assert!(!is_valid_unquoted_key("key[value]"));
228 assert!(!is_valid_unquoted_key("key{value}"));
229 }
230}