OLD | NEW |
(Empty) | |
| 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 |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #ifndef NET_QUIC_PLATFORM_IMPL_QUIC_TEXT_UTILS_IMPL_H_ |
| 6 #define NET_QUIC_PLATFORM_IMPL_QUIC_TEXT_UTILS_IMPL_H_ |
| 7 |
| 8 #include <algorithm> |
| 9 |
| 10 #include "base/strings/string_number_conversions.h" |
| 11 #include "base/strings/string_piece.h" |
| 12 #include "base/strings/string_split.h" |
| 13 #include "base/strings/string_util.h" |
| 14 #include "base/strings/stringprintf.h" |
| 15 |
| 16 namespace net { |
| 17 |
| 18 // google3 implementation of QuicTextUtils. |
| 19 class QuicTextUtilsImpl { |
| 20 public: |
| 21 // Returns true of |data| starts with |prefix|, case sensitively. |
| 22 static bool StartsWith(base::StringPiece data, base::StringPiece prefix) { |
| 23 return base::StartsWith(data, prefix, base::CompareCase::SENSITIVE); |
| 24 } |
| 25 |
| 26 // Returns true of |data| ends with |suffix|, case insensitively. |
| 27 static bool EndsWithIgnoreCase(base::StringPiece data, |
| 28 base::StringPiece suffix) { |
| 29 return base::EndsWith(data, suffix, base::CompareCase::INSENSITIVE_ASCII); |
| 30 } |
| 31 |
| 32 // Returns a new std::string in which |data| has been converted to lower case. |
| 33 static std::string ToLower(base::StringPiece data) { |
| 34 return base::ToLowerASCII(data); |
| 35 } |
| 36 |
| 37 // Remove leading and trailing whitespace from |data|. |
| 38 static void RemoveLeadingAndTrailingWhitespace(base::StringPiece* data) { |
| 39 *data = base::TrimWhitespaceASCII(*data, base::TRIM_ALL); |
| 40 } |
| 41 |
| 42 // Returns true if |in| represents a valid uint64, and stores that value in |
| 43 // |out|. |
| 44 static bool StringToUint64(base::StringPiece in, uint64_t* out) { |
| 45 return base::StringToUint64(in, out); |
| 46 } |
| 47 |
| 48 // Returns a new std::string representing |in|. |
| 49 static std::string Uint64ToString(uint64_t in) { |
| 50 return base::Uint64ToString(in); |
| 51 } |
| 52 |
| 53 // This converts |length| bytes of binary to a 2*|length|-character |
| 54 // hexadecimal representation. |
| 55 // Return value: 2*|length| characters of ASCII std::string. |
| 56 static std::string HexEncode(base::StringPiece data) { |
| 57 return ::base::HexEncode(data.data(), data.size()); |
| 58 } |
| 59 |
| 60 // Converts |data| from a hexadecimal ASCII std::string to binary. |
| 61 static std::string HexDecode(base::StringPiece data) { |
| 62 if (data.empty()) |
| 63 return ""; |
| 64 std::vector<uint8_t> v; |
| 65 if (!base::HexStringToBytes(data.as_string(), &v)) |
| 66 return ""; |
| 67 std::string out; |
| 68 if (!v.empty()) |
| 69 out.assign(reinterpret_cast<const char*>(&v[0]), v.size()); |
| 70 return out; |
| 71 } |
| 72 |
| 73 // Base64 encodes with no padding |data_len| bytes of |data| into |output|. |
| 74 static void Base64Encode(uint8_t* data, |
| 75 size_t data_len, |
| 76 std::string* output) {} |
| 77 |
| 78 // Returns a std::string containing hex and ASCII representations of |binary|, |
| 79 // side-by-side in the style of hexdump. Non-printable characters will be |
| 80 // printed as '.' in the ASCII output. |
| 81 // For example: |
| 82 // "0x0000: 4865 6c6c 6f2c 2051 5549 4321 0102 0304 Hello,.QUIC!...." |
| 83 static std::string HexDump(base::StringPiece binary_input) { |
| 84 int offset = 0; |
| 85 const int kBytesPerLine = 16; // Max bytes dumped per line |
| 86 const char* buf = binary_input.data(); |
| 87 int bytes_remaining = binary_input.size(); |
| 88 std::string s; // our output |
| 89 const char* p = buf; |
| 90 while (bytes_remaining > 0) { |
| 91 const int line_bytes = std::min(bytes_remaining, kBytesPerLine); |
| 92 base::StringAppendF(&s, "0x%04x: ", offset); // Do the line header |
| 93 for (int i = 0; i < kBytesPerLine; ++i) { |
| 94 if (i < line_bytes) { |
| 95 base::StringAppendF(&s, "%02x", static_cast<unsigned char>(p[i])); |
| 96 } else { |
| 97 s += " "; // two-space filler instead of two-space hex digits |
| 98 } |
| 99 if (i % 2) |
| 100 s += ' '; |
| 101 } |
| 102 s += ' '; |
| 103 for (int i = 0; i < line_bytes; ++i) { // Do the ASCII dump |
| 104 s += (p[i] > 32 && p[i] < 127) ? p[i] : '.'; |
| 105 } |
| 106 |
| 107 bytes_remaining -= line_bytes; |
| 108 offset += line_bytes; |
| 109 p += line_bytes; |
| 110 s += '\n'; |
| 111 } |
| 112 return s; |
| 113 } |
| 114 |
| 115 // Returns true if |data| contains any uppercase characters. |
| 116 static bool ContainsUpperCase(base::StringPiece data) { |
| 117 return std::any_of(data.begin(), data.end(), base::IsAsciiUpper<char>); |
| 118 } |
| 119 |
| 120 // Splits |data| into a vector of pieces delimited by |delim|. |
| 121 static std::vector<base::StringPiece> Split(base::StringPiece data, |
| 122 char delim) { |
| 123 return base::SplitStringPiece(data, base::StringPiece(&delim, 1), |
| 124 base::TRIM_WHITESPACE, base::SPLIT_WANT_ALL); |
| 125 } |
| 126 }; |
| 127 |
| 128 } // namespace net |
| 129 |
| 130 #endif // NET_QUIC_PLATFORM_IMPL_QUIC_TEXT_UTILS_IMPL_H_ |
OLD | NEW |