Implement Add and AddAssign to Position, fix bug in Position::is_beginning_of_line
This commit is contained in:
parent
cc90b5ffab
commit
f8a7ada2a2
28
src/token.rs
28
src/token.rs
@ -9,6 +9,7 @@ use crate::stdlib::{
|
|||||||
char, fmt, format,
|
char, fmt, format,
|
||||||
iter::Peekable,
|
iter::Peekable,
|
||||||
num::NonZeroUsize,
|
num::NonZeroUsize,
|
||||||
|
ops::{Add, AddAssign},
|
||||||
str::{Chars, FromStr},
|
str::{Chars, FromStr},
|
||||||
string::{String, ToString},
|
string::{String, ToString},
|
||||||
};
|
};
|
||||||
@ -120,7 +121,7 @@ impl Position {
|
|||||||
/// Is this [`Position`] at the beginning of a line?
|
/// Is this [`Position`] at the beginning of a line?
|
||||||
#[inline(always)]
|
#[inline(always)]
|
||||||
pub fn is_beginning_of_line(self) -> bool {
|
pub fn is_beginning_of_line(self) -> bool {
|
||||||
self.line == 0 && !self.is_none()
|
self.pos == 0 && !self.is_none()
|
||||||
}
|
}
|
||||||
/// Is there no [`Position`]?
|
/// Is there no [`Position`]?
|
||||||
#[inline(always)]
|
#[inline(always)]
|
||||||
@ -154,6 +155,31 @@ impl fmt::Debug for Position {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
impl Add for Position {
|
||||||
|
type Output = Self;
|
||||||
|
|
||||||
|
fn add(self, rhs: Self) -> Self::Output {
|
||||||
|
if rhs.is_none() {
|
||||||
|
self
|
||||||
|
} else {
|
||||||
|
Self {
|
||||||
|
line: self.line + rhs.line - 1,
|
||||||
|
pos: if rhs.is_beginning_of_line() {
|
||||||
|
self.pos
|
||||||
|
} else {
|
||||||
|
self.pos + rhs.pos - 1
|
||||||
|
},
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl AddAssign for Position {
|
||||||
|
fn add_assign(&mut self, rhs: Self) {
|
||||||
|
*self = *self + rhs;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/// _(INTERNALS)_ A Rhai language token.
|
/// _(INTERNALS)_ A Rhai language token.
|
||||||
/// Exported under the `internals` feature only.
|
/// Exported under the `internals` feature only.
|
||||||
///
|
///
|
||||||
|
Loading…
Reference in New Issue
Block a user