1 // Copyright 2022 The Chromium Authors 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 BSSL_PKI_STRING_UTIL_H_ 6 #define BSSL_PKI_STRING_UTIL_H_ 7 8 #include <cstdint> 9 #include <string> 10 #include <string_view> 11 #include <vector> 12 13 #include <openssl/base.h> 14 #include <openssl/span.h> 15 16 BSSL_NAMESPACE_BEGIN 17 namespace string_util { 18 19 // Returns true if the characters in |str| are all ASCII, false otherwise. 20 OPENSSL_EXPORT bool IsAscii(std::string_view str); 21 22 // Compares |str1| and |str2| ASCII case insensitively (independent of locale). 23 // Returns true if |str1| and |str2| match. 24 OPENSSL_EXPORT bool IsEqualNoCase(std::string_view str1, std::string_view str2); 25 26 // Compares |str1| and |prefix| ASCII case insensitively (independent of 27 // locale). Returns true if |str1| starts with |prefix|. 28 OPENSSL_EXPORT bool StartsWithNoCase(std::string_view str, 29 std::string_view prefix); 30 31 // Compares |str1| and |suffix| ASCII case insensitively (independent of 32 // locale). Returns true if |str1| starts with |suffix|. 33 OPENSSL_EXPORT bool EndsWithNoCase(std::string_view str, 34 std::string_view suffix); 35 36 // Finds and replaces all occurrences of |find| of non zero length with 37 // |replace| in |str|, returning the result. 38 OPENSSL_EXPORT std::string FindAndReplace(std::string_view str, 39 std::string_view find, 40 std::string_view replace); 41 42 // TODO(bbe) transition below to c++20 43 // Compares |str1| and |prefix|. Returns true if |str1| starts with |prefix|. 44 OPENSSL_EXPORT bool StartsWith(std::string_view str, std::string_view prefix); 45 46 // TODO(bbe) transition below to c++20 47 // Compares |str1| and |suffix|. Returns true if |str1| ends with |suffix|. 48 OPENSSL_EXPORT bool EndsWith(std::string_view str, std::string_view suffix); 49 50 // Returns a hexadecimal string encoding |data|. 51 OPENSSL_EXPORT std::string HexEncode(Span<const uint8_t> data); 52 53 // Returns a decimal string representation of |i|. 54 OPENSSL_EXPORT std::string NumberToDecimalString(int i); 55 56 // Splits |str| on |split_char| returning the list of resulting strings. 57 OPENSSL_EXPORT std::vector<std::string_view> SplitString(std::string_view str, 58 char split_char); 59 60 // Collapess whitespace in |text| to a single space and returns the result. 61 OPENSSL_EXPORT std::string CollapseWhitespaceASCII( 62 std::string_view text, bool trim_sequences_with_line_breaks); 63 64 // Base64 encodes |input| into |output| returning true on success, 65 // false otherwise. 66 OPENSSL_EXPORT bool Base64Encode(const std::string_view &input, 67 std::string *output); 68 69 // Base64 decodes |input| into |output| returning true on success, 70 // false otherwise. 71 OPENSSL_EXPORT bool Base64Decode(const std::string_view &input, 72 std::string *output); 73 74 } // namespace string_util 75 BSSL_NAMESPACE_END 76 77 #endif // BSSL_PKI_STRING_UTIL_H_ 78