webkit  2cdf99a9e3038c7e01b3c37e8ad903ecbe5eecf1
https://github.com/WebKit/webkit
numeric_lex.h
Go to the documentation of this file.
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 //
6 
7 // numeric_lex.h: Functions to extract numeric values from string.
8 
9 #ifndef COMPILER_PREPROCESSOR_NUMERICLEX_H_
10 #define COMPILER_PREPROCESSOR_NUMERICLEX_H_
11 
12 #include <sstream>
13 
14 namespace pp {
15 
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  if ((str.size() >= 1) && (str[0] == '0'))
25  {
26  return std::ios::oct;
27  }
28  return std::ios::dec;
29 }
30 
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.
35 
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);
43 
44  stream >> (*value);
45  return !stream.fail();
46 }
47 
48 template<typename FloatType>
49 bool numeric_lex_float(const std::string &str, FloatType *value)
50 {
51 // On 64-bit Intel Android, istringstream is broken. Until this is fixed in
52 // a newer NDK, don't use it. Android doesn't have locale support, so this
53 // doesn't have to force the C locale.
54 // TODO(thakis): Remove this once this bug has been fixed in the NDK and
55 // that NDK has been rolled into chromium.
56 #if defined(ANGLE_PLATFORM_ANDROID) && __x86_64__
57  *value = strtod(str.c_str(), nullptr);
58  return errno != ERANGE;
59 #else
60  std::istringstream stream(str);
61  // Force "C" locale so that decimal character is always '.', and
62  // not dependent on the current locale.
63  stream.imbue(std::locale::classic());
64 
65  stream >> (*value);
66  return !stream.fail();
67 #endif
68 }
69 
70 } // namespace pp.
71 
72 #endif // COMPILER_PREPROCESSOR_NUMERICLEX_H_
Definition: DiagnosticsBase.cpp:11
EGLStreamKHR stream
Definition: eglext.h:340
std::ios::fmtflags numeric_base_int(const std::string &str)
Definition: numeric_lex.h:16
EGLAttrib * value
Definition: eglext.h:120
str
Definition: make-dist.py:305
GLsizei const GLchar *const * string
Definition: gl2.h:479
bool numeric_lex_int(const std::string &str, IntType *value)
Definition: numeric_lex.h:37
bool numeric_lex_float(const std::string &str, FloatType *value)
Definition: numeric_lex.h:49