Skip to content

Commit 2734ca6

Browse files
davidbenmarcoscaceres
authored andcommitted
Fix(cpp): Fix C++ string handling. (#1825)
See https://bugs.chromium.org/p/gerrit/issues/detail?id=7748 and the grammar here: http://en.cppreference.com/w/cpp/language/string_literal u8 cannot be combined with L. Also fix the syntax for raw string literals. This doesn't support the mode with a prefix, just the basic R"( .... )" case.
1 parent 0ebdfa6 commit 2734ca6

File tree

3 files changed

+14
-11
lines changed

3 files changed

+14
-11
lines changed

src/languages/cpp.js

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,13 +15,16 @@ function(hljs) {
1515
className: 'string',
1616
variants: [
1717
{
18-
begin: '(u8?|U)?L?"', end: '"',
18+
begin: '(u8?|U|L)?"', end: '"',
1919
illegal: '\\n',
2020
contains: [hljs.BACKSLASH_ESCAPE]
2121
},
2222
{
23-
begin: '(u8?|U)?R"', end: '"',
24-
contains: [hljs.BACKSLASH_ESCAPE]
23+
// TODO: This does not handle raw string literals with prefixes. Using
24+
// a single regex with backreferences would work (note to use *?
25+
// instead of * to make it non-greedy), but the mode.terminators
26+
// computation in highlight.js breaks the counting.
27+
begin: '(u8?|U|L)?R"\\(', end: '\\)"',
2528
},
2629
{
2730
begin: '\'\\\\?.', end: '\'',

test/markup/cpp/string-literals.expect.txt

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,19 +9,19 @@
99

1010
<span class="hljs-comment">// Raw string literals (multiline)</span>
1111
<span class="hljs-keyword">auto</span> char_multi = <span class="hljs-string">R"(Hello
12-
normal
12+
"normal"
1313
muliline
1414
string.)"</span>;
1515
<span class="hljs-keyword">auto</span> utf8_multi = <span class="hljs-string">u8R"(Hello
16-
utf-8
16+
"utf-8"
1717
muliline
1818
string)"</span>;
1919
<span class="hljs-keyword">auto</span> utf16_multi = <span class="hljs-string">uR"(Hello
20-
utf-16
20+
"utf-16"
2121
muliline
2222
string)"</span>;
2323
<span class="hljs-keyword">auto</span> utf32_multi = <span class="hljs-string">UR"(Hello
24-
utf-32
24+
"utf-32"
2525
muliline
2626
string)"</span>;
2727

test/markup/cpp/string-literals.txt

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,19 +9,19 @@ auto wide_char = L"Hello wchar_t string";
99

1010
// Raw string literals (multiline)
1111
auto char_multi = R"(Hello
12-
normal
12+
"normal"
1313
muliline
1414
string.)";
1515
auto utf8_multi = u8R"(Hello
16-
utf-8
16+
"utf-8"
1717
muliline
1818
string)";
1919
auto utf16_multi = uR"(Hello
20-
utf-16
20+
"utf-16"
2121
muliline
2222
string)";
2323
auto utf32_multi = UR"(Hello
24-
utf-32
24+
"utf-32"
2525
muliline
2626
string)";
2727

0 commit comments

Comments
 (0)