Skip to content
This repository was archived by the owner on Jul 27, 2024. It is now read-only.

Commit c01300e

Browse files
committed
Revert PositionHelper refactor. Looks like it crashes on some themes.
1 parent 1092737 commit c01300e

File tree

1 file changed

+23
-6
lines changed

1 file changed

+23
-6
lines changed

lib/theme_check/position_helper.rb

Lines changed: 23 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,19 +3,36 @@
33

44
module ThemeCheck
55
module PositionHelper
6+
# Apparently this old implementation is 2x slower (with benchmark/ips),
7+
# so dropping with the following one... It's ugly af but
8+
# this shit runs 100K+ times in one theme-check so it gotta go
9+
# fast!
10+
#
611
def from_row_column_to_index(content, row, col)
712
return 0 unless content.is_a?(String) && !content.empty?
813
return 0 unless row.is_a?(Integer) && col.is_a?(Integer)
914
i = 0
1015
safe_row = bounded(0, row, content.count("\n"))
11-
charpos = -1
12-
charpos = content.index("\n", charpos + 1) while i < safe_row && (i += 1) && charpos
13-
result = charpos ? charpos + 1 : 0
14-
next_line = content.index("\n", result)
15-
upper_bound = next_line ? next_line : content.size - 1
16-
bounded(result, result + col, upper_bound)
16+
scanner = StringScanner.new(content)
17+
scanner.scan_until(/\n/) while i < safe_row && (i += 1)
18+
result = scanner.charpos || 0
19+
scanner.scan_until(/\n|\z/)
20+
bounded(result, result + col, scanner.pre_match.size)
1721
end
1822

23+
# def from_row_column_to_index(content, row, col)
24+
# return 0 unless content.is_a?(String) && !content.empty?
25+
# return 0 unless row.is_a?(Integer) && col.is_a?(Integer)
26+
# i = 0
27+
# safe_row = bounded(0, row, content.count("\n"))
28+
# charpos = -1
29+
# charpos = content.index("\n", charpos + 1) while i < safe_row && (i += 1) && charpos
30+
# result = charpos ? charpos + 1 : 0
31+
# next_line = content.index("\n", result)
32+
# upper_bound = next_line ? next_line : content.size - 1
33+
# bounded(result, result + col, upper_bound)
34+
# end
35+
1936
def from_index_to_row_column(content, index)
2037
return [0, 0] unless content.is_a?(String) && !content.empty?
2138
return [0, 0] unless index.is_a?(Integer)

0 commit comments

Comments
 (0)