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
/*!

Additional combinators specifically tailored for this parser.
 
Warning: It's likely the combinators are public to the crate only, and
thus can be absent from the public documentation.

*/

use super::Input;
use nom::IResult;
#[cfg(feature = "no_std")] use alloc::Vec;

/// `take_until_terminated(S, C)` is like `take_until` but with a
/// lookahead combinator `C`. It's not similar to
/// `terminated!(take_until(S, peek!(C)))` because it loops over the
/// input until `C` is true.
#[macro_export]
macro_rules! take_until_terminated (
    (_ $input:expr, $substr:expr, $consume:expr, $submac:ident!( $($args:tt)* )) => (
        {
            use ::nom::{
                ErrorKind,
                FindSubstring,
                IResult,
                Needed,
                Slice,
                need_more_err
            };

            let input = $input;
            let substr_length = $substr.len();
            let mut index = 0;
            let mut result: Option<IResult<_, _>> = None;

            while let Some(next_index) = input.slice(index..).find_substring($substr) {
                match $submac!(input.slice(index + next_index + substr_length..), $($args)*) {
                    Ok(_) => {
                        let separator = if $consume {
                            index + next_index + substr_length
                        } else {
                            index + next_index
                        };

                        result = Some(Ok((input.slice(separator..), input.slice(0..separator))));

                        break;
                    },

                    _ => {
                        index += next_index + 1;
                    }
                }
            }

            if let Some(result) = result {
                result
            } else {
                need_more_err(input, Needed::Unknown, ErrorKind::Custom(42u32))
            }
        }
    );

    ($input:expr, $substr:expr, $submac:ident!( $($args:tt)* )) => (
        take_until_terminated!(_ $input, $substr, false, $submac!($($args)*));
    );

    ($input:expr, $substr:expr, $f:expr) => (
        take_until_terminated!(_ $input, $substr, false, call!($f));
    );
);

/// `take_until_terminated_and_consume(S, C)` is similar to
/// `take_until_terminated` but it consumes `S`.
#[macro_export]
macro_rules! take_until_terminated_and_consume (
    ($input:expr, $substr:expr, $submac:ident!( $($args:tt)* )) => (
        take_until_terminated!(_ $input, $substr, true, $submac!($($args)*));
    );

    ($input:expr, $substr:expr, $f:expr) => (
        take_until_terminated!(_ $input, $substr, true, call!($f));
    );
);

/// `fold_into_vector_many0!(I -> IResult<I,O>, R) => I -> IResult<I, R>`
/// is a wrapper around `fold_many0!` specifically designed for vectors.
///
/// This is strictly equivalent to `fold_many0!(submacro!(…),
/// Vec::new(), fold_into_vector)` but it shrinks the capacity of the
/// vector to fit the current length.
///
/// # Examples
///
/// ```
/// #[macro_use] extern crate nom;
/// #[macro_use] extern crate gutenberg_post_parser;
///
/// # fn main() {
/// named!(
///     test<Vec<&[u8]>>,
///     fold_into_vector_many0!(
///         tag!("abc"),
///         Vec::new()
///     )
/// );
///
/// if let Ok((_, vector)) = test(b"abcabcabc") {
///     assert_eq!(vector.capacity(), vector.len());
/// }
/// # }
/// ```
#[macro_export]
macro_rules! fold_into_vector_many0(
    ($input:expr, $submacro:ident!($($arguments:tt)*), $init:expr) => (
        {
            let result = fold_many0!(
                $input,
                $submacro!($($arguments)*),
                $init,
                $crate::combinators::fold_into_vector
            );

            if let Ok((remaining, mut output)) = result {
                output.shrink_to_fit();

                Ok((remaining, output))
            } else {
                result
            }
        }
    );

    ($input:expr, $function:expr, $init:expr) => (
        fold_many0!($input, call!($function), $init);
    );
);

/// Helper to fold an item into a vector.
pub fn fold_into_vector<I>(mut accumulator: Vec<I>, item: I) -> Vec<I> {
    accumulator.push(item);

    accumulator
}

/// Check whether a character is in the set of alphanumeric extended
/// characters, i.e. `[a-z0-9_-]`.
pub(crate) fn is_alphanumeric_extended(chr: u8) -> bool {
    (chr >= 0x61 && chr <= 0x7a) || (chr >= 0x30 && chr <= 0x39) || chr == b'_' || chr == b'-'
}

/// The `id` combinator consumes the entire given input as the output.
pub(crate) fn id(input: Input) -> IResult<Input, Input> {
    Ok((&b""[..], input))
}

#[cfg(test)]
mod tests {
    #[test]
    fn test_take_until_terminated_ok() {
        named!(
            parser,
            take_until_terminated_and_consume!(
                "d",
                tag!("c")
            )
        );

        let input = &b"abcdcba"[..];
        let output: ::nom::IResult<_, _> = Ok((&b"cba"[..], &b"abcd"[..]));

        assert_eq!(parser(input), output);
    }

    #[test]
    fn test_take_until_terminated_ok_at_position_0() {
        named!(
            parser,
            take_until_terminated_and_consume!(
                "a",
                tag!("b")
            )
        );

        let input = &b"abcdcba"[..];
        let output = Ok((&b"bcdcba"[..], &b"a"[..]));

        assert_eq!(parser(input), output);
    }

    #[test]
    fn test_take_until_terminated_ok_at_position_eof_minus_one() {
        named!(
            parser,
            take_until_terminated_and_consume!(
                "b",
                tag!("a")
            )
        );

        let input = &b"abcdcba"[..];
        let output = Ok((&b"a"[..], &b"abcdcb"[..]));

        assert_eq!(parser(input), output);
    }

    #[test]
    fn test_take_until_terminated_ok_with_multiple_substring() {
        named!(
            parser,
            take_until_terminated_and_consume!(
                "c",
                tag!("b")
            )
        );

        let input = &b"abcdcba"[..];
        let output = Ok((&b"ba"[..], &b"abcdc"[..]));

        assert_eq!(parser(input), output);
    }

    #[test]
    fn test_take_until_terminated_error() {
        named!(
            parser,
            take_until_terminated_and_consume!(
                "a",
                tag!("z")
            )
        );

        use ::nom::{ErrorKind, Needed, need_more_err};

        let input = &b"abcdcba"[..];
        let output = need_more_err(input, Needed::Unknown, ErrorKind::Custom(42u32));

        assert_eq!(parser(input), output);
    }

    #[test]
    fn test_take_until_terminated_optional() {
        named!(
            parser<&[u8], Option<&[u8]>>,
            opt!(
                complete!(
                    take_until_terminated_and_consume!(
                        "a",
                        tag!("z")
                    )
                )
            )
        );

        let input = &b"abcdcba"[..];
        let output = Ok((input, None));

        assert_eq!(parser(input), output);
    }
}