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