# fallback.rs - source
Source of the Rust file `/home/justine/.cargo/registry/src/github.com-1ecc6299db9ec823/memchr-2.5.0/src/memchr/fallback.rs`.
Keywords: rust, rustlang, rust-lang

    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

    // This module defines pure Rust platform independent implementations of all
    // the memchr routines. We do our best to make them fast. Some of them may even
    // get auto-vectorized.

    use core::{cmp, usize};

    #[cfg(target_pointer_width = "16")]
    const USIZE_BYTES: usize = 2;

    #[cfg(target_pointer_width = "32")]
    const USIZE_BYTES: usize = 4;

    #[cfg(target_pointer_width = "64")]
    const USIZE_BYTES: usize = 8;

    // The number of bytes to loop at in one iteration of memchr/memrchr.
    const LOOP_SIZE: usize = 2 * USIZE_BYTES;

    /// Return `true` if `x` contains any zero byte.
    ///
    /// From *Matters Computational*, J. Arndt
    ///
    /// "The idea is to subtract one from each of the bytes and then look for
    /// bytes where the borrow propagated all the way to the most significant
    /// bit."
    #[inline(always)]
    fn contains_zero_byte(x: usize) -> bool {
        const LO_U64: u64 = 0x0101010101010101;
        const HI_U64: u64 = 0x8080808080808080;

        const LO_USIZE: usize = LO_U64 as usize;
        const HI_USIZE: usize = HI_U64 as usize;

        x.wrapping_sub(LO_USIZE) & !x & HI_USIZE != 0
    }

    /// Repeat the given byte into a word size number. That is, every 8 bits
    /// is equivalent to the given byte. For example, if `b` is `\x4E` or
    /// `01001110` in binary, then the returned value on a 32-bit system would be:
    /// `01001110_01001110_01001110_01001110`.
    #[inline(always)]
    fn repeat_byte(b: u8) -> usize {
        (b as usize) * (usize::MAX / 255)
    }

    pub fn memchr(n1: u8, haystack: &[u8]) -> Option<usize> {
        let vn1 = repeat_byte(n1);
        let confirm = |byte| byte == n1;
        let loop_size = cmp::min(LOOP_SIZE, haystack.len());
        let align = USIZE_BYTES - 1;
        let start_ptr = haystack.as_ptr();
        let mut ptr = start_ptr;

        unsafe {
            let end_ptr = start_ptr.add(haystack.len());
            if haystack.len() < USIZE_BYTES {
                return forward_search(start_ptr, end_ptr, ptr, confirm);
            }

            let chunk = (ptr as *const usize).read_unaligned();
            if contains_zero_byte(chunk ^ vn1) {
                return forward_search(start_ptr, end_ptr, ptr, confirm);
            }

            ptr = ptr.add(USIZE_BYTES - (start_ptr as usize & align));
            debug_assert!(ptr > start_ptr);
            debug_assert!(end_ptr.sub(USIZE_BYTES) >= start_ptr);
            while loop_size == LOOP_SIZE && ptr <= end_ptr.sub(loop_size) {
                debug_assert_eq!(0, (ptr as usize) % USIZE_BYTES);

                let a = *(ptr as *const usize);
                let b = *(ptr.add(USIZE_BYTES) as *const usize);
                let eqa = contains_zero_byte(a ^ vn1);
                let eqb = contains_zero_byte(b ^ vn1);
                if eqa || eqb {
                    break;
                }
                ptr = ptr.add(LOOP_SIZE);
            }
            forward_search(start_ptr, end_ptr, ptr, confirm)
        }
    }

    /// Like `memchr`, but searches for two bytes instead of one.
    pub fn memchr2(n1: u8, n2: u8, haystack: &[u8]) -> Option<usize> {
        let vn1 = repeat_byte(n1);
        let vn2 = repeat_byte(n2);
        let confirm = |byte| byte == n1 || byte == n2;
        let align = USIZE_BYTES - 1;
        let start_ptr = haystack.as_ptr();
        let mut ptr = start_ptr;

        unsafe {
            let end_ptr = start_ptr.add(haystack.len());
            if haystack.len() < USIZE_BYTES {
                return forward_search(start_ptr, end_ptr, ptr, confirm);
            }

            let chunk = (ptr as *const usize).read_unaligned();
            let eq1 = contains_zero_byte(chunk ^ vn1);
            let eq2 = contains_zero_byte(chunk ^ vn2);
            if eq1 || eq2 {
                return forward_search(start_ptr, end_ptr, ptr, confirm);
            }

            ptr = ptr.add(USIZE_BYTES - (start_ptr as usize & align));
            debug_assert!(ptr > start_ptr);
            debug_assert!(end_ptr.sub(USIZE_BYTES) >= start_ptr);
            while ptr <= end_ptr.sub(USIZE_BYTES) {
                debug_assert_eq!(0, (ptr as usize) % USIZE_BYTES);

                let chunk = *(ptr as *const usize);
                let eq1 = contains_zero_byte(chunk ^ vn1);
                let eq2 = contains_zero_byte(chunk ^ vn2);
                if eq1 || eq2 {
                    break;
                }
                ptr = ptr.add(USIZE_BYTES);
            }
            forward_search(start_ptr, end_ptr, ptr, confirm)
        }
    }

    /// Like `memchr`, but searches for three bytes instead of one.
    pub fn memchr3(n1: u8, n2: u8, n3: u8, haystack: &[u8]) -> Option<usize> {
        let vn1 = repeat_byte(n1);
        let vn2 = repeat_byte(n2);
        let vn3 = repeat_byte(n3);
        let confirm = |byte| byte == n1 || byte == n2 || byte == n3;
        let align = USIZE_BYTES - 1;
        let start_ptr = haystack.as_ptr();
        let mut ptr = start_ptr;

        unsafe {
            let end_ptr = start_ptr.add(haystack.len());
            if haystack.len() < USIZE_BYTES {
                return forward_search(start_ptr, end_ptr, ptr, confirm);
            }

            let chunk = (ptr as *const usize).read_unaligned();
            let eq1 = contains_zero_byte(chunk ^ vn1);
            let eq2 = contains_zero_byte(chunk ^ vn2);
            let eq3 = contains_zero_byte(chunk ^ vn3);
            if eq1 || eq2 || eq3 {
                return forward_search(start_ptr, end_ptr, ptr, confirm);
            }

            ptr = ptr.add(USIZE_BYTES - (start_ptr as usize & align));
            debug_assert!(ptr > start_ptr);
            debug_assert!(end_ptr.sub(USIZE_BYTES) >= start_ptr);
            while ptr <= end_ptr.sub(USIZE_BYTES) {
                debug_assert_eq!(0, (ptr as usize) % USIZE_BYTES);

                let chunk = *(ptr as *const usize);
                let eq1 = contains_zero_byte(chunk ^ vn1);
                let eq2 = contains_zero_byte(chunk ^ vn2);
                let eq3 = contains_zero_byte(chunk ^ vn3);
                if eq1 || eq2 || eq3 {
                    break;
                }
                ptr = ptr.add(USIZE_BYTES);
            }
            forward_search(start_ptr, end_ptr, ptr, confirm)
        }
    }

    /// Return the last index matching the byte `x` in `text`.
    pub fn memrchr(n1: u8, haystack: &[u8]) -> Option<usize> {
        let vn1 = repeat_byte(n1);
        let confirm = |byte| byte == n1;
        let loop_size = cmp::min(LOOP_SIZE, haystack.len());
        let align = USIZE_BYTES - 1;
        let start_ptr = haystack.as_ptr();

        unsafe {
            let end_ptr = start_ptr.add(haystack.len());
            let mut ptr = end_ptr;
            if haystack.len() < USIZE_BYTES {
                return reverse_search(start_ptr, end_ptr, ptr, confirm);
            }

            let chunk = (ptr.sub(USIZE_BYTES) as *const usize).read_unaligned();
            if contains_zero_byte(chunk ^ vn1) {
                return reverse_search(start_ptr, end_ptr, ptr, confirm);
            }

            ptr = (end_ptr as usize & !align) as *const u8;
            debug_assert!(start_ptr <= ptr && ptr <= end_ptr);
            while loop_size == LOOP_SIZE && ptr >= start_ptr.add(loop_size) {
                debug_assert_eq!(0, (ptr as usize) % USIZE_BYTES);

                let a = *(ptr.sub(2 * USIZE_BYTES) as *const usize);
                let b = *(ptr.sub(1 * USIZE_BYTES) as *const usize);
                let eqa = contains_zero_byte(a ^ vn1);
                let eqb = contains_zero_byte(b ^ vn1);
                if eqa || eqb {
                    break;
                }
                ptr = ptr.sub(loop_size);
            }
            reverse_search(start_ptr, end_ptr, ptr, confirm)
        }
    }

    /// Like `memrchr`, but searches for two bytes instead of one.
    pub fn memrchr2(n1: u8, n2: u8, haystack: &[u8]) -> Option<usize> {
        let vn1 = repeat_byte(n1);
        let vn2 = repeat_byte(n2);
        let confirm = |byte| byte == n1 || byte == n2;
        let align = USIZE_BYTES - 1;
        let start_ptr = haystack.as_ptr();

        unsafe {
            let end_ptr = start_ptr.add(haystack.len());
            let mut ptr = end_ptr;
            if haystack.len() < USIZE_BYTES {
                return reverse_search(start_ptr, end_ptr, ptr, confirm);
            }

            let chunk = (ptr.sub(USIZE_BYTES) as *const usize).read_unaligned();
            let eq1 = contains_zero_byte(chunk ^ vn1);
            let eq2 = contains_zero_byte(chunk ^ vn2);
            if eq1 || eq2 {
                return reverse_search(start_ptr, end_ptr, ptr, confirm);
            }

            ptr = (end_ptr as usize & !align) as *const u8;
            debug_assert!(start_ptr <= ptr && ptr <= end_ptr);
            while ptr >= start_ptr.add(USIZE_BYTES) {
                debug_assert_eq!(0, (ptr as usize) % USIZE_BYTES);

                let chunk = *(ptr.sub(USIZE_BYTES) as *const usize);
                let eq1 = contains_zero_byte(chunk ^ vn1);
                let eq2 = contains_zero_byte(chunk ^ vn2);
                if eq1 || eq2 {
                    break;
                }
                ptr = ptr.sub(USIZE_BYTES);
            }
            reverse_search(start_ptr, end_ptr, ptr, confirm)
        }
    }

    /// Like `memrchr`, but searches for three bytes instead of one.
    pub fn memrchr3(n1: u8, n2: u8, n3: u8, haystack: &[u8]) -> Option<usize> {
        let vn1 = repeat_byte(n1);
        let vn2 = repeat_byte(n2);
        let vn3 = repeat_byte(n3);
        let confirm = |byte| byte == n1 || byte == n2 || byte == n3;
        let align = USIZE_BYTES - 1;
        let start_ptr = haystack.as_ptr();

        unsafe {
            let end_ptr = start_ptr.add(haystack.len());
            let mut ptr = end_ptr;
            if haystack.len() < USIZE_BYTES {
                return reverse_search(start_ptr, end_ptr, ptr, confirm);
            }

            let chunk = (ptr.sub(USIZE_BYTES) as *const usize).read_unaligned();
            let eq1 = contains_zero_byte(chunk ^ vn1);
            let eq2 = contains_zero_byte(chunk ^ vn2);
            let eq3 = contains_zero_byte(chunk ^ vn3);
            if eq1 || eq2 || eq3 {
                return reverse_search(start_ptr, end_ptr, ptr, confirm);
            }

            ptr = (end_ptr as usize & !align) as *const u8;
            debug_assert!(start_ptr <= ptr && ptr <= end_ptr);
            while ptr >= start_ptr.add(USIZE_BYTES) {
                debug_assert_eq!(0, (ptr as usize) % USIZE_BYTES);

                let chunk = *(ptr.sub(USIZE_BYTES) as *const usize);
                let eq1 = contains_zero_byte(chunk ^ vn1);
                let eq2 = contains_zero_byte(chunk ^ vn2);
                let eq3 = contains_zero_byte(chunk ^ vn3);
                if eq1 || eq2 || eq3 {
                    break;
                }
                ptr = ptr.sub(USIZE_BYTES);
            }
            reverse_search(start_ptr, end_ptr, ptr, confirm)
        }
    }

    #[inline(always)]
    unsafe fn forward_search<F: Fn(u8) -> bool>(
        start_ptr: *const u8,
        end_ptr: *const u8,
        mut ptr: *const u8,
        confirm: F,
    ) -> Option<usize> {
        debug_assert!(start_ptr <= ptr);
        debug_assert!(ptr <= end_ptr);

        while ptr < end_ptr {
            if confirm(*ptr) {
                return Some(sub(ptr, start_ptr));
            }
            ptr = ptr.offset(1);
        }
        None
    }

    #[inline(always)]
    unsafe fn reverse_search<F: Fn(u8) -> bool>(
        start_ptr: *const u8,
        end_ptr: *const u8,
        mut ptr: *const u8,
        confirm: F,
    ) -> Option<usize> {
        debug_assert!(start_ptr <= ptr);
        debug_assert!(ptr <= end_ptr);

        while ptr > start_ptr {
            ptr = ptr.offset(-1);
            if confirm(*ptr) {
                return Some(sub(ptr, start_ptr));
            }
        }
        None
    }

    /// Subtract `b` from `a` and return the difference. `a` should be greater than
    /// or equal to `b`.
    fn sub(a: *const u8, b: *const u8) -> usize {
        debug_assert!(a >= b);
        (a as usize) - (b as usize)
    }