-
Notifications
You must be signed in to change notification settings - Fork 154
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
Add character APIs for locations #1809
Conversation
lib/prism/parse_result.rb
Outdated
# A lazily-computed cache of byte offset => character offset mappings. | ||
def character_offsets | ||
@character_offsets ||= { 0 => 0 } | ||
end |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm not sure this would pay off because it could become pretty large for a big file and consume N bytes for every byte in the source.
N seems to be about 60 for integer entries in a Hash:
ruby --disable-gems -e 'h={}; 1.times {|i| h[i]=i}'
12MB
ruby --disable-gems -e 'h={}; 10_000_000.times {|i| h[i]=i}'
588MB
irb(main):003:0> 576 * 1024**2 / 10_000_000.0
=> 60.3979776
I think it's better to leave this uncached for now, or do one cache entry per line.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Fair point!
633330b
to
e5bf7e7
Compare
eb14cf5
to
f005fa0
Compare
lib/prism/parse_result.rb
Outdated
end | ||
|
||
# Return the character offset for the given byte offset. | ||
def character(byte_offset) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
def character(byte_offset) | |
def character_offset(byte_offset) |
It seems clearer, (e.g. it doesn't return a character) and given this is a public method, it seems even more important to be clear.
f005fa0
to
b6f37ae
Compare
Fixes #1788 by introducing:
Location#start_character_offset
Location#end_character_offset
Location#start_character_column
Location#end_character_column