Created
January 25, 2011 00:29
-
-
Save matthewd/794278 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| From e69827d6fbfa919387fbc0dbb15ba5079d2ea947 Mon Sep 17 00:00:00 2001 | |
| From: Matthew Draper <[email protected]> | |
| Date: Tue, 25 Jan 2011 10:47:02 +1030 | |
| Subject: [PATCH] Search backwards rather than forwards. | |
| Most of the time, we're looking at values around the end of what we've | |
| parsed. | |
| --- | |
| lib/parslet/source.rb | 19 ++++++++++--------- | |
| 1 files changed, 10 insertions(+), 9 deletions(-) | |
| diff --git a/lib/parslet/source.rb b/lib/parslet/source.rb | |
| index 564e07e..70bd6d0 100644 | |
| --- a/lib/parslet/source.rb | |
| +++ b/lib/parslet/source.rb | |
| @@ -55,18 +55,19 @@ class Parslet::Source | |
| def line_and_column(position=nil) | |
| pos = (position || self.pos) | |
| - eol_idx = @line_ends.index { |o| o>pos } | |
| - if eol_idx | |
| - # eol_idx points to the offset that ends the current line. | |
| - # Let's try to find the offset that starts it: | |
| - offset = eol_idx>0 && @line_ends[eol_idx-1] || 0 | |
| - return [eol_idx+1, pos-offset+1] | |
| + if @line_ends.size > 0 && @line_ends.last > pos | |
| + if bol_idx = @line_ends.rindex { |o| o<=pos } | |
| + offset = @line_ends[bol_idx] | |
| + return [bol_idx+2, pos-offset+1] | |
| + else | |
| + return [1, pos+1] | |
| + end | |
| else | |
| - # eol_idx is nil, that means that we're beyond the last line end that | |
| - # we know about. Pretend for now that we're just on the last line. | |
| + # We're beyond the last line end that we know about. Pretend for | |
| + # now that we're just on the last line. | |
| offset = @line_ends.last || 0 | |
| return [@line_ends.size+1, pos-offset+1] | |
| end | |
| end | |
| -end | |
| \ No newline at end of file | |
| +end | |
| -- | |
| 1.7.2.3 | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment