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
//! Simple, shared algorithm utilities.

use crate::lib::convert::AsRef;
use crate::lib::{mem, ptr, slice};

// ALGORITHMS

/// Calculate the difference between two pointers.
#[inline]
pub fn distance<T>(first: *const T, last: *const T)
    -> usize
{
    debug_assert!(last >= first, "range must be positive.");
    let f = first as usize;
    let l = last as usize;
    l - f
}

/// Check if two slices are equal to each other.
#[inline]
pub fn equal_to_slice(l: &[u8], r: &[u8])
    -> bool
{
    l == r
}

/// Check if left iter starts with right iter.
#[inline]
#[cfg(feature = "format")]
pub fn starts_with_iter<'a, Iter1, Iter2>(mut l: Iter1, mut r: Iter2)
    -> (bool, Iter1)
    where Iter1: Iterator<Item=&'a u8>,
          Iter2: Iterator<Item=&'a u8>
{
    loop {
        // Only call `next()` on l if r is not None, otherwise,
        // we may incorrectly consume an l character.
        let ri = r.next();
        if ri.is_none() {
            return (true, l);
        } else if l.next() != ri {
            return (false, l);
        }
    }
}

/// Check if left iter starts with right iter without case-sensitivity.
#[inline]
pub fn case_insensitive_starts_with_iter<'a, Iter1, Iter2>(mut l: Iter1, mut r: Iter2)
    -> (bool, Iter1)
    where Iter1: Iterator<Item=&'a u8>,
          Iter2: Iterator<Item=&'a u8>
{
    loop {
        let ri = r.next().map(|x| x.to_ascii_lowercase());
        if ri.is_none() {
            return (true, l);
        } else if l.next().map(|x| x.to_ascii_lowercase()) != ri {
            return (false, l);
        }
    }
}

/// Check if left slice ends with right slice.
#[inline]
pub fn ends_with_slice(l: &[u8], r: &[u8])
    -> bool
{
    // This cannot be out-of-bounds, since we check `l.len() >= r.len()`
    // previous to extracting the subslice, so `l.len() - r.len()` must
    // also be <= l.len() and >= 0.
    let rget = move || unsafe {l.get_unchecked(l.len()-r.len()..)};
    l.len() >= r.len() && equal_to_slice(rget(), r)
}

/// Trim character from the left-side of a slice.
#[inline]
pub fn ltrim_char_slice<'a>(slc: &'a [u8], c: u8)
    -> (&'a [u8], usize)
{
    let count = slc.iter().take_while(|&&si| si == c).count();
    //  This count cannot exceed the bounds of the slice, since it is
    // derived from an iterator using the standard library to generate it.
    debug_assert!(count <= slc.len());
    let slc = unsafe {slc.get_unchecked(count..)};
    (slc, count)
}

/// Trim characters from the left-side of a slice.
#[inline]
#[cfg(feature = "format")]
pub fn ltrim_char2_slice<'a>(slc: &'a [u8], c1: u8, c2: u8)
    -> (&'a [u8], usize)
{
    let count = slc.iter().take_while(|&&si| si == c1 || si == c2).count();
    //  This count cannot exceed the bounds of the slice, since it is
    // derived from an iterator using the standard library to generate it.
    debug_assert!(count <= slc.len());
    let slc = unsafe {slc.get_unchecked(count..)};
    (slc, count)
}

/// Trim character from the right-side of a slice.
#[inline]
pub fn rtrim_char_slice<'a>(slc: &'a [u8], c: u8)
    -> (&'a [u8], usize)
{
    let count = slc.iter().rev().take_while(|&&si| si == c).count();
    let index = slc.len() - count;
    // Count must be <= slc.len(), and therefore, slc.len() - count must
    // also be <= slc.len(), since this is derived from an iterator
    // in the standard library.
    debug_assert!(count <= slc.len());
    debug_assert!(index <= slc.len());
    let slc = unsafe {slc.get_unchecked(..index)};
    (slc, count)
}

/// Trim character from the right-side of a slice.
#[inline]
#[cfg(feature = "format")]
pub fn rtrim_char2_slice<'a>(slc: &'a [u8], c1: u8, c2: u8)
    -> (&'a [u8], usize)
{
    let count = slc.iter().rev().take_while(|&&si| si == c1 || si == c2).count();
    let index = slc.len() - count;
    // Count must be <= slc.len(), and therefore, slc.len() - count must
    // also be <= slc.len(), since this is derived from an iterator
    // in the standard library.
    debug_assert!(count <= slc.len());
    debug_assert!(index <= slc.len());
    let slc = unsafe {slc.get_unchecked(..index)};
    (slc, count)
}

/// Copy from source-to-dst.
#[inline]
pub fn copy_to_dst<'a, Bytes: AsRef<[u8]>>(dst: &'a mut [u8], src: Bytes)
    -> usize
{
    let src = src.as_ref();
    let dst = &mut index_mut!(dst[..src.len()]);

    unsafe {
        ptr::copy_nonoverlapping(src.as_ptr(), dst.as_mut_ptr(), dst.len());
    }

    src.len()
}

