2 // Copyright (c) 2012 The ANGLE Project Authors. All rights reserved.
\r
3 // Use of this source code is governed by a BSD-style license that can be
\r
4 // found in the LICENSE file.
\r
7 // numeric_lex.h: Functions to extract numeric values from string.
\r
9 #ifndef COMPILER_PREPROCESSOR_NUMERIC_LEX_H_
\r
10 #define COMPILER_PREPROCESSOR_NUMERIC_LEX_H_
\r
16 inline std::ios::fmtflags numeric_base_int(const std::string& str)
\r
18 if ((str.size() >= 2) &&
\r
20 (str[1] == 'x' || str[1] == 'X'))
\r
22 return std::ios::hex;
\r
24 else if ((str.size() >= 1) && (str[0] == '0'))
\r
26 return std::ios::oct;
\r
28 return std::ios::dec;
\r
31 // The following functions parse the given string to extract a numerical
\r
32 // value of the given type. These functions assume that the string is
\r
33 // of the correct form. They can only fail if the parsed value is too big,
\r
34 // in which case false is returned.
\r
36 template<typename IntType>
\r
37 bool numeric_lex_int(const std::string& str, IntType* value)
\r
39 std::istringstream stream(str);
\r
40 // This should not be necessary, but MSVS has a buggy implementation.
\r
41 // It returns incorrect results if the base is not specified.
\r
42 stream.setf(numeric_base_int(str), std::ios::basefield);
\r
45 return !stream.fail();
\r
48 template<typename FloatType>
\r
49 bool numeric_lex_float(const std::string& str, FloatType* value)
\r
51 std::istringstream stream(str);
\r
52 // Force "C" locale so that decimal character is always '.', and
\r
53 // not dependent on the current locale.
\r
54 stream.imbue(std::locale::classic());
\r
57 return !stream.fail();
\r
61 #endif // COMPILER_PREPROCESSOR_NUMERIC_LEX_H_
\r