Skip to content

fix(cpp): add word boundary to float literal pattern#4407

Open
xxiaoxiong wants to merge 1 commit into
highlightjs:mainfrom
xxiaoxiong:fix/cpp-float-literal-boundary-4231
Open

fix(cpp): add word boundary to float literal pattern#4407
xxiaoxiong wants to merge 1 commit into
highlightjs:mainfrom
xxiaoxiong:fix/cpp-float-literal-boundary-4231

Conversation

@xxiaoxiong
Copy link
Copy Markdown

@xxiaoxiong xxiaoxiong commented May 25, 2026

Problem

In C++ code, identifiers that contain digits following a float pattern (e.g., var2_5) are incorrectly highlighted as float literals. The number 5 in var2_5 triggers the float literal regex.

Root cause

The C++ float literal pattern lacks a word boundary assertion at the beginning. Without it, the regex matches substrings like 2_5 within a larger identifier var2_5.

Changes

  • src/languages/cpp.js: Added a word boundary (\b) at the start of the float literal regex pattern to ensure only standalone numeric literals are matched, not digits embedded within identifiers.

Why

Without the word boundary, any identifier ending in digits suffixed with a numeric-looking segment would be partially matched as a float literal, breaking variable name highlighting.

Closes #4231

Closes highlightjs#4231

The floating-point literal regex was missing \b (word boundary) at the
start of its digit-based alternatives, causing identifiers like
to incorrectly highlight  as a float literal.

Added \b to each numeric alternative that starts with a digit, while
preserving the existing behavior for leading-dot floats (e.g. ).

Fixes highlightjs#4231
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

(cpp) 1.f is considered a floating-point literal in a1.foo()

1 participant