Skip to content

Commit e6ac0a2

Browse files
committed
Make generating source excerpt on error unicode aware
1 parent 2a8a940 commit e6ac0a2

File tree

2 files changed

+29
-11
lines changed

2 files changed

+29
-11
lines changed

src/parser.cpp

Lines changed: 25 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2588,23 +2588,37 @@ namespace Sass {
25882588
void Parser::css_error(const std::string& msg, const std::string& prefix, const std::string& middle)
25892589
{
25902590
int max_len = 18;
2591+
const char* end = this->end;
2592+
while (*end != 0) ++ end;
25912593
const char* pos = peek < optional_spaces >();
25922594

2593-
const char* last_pos(pos - 1);
2595+
const char* last_pos(pos);
2596+
if (last_pos > source) {
2597+
utf8::prior(last_pos, source);
2598+
}
25942599
// backup position to last significant char
2595-
while ((!*last_pos || Prelexer::is_space(*last_pos)) && last_pos > source) -- last_pos;
2600+
while (last_pos > source && last_pos < end) {
2601+
if (!Prelexer::is_space(*last_pos)) break;
2602+
utf8::prior(last_pos, source);
2603+
}
25962604

25972605
bool ellipsis_left = false;
2598-
const char* pos_left(last_pos + 1);
2599-
const char* end_left(last_pos + 1);
2606+
const char* pos_left(last_pos);
2607+
const char* end_left(last_pos);
2608+
2609+
utf8::next(pos_left, end);
2610+
utf8::next(end_left, end);
26002611
while (pos_left > source) {
2601-
if (end_left - pos_left >= max_len) {
2602-
ellipsis_left = *(pos_left-1) != '\n' &&
2603-
*(pos_left-1) != '\r';
2612+
if (utf8::distance(pos_left, end_left) >= max_len) {
2613+
utf8::prior(pos_left, source);
2614+
ellipsis_left = *(pos_left) != '\n' &&
2615+
*(pos_left) != '\r';
2616+
utf8::next(pos_left, end);
26042617
break;
26052618
}
26062619

2607-
const char* prev = pos_left - 1;
2620+
const char* prev = pos_left;
2621+
utf8::prior(prev, source);
26082622
if (*prev == '\r') break;
26092623
if (*prev == '\n') break;
26102624
pos_left = prev;
@@ -2616,15 +2630,15 @@ namespace Sass {
26162630
bool ellipsis_right = false;
26172631
const char* end_right(pos);
26182632
const char* pos_right(pos);
2619-
while (*end_right != 0) {
2620-
if (end_right - pos_right > max_len) {
2633+
while (end_right < end) {
2634+
if (utf8::distance(pos_right, end_right) > max_len) {
26212635
ellipsis_left = *(pos_right) != '\n' &&
26222636
*(pos_right) != '\r';
26232637
break;
26242638
}
26252639
if (*end_right == '\r') break;
26262640
if (*end_right == '\n') break;
2627-
++ end_right;
2641+
utf8::next(end_right, end);
26282642
}
26292643
// if (*end_right == 0) end_right ++;
26302644

src/utf8_string.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,8 @@ namespace Sass {
6969
}
7070
}
7171

72+
#ifdef _WIN32
73+
7274
// utf16 functions
7375
using std::wstring;
7476

@@ -94,5 +96,7 @@ namespace Sass {
9496
return utf16;
9597
}
9698

99+
#endif
100+
97101
}
98102
}

0 commit comments

Comments
 (0)