small_string 0.2.0

7-byte small string optimisation for use in Nova JavaScript engine
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this
// file, You can obtain one at https://mozilla.org/MPL/2.0/.

use core::cmp::Ordering;
use std::borrow::Cow;

use wtf8::{CodePoint, Wtf8};

/// Maximum number of bytes a [SmallString] can inline.
const MAX_LEN: usize = 7;

#[derive(Clone, Copy, PartialEq, Eq, Hash)]
pub struct SmallString {
    /// The string will be padded to 7 bytes with the 0xFF byte, which is never
    /// contained in valid UTF-8 or WTF-8.
    bytes: [u8; MAX_LEN],
}

impl Ord for SmallString {
    fn cmp(&self, other: &Self) -> Ordering {
        self.as_wtf8().cmp(other.as_wtf8())
    }
}

impl PartialOrd for SmallString {
    fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
        Some(self.cmp(other))
    }
}

impl PartialEq<str> for SmallString {
    #[inline]
    fn eq(&self, other: &str) -> bool {
        self.as_bytes().eq(other.as_bytes())
    }
}

impl PartialEq<&str> for SmallString {
    #[inline]
    fn eq(&self, other: &&str) -> bool {
        self.eq(*other)
    }
}

impl core::fmt::Debug for SmallString {
    fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
        write!(f, "\"{}\"", self.to_string_lossy())
    }
}

impl SmallString {
    pub const EMPTY: SmallString = Self {
        bytes: [0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF],
    };

    pub const fn len(&self) -> usize {
        // Find the first 0xFF byte. Small strings must be valid UTF-8, and
        // UTF-8 can never contain 0xFF, so that must mark the end of the
        // string.
        let mut position: u8 = 0;
        loop {
            let is_end_byte = self.bytes[position as usize] == 0xFF;
            if is_end_byte {
                break;
            }
            position += 1;
            if position == MAX_LEN as u8 {
                break;
            }
        }
        position as usize
    }

    /// Returns true if the SmallString contains only ASCII characters.
    ///
    /// ## Example
    ///
    /// ```rust
    /// use small_string::SmallString;
    /// assert!(SmallString::try_from("abc").unwrap().is_ascii());
    /// assert!(!SmallString::try_from("📦").unwrap().is_ascii());
    /// ```
    pub const fn is_ascii(&self) -> bool {
        let mut i = 0;
        while i < self.bytes.len() {
            let byte = self.bytes[i];
            // Padding byte means end of string.
            if byte == 0xFF {
                break;
            }
            if !byte.is_ascii() {
                return false;
            }
            i += 1;
        }
        true
    }

    pub fn utf16_len(&self) -> usize {
        if self.is_ascii() {
            return self.len();
        }
        self.as_wtf8()
            .code_points()
            .map(|cp| {
                let cp = cp.to_u32();
                if (cp & 0xFFFF) == cp { 1 } else { 2 }
            })
            .sum()
    }

    /// Find a CodePoint at a given u16 index; this will give a full CodePoint
    /// even when the index points at a latter surrogate pair half: in this
    /// case the returned boolean will be `true`.
    fn get_code_point_at(&self, idx: usize) -> (CodePoint, bool) {
        let buf = self.as_wtf8();
        let mut i = 0;
        for char in buf.code_points() {
            if i == idx {
                return (char, false);
            }
            let code = char.to_u32();
            // SAFETY: the WTF-16 index cannot overflow as otherwise the WTF-8
            // index would've overflowed a long time ago.
            i = unsafe { i.unchecked_add(if (code & 0xFFFF) == code { 1 } else { 2 }) };
            if i > idx {
                // We're asking for the latter half of this surrogate pair.
                return (char, true);
            }
            // Our match is still further in the buffer.
        }
        unreachable!("Could not find code point index");
    }

    // TODO: This should return a wtf8::CodePoint.
    pub fn char_code_at(&self, idx: usize) -> CodePoint {
        if self.is_ascii() {
            // SAFETY: ASCII is valid UTF-8.
            return unsafe {
                CodePoint::from_u32_unchecked(self.as_str_unchecked().as_bytes()[idx] as u32)
            };
        }
        let (char, take_latter_half) = self.get_code_point_at(idx);
        let code = char.to_u32();
        if (code & 0xFFFF) == code {
            // Single-char character.
            return char;
        }
        let char = char
            .to_char()
            .expect("Surrogate pair did not map to a char");
        let encoded = &mut [0; 2];
        let enc = char.encode_utf16(encoded);
        // Note: since this is a surrogate pair, it should always
        // encode into two u16s.
        debug_assert_eq!(enc.len(), 2);
        let surrogate = encoded[if take_latter_half { 1 } else { 0 }];
        // SAFETY: 0..0xFFFF is always less than 0x10FFFF.
        unsafe { CodePoint::from_u32_unchecked(surrogate as u32) }
    }

