Skip to content

Commit

Permalink
fix(c) fixed hex decimal numbers (#4094)
Browse files Browse the repository at this point in the history
  • Loading branch information
Dxuian authored Aug 25, 2024
1 parent 19819d5 commit 07ee351
Show file tree
Hide file tree
Showing 4 changed files with 101 additions and 6 deletions.
2 changes: 2 additions & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ Core Grammars:
- enh(erlang) OTP25/27 maybe statement [nixxquality][]
- enh(dart) Support digit-separators in number literals [Sam Rawlins][]
- enh(csharp) add Contextual keywords `file`, `args`, `dynamic`, `record`, `required` and `scoped` [Alvin Joy][]
- fix(c) - Fixed hex numbers with decimals [Dxuian]
- fix(ruby) - fix `|=` operator false positives (as block arguments) [Aboobacker MK]

New Grammars:
Expand Down Expand Up @@ -42,6 +43,7 @@ CONTRIBUTORS
[nixxquality]: https://github.com/nixxquality
[srawlins]: https://github.com/srawlins
[Alvin Joy]: https://github.com/alvinsjoy
[Dxuian]:https://github.com/Dxuian
[Aboobacker MK]: https://github.com/tachyons
[Imken]: https://github.com/immccn123

Expand Down
13 changes: 7 additions & 6 deletions src/languages/c.js
Original file line number Diff line number Diff line change
Expand Up @@ -57,13 +57,14 @@ export default function(hljs) {
const NUMBERS = {
className: 'number',
variants: [
{ begin: '\\b(0b[01\']+)' },
{ begin: '(-?)\\b([\\d\']+(\\.[\\d\']*)?|\\.[\\d\']+)((ll|LL|l|L)(u|U)?|(u|U)(ll|LL|l|L)?|f|F|b|B)' },
{ begin: '(-?)(\\b0[xX][a-fA-F0-9\']+|(\\b[\\d\']+(\\.[\\d\']*)?|\\.[\\d\']+)([eE][-+]?[\\d\']+)?)' }
],
{ match: /\b(0b[01']+)/ },
{ match: /(-?)\b([\d']+(\.[\d']*)?|\.[\d']+)((ll|LL|l|L)(u|U)?|(u|U)(ll|LL|l|L)?|f|F|b|B)/ },
{ match: /(-?)\b(0[xX][a-fA-F0-9]+(?:'[a-fA-F0-9]+)*(?:\.[a-fA-F0-9]*(?:'[a-fA-F0-9]*)*)?(?:[pP][-+]?[0-9]+)?(l|L)?(u|U)?)/ },
{ match: /(-?)\b\d+(?:'\d+)*(?:\.\d*(?:'\d*)*)?(?:[eE][-+]?\d+)?/ }
],
relevance: 0
};

};
const PREPROCESSOR = {
className: 'meta',
begin: /#\s*[a-z]+\b/,
Expand Down
46 changes: 46 additions & 0 deletions test/markup/c/number-literals.expect.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
<span class="hljs-comment">/* Floating-point literals. */</span>
<span class="hljs-comment">// Decimal.</span>
<span class="hljs-number">1.</span>
+<span class="hljs-number">12.</span>
<span class="hljs-number">1.2</span>
<span class="hljs-number">1234e56</span>

<span class="hljs-comment">// Hexadecimal.</span>
<span class="hljs-number">0x1p2</span>
+<span class="hljs-number">0x1.p2</span>
<span class="hljs-number">-0X1A.P2</span>
<span class="hljs-number">0x1.Ap2</span>

<span class="hljs-comment">// Literal suffixes.</span>
<span class="hljs-number">1.F</span>

<span class="hljs-comment">/* Integer literals. */</span>
<span class="hljs-comment">// Binary.</span>
+<span class="hljs-number">0b1</span> <span class="hljs-comment">// Note: Not standard C, but valid in some compilers</span>
<span class="hljs-number">0B</span>01 <span class="hljs-comment">// Note: Not standard C, but valid in some compilers</span>

<span class="hljs-comment">// Hexadecimal.</span>
+<span class="hljs-number">0x1</span>
<span class="hljs-number">0X1A</span>

<span class="hljs-comment">// Octal.</span>
+<span class="hljs-number">01</span>
<span class="hljs-number">012</span>

<span class="hljs-comment">// Decimal.</span>
<span class="hljs-number">0</span>
+<span class="hljs-number">1</span>
<span class="hljs-number">12</span>

<span class="hljs-comment">// Literal suffixes.</span>
<span class="hljs-number">0X2L</span>
<span class="hljs-number">0x2l</span>
<span class="hljs-number">03LL</span>
<span class="hljs-number">03ll</span>
<span class="hljs-number">4UL</span> <span class="hljs-number">4Ul</span> <span class="hljs-number">4uL</span> <span class="hljs-number">4ul</span>
<span class="hljs-number">5LU</span> <span class="hljs-number">5Lu</span> <span class="hljs-number">5lU</span> <span class="hljs-number">5lu</span>
<span class="hljs-number">6ULL</span> <span class="hljs-number">6Ull</span> <span class="hljs-number">6uLL</span> <span class="hljs-number">6ull</span>
<span class="hljs-number">7LLU</span> <span class="hljs-number">7LLu</span> <span class="hljs-number">7llU</span> <span class="hljs-number">7llu</span>

<span class="hljs-comment">// Character array.</span>
<span class="hljs-type">char</span> word[] = { <span class="hljs-string">&#x27;3&#x27;</span>, <span class="hljs-string">&#x27;\0&#x27;</span> };
46 changes: 46 additions & 0 deletions test/markup/c/number-literals.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
/* Floating-point literals. */
// Decimal.
1.
+12.
1.2
1234e56

// Hexadecimal.
0x1p2
+0x1.p2
-0X1A.P2
0x1.Ap2

// Literal suffixes.
1.F

/* Integer literals. */
// Binary.
+0b1 // Note: Not standard C, but valid in some compilers
0B01 // Note: Not standard C, but valid in some compilers

// Hexadecimal.
+0x1
0X1A

// Octal.
+01
012

// Decimal.
0
+1
12

// Literal suffixes.
0X2L
0x2l
03LL
03ll
4UL 4Ul 4uL 4ul
5LU 5Lu 5lU 5lu
6ULL 6Ull 6uLL 6ull
7LLU 7LLu 7llU 7llu

// Character array.
char word[] = { '3', '\0' };

0 comments on commit 07ee351

Please sign in to comment.