@@ -72,24 +72,35 @@ namespace Sass {
72
72
73
73
// now create the code trace (ToDo: maybe have util functions?)
74
74
if (e.pstate .line != std::string::npos && e.pstate .column != std::string::npos) {
75
- size_t line = e.pstate .line ;
75
+ size_t lines = e.pstate .line ;
76
76
const char * line_beg = e.pstate .src ;
77
- while (line_beg && *line_beg && line) {
78
- if (*line_beg == ' \n ' ) --line;
79
- ++line_beg;
77
+ // scan through src until target line
78
+ // move line_beg pointer to line start
79
+ while (line_beg && *line_beg && lines != 0 ) {
80
+ if (*line_beg == ' \n ' ) --lines;
81
+ utf8::unchecked::next (line_beg);
80
82
}
81
83
const char * line_end = line_beg;
84
+ // move line_end before next newline character
82
85
while (line_end && *line_end && *line_end != ' \n ' ) {
83
86
if (*line_end == ' \n ' ) break ;
84
87
if (*line_end == ' \r ' ) break ;
85
- line_end++;
88
+ utf8::unchecked::next ( line_end);
86
89
}
87
- size_t max_left = 42 ; size_t max_right = 78 ;
88
- size_t move_in = e.pstate .column > max_left ? e.pstate .column - max_left : 0 ;
89
- size_t shorten = (line_end - line_beg) - move_in > max_right ?
90
- (line_end - line_beg) - move_in - max_right : 0 ;
91
- msg_stream << " >> " << std::string (line_beg + move_in, line_end - shorten) << " \n " ;
92
- msg_stream << " " << std::string (e.pstate .column - move_in, ' -' ) << " ^\n " ;
90
+ if (line_end && *line_end != 0 ) ++ line_end;
91
+ size_t line_len = line_end - line_beg;
92
+ size_t move_in = 0 ; size_t shorten = 0 ;
93
+ size_t left_chars = 42 ; size_t max_chars = 76 ;
94
+ // reported excerpt should not exceed `max_chars` chars
95
+ if (e.pstate .column > line_len) left_chars = e.pstate .column ;
96
+ if (e.pstate .column > left_chars) move_in = e.pstate .column - left_chars;
97
+ if (line_len > max_chars + move_in) shorten = line_len - move_in - max_chars;
98
+ utf8::advance (line_beg, move_in, line_end);
99
+ utf8::retreat (line_end, shorten, line_beg);
100
+ std::string sanitized; std::string marker (e.pstate .column - move_in, ' -' );
101
+ utf8::replace_invalid (line_beg, line_end, std::back_inserter (sanitized));
102
+ msg_stream << " >> " << sanitized << " \n " ;
103
+ msg_stream << " " << marker << " ^\n " ;
93
104
}
94
105
95
106
JsonNode* json_err = json_mkobject ();
0 commit comments