diff --git a/lib/rouge/lexers/cpp.rb b/lib/rouge/lexers/cpp.rb index 31698c2594..5fe73c1cf3 100644 --- a/lib/rouge/lexers/cpp.rb +++ b/lib/rouge/lexers/cpp.rb @@ -62,9 +62,10 @@ def self.reserved rule %r/(class|struct)\b/, Keyword, :classname rule %r/template\b/, Keyword, :template rule %r/#{dq}(\.#{dq})?(?:y|d|h|(?:min)|s|(?:ms)|(?:us)|(?:ns)|i|(?:if)|(?:il))\b/, Num::Other - rule %r((#{dq}[.]#{dq}?|[.]#{dq})(e[+-]?#{dq}[lu]*)?)i, Num::Float - rule %r(#{dq}e[+-]?#{dq}[lu]*)i, Num::Float - rule %r/0x\h('?\h)*[lu]*/i, Num::Hex + rule %r((#{dq}[.]#{dq}?|[.]#{dq})([ep][+-]?#{dq})?[luf]*)i, Num::Float + rule %r(#{dq}[ep][+-]?#{dq}[luf]*)i, Num::Float + rule %r/0x\h('?\h)*([ep][+-]?#{dq})?[lu]*/i, Num::Hex + rule %r/0x\h('?\h)*[.]\h+([ep][+-]?#{dq})[luf]*/i, Num::Hex rule %r/0b[01]+('[01]+)*/, Num::Bin rule %r/0[0-7]('?[0-7])*[lu]*/i, Num::Oct rule %r/#{dq}[lu]*/i, Num::Integer diff --git a/spec/visual/samples/cpp b/spec/visual/samples/cpp index acde3bcffe..12bdc8c2b7 100644 --- a/spec/visual/samples/cpp +++ b/spec/visual/samples/cpp @@ -277,3 +277,44 @@ export struct Shape { int m_x; int m_y; } + +float test(8.874L); +float test(8.874l); +float test(8.874F); +float test(8.874f); +float test(8.874U); +float test(8.874u); + +// via https://en.cppreference.com/w/cpp/language/floating_literal +int main() +{ + std::cout + << "Literal" "\t" "Printed value" << std::left + << OUT( 58. ) // double + << OUT( 4e2 ) // double + << OUT( 123.456e-67 ) // double + << OUT( 123.456e-67f ) // float, truncated to zero + << OUT( .1E4f ) // float + << OUT( 0x10.1p0 ) // double + << OUT( 0x1p5 ) // double + << OUT( 0x1e5 ) // integer literal, not floating-point + << OUT( 3.14'15'92 ) // double, single quotes ignored (C++14) + << OUT( 1.18e-4932l ) // long double + << std::setprecision(39) + << OUT( 3.4028234e38f ) // float + << OUT( 3.4028234e38 ) // double + << OUT( 3.4028234e38l ) // long double + << '\n'; + + static_assert(3.4028234e38f == std::numeric_limits::max()); + + static_assert(3.4028234e38f == // ends with 4 + 3.4028235e38f); // ends with 5 + + static_assert(3.4028234e38 != // ends with 4 + 3.4028235e38); // ends with 5 + + // Both floating-point constants below are 3.4028234e38 + static_assert(3.4028234e38f != // a float (then promoted to double) + 3.4028234e38); // a double +}