Skip to content

Commit 8f40dc0

Browse files
glebmxzyfer
authored andcommitted
Optimize line_begin/end search in handle_error
There is no need to advance by UTF-8 code points when searching for an ASCII character, because UTF-8 is a prefix-free encoding.
1 parent 930857c commit 8f40dc0

File tree

1 file changed

+10
-10
lines changed

1 file changed

+10
-10
lines changed

src/sass_context.cpp

+10-10
Original file line numberDiff line numberDiff line change
@@ -67,23 +67,23 @@ namespace Sass {
6767
}
6868

6969
// now create the code trace (ToDo: maybe have util functions?)
70-
if (e.pstate.line != std::string::npos && e.pstate.column != std::string::npos) {
70+
if (e.pstate.line != std::string::npos &&
71+
e.pstate.column != std::string::npos &&
72+
e.pstate.src != nullptr) {
7173
size_t lines = e.pstate.line;
72-
const char* line_beg = e.pstate.src;
7374
// scan through src until target line
7475
// move line_beg pointer to line start
75-
while (line_beg && *line_beg && lines != 0) {
76+
const char* line_beg;
77+
for (line_beg = e.pstate.src; *line_beg != '\0'; ++line_beg) {
78+
if (lines == 0) break;
7679
if (*line_beg == '\n') --lines;
77-
utf8::unchecked::next(line_beg);
7880
}
79-
const char* line_end = line_beg;
8081
// move line_end before next newline character
81-
while (line_end && *line_end && *line_end != '\n') {
82-
if (*line_end == '\n') break;
83-
if (*line_end == '\r') break;
84-
utf8::unchecked::next(line_end);
82+
const char* line_end;
83+
for (line_end = line_beg; *line_end != '\0'; ++line_end) {
84+
if (*line_end == '\n' || *line_end == '\r') break;
8585
}
86-
if (line_end && *line_end != 0) ++ line_end;
86+
if (*line_end != '\0') ++line_end;
8787
size_t line_len = line_end - line_beg;
8888
size_t move_in = 0; size_t shorten = 0;
8989
size_t left_chars = 42; size_t max_chars = 76;

0 commit comments

Comments
 (0)