OSDN Git Service

Increment the version number.
[android-x86/external-swiftshader.git] / src / Radiance / compiler / preprocessor / numeric_lex.h
1 //\r
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
5 //\r
6 \r
7 // numeric_lex.h: Functions to extract numeric values from string.\r
8 \r
9 #ifndef COMPILER_PREPROCESSOR_NUMERIC_LEX_H_\r
10 #define COMPILER_PREPROCESSOR_NUMERIC_LEX_H_\r
11 \r
12 #include <sstream>\r
13 \r
14 namespace pp {\r
15 \r
16 inline std::ios::fmtflags numeric_base_int(const std::string& str)\r
17 {\r
18     if ((str.size() >= 2) &&\r
19         (str[0] == '0') &&\r
20         (str[1] == 'x' || str[1] == 'X'))\r
21     {\r
22         return std::ios::hex;\r
23     }\r
24     else if ((str.size() >= 1) && (str[0] == '0'))\r
25     {\r
26         return std::ios::oct;\r
27     }\r
28     return std::ios::dec;\r
29 }\r
30 \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
35 \r
36 template<typename IntType>\r
37 bool numeric_lex_int(const std::string& str, IntType* value)\r
38 {\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
43 \r
44     stream >> (*value);\r
45     return !stream.fail();\r
46 }\r
47 \r
48 template<typename FloatType>\r
49 bool numeric_lex_float(const std::string& str, FloatType* value)\r
50 {\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
55 \r
56     stream >> (*value);\r
57     return !stream.fail();\r
58 }\r
59 \r
60 } // namespace pp.\r
61 #endif // COMPILER_PREPROCESSOR_NUMERIC_LEX_H_\r