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
// Copyright 2018 Eduardo Sánchez Muñoz
//
// Licensed under the Apache License, Version 2.0, <LICENSE-APACHE or
// http://apache.org/licenses/LICENSE-2.0> or the MIT license <LICENSE-MIT or
// http://opensource.org/licenses/MIT>, at your option. This file may not be
// copied, modified, or distributed except according to those terms.

use std;
use ByteStr;

// An owned byte string. It provides similar functionality as `String`
/// and `Vec<u8>`.
#[derive(Clone, Default, PartialEq, Eq)]
pub struct ByteString {
    inner: Vec<u8>,
}

impl ByteString {
    /// Creates a new empty `ByteString` without allocating any memory.
    #[inline]
    pub fn new() -> Self {
        Self { inner: Vec::new() }
    }
    
    /// Converts a vector of bytes into a `ByteString` without clones or allocation.
    #[inline]
    pub fn from_vec(vec: Vec<u8>) -> Self {
        Self { inner: vec }
    }
    
    /// Creates a `ByteString` from a slice.
    #[inline]
    pub fn from_slice(slice: &[u8]) -> Self {
        Self::from_vec(slice.to_vec())
    }
    
    /// Returns a reference to the underlying byte vector.
    #[inline]
    pub fn as_vec(&self) -> &Vec<u8> {
        &self.inner
    }
    
    /// Returns a mutable reference to the underlying byte vector.
    #[inline]
    pub fn as_mut_vec(&mut self) -> &mut Vec<u8> {
        &mut self.inner
    }
    
    /// Converts `self` into a vector without clones or allocation.
    #[inline]
    pub fn into_vec(self) -> Vec<u8> {
        self.inner
    }
    
    /// Converts `self` into a boxed slice, dropping any excess capacity.
    #[inline]
    pub fn into_boxed_slice(self) -> Box<[u8]> {
        self.into_vec().into_boxed_slice()
    }
    
    /// Converts `self` into a boxed `ByteStr`, dropping any excess capacity.
    #[inline]
    pub fn into_boxed_byte_str(self) -> Box<ByteStr> {
        unsafe { Box::from_raw(Box::into_raw(self.into_boxed_slice()) as *mut ByteStr) }
    }
    
    /// Returns a `ByteStr` containing the entiere string.
    #[inline]
    pub fn as_byte_str(&self) -> &ByteStr {
        ByteStr::from_slice(self.as_vec().as_slice())
    }
    
    /// Returns a mutable `ByteStr` containing the entiere string.
    #[inline]
    pub fn as_mut_byte_str(&mut self) -> &mut ByteStr {
        ByteStr::from_slice_mut(self.as_mut_vec().as_mut_slice())
    }
    
    /// Returns the number of byte the string can hold without reallocating.
    #[inline]
    pub fn capacity(&self) -> usize {
        self.as_vec().capacity()
    }
    
    /// Reserves capacity for at least `additional` more bytes to be inserted into `self`.
    #[inline]
    pub fn reserve(&mut self, additional: usize) {
        self.as_mut_vec().reserve(additional);
    }
    
    /// Reserves the minimum capacity for exactly `additional` more elements to be inserted
    /// into `self`.
    #[inline]
    pub fn reserve_exact(&mut self, additional: usize) {
        self.as_mut_vec().reserve_exact(additional);
    }
    
    /// Shrinks the capacity of the string as much as possible.
    #[inline]
    pub fn shrink_to_fit(&mut self) {
        self.as_mut_vec().shrink_to_fit();
    }
    
    /// Shortens the string, keeping the first `len` bytes.
    #[inline]
    pub fn truncate(&mut self, len: usize) {
        self.as_mut_vec().truncate(len);
    }
    
    /// Clears the vector, removing all values and keeping the capacity of the string.
    #[inline]
    pub fn clear(&mut self) {
        self.as_mut_vec().clear();
    }
    
    /// Sets the length of a vector.
    ///
    /// This will explicitly set the size of the vector, without actually modifying
    /// its buffers, so it is up to the caller to ensure that the vector is actually
    /// the specified size.
    #[inline]
    pub unsafe fn set_len(&mut self, len: usize) {
        self.as_mut_vec().set_len(len);
    }
    
