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: Delimiter) -> 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(delimiter.as_char()) {
89 return true;
90 }
91
92 if s.contains('\n') || s.contains('\r') {
93 return true;
94 }
95
96 if s.contains(' ') || s.contains('\t') {
97 return true;
98 }
99
100 if s.starts_with("- ") {
101 return true;
102 }
103
104 false
105}
106
107pub fn quote_string(s: &str) -> String {
109 format!("\"{}\"", escape_string(s))
110}
111
112pub fn split_by_delimiter(s: &str, delimiter: Delimiter) -> Vec<String> {
113 let mut result = Vec::new();
114 let mut current = String::new();
115 let mut in_quotes = false;
116 let chars = s.chars().peekable();
117 let delim_char = delimiter.as_char();
118
119 for ch in chars {
120 if ch == '"' && (current.is_empty() || !current.ends_with('\\')) {
121 in_quotes = !in_quotes;
122 current.push(ch);
123 } else if ch == delim_char && !in_quotes {
124 result.push(current.trim().to_string());
125 current.clear();
126 } else {
127 current.push(ch);
128 }
129 }
130
131 if !current.is_empty() {
132 result.push(current.trim().to_string());
133 }
134
135 result
136}
137
138#[cfg(test)]
139mod tests {
140 use super::*;
141
142 #[test]
143 fn test_escape_string() {
144 assert_eq!(escape_string("hello"), "hello");
145 assert_eq!(escape_string("hello\nworld"), "hello\\nworld");
146 assert_eq!(escape_string("say \"hi\""), "say \\\"hi\\\"");
147 assert_eq!(escape_string("back\\slash"), "back\\\\slash");
148 }
149
150 #[test]
151 fn test_unescape_string() {
152 assert_eq!(unescape_string("hello"), "hello");
153 assert_eq!(unescape_string("hello\\nworld"), "hello\nworld");
154 assert_eq!(unescape_string("say \\\"hi\\\""), "say \"hi\"");
155 assert_eq!(unescape_string("back\\\\slash"), "back\\slash");
156 }
157
158 #[test]
159 fn test_needs_quoting() {
160 let comma = Delimiter::Comma;
161
162 assert!(needs_quoting("", comma));
163
164 assert!(needs_quoting("true", comma));
165 assert!(needs_quoting("false", comma));
166 assert!(needs_quoting("null", comma));
167 assert!(needs_quoting("123", comma));
168
169 assert!(needs_quoting("hello[world]", comma));
170 assert!(needs_quoting("key:value", comma));
171
172 assert!(needs_quoting("a,b", comma));
173 assert!(!needs_quoting("a,b", Delimiter::Pipe));
174
175 assert!(needs_quoting("hello world", comma));
176 assert!(needs_quoting(" hello", comma));
177 assert!(needs_quoting("hello ", comma));
178
179 assert!(!needs_quoting("hello", comma));
180 assert!(!needs_quoting("world", comma));
181 assert!(!needs_quoting("helloworld", comma));
182 }
183
184 #[test]
185 fn test_quote_string() {
186 assert_eq!(quote_string("hello"), "\"hello\"");
187 assert_eq!(quote_string("hello\nworld"), "\"hello\\nworld\"");
188 }
189
190 #[test]
191 fn test_split_by_delimiter() {
192 let comma = Delimiter::Comma;
193
194 assert_eq!(split_by_delimiter("a,b,c", comma), vec!["a", "b", "c"]);
195
196 assert_eq!(split_by_delimiter("a, b, c", comma), vec!["a", "b", "c"]);
197
198 assert_eq!(split_by_delimiter("\"a,b\",c", comma), vec!["\"a,b\"", "c"]);
199 }
200
201 #[test]
202 fn test_is_valid_unquoted_key() {
203 assert!(is_valid_unquoted_key("normal_key"));
205 assert!(is_valid_unquoted_key("key123"));
206 assert!(is_valid_unquoted_key("key.value"));
207 assert!(is_valid_unquoted_key("_private"));
208 assert!(is_valid_unquoted_key("KeyName"));
209 assert!(is_valid_unquoted_key("key_name"));
210 assert!(is_valid_unquoted_key("key.name.sub"));
211 assert!(is_valid_unquoted_key("a"));
212 assert!(is_valid_unquoted_key("_"));
213 assert!(is_valid_unquoted_key("key_123.value"));
214
215 assert!(!is_valid_unquoted_key(""));
216 assert!(!is_valid_unquoted_key("123"));
217 assert!(!is_valid_unquoted_key("key:value"));
218 assert!(!is_valid_unquoted_key("key-value"));
219 assert!(!is_valid_unquoted_key("key value"));
220 assert!(!is_valid_unquoted_key(".key"));
221 assert!(is_valid_unquoted_key("key.value.sub."));
222 assert!(is_valid_unquoted_key("key."));
223 assert!(!is_valid_unquoted_key("key[value]"));
224 assert!(!is_valid_unquoted_key("key{value}"));
225 }
226}