    /// Get the CodePoint at a given WTF-16 index.
    pub fn code_point_at(self, idx: usize) -> CodePoint {
        if self.is_ascii() {
            // SAFETY: ASCII is valid UTF-8.
            return unsafe {
                CodePoint::from_u32_unchecked(self.as_str_unchecked().as_bytes()[idx] as u32)
            };
        }
        let encoded = &mut [0; 2];
        let (char, take_latter_half) = self.get_code_point_at(idx);
        if take_latter_half {
            // We're asking for the latter half of this surrogate pair.
            let char = char
                .to_char()
                .expect("Surrogate pair did not map to a char");
            let encoded = char.encode_utf16(encoded);
            // Note: since this is a surrogate pair, it should always
            // encode into two u16s.
            debug_assert_eq!(encoded.len(), 2);
            let surrogate = encoded[1];
            // SAFETY: 0..0xFFFF is always less than 0x10FFFF.
            unsafe { CodePoint::from_u32_unchecked(surrogate as u32) }
        } else {
            char
        }
    }

    pub fn utf8_index(&self, utf16_idx: usize) -> Option<usize> {
        if self.is_ascii() {
            return Some(utf16_idx);
        }
        let mut current_utf16_index = 0;
        for (idx, _ch) in self.as_wtf8().code_points().enumerate() {
            match current_utf16_index.cmp(&utf16_idx) {
                Ordering::Equal => return Some(idx),
                Ordering::Greater => return None,
                Ordering::Less => {
                    current_utf16_index += 1;
                }
            }
        }
        if current_utf16_index > utf16_idx {
            return None;
        }
        debug_assert_eq!(utf16_idx, current_utf16_index);
        Some(self.len())
    }

    pub fn utf16_index(&self, utf8_idx: usize) -> usize {
        if self.is_ascii() {
            return utf8_idx;
        }
        let mut utf16_idx = 0;
        for (idx, ch) in self.to_string_lossy().char_indices() {
            if idx == utf8_idx {
                return utf16_idx;
            }
            assert!(idx < utf8_idx);
            utf16_idx += ch.len_utf16();
        }

        assert_eq!(utf8_idx, self.len());
        utf16_idx
    }