    /// Removes an element from the vector and returns it.
    ///
    /// The removed element is replaced by the last element of the vector.
    ///
    /// This does not preserve ordering, but is O(1).
    ///
    /// # Panics
    ///
    /// Panics if `index` is out of bounds.
    #[inline]
    pub fn swap_remove(&mut self, index: usize) -> u8 {
        self.as_mut_vec().swap_remove(index)
    }
    
    /// Inserts an element at position `index` within the string, shifting all elements
    /// after it to the right.
    ///
    /// # Panics
    ///
    /// Panics if `index` is out of bounds.
    #[inline]
    pub fn insert(&mut self, index: usize, element: u8) {
        self.as_mut_vec().insert(index, element);
    }
    
    /// Removes and returns the element at position `index` within the string, shifting
    /// all elements after it to the left.
    ///
    /// # Panics
    ///
    /// Panics if `index` is out of bounds.
    #[inline]
    pub fn remove(&mut self, index: usize) -> u8 {
        self.as_mut_vec().remove(index)
    }
    
    /// Retains only the elements specified by the predicate.
    #[inline]
    pub fn retain<F>(&mut self, mut f: F)
        where F: FnMut(u8) -> bool
    {
        self.as_mut_vec().retain(|&byte| f(byte));
    }
    
    /// Removes all but the first of consecutive elements in the string that
    /// resolve to the same key.
    ///
    /// Similar to `Vec::dedup_by_key()`.
    #[inline]
    pub fn dedup_by_key<F, K>(&mut self, key: F)
        where F: FnMut(&mut u8) -> K,
              K: PartialEq<K>
    {
        self.as_mut_vec().dedup_by_key(key);
    }
    
    /// Removes all but the first of consecutive elements in the string satisfying a given equality relation.
    ///
    /// Similar to `Vec::dedup_by()`.
    #[inline]
    pub fn dedup_by<F>(&mut self, same_bucket: F)
        where F: FnMut(&mut u8, &mut u8) -> bool
    {
        self.as_mut_vec().dedup_by(same_bucket);
    }
    
    /// Removes consecutive repeated elements in the vector.
    ///
    /// If the vector is sorted, this removes all duplicates.
    #[inline]
    pub fn dedup(&mut self) {
        self.as_mut_vec().dedup();
    }
    
    /// Appends an element to the back of the string.
    #[inline]
    pub fn push(&mut self, element: u8) {
        self.as_mut_vec().push(element);
    }
    
    /// Removes the last element from a string and returns it, or `None` if it is empty.
    #[inline]
    pub fn pop(&mut self) -> Option<u8> {
        self.as_mut_vec().pop()
    }
    
    /// Appends a slice to the back of the string.
    #[inline]
    pub fn push_slice(&mut self, other: &[u8]) {
        self.as_mut_vec().extend_from_slice(other);
    }
    
    /// Appends a `ByteStr` to the back of the string.
    #[inline]
    pub fn push_byte_str(&mut self, other: &ByteStr) {
        self.push_slice(other.as_slice());
    }
    
    /// Inserts a slice at position `index` within the string, shifting all elements
    /// after it to the right.
    ///
    /// # Panics
    ///
    /// Panics if `index` is out of bounds.
    pub fn insert_slice(&mut self, index: usize, other: &[u8]) {
        let old_len = self.len();
        assert!(index <= old_len);
        
        self.reserve(other.len());
        
        unsafe {
            std::ptr::copy(self.as_ptr().offset(index as isize),
                           self.as_mut_ptr().offset((index + other.len()) as isize),
                           old_len - index);
            std::ptr::copy_nonoverlapping(other.as_ptr(), self.as_mut_ptr().offset(index as isize), other.len());
        }
    }
    
    /// Inserts a `ByteStr` at position `index` within the string, shifting all elements
    /// after it to the right.
    ///
    /// # Panics
    ///
    /// Panics if `index` is out of bounds.
    #[inline]
    pub fn insert_byte_str(&mut self, index: usize, other: &ByteStr) {
        self.insert_slice(index, other.as_slice());
    }
    
    /// Creates a draining iterator that removes the specified range in the
    /// vector and yields the removed items.
    #[inline]
    pub fn drain(&mut self, start: Option<usize>, end: Option<usize>) -> std::vec::Drain<u8> {
        match (start, end) {
            (None, None) => self.as_mut_vec().drain(..),
            (Some(start), None) => self.as_mut_vec().drain(start ..),
            (None, Some(end)) => self.as_mut_vec().drain(.. end),
            (Some(start), Some(end)) => self.as_mut_vec().drain(start .. end),
        }
    }
    
