Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 1 // Copyright 2015 The Chromium Authors. All rights reserved. | |
| 2 // Use of this source code is governed by a BSD-style license that can be | |
| 3 // found in the LICENSE file. | |
| 4 | |
| 5 // Functions for converting between numbers and their representations as strings | |
| 6 // (in decimal, in a locale-independent way). | |
| 7 | |
| 8 #ifndef MOJO_EDK_UTIL_STRING_NUMBER_CONVERSIONS_H_ | |
| 9 #define MOJO_EDK_UTIL_STRING_NUMBER_CONVERSIONS_H_ | |
| 10 | |
| 11 #include <string> | |
| 12 | |
| 13 namespace mojo { | |
| 14 namespace util { | |
| 15 | |
| 16 // Converts |number| to a string with a locale-independent decimal | |
| 17 // representation of it. This is available for all |NumberType|s (u)intN_t (from | |
| 18 // <stdint.h>) and also (unsigned) int. | |
| 19 template <typename NumberType> | |
| 20 std::string NumberToString(NumberType number); | |
|
vardhan
2015/12/02 00:07:42
is locale the only reason you would want to use th
| |
| 21 | |
| 22 // Converts |string| containing a locale-independent decimal representation of a | |
| 23 // number to a numeric representation of that number. (On error, this returns | |
| 24 // false and leaves |*number| alone.) This is available for all |NumberType|s | |
| 25 // (u)intN_t (from <stdint.h>) and also (unsigned) int. | |
| 26 // | |
| 27 // Notes: Unary '+' is not allowed. Leading zeros are allowed (and ignored). For | |
| 28 // unsigned types, unary '-' is not allowed. For signed types, "-0", "-00", etc. | |
| 29 // are also allowed. | |
| 30 template <typename NumberType> | |
| 31 bool StringToNumberWithError(const std::string& string, NumberType* number); | |
| 32 | |
| 33 // Converts |string| containing a locale-independent decimal representation of a | |
| 34 // number to a numeric representation of that number. (On error, this returns | |
| 35 // zero.) This is available for all |NumberType|s (u)intN_t (from <stdint.h>) | |
| 36 // and also (unsigned) int. (See |StringToNumberWithError()| for more details.) | |
| 37 template <typename NumberType> | |
| 38 NumberType StringToNumber(const std::string& string) { | |
| 39 NumberType rv = static_cast<NumberType>(0); | |
| 40 return StringToNumberWithError(string, &rv) ? rv : static_cast<NumberType>(0); | |
| 41 } | |
| 42 | |
| 43 } // namespace util | |
| 44 } // namespace mojo | |
| 45 | |
| 46 #endif // MOJO_EDK_UTIL_STRING_NUMBER_CONVERSIONS_H_ | |
| OLD | NEW |