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

Unified Diff: net/base/parse_number.h

Issue 1829873002: Add base/parse_number.h to generalize number parsing done in //net. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: more tests Created 4 years, 9 months 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 | « net/base/ip_address.cc ('k') | net/base/parse_number.cc » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: net/base/parse_number.h
diff --git a/net/base/parse_number.h b/net/base/parse_number.h
new file mode 100644
index 0000000000000000000000000000000000000000..0b88cf0435b3af69d1330c33d07acdbc11c60e52
--- /dev/null
+++ b/net/base/parse_number.h
@@ -0,0 +1,68 @@
+// Copyright 2016 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.
+
+#ifndef NET_BASE_PARSE_NUMBER_H_
+#define NET_BASE_PARSE_NUMBER_H_
+
+#include "base/compiler_specific.h"
+#include "base/strings/string_piece.h"
+#include "net/base/net_export.h"
+
+// This file contains utility functions for parsing numbers, in the context of
+// network protocols.
+//
+// Q: Doesn't //base already provide these in string_number_conversions.h, with
+// functions like base::StringToInt()?
+//
+// A: Yes, and those functions are used under the hood by these
+// implementations.
+//
+// However using the number parsing functions from //base directly in network
+// code can lead to subtle bugs, as the //base versions are more permissive.
+// For instance "+99" is successfully parsed by base::StringToInt().
+//
+// However in the majority of places in //net, a leading plus on a number
+// should be considered invalid. For instance when parsing a host:port pair
+// you wouldn't want to recognize "foo:+99" as having a port of 99. The same
+// issue applies when parsing a content-length header.
+//
+// To reduce the risk of such problems, use of these functions over the
+// //base versions.
+
+class GURL;
+
+namespace net {
+
+// Parses a string representing a decimal number to an |int|. Returns true on
+// success, and fills |*output| with the result. Note that |*output| is not
+// modified on failure.
+//
+// Recognized inputs take the form:
+// 1*DIGIT
+//
+// Where DIGIT is an ASCII number in the range '0' - '9'
+//
+// Note that:
+// * Parsing is locale independent
+// * Leading zeros are allowed (numbers needn't be in minimal encoding)
+// * Inputs that would overflow the output are rejected.
+// * Only accepts integers
+//
+// Examples of recognized inputs are:
+// "13"
+// "0"
+// "00013"
+//
+// Examples of rejected inputs are:
+// " 13"
+// "-13"
+// "+13"
+// "0x15"
+// "13.3"
+NET_EXPORT bool ParseNonNegativeDecimalInt(const base::StringPiece& input,
+ int* output) WARN_UNUSED_RESULT;
+
+} // namespace net
+
+#endif // NET_BASE_PARSE_NUMBER_H_
« no previous file with comments | « net/base/ip_address.cc ('k') | net/base/parse_number.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698