Skip to content

Commit ab608ab

Browse files
authored
fix(cpp): require word boundary before numeric literals (#4442)
The floating-point literal pattern lacked a `\b` at the start of its digit-leading branches, so digits embedded in an identifier were matched as numbers. For example `a1.foo()` highlighted `1.f` as a floating-point literal instead of leaving `a1` as an identifier. Add `\b` to the decimal, exponent-only, and hexadecimal digit-leading branches (mirroring the integer literal pattern). Dot-leading forms such as `.5` are intentionally left untouched so they keep highlighting. Closes #4231
1 parent b353518 commit ab608ab

4 files changed

Lines changed: 13 additions & 3 deletions

File tree

CHANGES.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ Core Grammars:
44

55
- fix(lisp) preserve highlighting after quoted multiplication expressions [arturict][]
66
- fix(rust) recognize `\\` and `\"` char-literal escapes so highlighting doesn't leak, issue #4351 [Sarath Francis][]
7+
- fix(cpp) require a word boundary before numeric literals so digits inside identifiers aren't highlighted as numbers, issue #4231 [Mark Xian][]
78

89
Documentation:
910

@@ -16,6 +17,7 @@ CONTRIBUTORS
1617
[Dhruv Maniya]: https://github.com/iamdhrv
1718
[Elastic]: https://github.com/elastic
1819
[Sarath Francis]: https://github.com/sarathfrancis90
20+
[Mark Xian]: https://github.com/xianjianlf2
1921

2022

2123
## Version 11.11.3

src/languages/cpp.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -57,12 +57,12 @@ export default function(hljs) {
5757
"[+-]?(?:" // Leading sign.
5858
// Decimal.
5959
+ "(?:"
60-
+"[0-9](?:'?[0-9])*\\.(?:[0-9](?:'?[0-9])*)?"
60+
+ "\\b[0-9](?:'?[0-9])*\\.(?:[0-9](?:'?[0-9])*)?"
6161
+ "|\\.[0-9](?:'?[0-9])*"
6262
+ ")(?:[Ee][+-]?[0-9](?:'?[0-9])*)?"
63-
+ "|[0-9](?:'?[0-9])*[Ee][+-]?[0-9](?:'?[0-9])*"
63+
+ "|\\b[0-9](?:'?[0-9])*[Ee][+-]?[0-9](?:'?[0-9])*"
6464
// Hexadecimal.
65-
+ "|0[Xx](?:"
65+
+ "|\\b0[Xx](?:"
6666
+"[0-9A-Fa-f](?:'?[0-9A-Fa-f])*(?:\\.(?:[0-9A-Fa-f](?:'?[0-9A-Fa-f])*)?)?"
6767
+ "|\\.[0-9A-Fa-f](?:'?[0-9A-Fa-f])*"
6868
+ ")[Pp][+-]?[0-9](?:'?[0-9])*"

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,3 +53,7 @@
5353
<span class="hljs-number">9&#x27;0UZ</span> <span class="hljs-number">9&#x27;0Uz</span> <span class="hljs-number">9&#x27;0uZ</span> <span class="hljs-number">9&#x27;0uz</span>
5454

5555
<span class="hljs-type">char</span> word[] = { <span class="hljs-string">&#x27;3&#x27;</span>, <span class="hljs-string">&#x27;\0&#x27;</span> }; <span class="hljs-comment">// Make sure digit separators don&#x27;t mess up chars.</span>
56+
57+
<span class="hljs-comment">/* Digits inside identifiers must not be highlighted as numbers. */</span>
58+
a1.<span class="hljs-built_in">foo</span>()
59+
<span class="hljs-type">int</span> a1 = b2.<span class="hljs-built_in">size</span>();

test/markup/cpp/number-literals.txt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,3 +53,7 @@
5353
9'0UZ 9'0Uz 9'0uZ 9'0uz
5454

5555
char word[] = { '3', '\0' }; // Make sure digit separators don't mess up chars.
56+
57+
/* Digits inside identifiers must not be highlighted as numbers. */
58+
a1.foo()
59+
int a1 = b2.size();

0 commit comments

Comments
 (0)