    /// Splits the string into two at the given index.
    ///
    /// Returns a newly allocated `ByteString`. `self` contains elements `[0, at)`, and
    /// the returned `ByteString` contains elements `[at, len)`.
    ///
    /// # Panics
    ///
    /// Panics if `at > len`.
    #[inline]
    pub fn split_off(&mut self, at: usize) -> Self {
        Self::from_vec(self.as_mut_vec().split_off(at))
    }
}

// From
impl<'a> From<&'a [u8]> for ByteString {
    #[inline]
    fn from(src: &'a [u8]) -> Self {
        ByteString::from_slice(src)
    }
}

impl<'a> From<&'a ByteStr> for ByteString {
    #[inline]
    fn from(src: &'a ByteStr) -> Self {
        src.to_byte_string()
    }
}

impl<'a> From<&'a str> for ByteString {
    #[inline]
    fn from(src: &'a str) -> Self {
        ByteString::from_slice(src.as_bytes())
    }
}

impl From<Vec<u8>> for ByteString {
    #[inline]
    fn from(src: Vec<u8>) -> Self {
        ByteString::from_vec(src)
    }
}

impl From<Box<[u8]>> for ByteString {
    #[inline]
    fn from(src: Box<[u8]>) -> Self {
        ByteString::from_vec(Vec::from(src))
    }
}

impl From<Box<ByteStr>> for ByteString {
    #[inline]
    fn from(src: Box<ByteStr>) -> Self {
        src.into_byte_string()
    }
}

impl From<Box<str>> for ByteString {
    #[inline]
    fn from(src: Box<str>) -> Self {
        ByteString::from(Box::<[u8]>::from(src))
    }
}

impl From<String> for ByteString {
    #[inline]
    fn from(src: String) -> Self {
        ByteString::from(src.into_bytes())
    }
}

// FromIterator
impl std::iter::FromIterator<u8> for ByteString {
    #[inline]
    fn from_iter<T>(iter: T) -> Self
        where T: IntoIterator<Item=u8>
    {
        ByteString::from_vec(std::iter::FromIterator::from_iter(iter))
    }
}

// Debug
impl std::fmt::Debug for ByteString {
    fn fmt(&self, f: &mut std::fmt::Formatter) -> Result<(), std::fmt::Error> {
        std::fmt::Debug::fmt(self.as_byte_str(), f)
    }
}

// Deref
impl std::ops::Deref for ByteString {
    type Target = ByteStr;
    
    #[inline]
    fn deref(&self) -> &ByteStr {
        self.as_byte_str()
    }
}

impl std::ops::DerefMut for ByteString {
    #[inline]
    fn deref_mut(&mut self) -> &mut ByteStr {
        self.as_mut_byte_str()
    }
}

// Borrow
impl std::borrow::Borrow<ByteStr> for ByteString {
    #[inline]
    fn borrow(&self) -> &ByteStr {
        self.as_byte_str()
    }
}

impl std::borrow::BorrowMut<ByteStr> for ByteString {
    #[inline]
    fn borrow_mut(&mut self) -> &mut ByteStr {
        self.as_mut_byte_str()
    }
}

impl std::borrow::Borrow<[u8]> for ByteString {
    #[inline]
    fn borrow(&self) -> &[u8] {
        self.as_slice()
    }
}

impl std::borrow::BorrowMut<[u8]> for ByteString {
    #[inline]
    fn borrow_mut(&mut self) -> &mut [u8] {
        self.as_mut_slice()
    }
}

// AsRef
impl std::convert::AsRef<ByteStr> for ByteString {
    #[inline]
    fn as_ref(&self) -> &ByteStr {
        self.as_byte_str()
    }
}

impl std::convert::AsRef<[u8]> for ByteString {
    #[inline]
    fn as_ref(&self) -> &[u8] {
        self.as_slice()
    }
}

// AsMut
impl std::convert::AsMut<ByteStr> for ByteString {
    #[inline]
    fn as_mut(&mut self) -> &mut ByteStr {
        self.as_mut_byte_str()
    }
}

impl std::convert::AsMut<[u8]> for ByteString {
    #[inline]
    fn as_mut(&mut self) -> &mut [u8] {
        self.as_mut_slice()
    }
}