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
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
//! Implementations for the [`WString`] type.
//!
//! The type itself lives in the `lib.rs` file to avoid having to have a public alias, but
//! implementations live here.

use std::marker::PhantomData;
use std::ops::{Deref, DerefMut};

use byteorder::{BigEndian, ByteOrder, LittleEndian};

use crate::utf16::{validate_raw_utf16, Utf16CharExt};
use crate::{Utf16Error, WStr, WString};

impl WString<LittleEndian> {
    /// Creates a new [`WString`] from raw bytes in little-endian byte order.
    pub fn from_utf16le(buf: Vec<u8>) -> Result<Self, Utf16Error> {
        Self::from_utf16(buf)
    }

    /// Converts a vector of bytes to a [`WString`], not checking validity.
    ///
    /// # Safety
    ///
    /// You must ensure the vector contains already valid UTF-16 with little-endian
    /// byte-order, otherwise you will get undefined behaviour.
    #[inline]
    pub unsafe fn from_utf16le_unchecked(buf: Vec<u8>) -> Self {
        Self::from_utf16_unchecked(buf)
    }
}

impl WString<BigEndian> {
    /// Creates a new [`WString`] from raw bytes in big-endian byte-order.
    pub fn from_utf16be(buf: Vec<u8>) -> Result<Self, Utf16Error> {
        Self::from_utf16(buf)
    }

    /// Converts a vector of bytes to a [`WString`], not checking validity.
    ///
    /// # Safety
    ///
    /// You must ensure the vector contains already valid UTF-16 with big-endian byte-order,
    /// otherwise you will get undefined behaviour.
    #[inline]
    pub unsafe fn from_utf16be_unchecked(buf: Vec<u8>) -> Self {
        Self::from_utf16_unchecked(buf)
    }
}

