QUICHE team | 83e0444 | 2019-12-11 06:49:49 -0800 | [diff] [blame] | 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 | |
bnc | 9de6abe | 2021-04-28 06:24:19 -0700 | [diff] [blame] | 5 | #ifndef QUICHE_COMMON_QUICHE_TEXT_UTILS_H_ |
| 6 | #define QUICHE_COMMON_QUICHE_TEXT_UTILS_H_ |
QUICHE team | 83e0444 | 2019-12-11 06:49:49 -0800 | [diff] [blame] | 7 | |
| 8 | #include <string> |
| 9 | |
bnc | 9de6abe | 2021-04-28 06:24:19 -0700 | [diff] [blame] | 10 | #include "absl/strings/ascii.h" |
vasilvv | 72b789c | 2020-10-27 17:39:17 -0700 | [diff] [blame] | 11 | #include "absl/strings/escaping.h" |
vasilvv | 652cd42 | 2020-10-09 15:50:02 -0700 | [diff] [blame] | 12 | #include "absl/strings/string_view.h" |
vasilvv | 7df418b | 2020-10-13 13:47:09 -0700 | [diff] [blame] | 13 | #include "absl/types/optional.h" |
QUICHE team | 5be974e | 2020-12-29 18:35:24 -0500 | [diff] [blame] | 14 | #include "common/platform/api/quiche_export.h" |
QUICHE team | 83e0444 | 2019-12-11 06:49:49 -0800 | [diff] [blame] | 15 | |
| 16 | namespace quiche { |
| 17 | |
| 18 | // Various utilities for manipulating text. |
| 19 | class QUICHE_EXPORT QuicheTextUtils { |
| 20 | public: |
QUICHE team | 83e0444 | 2019-12-11 06:49:49 -0800 | [diff] [blame] | 21 | // Returns a new string in which |data| has been converted to lower case. |
vasilvv | 652cd42 | 2020-10-09 15:50:02 -0700 | [diff] [blame] | 22 | static std::string ToLower(absl::string_view data) { |
bnc | 9de6abe | 2021-04-28 06:24:19 -0700 | [diff] [blame] | 23 | return absl::AsciiStrToLower(data); |
QUICHE team | 83e0444 | 2019-12-11 06:49:49 -0800 | [diff] [blame] | 24 | } |
| 25 | |
| 26 | // Removes leading and trailing whitespace from |data|. |
vasilvv | 652cd42 | 2020-10-09 15:50:02 -0700 | [diff] [blame] | 27 | static void RemoveLeadingAndTrailingWhitespace(absl::string_view* data) { |
bnc | 9de6abe | 2021-04-28 06:24:19 -0700 | [diff] [blame] | 28 | *data = absl::StripAsciiWhitespace(*data); |
QUICHE team | 83e0444 | 2019-12-11 06:49:49 -0800 | [diff] [blame] | 29 | } |
| 30 | |
QUICHE team | 83e0444 | 2019-12-11 06:49:49 -0800 | [diff] [blame] | 31 | // Base64 encodes with no padding |data_len| bytes of |data| into |output|. |
| 32 | static void Base64Encode(const uint8_t* data, |
| 33 | size_t data_len, |
bnc | 9de6abe | 2021-04-28 06:24:19 -0700 | [diff] [blame] | 34 | std::string* output); |
QUICHE team | 83e0444 | 2019-12-11 06:49:49 -0800 | [diff] [blame] | 35 | |
vasilvv | a7300b8 | 2020-04-20 20:32:51 -0400 | [diff] [blame] | 36 | // Decodes a base64-encoded |input|. Returns nullopt when the input is |
| 37 | // invalid. |
bnc | 9de6abe | 2021-04-28 06:24:19 -0700 | [diff] [blame] | 38 | static absl::optional<std::string> Base64Decode(absl::string_view input); |
vasilvv | a7300b8 | 2020-04-20 20:32:51 -0400 | [diff] [blame] | 39 | |
QUICHE team | 83e0444 | 2019-12-11 06:49:49 -0800 | [diff] [blame] | 40 | // Returns a string containing hex and ASCII representations of |binary|, |
| 41 | // side-by-side in the style of hexdump. Non-printable characters will be |
| 42 | // printed as '.' in the ASCII output. |
| 43 | // For example, given the input "Hello, QUIC!\01\02\03\04", returns: |
| 44 | // "0x0000: 4865 6c6c 6f2c 2051 5549 4321 0102 0304 Hello,.QUIC!...." |
bnc | 9de6abe | 2021-04-28 06:24:19 -0700 | [diff] [blame] | 45 | static std::string HexDump(absl::string_view binary_data); |
QUICHE team | 83e0444 | 2019-12-11 06:49:49 -0800 | [diff] [blame] | 46 | |
| 47 | // Returns true if |data| contains any uppercase characters. |
vasilvv | 652cd42 | 2020-10-09 15:50:02 -0700 | [diff] [blame] | 48 | static bool ContainsUpperCase(absl::string_view data) { |
bnc | 9de6abe | 2021-04-28 06:24:19 -0700 | [diff] [blame] | 49 | return std::any_of(data.begin(), data.end(), absl::ascii_isupper); |
QUICHE team | 83e0444 | 2019-12-11 06:49:49 -0800 | [diff] [blame] | 50 | } |
| 51 | |
| 52 | // Returns true if |data| contains only decimal digits. |
vasilvv | 652cd42 | 2020-10-09 15:50:02 -0700 | [diff] [blame] | 53 | static bool IsAllDigits(absl::string_view data) { |
bnc | 9de6abe | 2021-04-28 06:24:19 -0700 | [diff] [blame] | 54 | return std::all_of(data.begin(), data.end(), absl::ascii_isdigit); |
QUICHE team | 83e0444 | 2019-12-11 06:49:49 -0800 | [diff] [blame] | 55 | } |
QUICHE team | 83e0444 | 2019-12-11 06:49:49 -0800 | [diff] [blame] | 56 | }; |
| 57 | |
| 58 | } // namespace quiche |
| 59 | |
bnc | 9de6abe | 2021-04-28 06:24:19 -0700 | [diff] [blame] | 60 | #endif // QUICHE_COMMON_QUICHE_TEXT_UTILS_H_ |