Path: blob/main_old/src/compiler/preprocessor/numeric_lex.h
1693 views
//1// Copyright 2012 The ANGLE Project Authors. All rights reserved.2// Use of this source code is governed by a BSD-style license that can be3// found in the LICENSE file.4//56// numeric_lex.h: Functions to extract numeric values from string.78#ifndef COMPILER_PREPROCESSOR_NUMERICLEX_H_9#define COMPILER_PREPROCESSOR_NUMERICLEX_H_1011#include <sstream>1213namespace angle14{1516namespace pp17{1819inline std::ios::fmtflags numeric_base_int(const std::string &str)20{21if ((str.size() >= 2) && (str[0] == '0') && (str[1] == 'x' || str[1] == 'X'))22{23return std::ios::hex;24}25if ((str.size() >= 1) && (str[0] == '0'))26{27return std::ios::oct;28}29return std::ios::dec;30}3132// The following functions parse the given string to extract a numerical33// value of the given type. These functions assume that the string is34// of the correct form. They can only fail if the parsed value is too big,35// in which case false is returned.3637template <typename IntType>38bool numeric_lex_int(const std::string &str, IntType *value)39{40std::istringstream stream(str);41// This should not be necessary, but MSVS has a buggy implementation.42// It returns incorrect results if the base is not specified.43stream.setf(numeric_base_int(str), std::ios::basefield);4445stream >> (*value);46return !stream.fail();47}4849} // namespace pp5051} // namespace angle5253#endif // COMPILER_PREPROCESSOR_NUMERICLEX_H_545556