impl<E> WString<E>
where
    E: ByteOrder,
{
    /// Creates a new empty [`WString`].
    #[inline]
    pub fn new() -> Self {
        Self {
            buf: Vec::new(),
            _endian: PhantomData,
        }
    }

    /// Creates a new empty [`WString`] with a capacity.
    #[inline]
    pub fn with_capacity(capacity: usize) -> Self {
        Self {
            buf: Vec::with_capacity(capacity),
            _endian: PhantomData,
        }
    }

    /// Converts a vector of bytes to a [`WString`].
    #[inline]
    pub fn from_utf16(buf: Vec<u8>) -> Result<Self, Utf16Error> {
        validate_raw_utf16::<E>(buf.as_slice())?;
        Ok(Self {
            buf,
            _endian: PhantomData,
        })
    }

    /// Converts a vector of bytes to a [`WString`], not checking validity.
    ///
    /// # Safety
    ///
    /// You must ensure the vector contains already valid UTF-16 in the correct byte-order,
    /// otherwise you will get undefined behaviour.
    #[inline]
    pub unsafe fn from_utf16_unchecked(buf: Vec<u8>) -> Self {
        Self {
            buf,
            _endian: PhantomData,
        }
    }

    /// Converts this string into a byte vector.
    #[inline]
    pub fn into_bytes(self) -> Vec<u8> {
        self.buf
    }

    /// Returns a `&WStr` slice containing the entire string.
    #[inline]
    pub fn as_wstr(&self) -> &WStr<E> {
        self
    }

    /// Returns a `&mut WStr` slice containing the entire string.
    #[inline]
    pub fn as_mut_wstr(&mut self) -> &mut WStr<E> {
        self
    }

    /// Appends a string slice onto the end of this string.
    #[inline]
    pub fn push_wstr(&mut self, string: &WStr<E>) {
        self.buf.extend_from_slice(string.as_bytes())
    }

    /// Returns the capacity in bytes.
    #[inline]
    pub fn capacity(&self) -> usize {
        self.buf.capacity()
    }

    /// Ensure that this string has spare capacity of at least `additional` bytes.
    #[inline]
    pub fn reserve(&mut self, additional: usize) {
        self.buf.reserve(additional)
    }

    /// Shrinks the capacity of this string to match its length.
    #[inline]
    pub fn shrink_to_fit(&mut self) {
        self.buf.shrink_to_fit()
    }

    /// Appends the given [`char`] to the end of this string.
    #[inline]
    pub fn push(&mut self, ch: char) {
        let mut buf = [0u8; 4];
        let byte_count = ch.encode_utf16_into::<E>(&mut buf);
        self.buf.extend_from_slice(&buf[..byte_count]);
    }

    /// Shortens this string to the specified length.
    ///
    /// The `new_len` is specified in bytes and not characters, just as [WString::len]
    /// returns the length in bytes.  If `new_len` is greater than the string's current
    /// length, this has no effect.
    ///
    /// Note that this method has no effect on the allocated capacity of the string.
    ///
    /// # Panics
    ///
    /// Panics if `new_len` does not lie on a [char] boundary.
    #[inline]
    pub fn truncate(&mut self, new_len: usize) {
        if new_len < self.len() {
            assert!(
                self.is_char_boundary(new_len),
                "new WString length not on char boundary"
            );
            self.buf.truncate(new_len)
        }
    }

    /// Removes the last character from the string buffer and returns it.
    ///
    /// Returns [`None`] if this string is empty.
    #[inline]
    pub fn pop(&mut self) -> Option<char> {
        let ch = self.chars().next_back()?;
        let newlen = self.len() - ch.encoded_utf16_len();
        unsafe {
            self.buf.set_len(newlen);
        }
        Some(ch)
    }

    /// Removes a [`char`] from this string at the given byte position and returns it.
    ///
    /// This is an `O(n)` operation as it requires copying every element in the buffer.
    ///
    /// # Panics
    ///
    /// Panics if `idx` is larger than or equal to the string's length, or if it does not
    /// lie on a [`char`] boundary.
    #[inline]
    pub fn remove(&mut self, idx: usize) -> char {
        let ch = match self[idx..].chars().next() {
            Some(ch) => ch,
            None => panic!("cannot remove a char from the end of a string"),
        };
        let next = idx + ch.encoded_utf16_len();
        let len = self.len();
        unsafe {
            std::ptr::copy(
                self.buf.as_ptr().add(next),
                self.buf.as_mut_ptr().add(idx),
                len - next,
            );
            self.buf.set_len(len - (next - idx));
        }
        ch
    }

    /// Retains only the characters specified by the predicate.
    #[inline]
    pub fn retain<F>(&mut self, mut f: F)
    where
        F: FnMut(char) -> bool,
    {
        let len = self.len();
        let mut del_bytes = 0;
        let mut idx = 0;

        while idx < len {
            let ch = unsafe { self.get_unchecked(idx..len).chars().next().unwrap() };
            let ch_len = ch.encoded_utf16_len();

            if !f(ch) {
                del_bytes += ch_len;
            } else if del_bytes > 0 {
                unsafe {
                    std::ptr::copy(
                        self.buf.as_ptr().add(idx),
                        self.buf.as_mut_ptr().add(idx - del_bytes),
                        ch_len,
                    );
                }
            }
            idx += ch_len;
        }

        if del_bytes > 0 {
            unsafe { self.buf.set_len(len - del_bytes) }
        }
    }

    /// Inserts a [`char`] into this string at the given byte position.
    ///
    /// This is an `O(n)` operation as it requires copying every element in the buffer.
    ///
    /// # Panics
    ///
    /// Panics if `idx` is larger than the string's length or if it does not lie on a
    /// [`char`] boundary.
    #[inline]
    pub fn insert(&mut self, idx: usize, ch: char) {
        assert!(self.is_char_boundary(idx), "insert not on char boundary");
        let mut buf = [0u8; 4];
        let len = ch.encode_utf16_into::<E>(&mut buf);

        unsafe {
            self.insert_bytes(idx, &buf[..len]);
        }
    }

    unsafe fn insert_bytes(&mut self, idx: usize, bytes: &[u8]) {
        #![allow(unused_unsafe)]
        let orig_len = self.len();
        let len_bytes = bytes.len();
        self.buf.reserve(len_bytes);

        unsafe {
            std::ptr::copy(
                self.buf.as_ptr().add(idx),
                self.buf.as_mut_ptr().add(idx + len_bytes),
                orig_len - idx,
            );
            std::ptr::copy(bytes.as_ptr(), self.buf.as_mut_ptr().add(idx), len_bytes);
            self.buf.set_len(orig_len + len_bytes);
        }
    }

    /// Inserts a string slice into this string at the given byte position.
    ///
    /// This is an `O(n)` operation as it requires copying every element in the buffer.  The
    /// string slice must have the same endianess.
    ///
    /// # Panics
    ///
    /// Panics if `idx` is larger than the string's length or if it does not lie on a
    /// [`char`] boundary.
    #[inline]
    pub fn insert_wstr(&mut self, idx: usize, string: &WStr<E>) {
        assert!(self.is_char_boundary(idx));
        unsafe {
            self.insert_bytes(idx, string.as_bytes());
        }
    }

    /// Returns a mutable reference to the contents of this string.
    ///
    /// # Safety
    ///
    /// You must ensure that the bytes remain encoded in UTF-16 with the correct byte-order,
    /// otherwise you will get undefined behaviour trying to use the string.
    #[inline]
    pub unsafe fn as_mut_vec(&mut self) -> &mut Vec<u8> {
        &mut self.buf
    }

    /// Returns the length in bytes, not chars or graphemes.
    #[inline]
    pub fn len(&self) -> usize {
        self.buf.len()
    }

    /// Returns `true` if the string has a [`WString::len`] of zero, `false` otherwise.
    #[inline]
    pub fn is_empty(&self) -> bool {
        self.len() == 0
    }

    /// Splits the string into two at the given index.
    ///
    /// Returns a newly allocated [`WString`].  `self` contains bytes `[0..at]` and the
    /// returned [WString] contains bytes `[at..len]]`.
    ///
    /// # Panics
    ///
    /// Panics if `at` is not on a character boundary or is beyond the end of the string.
    #[inline]
    #[must_use = "use `.truncate()` if you don't need the other half"]
    pub fn split_off(&mut self, at: usize) -> WString<E> {
        assert!(
            self.is_char_boundary(at),
            "split_off not on a char boundary"
        );
        let other = self.buf.split_off(at);
        unsafe { WString::from_utf16_unchecked(other) }
    }

    /// Truncates this string, removing all contents.
    ///
    /// The length will be zero, but the capacity will remain unchanged.
    #[inline]
    pub fn clear(&mut self) {
        self.buf.clear();
    }
}

