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
//! Trim leading and trailing 0s and digit separators.

use crate::util::*;

// TRIM

// Trim leading 0s.
// Does not consume any digit separators.
perftools_inline!{
pub(super) fn ltrim_zero_no_separator<'a>(bytes: &'a [u8], _: u8)
    -> (&'a [u8], usize)
{
    ltrim_char_slice(bytes, b'0')
}}

// Trim leading 0s and digit separators.
perftools_inline!{
#[cfg(feature = "format")]
pub(super) fn ltrim_zero_separator<'a>(bytes: &'a [u8], digit_separator: u8)
    -> (&'a [u8], usize)
{
    ltrim_char2_slice(bytes, b'0', digit_separator)
}}

// Trim leading digit separators (so, nothing).
// Does not consume any digit separators.
perftools_inline!{
pub(super) fn ltrim_separator_no_separator<'a>(bytes: &'a [u8], _: u8)
    -> (&'a [u8], usize)
{
    (bytes, 0)
}}

// Trim leading digit separators.
perftools_inline!{
#[cfg(feature = "format")]
pub(super) fn ltrim_separator_separator<'a>(bytes: &'a [u8], digit_separator: u8)
    -> (&'a [u8], usize)
{
    ltrim_char_slice(bytes, digit_separator)
}}

// Trim trailing 0s.
// Does not consume any digit separators.
perftools_inline!{
pub(super) fn rtrim_zero_no_separator<'a>(bytes: &'a [u8], _: u8)
    -> (&'a [u8], usize)
{
    rtrim_char_slice(bytes, b'0')
}}

// Trim trailing 0s and digit separators.
perftools_inline!{
#[cfg(feature = "format")]
pub(super) fn rtrim_zero_separator<'a>(bytes: &'a [u8], digit_separator: u8)
    -> (&'a [u8], usize)
{
    rtrim_char2_slice(bytes, b'0', digit_separator)
}}

// Trim trailing digit separators (so, nothing).
// Does not consume any digit separators.
perftools_inline!{
pub(super) fn rtrim_separator_no_separator<'a>(bytes: &'a [u8], _: u8)
    -> (&'a [u8], usize)
{
    (bytes, 0)
}}

// Trim trailing digit separators.
perftools_inline!{
#[cfg(feature = "format")]
pub(super) fn rtrim_separator_separator<'a>(bytes: &'a [u8], digit_separator: u8)
    -> (&'a [u8], usize)
{
    rtrim_char_slice(bytes, digit_separator)
}}

// TESTS
// -----

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

    #[test]
    fn trim_zero_no_separator_test() {
        assert_eq!(ltrim_zero_no_separator(b!("01"), b'_'), (b!("1"), 1));
        assert_eq!(rtrim_zero_no_separator(b!("23450"), b'_'), (b!("2345"), 1));
    }

    #[test]
    fn trim_separator_no_separator_test() {
        assert_eq!(ltrim_separator_no_separator(b!("01"), b'_'), (b!("01"), 0));
        assert_eq!(rtrim_separator_no_separator(b!("23450"), b'_'), (b!("23450"), 0));
    }

    #[test]
    #[cfg(feature = "format")]
    fn trim_zero_iltc_separator_test() {
        assert_eq!(ltrim_zero_separator(b!("0_1_2"), b'_'), (b!("1_2"), 2));
        assert_eq!(rtrim_zero_separator(b!("2345_0_"), b'_'), (b!("2345"), 3));
    }

    #[test]
    #[cfg(feature = "format")]
    fn trim_separator_iltc_separator_test() {
        assert_eq!(ltrim_separator_separator(b!("0_1_2"), b'_'), (b!("0_1_2"), 0));
        assert_eq!(rtrim_separator_separator(b!("2345_0_"), b'_'), (b!("2345_0"), 1));
    }
}