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
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
#![deny(clippy::all)]
#![deny(clippy::pedantic)]
//! This library expose `Spanned`, a struct that will wraps your input and allow you to keep track of the line number, the column number and the byte offset
//!
//! ## How to use it?
//!
//! Here is a basic example of how to create the input and how to retrieve all the informations you need.
//!
//! ```ignore
//! use nom_span::Spanned;
//!
//! type Span<'a> = Spanned<&'a str>;
//!
//! fn main() {
//!     let span = Span::new(
//!       r#"{"hello": "world 🙌"}"#,
//!       // Supporting UTF-8
//!       true
//!     );
//!
//!     assert_eq!(span.line(), 1);
//!     assert_eq!(span.col(), 1);
//!     assert_eq!(span.byte_offset(), 0);
//! }
//! ```
//!
//! You can notice that supporting UTF-8 is optional. The reason is that UTF-8 strings need to be handled in a different way than pure ASCII strings, and thus, there can be a performance gap with UTF-8 support (see the benchmark below)
//!
//! ### UTF-8 and ASCII comparison
//!
//! A UTF-8 char can be made of 1 to 4 bytes, so counting it the ASCII way would result in counting each byte of the UTF-8 char, and will result in unexpected column number:
//!
//! ```ignore
//! use nom_span::Spanned;
//!
//! type Span<'a> = Spanned<&'a str>;
//!
//! fn utf8_vs_ascii() {
//!     let utf8 = Span::new("🙌", true);
//!     let ascii = Span::new("🙌", false);
//!
//!     let utf8_after: IResult<Span<'_>, Vec<char>> = many1(anychar)(utf8);
//!     let ascii_after: IResult<Span<'_>, Vec<char>> = many1(anychar)(ascii);
//!
//!     let (utf8_after, _) = utf8_after.unwrap();
//!     let (ascii_after, _) = ascii_after.unwrap();
//!
//!     assert_eq!(utf8_after.col(), 2);
//!     assert_eq!(ascii_after.col(), 5);
//! }
//!
//! ```
//!
//! ## What about [nom_locate](https://github.com/fflorent/nom_locate)?
//!
//! I was initially using [nom_locate](https://github.com/fflorent/nom_locate), but I faced some huge performance issue while building a [json parser](https://github.com/julesguesnon/spanned-json-parser), so I decided to implement my own input. I basically cloned [nom_locate](https://github.com/fflorent/nom_locate) and modified the counting function that was causing the performance issue. So thanks a lot for this awesome crate and please go add a star to it!
//!
//! ### What's the difference with [nom_locate](https://github.com/fflorent/nom_locate)?
//!
//! [nom_locate](https://github.com/fflorent/nom_locate) is recounting all the chars of your entire input (even if you already consumed it) when you're calling `get_column`. If you're calling `get_column` every char, runtime would be: `O(N^2)`
//! With this crate, it's counting lines and columns everytime you're consuming your input. If you're calling `col` every char, runtime would be: `O(2N)`
//!
//! So if you're planning to get the column only a few times, for example, only when an error occur, it may be better to use [nom_locate](https://github.com/fflorent/nom_locate), but if you need it quite often, this crate should be better.

use bytecount::num_chars;
use memchr::Memchr;
use nom::{
    AsBytes, Compare, Err, ExtendInto, FindSubstring, FindToken, InputIter, InputLength, InputTake,
    InputTakeAtPosition, Offset, ParseTo, Slice,
};
use std::{
    ops::{RangeFrom, RangeTo},
    str::FromStr,
};

extern crate bytecount;
extern crate memchr;
extern crate nom;

/// You can wrap your input in this struct with [`Spanned::new`]
#[derive(Clone, Debug, Copy, PartialEq, Eq)]
pub struct Spanned<T> {
    data: T,
    line: usize,
    col: usize,
    offset: usize,
    handle_utf8: bool,
}

impl<T> Spanned<T> {
    pub fn new(data: T, handle_utf8: bool) -> Self {
        Self {
            data,
            line: 1,
            col: 1,
            offset: 0,
            handle_utf8,
        }
    }

    /// Get the current line number
    pub fn line(&self) -> usize {
        self.line
    }

    /// Get the current column number
    pub fn col(&self) -> usize {
        self.col
    }

    /// Get the current byte offset
    pub fn byte_offset(&self) -> usize {
        self.offset
    }

    /// Get the current data in the span
    pub fn data(&self) -> &T {
        &self.data
    }
}

impl<T> core::ops::Deref for Spanned<T> {
    type Target = T;
    fn deref(&self) -> &Self::Target {
        &self.data
    }
}

impl<T, U> core::convert::AsRef<U> for Spanned<&T>
where
    T: ?Sized + core::convert::AsRef<U>,
    U: ?Sized,
{
    fn as_ref(&self) -> &U {
        self.data.as_ref()
    }
}

impl<T> AsBytes for Spanned<T>
where
    T: AsBytes,
{
    fn as_bytes(&self) -> &[u8] {
        self.data.as_bytes()
    }
}

impl<T> Compare<T> for Spanned<T>
where
    T: Compare<T>,
{
    fn compare(&self, t: T) -> nom::CompareResult {
        self.data.compare(t)
    }

    fn compare_no_case(&self, t: T) -> nom::CompareResult {
        self.data.compare_no_case(t)
    }
}

impl<T> ExtendInto for Spanned<T>
where
    T: ExtendInto,
{
    type Item = T::Item;

    type Extender = T::Extender;

    fn new_builder(&self) -> Self::Extender {
        self.data.new_builder()
    }

    fn extend_into(&self, acc: &mut Self::Extender) {
        self.data.extend_into(acc);
    }
}