impl<E> Default for WString<E>
where
    E: ByteOrder,
{
    #[inline]
    fn default() -> Self {
        Self::new()
    }
}

impl<E> Deref for WString<E>
where
    E: ByteOrder,
{
    type Target = WStr<E>;

    #[inline]
    fn deref(&self) -> &Self::Target {
        unsafe { WStr::from_utf16_unchecked(self.buf.as_slice()) }
    }
}

impl<E> DerefMut for WString<E>
where
    E: ByteOrder,
{
    fn deref_mut(&mut self) -> &mut Self::Target {
        unsafe { WStr::from_utf16_unchecked_mut(self.buf.as_mut_slice()) }
    }
}

impl<E> From<&str> for WString<E>
where
    E: ByteOrder,
{
    #[inline]
    fn from(source: &str) -> Self {
        let mut new = Self::with_capacity(source.len());
        for ch in source.chars() {
            new.push(ch);
        }
        new
    }
}

impl<E> From<&mut str> for WString<E>
where
    E: ByteOrder,
{
    #[inline]
    fn from(source: &mut str) -> Self {
        let mut new = Self::with_capacity(source.len());
        for ch in source.chars() {
            new.push(ch);
        }
        new
    }
}

impl<E> From<&String> for WString<E>
where
    E: ByteOrder,
{
    #[inline]
    fn from(source: &String) -> Self {
        Self::from(source.as_str())
    }
}

#[cfg(test)]
mod tests {
    use byteorder::{BE, LE};

    use super::*;

    #[test]
    fn test_new() {
        let s: WString<LE> = WString::new();
        assert_eq!(s.len(), 0);
        assert_eq!(s.capacity(), 0);
        assert_eq!(s.to_utf8(), "");
    }

    #[test]
    fn test_with_capacity() {
        let s: WString<LE> = WString::with_capacity(5);
        assert_eq!(s.capacity(), 5);
        assert_eq!(s.len(), 0);
        assert_eq!(s.to_utf8(), "");
    }

    #[test]
    fn test_from_utf16() {
        let b = b"h\x00e\x00l\x00l\x00o\x00";
        let s: WString<LE> = WString::from_utf16(b.to_vec()).unwrap();
        assert_eq!(s.buf, b);
        assert_eq!(s.to_utf8(), "hello");
    }

