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