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