xref: /aosp_15_r20/external/angle/src/common/string_utils.h (revision 8975f5c5ed3d1c378011245431ada316dfb6f244)
1 //
2 // Copyright 2015 The ANGLE Project Authors. All rights reserved.
3 // Use of this source code is governed by a BSD-style license that can be
4 // found in the LICENSE file.
5 //
6 // string_utils:
7 //   String helper functions.
8 //
9 
10 #ifndef LIBANGLE_STRING_UTILS_H_
11 #define LIBANGLE_STRING_UTILS_H_
12 
13 #include <string>
14 #include <vector>
15 
16 #include "common/Optional.h"
17 
18 namespace angle
19 {
20 
21 extern const char kWhitespaceASCII[];
22 
23 enum WhitespaceHandling
24 {
25     KEEP_WHITESPACE,
26     TRIM_WHITESPACE,
27 };
28 
29 enum SplitResult
30 {
31     SPLIT_WANT_ALL,
32     SPLIT_WANT_NONEMPTY,
33 };
34 
35 std::vector<std::string> SplitString(const std::string &input,
36                                      const std::string &delimiters,
37                                      WhitespaceHandling whitespace,
38                                      SplitResult resultType);
39 
40 void SplitStringAlongWhitespace(const std::string &input, std::vector<std::string> *tokensOut);
41 
42 std::string TrimString(const std::string &input, const std::string &trimChars);
43 
44 // Return the substring starting at offset and up to the first occurance of the |delimeter|.
45 std::string GetPrefix(const std::string &input, size_t offset, const char *delimiter);
46 std::string GetPrefix(const std::string &input, size_t offset, char delimiter);
47 
48 bool HexStringToUInt(const std::string &input, unsigned int *uintOut);
49 
50 bool ReadFileToString(const std::string &path, std::string *stringOut);
51 
52 // Check if the string str begins with the given prefix.
53 // The comparison is case sensitive.
54 bool BeginsWith(const std::string &str, const std::string &prefix);
55 
56 // Check if the string str begins with the given prefix.
57 // Prefix may not be NULL and needs to be NULL terminated.
58 // The comparison is case sensitive.
59 bool BeginsWith(const std::string &str, const char *prefix);
60 
61 // Check if the string str begins with the given prefix.
62 // str and prefix may not be NULL and need to be NULL terminated.
63 // The comparison is case sensitive.
64 bool BeginsWith(const char *str, const char *prefix);
65 
66 // Check if the string str begins with the first prefixLength characters of the given prefix.
67 // The length of the prefix string should be greater than or equal to prefixLength.
68 // The comparison is case sensitive.
69 bool BeginsWith(const std::string &str, const std::string &prefix, const size_t prefixLength);
70 
71 // Check if the string str ends with the given suffix.
72 // The comparison is case sensitive.
73 bool EndsWith(const std::string &str, const std::string &suffix);
74 
75 // Check if the string str ends with the given suffix.
76 // Suffix may not be NULL and needs to be NULL terminated.
77 // The comparison is case sensitive.
78 bool EndsWith(const std::string &str, const char *suffix);
79 
80 // Check if the string str ends with the given suffix.
81 // str and suffix may not be NULL and need to be NULL terminated.
82 // The comparison is case sensitive.
83 bool EndsWith(const char *str, const char *suffix);
84 
85 // Check if the given token string contains the given token.
86 // The tokens are separated by the given delimiter.
87 // The comparison is case sensitive.
88 bool ContainsToken(const std::string &tokenStr, char delimiter, const std::string &token);
89 
90 // Convert to lower-case.
91 void ToLower(std::string *str);
92 
93 // Convert to upper-case.
94 void ToUpper(std::string *str);
95 
96 // Replaces the substring 'substring' in 'str' with 'replacement'. Returns true if successful.
97 bool ReplaceSubstring(std::string *str,
98                       const std::string &substring,
99                       const std::string &replacement);
100 
101 // Replaces all substrings 'substring' in 'str' with 'replacement'. Returns count of replacements.
102 int ReplaceAllSubstrings(std::string *str,
103                          const std::string &substring,
104                          const std::string &replacement);
105 
106 // Takes a snake_case string and turns it into camelCase.
107 std::string ToCamelCase(const std::string &str);
108 
109 // Split up a string parsed from an environment variable.
110 std::vector<std::string> GetStringsFromEnvironmentVarOrAndroidProperty(const char *varName,
111                                                                        const char *propertyName,
112                                                                        const char *separator);
113 
114 // Split up a string parsed from environment variable or via Android property, use cached result if
115 // available.
116 std::vector<std::string> GetCachedStringsFromEnvironmentVarOrAndroidProperty(
117     const char *varName,
118     const char *propertyName,
119     const char *separator);
120 
121 // glob can have * as wildcard
122 bool NamesMatchWithWildcard(const char *glob, const char *name);
123 }  // namespace angle
124 
125 #endif  // LIBANGLE_STRING_UTILS_H_
126