    /// Lossily convert the string to UTF-8.
    /// Return an UTF-8 `&str` slice if the contents are well-formed in UTF-8.
    ///
    /// Surrogates are replaced with `"\u{FFFD}"` (the replacement character “�”).
    ///
    /// This only copies the data if necessary (if it contains any surrogate).
    pub fn to_string_lossy(&self) -> Cow<'_, str> {
        self.as_wtf8().to_string_lossy()
    }

    /// Try to convert the string to UTF-8 and return a `&str` slice.
    ///
    /// Return `None` if the string contains surrogates.
    ///
    /// This does not copy the data.
    #[inline]
    pub fn as_str(&self) -> Option<&str> {
        self.as_wtf8().as_str()
    }

    /// Get the SmallString data as a string slice without checking that the SmallString contains valid UTF-8.
    ///
    /// ## Safety
    ///
    /// The SmallString must contain valid UTF-8.
    #[inline]
    pub const unsafe fn as_str_unchecked(&self) -> &str {
        // SAFETY: caller promises they've checked the UTF-8 validity.
        unsafe { core::str::from_utf8_unchecked(self.as_bytes()) }
    }

    #[inline]
    pub const fn as_wtf8(&self) -> &Wtf8 {
        // SAFETY: guaranteed to be WTF-8.
        unsafe { core::mem::transmute::<&[u8], &Wtf8>(self.as_bytes()) }
    }

    #[inline]
    pub const fn as_bytes(&self) -> &[u8] {
        self.bytes.as_slice().split_at(self.len()).0
    }

    #[inline]
    pub const fn data(&self) -> &[u8; MAX_LEN] {
        &self.bytes
    }

    #[inline]
    pub const fn data_mut(&mut self) -> &mut [u8; MAX_LEN] {
        &mut self.bytes
    }

    #[inline]
    pub const fn is_empty(&self) -> bool {
        matches!(self.bytes, [0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF])
    }

    /// Create a [SmallString] from a [str] without checking that it is small
    /// enough to fit in the inline buffer.
    ///
    /// # Safety
    ///
    /// Caller must ensure that `string` 7 bytes or fewer long.
    pub const unsafe fn from_str_unchecked(string: &str) -> Self {
        let string_bytes = string.as_bytes();

        // We have only 7 bytes to work with, so we must fail to convert if the
        // string is longer than that.
        unsafe { std::hint::assert_unchecked(string_bytes.len() <= MAX_LEN) };

        match string_bytes.len() {
            0 => Self {
                bytes: [0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF],
            },
            1 => Self {
                bytes: [string_bytes[0], 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF],
            },
            2 => Self {
                bytes: [
                    string_bytes[0],
                    string_bytes[1],
                    0xFF,
                    0xFF,
                    0xFF,
                    0xFF,
                    0xFF,
                ],
            },
            3 => Self {
                bytes: [
                    string_bytes[0],
                    string_bytes[1],
                    string_bytes[2],
                    0xFF,
                    0xFF,
                    0xFF,
                    0xFF,
                ],
            },
            4 => Self {
                bytes: [
                    string_bytes[0],
                    string_bytes[1],
                    string_bytes[2],
                    string_bytes[3],
                    0xFF,
                    0xFF,
                    0xFF,
                ],
            },
            5 => Self {
                bytes: [
                    string_bytes[0],
                    string_bytes[1],
                    string_bytes[2],
                    string_bytes[3],
                    string_bytes[4],
                    0xFF,
                    0xFF,
                ],
            },
            6 => Self {
                bytes: [
                    string_bytes[0],
                    string_bytes[1],
                    string_bytes[2],
                    string_bytes[3],
                    string_bytes[4],
                    string_bytes[5],
                    0xFF,
                ],
            },
            7 => Self {
                bytes: [
                    string_bytes[0],
                    string_bytes[1],
                    string_bytes[2],
                    string_bytes[3],
                    string_bytes[4],
                    string_bytes[5],
                    string_bytes[6],
                ],
            },
            _ => unreachable!(),
        }
    }

    /// Inline a [Wtf8] into a [SmallString].
    ///
    /// # Panics
    ///
    /// If `string` is longer than 7 bytes.
    #[inline]
    pub fn from_wtf8(string: &Wtf8) -> Self {
        assert!(string.len() <= MAX_LEN);
        unsafe { Self::from_wtf8_unchecked(string) }
    }

    /// Create a [SmallString] from a [Wtf8] without checking that it is small
    /// enough to fit in the inline buffer.
    ///
    /// # Safety
    ///
    /// Caller must ensure that `string` 7 bytes or fewer long.
    pub const unsafe fn from_wtf8_unchecked(string: &Wtf8) -> Self {
        // SAFETY: The backing data of a WTF8 buffer is indeed a u8 buffer.
        // This is very sketchy but completely safe.
        let string_bytes = unsafe { core::mem::transmute::<&Wtf8, &[u8]>(string) };

        // We have only 7 bytes to work with, so we must fail to convert if the
        // string is longer than that.
        unsafe { std::hint::assert_unchecked(string_bytes.len() <= MAX_LEN) };

        match string_bytes.len() {
            0 => Self {
                bytes: [0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF],
            },
            1 => Self {
                bytes: [string_bytes[0], 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF],
            },
            2 => Self {
                bytes: [
                    string_bytes[0],
                    string_bytes[1],
                    0xFF,
                    0xFF,
                    0xFF,
                    0xFF,
                    0xFF,
                ],
            },
            3 => Self {
                bytes: [
                    string_bytes[0],
                    string_bytes[1],
                    string_bytes[2],
                    0xFF,
                    0xFF,
                    0xFF,
                    0xFF,
                ],
            },
            4 => Self {
                bytes: [
                    string_bytes[0],
                    string_bytes[1],
                    string_bytes[2],
                    string_bytes[3],
                    0xFF,
                    0xFF,
                    0xFF,
                ],
            },
            5 => Self {
                bytes: [
                    string_bytes[0],
                    string_bytes[1],
                    string_bytes[2],
                    string_bytes[3],
                    string_bytes[4],
                    0xFF,
                    0xFF,
                ],
            },
            6 => Self {
                bytes: [
                    string_bytes[0],
                    string_bytes[1],
                    string_bytes[2],
                    string_bytes[3],
                    string_bytes[4],
                    string_bytes[5],
                    0xFF,
                ],
            },
            7 => Self {
                bytes: [
                    string_bytes[0],
                    string_bytes[1],
                    string_bytes[2],
                    string_bytes[3],
                    string_bytes[4],
                    string_bytes[5],
                    string_bytes[6],
                ],
            },
            _ => unreachable!(),
        }
    }

    pub fn from_char(ch: char) -> Self {
        let mut bytes = [0xFF; MAX_LEN];
        ch.encode_utf8(&mut bytes);
        SmallString { bytes }
    }

    pub fn from_code_point(ch: CodePoint) -> Self {
        if let Some(char) = ch.to_char() {
            Self::from_char(char)
        } else {
            let mut bytes = [0xFFu8; MAX_LEN];

            // Lone surrogate: these are U+D800 to U+DFFF.
            let p = ch.to_u32();
            debug_assert!(p <= 0xFFFF);
            let p = p as u16;
            bytes[0] = (0xE0 | (p >> 12)) as u8;
            bytes[1] = (0x80 | ((p >> 6) & 0x3F)) as u8;
            bytes[2] = (0x80 | (p & 0x3F)) as u8;
            SmallString { bytes }
        }
    }
}

