OLD | NEW |
1 // Copyright 2016 The Chromium Authors. All rights reserved. | 1 // Copyright 2016 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #ifndef NET_QUIC_PLATFORM_IMPL_QUIC_TEXT_UTILS_IMPL_H_ | 5 #ifndef NET_QUIC_PLATFORM_IMPL_QUIC_TEXT_UTILS_IMPL_H_ |
6 #define NET_QUIC_PLATFORM_IMPL_QUIC_TEXT_UTILS_IMPL_H_ | 6 #define NET_QUIC_PLATFORM_IMPL_QUIC_TEXT_UTILS_IMPL_H_ |
7 | 7 |
8 #include <algorithm> | 8 #include <algorithm> |
9 | 9 |
10 #include "base/base64.h" | 10 #include "base/base64.h" |
11 #include "base/strings/string_number_conversions.h" | 11 #include "base/strings/string_number_conversions.h" |
12 #include "base/strings/string_piece.h" | |
13 #include "base/strings/string_split.h" | 12 #include "base/strings/string_split.h" |
14 #include "base/strings/string_util.h" | 13 #include "base/strings/string_util.h" |
15 #include "base/strings/stringprintf.h" | 14 #include "base/strings/stringprintf.h" |
16 #include "net/base/parse_number.h" | 15 #include "net/base/parse_number.h" |
| 16 #include "net/quic/platform/api/quic_string_piece.h" |
17 | 17 |
18 namespace net { | 18 namespace net { |
19 | 19 |
20 // google3 implementation of QuicTextUtils. | 20 // google3 implementation of QuicTextUtils. |
21 class QuicTextUtilsImpl { | 21 class QuicTextUtilsImpl { |
22 public: | 22 public: |
23 // Returns true of |data| starts with |prefix|, case sensitively. | 23 // Returns true of |data| starts with |prefix|, case sensitively. |
24 static bool StartsWith(base::StringPiece data, base::StringPiece prefix) { | 24 static bool StartsWith(QuicStringPiece data, QuicStringPiece prefix) { |
25 return base::StartsWith(data, prefix, base::CompareCase::SENSITIVE); | 25 return base::StartsWith(data, prefix, base::CompareCase::SENSITIVE); |
26 } | 26 } |
27 | 27 |
28 // Returns true of |data| ends with |suffix|, case insensitively. | 28 // Returns true of |data| ends with |suffix|, case insensitively. |
29 static bool EndsWithIgnoreCase(base::StringPiece data, | 29 static bool EndsWithIgnoreCase(QuicStringPiece data, QuicStringPiece suffix) { |
30 base::StringPiece suffix) { | |
31 return base::EndsWith(data, suffix, base::CompareCase::INSENSITIVE_ASCII); | 30 return base::EndsWith(data, suffix, base::CompareCase::INSENSITIVE_ASCII); |
32 } | 31 } |
33 | 32 |
34 // Returns a new std::string in which |data| has been converted to lower case. | 33 // Returns a new std::string in which |data| has been converted to lower case. |
35 static std::string ToLower(base::StringPiece data) { | 34 static std::string ToLower(QuicStringPiece data) { |
36 return base::ToLowerASCII(data); | 35 return base::ToLowerASCII(data); |
37 } | 36 } |
38 | 37 |
39 // Remove leading and trailing whitespace from |data|. | 38 // Remove leading and trailing whitespace from |data|. |
40 static void RemoveLeadingAndTrailingWhitespace(base::StringPiece* data) { | 39 static void RemoveLeadingAndTrailingWhitespace(QuicStringPiece* data) { |
41 *data = base::TrimWhitespaceASCII(*data, base::TRIM_ALL); | 40 *data = base::TrimWhitespaceASCII(*data, base::TRIM_ALL); |
42 } | 41 } |
43 | 42 |
44 // Returns true if |in| represents a valid uint64, and stores that value in | 43 // Returns true if |in| represents a valid uint64, and stores that value in |
45 // |out|. | 44 // |out|. |
46 static bool StringToUint64(base::StringPiece in, uint64_t* out) { | 45 static bool StringToUint64(QuicStringPiece in, uint64_t* out) { |
47 return base::StringToUint64(in, out); | 46 return base::StringToUint64(in, out); |
48 } | 47 } |
49 | 48 |
50 // Returns true if |in| represents a valid int, and stores that value in | 49 // Returns true if |in| represents a valid int, and stores that value in |
51 // |out|. | 50 // |out|. |
52 static bool StringToInt(base::StringPiece in, int* out) { | 51 static bool StringToInt(QuicStringPiece in, int* out) { |
53 return base::StringToInt(in, out); | 52 return base::StringToInt(in, out); |
54 } | 53 } |
55 | 54 |
56 // Returns true if |in| represents a valid uint32, and stores that value in | 55 // Returns true if |in| represents a valid uint32, and stores that value in |
57 // |out|. | 56 // |out|. |
58 static bool StringToUint32(base::StringPiece in, uint32_t* out) { | 57 static bool StringToUint32(QuicStringPiece in, uint32_t* out) { |
59 return ParseUint32(in, out, nullptr); | 58 return ParseUint32(in, out, nullptr); |
60 } | 59 } |
61 | 60 |
62 // Returns true if |in| represents a valid size_t, and stores that value in | 61 // Returns true if |in| represents a valid size_t, and stores that value in |
63 // |out|. | 62 // |out|. |
64 static bool StringToSizeT(base::StringPiece in, size_t* out) { | 63 static bool StringToSizeT(QuicStringPiece in, size_t* out) { |
65 return base::StringToSizeT(in, out); | 64 return base::StringToSizeT(in, out); |
66 } | 65 } |
67 | 66 |
68 // Returns a new std::string representing |in|. | 67 // Returns a new std::string representing |in|. |
69 static std::string Uint64ToString(uint64_t in) { | 68 static std::string Uint64ToString(uint64_t in) { |
70 return base::Uint64ToString(in); | 69 return base::Uint64ToString(in); |
71 } | 70 } |
72 | 71 |
73 // This converts |length| bytes of binary to a 2*|length|-character | 72 // This converts |length| bytes of binary to a 2*|length|-character |
74 // hexadecimal representation. | 73 // hexadecimal representation. |
75 // Return value: 2*|length| characters of ASCII std::string. | 74 // Return value: 2*|length| characters of ASCII std::string. |
76 static std::string HexEncode(base::StringPiece data) { | 75 static std::string HexEncode(QuicStringPiece data) { |
77 return base::ToLowerASCII(::base::HexEncode(data.data(), data.size())); | 76 return base::ToLowerASCII(::base::HexEncode(data.data(), data.size())); |
78 } | 77 } |
79 | 78 |
80 // Converts |data| from a hexadecimal ASCII string to a binary string | 79 // Converts |data| from a hexadecimal ASCII string to a binary string |
81 // that is |data.length()/2| bytes long. | 80 // that is |data.length()/2| bytes long. |
82 static std::string HexDecode(base::StringPiece data) { | 81 static std::string HexDecode(QuicStringPiece data) { |
83 if (data.empty()) | 82 if (data.empty()) |
84 return ""; | 83 return ""; |
85 std::vector<uint8_t> v; | 84 std::vector<uint8_t> v; |
86 if (!base::HexStringToBytes(data.as_string(), &v)) | 85 if (!base::HexStringToBytes(data.as_string(), &v)) |
87 return ""; | 86 return ""; |
88 std::string out; | 87 std::string out; |
89 if (!v.empty()) | 88 if (!v.empty()) |
90 out.assign(reinterpret_cast<const char*>(&v[0]), v.size()); | 89 out.assign(reinterpret_cast<const char*>(&v[0]), v.size()); |
91 return out; | 90 return out; |
92 } | 91 } |
(...skipping 15 matching lines...) Expand all Loading... |
108 output->resize(len); | 107 output->resize(len); |
109 } | 108 } |
110 } | 109 } |
111 } | 110 } |
112 | 111 |
113 // Returns a std::string containing hex and ASCII representations of |binary|, | 112 // Returns a std::string containing hex and ASCII representations of |binary|, |
114 // side-by-side in the style of hexdump. Non-printable characters will be | 113 // side-by-side in the style of hexdump. Non-printable characters will be |
115 // printed as '.' in the ASCII output. | 114 // printed as '.' in the ASCII output. |
116 // For example, given the input "Hello, QUIC!\01\02\03\04", returns: | 115 // For example, given the input "Hello, QUIC!\01\02\03\04", returns: |
117 // "0x0000: 4865 6c6c 6f2c 2051 5549 4321 0102 0304 Hello,.QUIC!...." | 116 // "0x0000: 4865 6c6c 6f2c 2051 5549 4321 0102 0304 Hello,.QUIC!...." |
118 static std::string HexDump(base::StringPiece binary_input) { | 117 static std::string HexDump(QuicStringPiece binary_input) { |
119 int offset = 0; | 118 int offset = 0; |
120 const int kBytesPerLine = 16; // Max bytes dumped per line | 119 const int kBytesPerLine = 16; // Max bytes dumped per line |
121 const char* buf = binary_input.data(); | 120 const char* buf = binary_input.data(); |
122 int bytes_remaining = binary_input.size(); | 121 int bytes_remaining = binary_input.size(); |
123 std::string s; // our output | 122 std::string s; // our output |
124 const char* p = buf; | 123 const char* p = buf; |
125 while (bytes_remaining > 0) { | 124 while (bytes_remaining > 0) { |
126 const int line_bytes = std::min(bytes_remaining, kBytesPerLine); | 125 const int line_bytes = std::min(bytes_remaining, kBytesPerLine); |
127 base::StringAppendF(&s, "0x%04x: ", offset); // Do the line header | 126 base::StringAppendF(&s, "0x%04x: ", offset); // Do the line header |
128 for (int i = 0; i < kBytesPerLine; ++i) { | 127 for (int i = 0; i < kBytesPerLine; ++i) { |
(...skipping 12 matching lines...) Expand all Loading... |
141 | 140 |
142 bytes_remaining -= line_bytes; | 141 bytes_remaining -= line_bytes; |
143 offset += line_bytes; | 142 offset += line_bytes; |
144 p += line_bytes; | 143 p += line_bytes; |
145 s += '\n'; | 144 s += '\n'; |
146 } | 145 } |
147 return s; | 146 return s; |
148 } | 147 } |
149 | 148 |
150 // Returns true if |data| contains any uppercase characters. | 149 // Returns true if |data| contains any uppercase characters. |
151 static bool ContainsUpperCase(base::StringPiece data) { | 150 static bool ContainsUpperCase(QuicStringPiece data) { |
152 return std::any_of(data.begin(), data.end(), base::IsAsciiUpper<char>); | 151 return std::any_of(data.begin(), data.end(), base::IsAsciiUpper<char>); |
153 } | 152 } |
154 | 153 |
155 // Splits |data| into a vector of pieces delimited by |delim|. | 154 // Splits |data| into a vector of pieces delimited by |delim|. |
156 static std::vector<base::StringPiece> Split(base::StringPiece data, | 155 static std::vector<QuicStringPiece> Split(QuicStringPiece data, char delim) { |
157 char delim) { | 156 return base::SplitStringPiece(data, QuicStringPiece(&delim, 1), |
158 return base::SplitStringPiece(data, base::StringPiece(&delim, 1), | |
159 base::TRIM_WHITESPACE, base::SPLIT_WANT_ALL); | 157 base::TRIM_WHITESPACE, base::SPLIT_WANT_ALL); |
160 } | 158 } |
161 }; | 159 }; |
162 | 160 |
163 } // namespace net | 161 } // namespace net |
164 | 162 |
165 #endif // NET_QUIC_PLATFORM_IMPL_QUIC_TEXT_UTILS_IMPL_H_ | 163 #endif // NET_QUIC_PLATFORM_IMPL_QUIC_TEXT_UTILS_IMPL_H_ |
OLD | NEW |