aboutsummaryrefslogtreecommitdiff
path: root/clang/lib/Format/NumericLiteralInfo.cpp
blob: 81e6dd5e58bbcf7509cb9f71ef6a2881b3ea85fd (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
//===--- NumericLiteralInfo.cpp ---------------------------------*- C++ -*-===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//
///
/// \file
/// This file implements the functionality of getting information about a
/// numeric literal string, including 0-based positions of the base letter, the
/// decimal/hexadecimal point, the exponent letter, and the suffix, or npos if
/// absent.
///
//===----------------------------------------------------------------------===//

#include "NumericLiteralInfo.h"
#include "llvm/ADT/StringExtras.h"

namespace clang {
namespace format {

using namespace llvm;

NumericLiteralInfo::NumericLiteralInfo(StringRef Text, char Separator) {
  if (Text.size() < 2)
    return;

  bool IsHex = false;
  if (Text[0] == '0') {
    switch (Text[1]) {
    case 'x':
    case 'X':
      IsHex = true;
      [[fallthrough]];
    case 'b':
    case 'B':
    case 'o': // JavaScript octal.
    case 'O':
      BaseLetterPos = 1; // e.g. 0xF
      break;
    }
  }

  DotPos = Text.find('.', BaseLetterPos + 1); // e.g. 0x.1 or .1

  // e.g. 1.e2 or 0xFp2
  const auto Pos = DotPos != StringRef::npos ? DotPos + 1 : BaseLetterPos + 2;

  ExponentLetterPos =
      // Trim C++ user-defined suffix as in `1_Pa`.
      (Separator == '\'' ? Text.take_front(Text.find('_')) : Text)
          .find_insensitive(IsHex ? 'p' : 'e', Pos);

  const bool HasExponent = ExponentLetterPos != StringRef::npos;
  SuffixPos = Text.find_if_not(
      [&](char C) {
        return (HasExponent || !IsHex ? isDigit : isHexDigit)(C) ||
               C == Separator;
      },
      HasExponent ? ExponentLetterPos + 2 : Pos); // e.g. 1e-2f
}

} // namespace format
} // namespace clang