Module: PlatformosCheck::PositionHelper
- Included in:
- HtmlNode, LanguageServer::CodeActionEngine, LanguageServer::CompletionContext, LanguageServer::DocumentLinkProvider, Offense, Position, StrictPosition
- Defined in:
- lib/platformos_check/position_helper.rb
Instance Method Summary collapse
- #bounded(a, x, b) ⇒ Object
-
#from_index_to_row_column(content, index) ⇒ Object
def from_row_column_to_index(content, row, col) return 0 unless content.is_a?(String) && !content.empty? return 0 unless row.is_a?(Integer) && col.is_a?(Integer) i = 0 safe_row = bounded(0, row, content.count(“n”)) charpos = -1 charpos = content.index(“n”, charpos + 1) while i < safe_row && (i = 1) && charpos result = charpos ? charpos 1 : 0 next_line = content.index(“n”, result) upper_bound = next_line ? next_line : content.size - 1 bounded(result, result + col, upper_bound) end.
-
#from_row_column_to_index(content, row, col) ⇒ Object
Apparently this old implementation is 2x slower (with benchmark/ips), so dropping with the following one…
Instance Method Details
#bounded(a, x, b) ⇒ Object
50 51 52 53 54 55 |
# File 'lib/platformos_check/position_helper.rb', line 50 def bounded(a, x, b) return a if x < a return b if x > b x end |
#from_index_to_row_column(content, index) ⇒ Object
def from_row_column_to_index(content, row, col)
return 0 unless content.is_a?(String) && !content.empty?
return 0 unless row.is_a?(Integer) && col.is_a?(Integer)
i = 0
safe_row = bounded(0, row, content.count("\n"))
charpos = -1
charpos = content.index("\n", charpos + 1) while i < safe_row && (i += 1) && charpos
result = charpos ? charpos + 1 : 0
next_line = content.index("\n", result)
upper_bound = next_line ? next_line : content.size - 1
bounded(result, result + col, upper_bound)
end
38 39 40 41 42 43 44 45 46 47 48 |
# File 'lib/platformos_check/position_helper.rb', line 38 def from_index_to_row_column(content, index) return [0, 0] unless content.is_a?(String) && !content.empty? return [0, 0] unless index.is_a?(Integer) safe_index = bounded(0, index, content.size - 1) content_up_to_index = content[0...safe_index] row = content_up_to_index.count("\n") col = 0 col += 1 while (safe_index -= 1) && safe_index >= 0 && content[safe_index] != "\n" [row, col] end |
#from_row_column_to_index(content, row, col) ⇒ Object
Apparently this old implementation is 2x slower (with benchmark/ips), so dropping with the following one… It’s ugly af but this shit runs 100K+ times in one platformos-check so it gotta go fast!
12 13 14 15 16 17 18 19 20 21 22 23 |
# File 'lib/platformos_check/position_helper.rb', line 12 def from_row_column_to_index(content, row, col) return 0 unless content.is_a?(String) && !content.empty? return 0 unless row.is_a?(Integer) && col.is_a?(Integer) i = 0 safe_row = bounded(0, row, content.count("\n")) scanner = StringScanner.new(content) scanner.scan_until(/\n/) while i < safe_row && (i += 1) result = scanner.charpos || 0 scanner.scan_until(/\n|\z/) bounded(result, result + col, scanner.pre_match.size) end |