Skip to content

Commit f995957

Browse files
authored
Merge pull request #2541 from mgreter/bugfix/sourcemap/unicode
Make sourcemaps fully unicode aware
2 parents e993a93 + e0563da commit f995957

File tree

3 files changed

+22
-5
lines changed

3 files changed

+22
-5
lines changed

src/emitter.cpp

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,11 @@ namespace Sass {
103103
// prepend some text or token to the buffer
104104
void Emitter::prepend_string(const std::string& text)
105105
{
106-
wbuf.smap.prepend(Offset(text));
106+
// do not adjust mappings for utf8 bom
107+
// seems they are not counted in any UA
108+
if (text.compare("\xEF\xBB\xBF") != 0) {
109+
wbuf.smap.prepend(Offset(text));
110+
}
107111
wbuf.buffer = text + wbuf.buffer;
108112
}
109113

src/position.cpp

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,6 @@ namespace Sass {
3737

3838
// increase offset by given string (mostly called by lexer)
3939
// increase line counter and count columns on the last line
40-
// ToDo: make the col count utf8 aware
4140
Offset Offset::add(const char* begin, const char* end)
4241
{
4342
if (end == 0) return *this;
@@ -47,9 +46,23 @@ namespace Sass {
4746
// start new line
4847
column = 0;
4948
} else {
50-
++ column;
49+
// do not count any utf8 continuation bytes
50+
// https://stackoverflow.com/a/9356203/1550314
51+
// https://en.wikipedia.org/wiki/UTF-8#Description
52+
unsigned char chr = *begin;
53+
// skip over 10xxxxxx
54+
// is 1st bit not set
55+
if ((chr & 128) == 0) {
56+
// regular ascii char
57+
column += 1;
58+
}
59+
// is 2nd bit not set
60+
else if ((chr & 64) == 0) {
61+
// first utf8 byte
62+
column += 1;
63+
}
5164
}
52-
++begin;
65+
++ begin;
5366
}
5467
return *this;
5568
}

src/source_map.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -136,7 +136,7 @@ namespace Sass {
136136
}
137137
}
138138
}
139-
// will adjust the offset
139+
// adjust the buffer offset
140140
prepend(Offset(out.buffer));
141141
// now add the new mappings
142142
VECTOR_UNSHIFT(mappings, out.smap.mappings);

0 commit comments

Comments
 (0)