|
3 | 3 |
|
4 | 4 | module ThemeCheck |
5 | 5 | 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 | + # |
6 | 11 | def from_row_column_to_index(content, row, col) |
7 | 12 | return 0 unless content.is_a?(String) && !content.empty? |
8 | 13 | return 0 unless row.is_a?(Integer) && col.is_a?(Integer) |
9 | 14 | i = 0 |
10 | 15 | 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) |
17 | 21 | end |
18 | 22 |
|
| 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 | + |
19 | 36 | def from_index_to_row_column(content, index) |
20 | 37 | return [0, 0] unless content.is_a?(String) && !content.empty? |
21 | 38 | return [0, 0] unless index.is_a?(Integer) |
|
0 commit comments