Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ Core Grammars:
- fix(cmake) only highlight standalone numbers, not digits that begin an identifier (e.g. `3rdparty`), issue #4170 [MarkXian][]
- fix(cpp) require a word boundary before numeric literals so digits inside identifiers aren't highlighted as numbers, issue #4231 [Mark Xian][]
- fix(c) only match real `atomic_*` type names, not C11 atomic functions, issue #3837 [MarkXian][]
- fix(csharp) support digit separators in binary literals and numeric type suffixes, and stop highlighting the leading `_` of an identifier, issue #4258 [Sarath Francis][]
- enh(python) add missing builtins: `aiter` and `anext` (Python 3.10), `frozendict` and `sentinel` (Python 3.15) [Hugo van Kemenade][]

Documentation:
Expand Down
12 changes: 9 additions & 3 deletions src/languages/csharp.js
Original file line number Diff line number Diff line change
Expand Up @@ -161,12 +161,18 @@ export default function(hljs) {
literal: LITERAL_KEYWORDS
};
const TITLE_MODE = hljs.inherit(hljs.TITLE_MODE, { begin: '[a-zA-Z](\\.?\\w)*' });
// https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/builtin-types/integral-numeric-types
// https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/builtin-types/floating-point-numeric-types
// `_` separators sit between digits, and may also follow the `0x`/`0b` prefix
const DIGITS = '\\d(_*\\d)*';
const INTEGER_SUFFIX = '([uU][lL]?|[lL][uU]?)?';
const REAL_SUFFIX = '([fFdDmM]|[uU][lL]?|[lL][uU]?)?';
const NUMBERS = {
className: 'number',
variants: [
{ begin: '\\b(0b[01\']+)' },
{ begin: '(-?)\\b([\\d\']+(\\.[\\d\']*)?|\\.[\\d\']+)(u|U|l|L|ul|UL|f|F|b|B)' },
{ begin: '(-?)(\\b0[xX][a-fA-F0-9\'_]+|(\\b[\\d\'_]+(\\.[\\d\'_]*)?|\\.[\\d\'_]+)([eE][-+]?[\\d\'_]+)?)' }
{ begin: '\\b0[bB]_*[01](_*[01])*' + INTEGER_SUFFIX },
{ begin: '(-?)\\b0[xX]_*[a-fA-F0-9](_*[a-fA-F0-9])*' + INTEGER_SUFFIX },
{ begin: '(-?)(\\b' + DIGITS + '(\\.(' + DIGITS + ')?)?|\\.' + DIGITS + ')([eE][-+]?' + DIGITS + ')?' + REAL_SUFFIX }
],
relevance: 0
};
Expand Down
11 changes: 11 additions & 0 deletions test/markup/csharp/numbers.expect.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<span class="hljs-built_in">int</span> bin = <span class="hljs-number">0b1010_1010</span>;
<span class="hljs-built_in">int</span> binSeparatorFirst = <span class="hljs-number">0b_1010_1010</span>;
<span class="hljs-built_in">ulong</span> hex = <span class="hljs-number">0xFF_FFUL</span>;
<span class="hljs-built_in">uint</span> hexSuffix = <span class="hljs-number">0xFFu</span>;
<span class="hljs-built_in">ulong</span> decSuffix = <span class="hljs-number">1_000_000UL</span>;
<span class="hljs-built_in">decimal</span> price = <span class="hljs-number">19.99m</span>;
<span class="hljs-built_in">double</span> ratio = <span class="hljs-number">3.14d</span>;
<span class="hljs-built_in">decimal</span> big = <span class="hljs-number">1.5e10M</span>;
<span class="hljs-comment">// _count and point._1 are identifiers, not numbers</span>
<span class="hljs-built_in">int</span> _count = <span class="hljs-number">0</span>;
<span class="hljs-built_in">int</span> index = point._1;
11 changes: 11 additions & 0 deletions test/markup/csharp/numbers.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
int bin = 0b1010_1010;
int binSeparatorFirst = 0b_1010_1010;
ulong hex = 0xFF_FFUL;
uint hexSuffix = 0xFFu;
ulong decSuffix = 1_000_000UL;
decimal price = 19.99m;
double ratio = 3.14d;
decimal big = 1.5e10M;
// _count and point._1 are identifiers, not numbers
int _count = 0;
int index = point._1;

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If this is a false positive lets add a comment making that explicit.