• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 "fillins/openssl_util.h"
9 
10 
11 #include <stdint.h>
12 
13 #include <cstdint>
14 #include <string_view>
15 #include <vector>
16 
17 namespace bssl::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,
25                                       std::string_view str2);
26 
27 // Compares |str1| and |prefix| ASCII case insensitively (independent of
28 // locale). Returns true if |str1| starts with |prefix|.
29 OPENSSL_EXPORT bool StartsWithNoCase(std::string_view str,
30                                          std::string_view prefix);
31 
32 // Compares |str1| and |suffix| ASCII case insensitively (independent of
33 // locale). Returns true if |str1| starts with |suffix|.
34 OPENSSL_EXPORT bool EndsWithNoCase(std::string_view str,
35                                        std::string_view suffix);
36 
37 // Finds and replaces all occurrences of |find| of non zero length with
38 // |replace| in |str|, returning the result.
39 OPENSSL_EXPORT std::string FindAndReplace(std::string_view str,
40                                               std::string_view find,
41                                               std::string_view replace);
42 
43 // TODO(bbe) transition below to c++20
44 // Compares |str1| and |prefix|. Returns true if |str1| starts with |prefix|.
45 OPENSSL_EXPORT bool StartsWith(std::string_view str,
46                                    std::string_view prefix);
47 
48 // TODO(bbe) transition below to c++20
49 // Compares |str1| and |suffix|. Returns true if |str1| ends with |suffix|.
50 OPENSSL_EXPORT bool EndsWith(std::string_view str, std::string_view suffix);
51 
52 // Returns a hexadecimal string encoding |data| of length |length|.
53 OPENSSL_EXPORT std::string HexEncode(const uint8_t* data, size_t length);
54 
55 // Returns a decimal string representation of |i|.
56 OPENSSL_EXPORT std::string NumberToDecimalString(int i);
57 
58 // Splits |str| on |split_char| returning the list of resulting strings.
59 OPENSSL_EXPORT std::vector<std::string_view> SplitString(
60     std::string_view str,
61     char split_char);
62 
63 }  // namespace bssl::string_util
64 
65 #endif  // BSSL_PKI_STRING_UTIL_H_
66