    #[test]
    fn test_from_utf16_le_be() {
        let b_le = b"h\x00e\x00l\x00l\x00o\x00";
        let s_le = WString::from_utf16le(b_le.to_vec()).unwrap();
        assert_eq!(s_le.to_utf8(), "hello");

        let b_be = b"\x00h\x00e\x00l\x00l\x00o";
        let s_be = WString::from_utf16be(b_be.to_vec()).unwrap();
        assert_eq!(s_be.to_utf8(), "hello");
    }

    #[test]
    fn test_from_utf16_unchecked() {
        let b_le = b"h\x00e\x00l\x00l\x00o\x00";
        let s_le: WString<LE> = unsafe { WString::from_utf16_unchecked(b_le.to_vec()) };
        assert_eq!(s_le.to_utf8(), "hello");

        let s_le = unsafe { WString::from_utf16le_unchecked(b_le.to_vec()) };
        assert_eq!(s_le.to_utf8(), "hello");

        let b_be = b"\x00h\x00e\x00l\x00l\x00o";
        let s_be = unsafe { WString::from_utf16be_unchecked(b_be.to_vec()) };
        assert_eq!(s_be.to_utf8(), "hello");
    }

    #[test]
    fn test_from_str() {
        let s: WString<LE> = WString::from("hello");
        assert_eq!(s.as_bytes(), b"h\x00e\x00l\x00l\x00o\x00");

        let s: WString<BE> = WString::from("hello");
        assert_eq!(s.as_bytes(), b"\x00h\x00e\x00l\x00l\x00o");

        let s: WString<LE> = From::from("hello");
        assert_eq!(s.as_bytes(), b"h\x00e\x00l\x00l\x00o\x00");

        let mut v = String::from("hello");
        let s: WString<LE> = From::from(v.as_mut_str());
        assert_eq!(s.as_bytes(), b"h\x00e\x00l\x00l\x00o\x00");
    }

    #[test]
    fn test_from_string() {
        let v = String::from("hello");

        let s: WString<LE> = WString::from(&v);
        assert_eq!(s.as_bytes(), b"h\x00e\x00l\x00l\x00o\x00");

        let s: WString<LE> = From::from(&v);
        assert_eq!(s.as_bytes(), b"h\x00e\x00l\x00l\x00o\x00");
    }

    #[test]
    fn test_into_bytes() {
        let b = b"h\x00e\x00l\x00l\x00o\x00";
        let s = WString::from_utf16le(b.to_vec()).unwrap();
        assert_eq!(s.into_bytes(), b);
    }

    #[test]
    fn test_as_wstr() {
        let b = b"h\x00e\x00l\x00l\x00o\x00";
        let wstr = WStr::from_utf16le(b).unwrap();
        let wstring = WString::from_utf16le(b.to_vec()).unwrap();
        assert_eq!(wstr, wstring.as_wstr());
    }

    #[test]
    fn test_as_mut_wstr() {
        let b = b"h\x00e\x00l\x00l\x00o\x00";
        let wstr = WStr::from_utf16le(b).unwrap();
        let mut wstring = WString::from_utf16le(b.to_vec()).unwrap();
        let m: &mut WStr<_> = wstring.as_mut_wstr();
        assert_eq!(m, wstr);
    }

    #[test]
    fn test_push_wstr() {
        let b = b"h\x00e\x00l\x00l\x00o\x00";
        let mut wstring = WString::from_utf16le(b.to_vec()).unwrap();
        let b = b" \x00w\x00o\x00r\x00l\x00d\x00";
        let wstr = WStr::from_utf16le(b).unwrap();
        wstring.push_wstr(wstr);
        assert_eq!(wstring.to_utf8(), "hello world");
    }

    #[test]
    fn test_reserve() {
        let mut s: WString<LE> = WString::with_capacity(0);
        assert_eq!(s.capacity(), 0);
        s.reserve(42);
        assert!(s.capacity() >= 42);
    }

    #[test]
    fn test_shrink_to_fit() {
        let mut s: WString<LE> = WString::with_capacity(42);
        assert!(s.capacity() >= 42);
        s.shrink_to_fit();
        assert_eq!(s.capacity(), 0);
    }

    #[test]
    fn test_push() {
        let mut s: WString<LE> = WString::new();
        s.push('h');
        s.push('i');
        assert_eq!(s.as_bytes(), b"h\x00i\x00");
        assert_eq!(s.to_utf8(), "hi");

        s.push('\u{10000}');
        assert_eq!(s.as_bytes(), b"h\x00i\x00\x00\xd8\x00\xdc");
        assert_eq!(s.to_utf8(), "hi\u{10000}");
    }

