Debian Codegraph @

clang/lib/Format/NumericLiteralInfo.cpp

raw
1 //===--- NumericLiteralInfo.cpp ---------------------------------*- C++ -*-===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 ///
9 /// \file
10 /// This file implements the functionality of getting information about a
11 /// numeric literal string, including 0-based positions of the base letter, the
12 /// decimal/hexadecimal point, the exponent letter, and the suffix, or npos if
13 /// absent.
14 ///
15 //===----------------------------------------------------------------------===//
16
17 #include "NumericLiteralInfo.h"
18 #include "llvm/ADT/StringExtras.h"
19
20 namespace clang {
21 namespace format {
22
23 using namespace llvm;
24
25 NumericLiteralInfo::NumericLiteralInfo(StringRef Text, char Separator) {
26 if (Text.size() < 2)
27 return;
28
29 bool IsHex = false;
30 if (Text[0] == '0') {
31 switch (Text[1]) {
32 case 'x':
33 case 'X':
34 IsHex = true;
35 [[fallthrough]];
36 case 'b':
37 case 'B':
38 case 'o': // JavaScript octal.
39 case 'O':
40 BaseLetterPos = 1; // e.g. 0xF
41 break;
42 }
43 }
44
45 DotPos = Text.find('.', BaseLetterPos + 1); // e.g. 0x.1 or .1
46
47 // e.g. 1.e2 or 0xFp2
48 const auto Pos = DotPos != StringRef::npos ? DotPos + 1 : BaseLetterPos + 2;
49
50 ExponentLetterPos =
51 // Trim C++ user-defined suffix as in `1_Pa`.
52 (Separator == '\'' ? Text.take_front(Text.find('_')) : Text)
53 .find_insensitive(IsHex ? 'p' : 'e', Pos);
54
55 const bool HasExponent = ExponentLetterPos != StringRef::npos;
56 SuffixPos = Text.find_if_not(
57 [&](char C) {
58 return (HasExponent || !IsHex ? isDigit : isHexDigit)(C) ||
59 C == Separator;
60 },
61 HasExponent ? ExponentLetterPos + 2 : Pos); // e.g. 1e-2f
62 }
63
64 } // namespace format
65 } // namespace clang