66 lines · cpp
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-exception6//7//===----------------------------------------------------------------------===//8///9/// \file10/// This file implements the functionality of getting information about a11/// numeric literal string, including 0-based positions of the base letter, the12/// decimal/hexadecimal point, the exponent letter, and the suffix, or npos if13/// absent.14///15//===----------------------------------------------------------------------===//16 17#include "NumericLiteralInfo.h"18#include "llvm/ADT/StringExtras.h"19 20namespace clang {21namespace format {22 23using namespace llvm;24 25NumericLiteralInfo::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. 0xF41 break;42 }43 }44 45 DotPos = Text.find('.', BaseLetterPos + 1); // e.g. 0x.1 or .146 47 // e.g. 1.e2 or 0xFp248 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-2f62}63 64} // namespace format65} // namespace clang66