impl<T> FindSubstring<T> for Spanned<T>
where
    T: FindSubstring<T>,
{
    fn find_substring(&self, substr: T) -> Option<usize> {
        self.data.find_substring(substr)
    }
}

impl<T, Token> FindToken<Token> for Spanned<T>
where
    T: FindToken<Token>,
{
    fn find_token(&self, token: Token) -> bool {
        self.data.find_token(token)
    }
}

impl<T> InputIter for Spanned<T>
where
    T: InputIter,
{
    type Item = T::Item;

    type Iter = T::Iter;

    type IterElem = T::IterElem;

    fn iter_indices(&self) -> Self::Iter {
        self.data.iter_indices()
    }

    fn iter_elements(&self) -> Self::IterElem {
        self.data.iter_elements()
    }

    fn position<P>(&self, predicate: P) -> Option<usize>
    where
        P: Fn(Self::Item) -> bool,
    {
        self.data.position(predicate)
    }

    fn slice_index(&self, count: usize) -> Result<usize, nom::Needed> {
        self.data.slice_index(count)
    }
}

impl<T> InputLength for Spanned<T>
where
    T: InputLength,
{
    fn input_len(&self) -> usize {
        self.data.input_len()
    }
}

impl<T> InputTake for Spanned<T>
where
    Self: Slice<RangeFrom<usize>> + Slice<RangeTo<usize>>,
{
    fn take(&self, count: usize) -> Self {
        self.slice(..count)
    }

    fn take_split(&self, count: usize) -> (Self, Self) {
        (self.slice(count..), self.slice(..count))
    }
}

impl<T> InputTakeAtPosition for Spanned<T>
where
    T: InputTakeAtPosition + InputLength + InputIter,
    Self: Slice<RangeFrom<usize>> + Slice<RangeTo<usize>> + Clone,
{
    type Item = <T as InputIter>::Item;

    fn split_at_position<P, E: nom::error::ParseError<Self>>(
        &self,
        predicate: P,
    ) -> nom::IResult<Self, Self, E>
    where
        P: Fn(Self::Item) -> bool,
    {
        match self.data.position(predicate) {
            Some(n) => Ok(self.take_split(n)),
            None => Err(Err::Incomplete(nom::Needed::new(1))),
        }
    }

    fn split_at_position1<P, E: nom::error::ParseError<Self>>(
        &self,
        predicate: P,
        _e: nom::error::ErrorKind,
    ) -> nom::IResult<Self, Self, E>
    where
        P: Fn(Self::Item) -> bool,
    {
        match self.data.position(predicate) {
            Some(n) => Ok(self.take_split(n)),
            None => Err(Err::Incomplete(nom::Needed::new(1))),
        }
    }

    fn split_at_position_complete<P, E: nom::error::ParseError<Self>>(
        &self,
        predicate: P,
    ) -> nom::IResult<Self, Self, E>
    where
        P: Fn(Self::Item) -> bool,
    {
        match self.split_at_position(predicate) {
            Err(Err::Incomplete(_)) => Ok(self.take_split(self.input_len())),
            res => res,
        }
    }

    fn split_at_position1_complete<P, E: nom::error::ParseError<Self>>(
        &self,
        predicate: P,
        e: nom::error::ErrorKind,
    ) -> nom::IResult<Self, Self, E>
    where
        P: Fn(Self::Item) -> bool,
    {
        match self.data.position(predicate) {
            Some(0) => Err(Err::Error(E::from_error_kind(self.clone(), e))),
            Some(n) => Ok(self.take_split(n)),
            None => {
                if self.data.input_len() == 0 {
                    Err(Err::Error(E::from_error_kind(self.clone(), e)))
                } else {
                    Ok(self.take_split(self.input_len()))
                }
            }
        }
    }
}

impl<T> Offset for Spanned<T>
where
    T: Offset,
{
    fn offset(&self, second: &Self) -> usize {
        self.data.offset(&second.data)
    }
}

impl<T, R: FromStr> ParseTo<R> for Spanned<T>
where
    T: ParseTo<R>,
{
    fn parse_to(&self) -> Option<R> {
        self.data.parse_to()
    }
}

impl<T, R> Slice<R> for Spanned<T>
where
    T: Slice<R> + Offset + AsBytes + Slice<RangeTo<usize>>,
{
    fn slice(&self, range: R) -> Self {
        let next_data = self.data.slice(range);

        let offset = self.data.offset(&next_data);

        let old_data = self.data.slice(..offset);

        if offset == 0 {
            return Self {
                data: next_data,
                line: self.line,
                col: self.col,
                offset: self.offset,
                handle_utf8: self.handle_utf8,
            };
        }

        let new_line_iter = Memchr::new(b'\n', old_data.as_bytes());

        let mut lines_to_add = 0;
        let mut last_index = None;
        for i in new_line_iter {
            lines_to_add += 1;
            last_index = Some(i);
        }
        let last_index = last_index.map_or(0, |v| v + 1);

        let col = if self.handle_utf8 {
            num_chars(old_data.as_bytes().slice(last_index..))
        } else {
            old_data.as_bytes().len() - last_index
        };

        Self {
            data: next_data,
            line: self.line + lines_to_add,
            col: if lines_to_add == 0 {
                self.col + col
            } else {
                // When going to a new line, char starts at 1
                col + 1
            },
            offset: self.offset + offset,
            handle_utf8: self.handle_utf8,
        }
    }
}