blob: c99d9e2ac028549e1587c525c86e915c3f031160 [file] [log] [blame]
QUICHE team83e04442019-12-11 06:49:49 -08001// 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
bnc9de6abe2021-04-28 06:24:19 -07005#ifndef QUICHE_COMMON_QUICHE_TEXT_UTILS_H_
6#define QUICHE_COMMON_QUICHE_TEXT_UTILS_H_
QUICHE team83e04442019-12-11 06:49:49 -08007
8#include <string>
9
bnc9de6abe2021-04-28 06:24:19 -070010#include "absl/strings/ascii.h"
vasilvv72b789c2020-10-27 17:39:17 -070011#include "absl/strings/escaping.h"
vasilvv652cd422020-10-09 15:50:02 -070012#include "absl/strings/string_view.h"
vasilvv7df418b2020-10-13 13:47:09 -070013#include "absl/types/optional.h"
QUICHE team5be974e2020-12-29 18:35:24 -050014#include "common/platform/api/quiche_export.h"
QUICHE team83e04442019-12-11 06:49:49 -080015
16namespace quiche {
17
18// Various utilities for manipulating text.
19class QUICHE_EXPORT QuicheTextUtils {
20 public:
QUICHE team83e04442019-12-11 06:49:49 -080021 // Returns a new string in which |data| has been converted to lower case.
vasilvv652cd422020-10-09 15:50:02 -070022 static std::string ToLower(absl::string_view data) {
bnc9de6abe2021-04-28 06:24:19 -070023 return absl::AsciiStrToLower(data);
QUICHE team83e04442019-12-11 06:49:49 -080024 }
25
26 // Removes leading and trailing whitespace from |data|.
vasilvv652cd422020-10-09 15:50:02 -070027 static void RemoveLeadingAndTrailingWhitespace(absl::string_view* data) {
bnc9de6abe2021-04-28 06:24:19 -070028 *data = absl::StripAsciiWhitespace(*data);
QUICHE team83e04442019-12-11 06:49:49 -080029 }
30
QUICHE team83e04442019-12-11 06:49:49 -080031 // 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,
bnc9de6abe2021-04-28 06:24:19 -070034 std::string* output);
QUICHE team83e04442019-12-11 06:49:49 -080035
vasilvva7300b82020-04-20 20:32:51 -040036 // Decodes a base64-encoded |input|. Returns nullopt when the input is
37 // invalid.
bnc9de6abe2021-04-28 06:24:19 -070038 static absl::optional<std::string> Base64Decode(absl::string_view input);
vasilvva7300b82020-04-20 20:32:51 -040039
QUICHE team83e04442019-12-11 06:49:49 -080040 // 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!...."
bnc9de6abe2021-04-28 06:24:19 -070045 static std::string HexDump(absl::string_view binary_data);
QUICHE team83e04442019-12-11 06:49:49 -080046
47 // Returns true if |data| contains any uppercase characters.
vasilvv652cd422020-10-09 15:50:02 -070048 static bool ContainsUpperCase(absl::string_view data) {
bnc9de6abe2021-04-28 06:24:19 -070049 return std::any_of(data.begin(), data.end(), absl::ascii_isupper);
QUICHE team83e04442019-12-11 06:49:49 -080050 }
51
52 // Returns true if |data| contains only decimal digits.
vasilvv652cd422020-10-09 15:50:02 -070053 static bool IsAllDigits(absl::string_view data) {
bnc9de6abe2021-04-28 06:24:19 -070054 return std::all_of(data.begin(), data.end(), absl::ascii_isdigit);
QUICHE team83e04442019-12-11 06:49:49 -080055 }
QUICHE team83e04442019-12-11 06:49:49 -080056};
57
58} // namespace quiche
59
bnc9de6abe2021-04-28 06:24:19 -070060#endif // QUICHE_COMMON_QUICHE_TEXT_UTILS_H_