1#![cfg_attr(not(test), no_std)]
2
3#![warn(clippy::std_instead_of_alloc, clippy::std_instead_of_core)]
4
5extern crate alloc;
6
7mod segment;
8mod segmented_string;
9mod string;
10
11pub use segmented_string::SizedSegmentedString;
12
13pub type SegmentedString16 = SizedSegmentedString<16>;
14pub type SegmentedString32 = SizedSegmentedString<32>;
15
16
17#[cfg(target_pointer_width = "64")]
19pub type SegmentedString = SegmentedString16;
20
21#[cfg(target_pointer_width = "32")]
22pub type SegmentedString = SegmentedString32;
23
24
25#[cfg(test)]
26mod test {
27 use unicode_segmentation::UnicodeSegmentation;
28 use super::*;
29
30
31 fn make_string(s: &str) -> SizedSegmentedString::<4> {
33 SizedSegmentedString::<4>::from_str_with_segment_size(4, s)
34 }
35
36
37 #[test]
38 fn chunking() {
39 assert_eq!(
40 format!("{:?}", SizedSegmentedString::<4>::from("hello there!")),
41 "SegmentedString<4, 128>(12:12)\"hello there!\""
42 );
43
44 assert_eq!(
45 format!("{:?}", make_string("abcdefgh")),
46 "SegmentedString<4, 4>(8:8)[ (4:4)\"abcd\", (4:4)\"efgh\" ]"
47 );
48 assert_eq!(
49 format!("{:?}", make_string("abcdefghijklmnop")),
50 "SegmentedString<4, 4>(16:16)[ (4:4)\"abcd\", (4:4)\"efgh\", (4:4)\"ijkl\", (4:4)\"mnop\" ]"
51 );
52 assert_eq!(
53 format!("{:?}", make_string("abcdefghijklmnopq")),
54 "SegmentedString<4, 4>(17:17)[ \
55 (16:16)[ (4:4)\"abcd\", (4:4)\"efgh\", (4:4)\"ijkl\", (4:4)\"mnop\" ], \
56 (1:1)[ (1:1)\"q\" ] ]"
57 );
58
59 let long_string = "abcdefghijklmnopqrstuvwxyz-:ABCDEFGHIJKLMNOPQRSTUVWXYZ_!/0123456789*@.";
60 assert_eq!(
61 format!("{:?}", make_string(long_string)),
62 "SegmentedString<4, 4>(70:70)[ \
63 (64:64)[ \
64 (16:16)[ (4:4)\"abcd\", (4:4)\"efgh\", (4:4)\"ijkl\", (4:4)\"mnop\" ], \
65 (16:16)[ (4:4)\"qrst\", (4:4)\"uvwx\", (4:4)\"yz-:\", (4:4)\"ABCD\" ], \
66 (16:16)[ (4:4)\"EFGH\", (4:4)\"IJKL\", (4:4)\"MNOP\", (4:4)\"QRST\" ], \
67 (16:16)[ (4:4)\"UVWX\", (4:4)\"YZ_!\", (4:4)\"/012\", (4:4)\"3456\" ] \
68 ], \
69 (6:6)[ (6:6)[ (4:4)\"789*\", (2:2)\"@.\" ] ] \
70 ]"
71 );
72 }
73
74 #[test]
75 fn chunking_length() {
76 let s = make_string("e\u{301}o\u{308}");
77 assert_eq!(
78 format!("{:?}", s),
79 "SegmentedString<4, 4>(6:2)[ (3:1)\"e\\u{301}\", (3:1)\"o\\u{308}\" ]"
80 );
81 assert_eq!(s.len(), 6);
82 assert_eq!(s.char_len(), 2);
83 }
84
85 #[test]
86 fn get() {
87 let long_string = "abcdefghijklmnopqrstuvwxyz-:ABCDEFGHIJKLMNOPQRSTUVWXYZ_!/0123456789*@.";
88 for len in 1..(long_string.len()) {
90 let s = make_string(&long_string[..len]);
91 assert_eq!(s.char_len(), len);
92 for i in 0..(s.char_len()) {
93 assert_eq!(s.get(i).unwrap(), UnicodeSegmentation::graphemes(long_string, true).nth(i).unwrap());
94 }
95 }
96 }
97
98 #[test]
99 fn chars_iter() {
100 let long_string = "abcdefghijklmnopqrstuvwxyz-:ABCDEFGHIJKLMNOPQRSTUVWXYZ_!/0123456789*@.";
101 let s = make_string(long_string);
102 let mut iter = s.chars();
103 for i in 0..(s.char_len()) {
104 assert_eq!(iter.next(), UnicodeSegmentation::graphemes(long_string, true).nth(i));
105 }
106 assert_eq!(iter.next(), None);
107
108 let mut iter = s.chars().rev();
110 for i in 0..(s.char_len()) {
111 assert_eq!(iter.next(), UnicodeSegmentation::graphemes(long_string, true).nth(s.char_len() - i - 1));
112 }
113 assert_eq!(iter.next(), None);
114 }
115
116 #[test]
117 fn char_codes_iter() {
118 let s = SegmentedString::from("a\u{308}b😈c");
119 assert_eq!(s.char_codes().map(|ch| ch as usize).collect::<Vec<usize>>(), vec![ 97, 776, 98, 128520, 99 ]);
120 }
121
122 #[test]
123 fn substr() {
124 let long_string = "abcdefghijklmnopqrstuvwxyz-:ABCDEFGHIJKLMNOPQRSTUVWXYZ_!/0123456789*@.";
125 let s = make_string(long_string);
126 for start in 0..(s.char_len()) {
127 for end in start..(s.char_len()) {
128 assert_eq!(s.substr(start, end).to_string(), &long_string[start..end]);
129 }
130 }
131 }
132
133 #[test]
134 fn substr_equals_str() {
135 let long_string = "abcdefghijklmnopqrstuvwxyz-:ABCDEFGHIJKLMNOPQRSTUVWXYZ_!/0123456789*@.";
136 let s1 = make_string("");
137 let s2 = make_string("qrs"); let s3 = make_string(long_string);
139
140 assert!(s1.substr_equals_str(0, 0, ""));
142 assert!(!s1.substr_equals_str(0, 0, "x"));
143 assert!(s2.substr_equals_str(2, 2, ""));
144 assert!(!s2.substr_equals_str(0, 1, ""));
145
146 assert!(s2.substr_equals_str(1, 3, "rs"));
148 assert!(!s2.substr_equals_str(0, 2, "rs"));
149 assert!(s2.substr_equals_str(0, 5, "qrs"));
150
151 assert!(s3.substr_equals_str(13, 13, ""));
153 assert!(s3.substr_equals_str(16, 19, "qrs"));
154 assert!(s3.substr_equals_str(16, 19, s2.to_string().as_str()));
155 assert!(s3.substr_equals_str(2, 30, &long_string[2..30]));
156 }
157
158 #[test]
159 fn substr_equals() {
160 let long_string = "abcdefghijklmnopqrstuvwxyz-:ABCDEFGHIJKLMNOPQRSTUVWXYZ_!/0123456789*@.";
161 let s1 = make_string("");
162 let s2 = make_string("qrs"); let s3 = make_string(long_string);
164
165 assert!(s1.substr_equals(0, 0, &s2, 0, 0));
167 assert!(!s1.substr_equals(0, 0, &s2, 0, 1));
168 assert!(s2.substr_equals(2, 2, &s2, 0, 0));
169 assert!(!s2.substr_equals(0, 1, &s2, 1, 1));
170
171 assert!(s2.substr_equals(1, 3, &s2, 1, 3));
173 assert!(!s2.substr_equals(0, 2, &s2, 1, 3));
174 assert!(s2.substr_equals(0, 5, &s3, 16, 19));
175
176 assert!(s3.substr_equals(13, 13, &s1, 0, 0));
178 assert!(s3.substr_equals(16, 19, &s2, 0, s2.char_len()));
179 assert!(s3.substr_equals(16, 19, &s3, 16, 19));
180 assert!(s3.substr_equals(2, 30, &s3, 2, 30));
181
182 let s4 = SizedSegmentedString::<4>::from_str_with_segment_size(5, &long_string[7..]);
184 assert!(s3.substr_equals(7, s3.char_len(), &s4, 0, s4.char_len()));
185 assert!(!s3.substr_equals(5, s3.char_len() - 2, &s4, 0, s4.char_len()));
186 let s4 = SizedSegmentedString::<4>::from_str_with_segment_size(3, &long_string[7..]);
187 assert!(s3.substr_equals(7, s3.char_len(), &s4, 0, s4.char_len()));
188 assert!(!s3.substr_equals(5, s3.char_len() - 2, &s4, 0, s4.char_len()));
189 }
190
191 #[test]
192 fn starts_ends_with() {
193 let long_string = "abcdefghijklmnopqrstuvwxyz-:ABCDEFGHIJKLMNOPQRSTUVWXYZ_!/0123456789*@.";
194 let s = SegmentedString::from(long_string);
195
196 assert!(s.starts_with(&"abc".into()));
197 assert!(!s.starts_with(&"bcd".into()));
198 assert!(s.ends_with(&"9*@.".into()));
199
200 assert!(SegmentedString::from("rabbit").starts_with(&"rabbit".into()));
201 assert!(SegmentedString::from("rabbit").ends_with(&"rabbit".into()));
202 assert!(!SegmentedString::from("rabbit").starts_with(&"rabbity".into()));
203 assert!(!SegmentedString::from("rabbit").ends_with(&"crabbit".into()));
204 }
205
206 #[test]
207 fn trim() {
208 assert_eq!(SegmentedString::from("abc").trim_left(), SegmentedString::from("abc"));
209 assert_eq!(SegmentedString::from(" \tabc").trim_left(), SegmentedString::from("abc"));
210 assert_eq!(SegmentedString::from("\n abc").trim_left(), SegmentedString::from("abc"));
211 assert_eq!(SegmentedString::from(" ").trim_left(), SegmentedString::from(""));
212
213 assert_eq!(SegmentedString::from("abc").trim_right(), SegmentedString::from("abc"));
214 assert_eq!(SegmentedString::from("abc\t ").trim_right(), SegmentedString::from("abc"));
215 assert_eq!(SegmentedString::from("abc \n").trim_right(), SegmentedString::from("abc"));
216 assert_eq!(SegmentedString::from(" ").trim_right(), SegmentedString::from(""));
217
218 assert_eq!(SegmentedString::from("abc").trim(), SegmentedString::from("abc"));
219 assert_eq!(SegmentedString::from(" \tabc\t ").trim(), SegmentedString::from("abc"));
220 assert_eq!(SegmentedString::from("\n abc \n").trim(), SegmentedString::from("abc"));
221 assert_eq!(SegmentedString::from(" ").trim(), SegmentedString::from(""));
222 }
223
224 #[test]
225 fn concat() {
226 assert_eq!(SizedSegmentedString::<4>::concat_str_with_segment_size(4, &[]).to_string(), "");
227 assert_eq!(SizedSegmentedString::<4>::concat_str_with_segment_size(4, &[ "a", "b" ]).to_string(), "ab");
228 assert_eq!(
229 SizedSegmentedString::<4>::concat_str_with_segment_size(4, &[ "abcde", "fghij" ]).to_string(),
230 "abcdefghij"
231 );
232 assert_eq!(
233 SizedSegmentedString::<4>::concat_str_with_segment_size(4, &[ "a", "bc", "def", "ghij" ]).to_string(),
234 "abcdefghij"
235 );
236
237 assert_eq!(SizedSegmentedString::<4>::concat(&[] as &[SizedSegmentedString<4>]).to_string(), "");
238 assert_eq!(
239 SizedSegmentedString::<4>::concat(&[ make_string("a"), make_string("b") ]).to_string(),
240 "ab"
241 );
242 assert_eq!(
243 SizedSegmentedString::<4>::concat(&[ make_string("abcdef"), make_string("ghijkl") ]).to_string(),
244 "abcdefghijkl"
245 );
246 }
247
248 #[test]
249 fn index_of() {
250 let long_string = "abcdefghijklmnopqrstuvwxyz-:ABCDEFGHIJKLMNOPQRSTUVWXYZ_!/0123456789*@.";
251 let s1 = make_string(long_string);
252 for i in 0 .. s1.len() - 5 {
253 assert_eq!(s1.index_of(&s1.substr(i, i + 5), 0), Some(i));
254 }
255 for i in (0 .. s1.len() - 5).rev() {
256 assert_eq!(s1.reverse_index_of(&s1.substr(i, i + 5), s1.len()), Some(i));
257 }
258
259 let s2 = make_string("abcdefghijabcdefghij");
260 let s3 = make_string("def");
261 for i in 0..4 {
262 assert_eq!(s2.index_of(&s3, i), Some(3));
263 }
264 for i in 4..14 {
265 assert_eq!(s2.index_of(&s3, i), Some(13));
266 }
267 for i in 14..20 {
268 assert_eq!(s2.index_of(&s3, i), None);
269 }
270 for i in 0..4 {
271 assert_eq!(s2.reverse_index_of(&s3, i), None);
272 }
273 for i in 4..14 {
274 assert_eq!(s2.reverse_index_of(&s3, i), Some(3));
275 }
276 for i in 14..20 {
277 assert_eq!(s2.reverse_index_of(&s3, i), Some(13));
278 }
279 }
280
281 #[test]
282 fn with_append() {
283 let mut s1 = SizedSegmentedString::<4>::empty_with_segment_size(4);
284 s1 = s1.with_append_str("b");
285 assert_eq!(format!("{s1:?}"), "SegmentedString<4, 4>(1:1)\"b\"");
286
287 let mut s1 = make_string("qr");
288 s1 = s1.with_append_str("b");
289 assert_eq!(format!("{s1:?}"), "SegmentedString<4, 4>(3:3)\"qrb\"");
290
291 let mut s1 = make_string("qr");
292 s1 = s1.with_append_str("bcd");
293 assert_eq!(format!("{s1:?}"), "SegmentedString<4, 4>(5:5)[ (4:4)\"qrbc\", (1:1)\"d\" ]");
294
295 let mut s1 = make_string("e\u{301}");
296 assert_eq!(format!("{s1:?}"), "SegmentedString<4, 4>(3:1)\"e\\u{301}\"");
297 s1 = s1.with_append_str("po\u{308}");
298 assert_eq!(format!("{s1:?}"), "SegmentedString<4, 4>(7:3)[ (4:2)\"e\\u{301}p\", (3:1)\"o\\u{308}\" ]");
299
300 let long_string = "abcdefghijklmnopqrstuvwxyz-:ABCDEFGHIJKLMNOPQRSTUVWXYZ_!/0123456789*@.";
301 let mut s1 = make_string("");
302 for i in 0 .. long_string.len() / 2 {
303 s1 = s1.with_append_str(&long_string[i * 2 .. i * 2 + 2]);
304 }
305 assert_eq!(
306 format!("{s1:?}"),
307 "SegmentedString<4, 4>(70:70)[ \
308 (64:64)[ \
309 (16:16)[ (4:4)\"abcd\", (4:4)\"efgh\", (4:4)\"ijkl\", (4:4)\"mnop\" ], \
310 (16:16)[ (4:4)\"qrst\", (4:4)\"uvwx\", (4:4)\"yz-:\", (4:4)\"ABCD\" ], \
311 (16:16)[ (4:4)\"EFGH\", (4:4)\"IJKL\", (4:4)\"MNOP\", (4:4)\"QRST\" ], \
312 (16:16)[ (4:4)\"UVWX\", (4:4)\"YZ_!\", (4:4)\"/012\", (4:4)\"3456\" ] \
313 ], \
314 (6:6)[ \
315 (6:6)[ (4:4)\"789*\", (2:2)\"@.\" ] \
316 ] \
317 ]"
318 );
319 }
320}