    #[test]
    fn test_truncate() {
        let b = b"h\x00e\x00l\x00l\x00o\x00";
        let mut s = WString::from_utf16le(b.to_vec()).unwrap();

        s.truncate(20);
        assert_eq!(s.to_utf8(), "hello");

        s.truncate(4);
        assert_eq!(s.to_utf8(), "he");
    }

    #[test]
    #[should_panic]
    fn test_truncate_no_char_boundary() {
        let b = b"h\x00e\x00l\x00l\x00o\x00";
        let mut s = WString::from_utf16le(b.to_vec()).unwrap();

        s.truncate(1);
    }

    #[test]
    fn test_pop() {
        let b = b"a\x00\x00\xd8\x00\xdch\x00i\x00";
        let mut s = WString::from_utf16le(b.to_vec()).unwrap();
        assert_eq!(s.to_utf8(), "a\u{10000}hi");

        assert_eq!(s.pop(), Some('i'));
        assert_eq!(s.to_utf8(), "a\u{10000}h");

        assert_eq!(s.pop(), Some('h'));
        assert_eq!(s.to_utf8(), "a\u{10000}");

        assert_eq!(s.pop(), Some('\u{10000}'));
        assert_eq!(s.to_utf8(), "a");

        assert_eq!(s.pop(), Some('a'));
        assert!(s.is_empty());
    }

    #[test]
    fn test_remove() {
        let b = b"a\x00\x00\xd8\x00\xdch\x00i\x00";
        let mut s = WString::from_utf16le(b.to_vec()).unwrap();

        assert_eq!(s.remove(2), '\u{10000}');
        assert_eq!(s.remove(2), 'h');
        assert_eq!(s.to_utf8(), "ai");
    }

    #[test]
    fn test_retain() {
        let mut s: WString<LE> = From::from("h_e__ll_o");
        s.retain(|c| c != '_');
        assert_eq!(s.to_utf8(), "hello");
    }

    #[test]
    fn test_insert() {
        let mut s: WString<LE> = From::from("hllo");
        s.insert(2, 'e');
        assert_eq!(s.to_utf8(), "hello");
    }

    #[test]
    fn test_insert_wstr() {
        let mut s: WString<LE> = From::from("ho");
        let slice: WString<LE> = From::from("ell");
        s.insert_wstr(2, slice.as_wstr());
        assert_eq!(s.to_string(), "hello");
    }

    #[test]
    fn test_as_mut_vec() {
        let mut s: WString<LE> = From::from("hello");
        unsafe {
            let v: &mut Vec<u8> = s.as_mut_vec();
            v.extend(b" \x00w\x00o\x00r\x00l\x00d\x00");
        }
        assert_eq!(s.to_string(), "hello world");
    }

    #[test]
    fn test_split_off() {
        let mut s: WString<LE> = From::from("helloworld");
        let t = s.split_off(10);
        assert_eq!(s.to_string(), "hello");
        assert_eq!(t.to_string(), "world");
    }

    #[test]
    #[should_panic]
    fn test_split_off_bad_index() {
        let mut s: WString<LE> = From::from("hi");
        let _t = s.split_off(1);
    }

    #[test]
    fn test_clear() {
        let mut s: WString<LE> = From::from("hello");
        assert_eq!(s.to_string(), "hello");
        let cap = s.capacity();

        s.clear();
        assert!(s.is_empty());
        assert_eq!(s.capacity(), cap)
    }

    #[test]
    fn test_deref() {
        let b = b"h\x00e\x00l\x00l\x00o\x00";
        let wstring = WString::from_utf16le(b.to_vec()).unwrap();
        let wstr = WStr::from_utf16le(b).unwrap();
        assert_eq!(wstring.deref(), wstr);
    }

    #[test]
    fn test_deref_mut() {
        let b = b"h\x00e\x00l\x00l\x00o\x00";
        let v = Vec::from(&b[..]);
        let mut s = WString::from_utf16le(v).unwrap();
        let wstr = s.deref_mut();
        unsafe {
            let buf = wstr.as_bytes_mut();
            buf.copy_from_slice(b"w\x00o\x00r\x00l\x00d\x00");
        }
        assert_eq!(s.to_utf8(), "world");
    }
}