/// Length-check variant of ptr::write_bytes for a slice.
#[cfg(not(any(feature = "grisu3", feature = "ryu")))]
#[inline]
pub fn write_bytes(dst: &mut [u8], byte: u8)
{
    unsafe {
        ptr::write_bytes(dst.as_mut_ptr(), byte, dst.len());
    }
}

// TEST
// ----

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn distance_test() {
        unsafe {
            let x: [u8; 10] = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9];
            let first: *const u8 = x.as_ptr();
            let last = first.add(x.len());
            assert_eq!(distance(first, last), 10);
        }
    }

    #[test]
    fn equal_to_test() {
        let x = "Hello";
        let y = "Hello";
        let z = "hello";

        assert!(equal_to_slice(x.as_bytes(), y.as_bytes()));
        assert!(!equal_to_slice(x.as_bytes(), z.as_bytes()));
        assert!(!equal_to_slice(y.as_bytes(), z.as_bytes()));
    }

    #[test]
    #[cfg(feature = "format")]
    fn starts_with_test() {
        let w = b"Hello";
        let x = b"H";
        let y = b"h";
        let z = b"a";

        // forward
        assert!(starts_with_iter(w.iter(), x.iter()).0);
        assert!(!starts_with_iter(w.iter(), y.iter()).0);
        assert!(!starts_with_iter(x.iter(), y.iter()).0);
        assert!(!starts_with_iter(w.iter(), z.iter()).0);
        assert!(!starts_with_iter(x.iter(), z.iter()).0);
        assert!(!starts_with_iter(y.iter(), z.iter()).0);

        // back
        assert!(!starts_with_iter(x.iter(), w.iter()).0);
        assert!(!starts_with_iter(y.iter(), w.iter()).0);
        assert!(!starts_with_iter(z.iter(), w.iter()).0);
    }

    #[test]
    fn case_insensitive_starts_with_test() {
        let w = b"Hello";
        let x = b"H";
        let y = b"h";
        let z = b"a";

        // forward
        assert!(case_insensitive_starts_with_iter(w.iter(), x.iter()).0);
        assert!(case_insensitive_starts_with_iter(w.iter(), y.iter()).0);
        assert!(case_insensitive_starts_with_iter(x.iter(), y.iter()).0);
        assert!(!case_insensitive_starts_with_iter(w.iter(), z.iter()).0);
        assert!(!case_insensitive_starts_with_iter(x.iter(), z.iter()).0);
        assert!(!case_insensitive_starts_with_iter(y.iter(), z.iter()).0);

        // back
        assert!(!case_insensitive_starts_with_iter(x.iter(), w.iter()).0);
        assert!(!case_insensitive_starts_with_iter(y.iter(), w.iter()).0);
        assert!(!case_insensitive_starts_with_iter(z.iter(), w.iter()).0);
    }

    #[test]
    fn ends_with_test() {
        let w = "Hello";
        let x = "lO";
        let y = "lo";
        let z = "o";

        // forward
        assert!(!ends_with_slice(w.as_bytes(), x.as_bytes()));
        assert!(ends_with_slice(w.as_bytes(), y.as_bytes()));
        assert!(ends_with_slice(w.as_bytes(), z.as_bytes()));
        assert!(!ends_with_slice(x.as_bytes(), y.as_bytes()));
        assert!(!ends_with_slice(x.as_bytes(), z.as_bytes()));
        assert!(ends_with_slice(y.as_bytes(), z.as_bytes()));

        // back
        assert!(!ends_with_slice(z.as_bytes(), y.as_bytes()));
        assert!(!ends_with_slice(z.as_bytes(), x.as_bytes()));
        assert!(!ends_with_slice(z.as_bytes(), w.as_bytes()));
        assert!(!ends_with_slice(y.as_bytes(), x.as_bytes()));
        assert!(!ends_with_slice(y.as_bytes(), w.as_bytes()));
        assert!(!ends_with_slice(x.as_bytes(), w.as_bytes()));
    }

    #[test]
    fn ltrim_char_test() {
        let w = "0001";
        let x = "1010";
        let y = "1.00";
        let z = "1e05";

        assert_eq!(ltrim_char_slice(w.as_bytes(), b'0').1, 3);
        assert_eq!(ltrim_char_slice(x.as_bytes(), b'0').1, 0);
        assert_eq!(ltrim_char_slice(x.as_bytes(), b'1').1, 1);
        assert_eq!(ltrim_char_slice(y.as_bytes(), b'0').1, 0);
        assert_eq!(ltrim_char_slice(y.as_bytes(), b'1').1, 1);
        assert_eq!(ltrim_char_slice(z.as_bytes(), b'0').1, 0);
        assert_eq!(ltrim_char_slice(z.as_bytes(), b'1').1, 1);
    }

    #[test]
    #[cfg(feature = "format")]
    fn ltrim_char2_test() {
        let w = "0001";
        let x = "1010";
        let y = "1.00";
        let z = "1e05";
        let a = "0_01";

        assert_eq!(ltrim_char2_slice(w.as_bytes(), b'0', b'_').1, 3);
        assert_eq!(ltrim_char2_slice(x.as_bytes(), b'0', b'_').1, 0);
        assert_eq!(ltrim_char2_slice(x.as_bytes(), b'1', b'_').1, 1);
        assert_eq!(ltrim_char2_slice(y.as_bytes(), b'0', b'_').1, 0);
        assert_eq!(ltrim_char2_slice(y.as_bytes(), b'1', b'_').1, 1);
        assert_eq!(ltrim_char2_slice(z.as_bytes(), b'0', b'_').1, 0);
        assert_eq!(ltrim_char2_slice(z.as_bytes(), b'1', b'_').1, 1);
        assert_eq!(ltrim_char2_slice(a.as_bytes(), b'0', b'_').1, 3);
        assert_eq!(ltrim_char2_slice(a.as_bytes(), b'1', b'_').1, 0);
    }

    #[test]
    fn rtrim_char_test() {
        let w = "0001";
        let x = "1010";
        let y = "1.00";
        let z = "1e05";

        assert_eq!(rtrim_char_slice(w.as_bytes(), b'0').1, 0);
        assert_eq!(rtrim_char_slice(x.as_bytes(), b'0').1, 1);
        assert_eq!(rtrim_char_slice(x.as_bytes(), b'1').1, 0);
        assert_eq!(rtrim_char_slice(y.as_bytes(), b'0').1, 2);
        assert_eq!(rtrim_char_slice(y.as_bytes(), b'1').1, 0);
        assert_eq!(rtrim_char_slice(z.as_bytes(), b'0').1, 0);
        assert_eq!(rtrim_char_slice(z.as_bytes(), b'5').1, 1);
    }

    #[test]
    #[cfg(feature = "format")]
    fn rtrim_char2_test() {
        let w = "0001";
        let x = "1010";
        let y = "1.00";
        let z = "1e05";
        let a = "0_01";

        assert_eq!(rtrim_char2_slice(w.as_bytes(), b'0', b'_').1, 0);
        assert_eq!(rtrim_char2_slice(x.as_bytes(), b'0', b'_').1, 1);
        assert_eq!(rtrim_char2_slice(x.as_bytes(), b'1', b'_').1, 0);
        assert_eq!(rtrim_char2_slice(y.as_bytes(), b'0', b'_').1, 2);
        assert_eq!(rtrim_char2_slice(y.as_bytes(), b'1', b'_').1, 0);
        assert_eq!(rtrim_char2_slice(z.as_bytes(), b'0', b'_').1, 0);
        assert_eq!(rtrim_char2_slice(z.as_bytes(), b'1', b'_').1, 0);
        assert_eq!(rtrim_char2_slice(a.as_bytes(), b'0', b'_').1, 0);
        assert_eq!(rtrim_char2_slice(a.as_bytes(), b'1', b'_').1, 1);
    }
}