Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(106)

Unified Diff: mojo/edk/util/string_number_conversions.h

Issue 1492623002: EDK: Add functions for converting between strings and numbers. (Closed) Base URL: https://github.com/domokit/mojo.git@master
Patch Set: Created 5 years ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
« no previous file with comments | « mojo/edk/util/BUILD.gn ('k') | mojo/edk/util/string_number_conversions.cc » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: mojo/edk/util/string_number_conversions.h
diff --git a/mojo/edk/util/string_number_conversions.h b/mojo/edk/util/string_number_conversions.h
new file mode 100644
index 0000000000000000000000000000000000000000..145edd4f5f58f96ce25a8b48f177c1c460f2d7ac
--- /dev/null
+++ b/mojo/edk/util/string_number_conversions.h
@@ -0,0 +1,46 @@
+// Copyright 2015 The Chromium Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+// Functions for converting between numbers and their representations as strings
+// (in decimal, in a locale-independent way).
+
+#ifndef MOJO_EDK_UTIL_STRING_NUMBER_CONVERSIONS_H_
+#define MOJO_EDK_UTIL_STRING_NUMBER_CONVERSIONS_H_
+
+#include <string>
+
+namespace mojo {
+namespace util {
+
+// Converts |number| to a string with a locale-independent decimal
+// representation of it. This is available for all |NumberType|s (u)intN_t (from
+// <stdint.h>) and also (unsigned) int.
+template <typename NumberType>
+std::string NumberToString(NumberType number);
vardhan 2015/12/02 00:07:42 is locale the only reason you would want to use th
+
+// Converts |string| containing a locale-independent decimal representation of a
+// number to a numeric representation of that number. (On error, this returns
+// false and leaves |*number| alone.) This is available for all |NumberType|s
+// (u)intN_t (from <stdint.h>) and also (unsigned) int.
+//
+// Notes: Unary '+' is not allowed. Leading zeros are allowed (and ignored). For
+// unsigned types, unary '-' is not allowed. For signed types, "-0", "-00", etc.
+// are also allowed.
+template <typename NumberType>
+bool StringToNumberWithError(const std::string& string, NumberType* number);
+
+// Converts |string| containing a locale-independent decimal representation of a
+// number to a numeric representation of that number. (On error, this returns
+// zero.) This is available for all |NumberType|s (u)intN_t (from <stdint.h>)
+// and also (unsigned) int. (See |StringToNumberWithError()| for more details.)
+template <typename NumberType>
+NumberType StringToNumber(const std::string& string) {
+ NumberType rv = static_cast<NumberType>(0);
+ return StringToNumberWithError(string, &rv) ? rv : static_cast<NumberType>(0);
+}
+
+} // namespace util
+} // namespace mojo
+
+#endif // MOJO_EDK_UTIL_STRING_NUMBER_CONVERSIONS_H_
« no previous file with comments | « mojo/edk/util/BUILD.gn ('k') | mojo/edk/util/string_number_conversions.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698