summaryrefslogtreecommitdiffstats
path: root/vendor/bstr/src/unicode/whitespace.rs
blob: bf1f47c8e5edf88ddff92c5d894abe168bd710fd (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
use regex_automata::{dfa::Automaton, Anchored, Input};

use crate::unicode::fsm::{
    whitespace_anchored_fwd::WHITESPACE_ANCHORED_FWD,
    whitespace_anchored_rev::WHITESPACE_ANCHORED_REV,
};

/// Return the first position of a non-whitespace character.
pub fn whitespace_len_fwd(slice: &[u8]) -> usize {
    let input = Input::new(slice).anchored(Anchored::Yes);
    WHITESPACE_ANCHORED_FWD
        .try_search_fwd(&input)
        .unwrap()
        .map_or(0, |hm| hm.offset())
}

/// Return the last position of a non-whitespace character.
pub fn whitespace_len_rev(slice: &[u8]) -> usize {
    let input = Input::new(slice).anchored(Anchored::Yes);
    WHITESPACE_ANCHORED_REV
        .try_search_rev(&input)
        .unwrap()
        .map_or(slice.len(), |hm| hm.offset())
}