impl TryFrom<&str> for SmallString {
    type Error = ();
    fn try_from(value: &str) -> Result<Self, Self::Error> {
        // We have only 7 bytes to work with, so we must fail to convert if the
        // string is longer than that.
        if value.len() <= MAX_LEN {
            // SAFETY: we just checked that the string is 7 bytes or fewer.
            Ok(unsafe { Self::from_str_unchecked(value) })
        } else {
            Err(())
        }
    }
}

impl TryFrom<&Wtf8> for SmallString {
    type Error = ();
    fn try_from(value: &Wtf8) -> Result<Self, Self::Error> {
        // We have only 7 bytes to work with, so we must fail to convert if the
        // string is longer than that.
        if value.len() <= MAX_LEN {
            // SAFETY: we just checked that the string is 7 bytes or fewer.
            Ok(unsafe { Self::from_wtf8_unchecked(value) })
        } else {
            Err(())
        }
    }
}

impl From<char> for SmallString {
    fn from(ch: char) -> Self {
        Self::from_char(ch)
    }
}

#[test]
fn valid_stack_strings() {
    assert!(SmallString::try_from("").is_ok());
    assert_eq!(SmallString::try_from("").unwrap().len(), 0);
    assert!(SmallString::try_from("asd").is_ok());
    assert_eq!(SmallString::try_from("asd").unwrap().len(), 3);
    assert!(SmallString::try_from("asdasd").is_ok());
    assert_eq!(SmallString::try_from("asdasd").unwrap().len(), 6);
    assert!(SmallString::try_from("asdasda").is_ok());
    assert_eq!(SmallString::try_from("asdasda").unwrap().len(), 7);
    assert!(SmallString::try_from("asd76fd").is_ok());
    assert_eq!(SmallString::try_from("asd76fd").unwrap().len(), 7);
    assert!(SmallString::try_from("💩").is_ok());
    assert_eq!(SmallString::try_from("💩 ").unwrap().len(), 5);
    assert!(SmallString::try_from("asd\0foo").is_ok());
    assert_eq!(SmallString::try_from("asd\0foo").unwrap().len(), 7);
    assert!(SmallString::try_from("asdfoo\0").is_ok());
    assert_eq!(SmallString::try_from("asdfoo\0").unwrap().len(), 7);
}

#[test]
fn not_valid_stack_strings() {
    assert!(SmallString::try_from("asd asd r 547 gdfg").is_err());
}

#[test]
fn test_ascii() {
    let ascii = ["", "abc", "a\0bc"];
    for s in ascii {
        assert!(SmallString::try_from(s).unwrap().is_ascii());
    }

    let non_ascii = ["📦", "f📦"];
    for s in non_ascii {
        assert!(!SmallString::try_from(s).unwrap().is_ascii());
    }
}

#[test]
fn str_conversion() {
    let unicode = "🤗";
    let str = SmallString::try_from(unicode).unwrap();
    assert_eq!(str.len(), 4);
    assert_eq!(str, unicode);

    let str = SmallString::try_from(Wtf8::from_str(unicode)).unwrap();
    assert_eq!(str.len(), 4);
    assert_eq!(str, unicode);

    // less than 7 characters, but more than 7 bytes
    let too_large_unicode = "🤗🤗🤗";
    assert!(SmallString::try_from(too_large_unicode).is_err());
    assert!(SmallString::try_from(Wtf8::from_str(too_large_unicode)).is_err());
}