1 // Copyright 2022 The Chromium Authors
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 //     https://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14 
15 #ifndef BSSL_PKI_STRING_UTIL_H_
16 #define BSSL_PKI_STRING_UTIL_H_
17 
18 #include <cstdint>
19 #include <string>
20 #include <string_view>
21 #include <vector>
22 
23 #include <openssl/base.h>
24 #include <openssl/span.h>
25 
26 BSSL_NAMESPACE_BEGIN
27 namespace string_util {
28 
29 // Returns true if the characters in |str| are all ASCII, false otherwise.
30 OPENSSL_EXPORT bool IsAscii(std::string_view str);
31 
32 // Compares |str1| and |str2| ASCII case insensitively (independent of locale).
33 // Returns true if |str1| and |str2| match.
34 OPENSSL_EXPORT bool IsEqualNoCase(std::string_view str1, std::string_view str2);
35 
36 // Compares |str1| and |prefix| ASCII case insensitively (independent of
37 // locale). Returns true if |str1| starts with |prefix|.
38 OPENSSL_EXPORT bool StartsWithNoCase(std::string_view str,
39                                      std::string_view prefix);
40 
41 // Compares |str1| and |suffix| ASCII case insensitively (independent of
42 // locale). Returns true if |str1| starts with |suffix|.
43 OPENSSL_EXPORT bool EndsWithNoCase(std::string_view str,
44                                    std::string_view suffix);
45 
46 // Finds and replaces all occurrences of |find| of non zero length with
47 // |replace| in |str|, returning the result.
48 OPENSSL_EXPORT std::string FindAndReplace(std::string_view str,
49                                           std::string_view find,
50                                           std::string_view replace);
51 
52 // TODO(bbe) transition below to c++20
53 // Compares |str1| and |prefix|. Returns true if |str1| starts with |prefix|.
54 OPENSSL_EXPORT bool StartsWith(std::string_view str, std::string_view prefix);
55 
56 // TODO(bbe) transition below to c++20
57 // Compares |str1| and |suffix|. Returns true if |str1| ends with |suffix|.
58 OPENSSL_EXPORT bool EndsWith(std::string_view str, std::string_view suffix);
59 
60 // Returns a hexadecimal string encoding |data|.
61 OPENSSL_EXPORT std::string HexEncode(Span<const uint8_t> data);
62 
63 // Returns a decimal string representation of |i|.
64 OPENSSL_EXPORT std::string NumberToDecimalString(int i);
65 
66 // Splits |str| on |split_char| returning the list of resulting strings.
67 OPENSSL_EXPORT std::vector<std::string_view> SplitString(std::string_view str,
68                                                          char split_char);
69 
70 // Collapess whitespace in |text| to a single space and returns the result.
71 OPENSSL_EXPORT std::string CollapseWhitespaceASCII(
72     std::string_view text, bool trim_sequences_with_line_breaks);
73 
74 // Base64 encodes |input| into |output| returning true on success,
75 // false otherwise.
76 OPENSSL_EXPORT bool Base64Encode(const std::string_view &input,
77                                  std::string *output);
78 
79 // Base64 decodes |input| into |output| returning true on success,
80 // false otherwise.
81 OPENSSL_EXPORT bool Base64Decode(const std::string_view &input,
82                                  std::string *output);
83 
84 }  // namespace string_util
85 BSSL_NAMESPACE_END
86 
87 #endif  // BSSL_PKI_STRING_UTIL_H_
88