1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/gfx/angle/src/compiler/preprocessor/numeric_lex.h Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,61 @@ 1.4 +// 1.5 +// Copyright (c) 2012 The ANGLE Project Authors. All rights reserved. 1.6 +// Use of this source code is governed by a BSD-style license that can be 1.7 +// found in the LICENSE file. 1.8 +// 1.9 + 1.10 +// numeric_lex.h: Functions to extract numeric values from string. 1.11 + 1.12 +#ifndef COMPILER_PREPROCESSOR_NUMERIC_LEX_H_ 1.13 +#define COMPILER_PREPROCESSOR_NUMERIC_LEX_H_ 1.14 + 1.15 +#include <sstream> 1.16 + 1.17 +namespace pp { 1.18 + 1.19 +inline std::ios::fmtflags numeric_base_int(const std::string& str) 1.20 +{ 1.21 + if ((str.size() >= 2) && 1.22 + (str[0] == '0') && 1.23 + (str[1] == 'x' || str[1] == 'X')) 1.24 + { 1.25 + return std::ios::hex; 1.26 + } 1.27 + else if ((str.size() >= 1) && (str[0] == '0')) 1.28 + { 1.29 + return std::ios::oct; 1.30 + } 1.31 + return std::ios::dec; 1.32 +} 1.33 + 1.34 +// The following functions parse the given string to extract a numerical 1.35 +// value of the given type. These functions assume that the string is 1.36 +// of the correct form. They can only fail if the parsed value is too big, 1.37 +// in which case false is returned. 1.38 + 1.39 +template<typename IntType> 1.40 +bool numeric_lex_int(const std::string& str, IntType* value) 1.41 +{ 1.42 + std::istringstream stream(str); 1.43 + // This should not be necessary, but MSVS has a buggy implementation. 1.44 + // It returns incorrect results if the base is not specified. 1.45 + stream.setf(numeric_base_int(str), std::ios::basefield); 1.46 + 1.47 + stream >> (*value); 1.48 + return !stream.fail(); 1.49 +} 1.50 + 1.51 +template<typename FloatType> 1.52 +bool numeric_lex_float(const std::string& str, FloatType* value) 1.53 +{ 1.54 + std::istringstream stream(str); 1.55 + // Force "C" locale so that decimal character is always '.', and 1.56 + // not dependent on the current locale. 1.57 + stream.imbue(std::locale::classic()); 1.58 + 1.59 + stream >> (*value); 1.60 + return !stream.fail(); 1.61 +} 1.62 + 1.63 +} // namespace pp. 1.64 +#endif // COMPILER_PREPROCESSOR_NUMERIC_LEX_H_