Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement LocatedSpan::get_line(). #66

Merged
merged 10 commits into from
Oct 18, 2020
32 changes: 15 additions & 17 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -257,17 +257,24 @@ impl<T: AsBytes, X> LocatedSpan<T, X> {
&self.fragment
}

fn get_columns_and_bytes_before(&self) -> (usize, &[u8]) {
fn get_unoffsetted_slice(&self) -> &[u8] {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmm, the name doesn't make it clear what this does, but I can't think of a better one. :/
Also some comment to explain what it does would be good

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, I considered get_original_size, as that is pretty much the intent. But I didn't, since it has no way of reconstructing the original length. So unoffsetted is what it actually does, it undos the offset.

As for the larger question about a possibly missing trailer of get_line(); Yes, I was a bit worried about that myself, but decided that it's not a big problem in my main use-case of reporting parse errors. In cases I can think of that involves marking an interval (and not just a position) of a line, I think I would have two LocatedSpans to combine, where each of them would (probably) be "the rest of input from a starting point".

But I agree that this should be explained somehow, both in i a comment at get_unoffsetted_slice and in the docstring of get_line. I'll try to write something.

let self_bytes = self.fragment.as_bytes();
let self_ptr = self_bytes.as_ptr();
let before_self = unsafe {
unsafe {
assert!(
self.offset <= isize::max_value() as usize,
"offset is too big"
);
let orig_input_ptr = self_ptr.offset(-(self.offset as isize));
slice::from_raw_parts(orig_input_ptr, self.offset)
};
slice::from_raw_parts(
orig_input_ptr,
self.offset + self_bytes.len(),
)
}
}

fn get_columns_and_bytes_before(&self) -> (usize, &[u8]) {
let before_self = &self.get_unoffsetted_slice()[..self.offset];

let column = match memchr::memrchr(b'\n', before_self) {
None => self.offset + 1,
Expand Down Expand Up @@ -302,20 +309,11 @@ impl<T: AsBytes, X> LocatedSpan<T, X> {
/// # }
/// ```
pub fn get_line(&self) -> &[u8] {
let self_bytes = self.fragment.as_bytes();
let self_ptr = self_bytes.as_ptr();
let offset = self.get_column() - 1;
let the_line = unsafe {
assert!(
offset <= isize::max_value() as usize,
"offset is too big"
);
let line_start_ptr = self_ptr.offset(-(offset as isize));
slice::from_raw_parts(line_start_ptr, offset + self_bytes.len())
};
match memchr::memchr(b'\n', the_line) {
let column0 = self.get_column() - 1;
let the_line = &self.get_unoffsetted_slice()[self.offset - column0..];
match memchr::memchr(b'\n', &the_line[column0..]) {
None => the_line,
Some(pos) => &the_line[..pos],
Some(pos) => &the_line[..column0 + pos],
}
}

Expand Down