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 | #include "string_utils.h" |
11 | |
12 | #include <stdlib.h> |
13 | #include <string.h> |
14 | #include <algorithm> |
15 | #include <fstream> |
16 | #include <sstream> |
17 | |
18 | #include "common/platform.h" |
19 | |
20 | namespace angle |
21 | { |
22 | |
23 | const char kWhitespaceASCII[] = " \f\n\r\t\v" ; |
24 | |
25 | std::vector<std::string> SplitString(const std::string &input, |
26 | const std::string &delimiters, |
27 | WhitespaceHandling whitespace, |
28 | SplitResult resultType) |
29 | { |
30 | std::vector<std::string> result; |
31 | if (input.empty()) |
32 | { |
33 | return result; |
34 | } |
35 | |
36 | std::string::size_type start = 0; |
37 | while (start != std::string::npos) |
38 | { |
39 | auto end = input.find_first_of(delimiters, start); |
40 | |
41 | std::string piece; |
42 | if (end == std::string::npos) |
43 | { |
44 | piece = input.substr(start); |
45 | start = std::string::npos; |
46 | } |
47 | else |
48 | { |
49 | piece = input.substr(start, end - start); |
50 | start = end + 1; |
51 | } |
52 | |
53 | if (whitespace == TRIM_WHITESPACE) |
54 | { |
55 | piece = TrimString(piece, kWhitespaceASCII); |
56 | } |
57 | |
58 | if (resultType == SPLIT_WANT_ALL || !piece.empty()) |
59 | { |
60 | result.push_back(piece); |
61 | } |
62 | } |
63 | |
64 | return result; |
65 | } |
66 | |
67 | void SplitStringAlongWhitespace(const std::string &input, std::vector<std::string> *tokensOut) |
68 | { |
69 | |
70 | std::istringstream stream(input); |
71 | std::string line; |
72 | |
73 | while (std::getline(stream, line)) |
74 | { |
75 | size_t prev = 0, pos; |
76 | while ((pos = line.find_first_of(kWhitespaceASCII, prev)) != std::string::npos) |
77 | { |
78 | if (pos > prev) |
79 | tokensOut->push_back(line.substr(prev, pos - prev)); |
80 | prev = pos + 1; |
81 | } |
82 | if (prev < line.length()) |
83 | tokensOut->push_back(line.substr(prev, std::string::npos)); |
84 | } |
85 | } |
86 | |
87 | std::string TrimString(const std::string &input, const std::string &trimChars) |
88 | { |
89 | auto begin = input.find_first_not_of(trimChars); |
90 | if (begin == std::string::npos) |
91 | { |
92 | return "" ; |
93 | } |
94 | |
95 | std::string::size_type end = input.find_last_not_of(trimChars); |
96 | if (end == std::string::npos) |
97 | { |
98 | return input.substr(begin); |
99 | } |
100 | |
101 | return input.substr(begin, end - begin + 1); |
102 | } |
103 | |
104 | bool HexStringToUInt(const std::string &input, unsigned int *uintOut) |
105 | { |
106 | unsigned int offset = 0; |
107 | |
108 | if (input.size() >= 2 && input[0] == '0' && input[1] == 'x') |
109 | { |
110 | offset = 2u; |
111 | } |
112 | |
113 | // Simple validity check |
114 | if (input.find_first_not_of("0123456789ABCDEFabcdef" , offset) != std::string::npos) |
115 | { |
116 | return false; |
117 | } |
118 | |
119 | std::stringstream inStream(input); |
120 | inStream >> std::hex >> *uintOut; |
121 | return !inStream.fail(); |
122 | } |
123 | |
124 | bool ReadFileToString(const std::string &path, std::string *stringOut) |
125 | { |
126 | std::ifstream inFile(path.c_str()); |
127 | if (inFile.fail()) |
128 | { |
129 | return false; |
130 | } |
131 | |
132 | inFile.seekg(0, std::ios::end); |
133 | stringOut->reserve(static_cast<std::string::size_type>(inFile.tellg())); |
134 | inFile.seekg(0, std::ios::beg); |
135 | |
136 | stringOut->assign(std::istreambuf_iterator<char>(inFile), std::istreambuf_iterator<char>()); |
137 | return !inFile.fail(); |
138 | } |
139 | |
140 | Optional<std::vector<wchar_t>> WidenString(size_t length, const char *cString) |
141 | { |
142 | std::vector<wchar_t> wcstring(length + 1); |
143 | #if !defined(ANGLE_PLATFORM_WINDOWS) |
144 | mbstate_t mbstate = {}; |
145 | size_t written = mbsrtowcs(wcstring.data(), &cString, length + 1, &mbstate); |
146 | if (written == 0) |
147 | { |
148 | return Optional<std::vector<wchar_t>>::Invalid(); |
149 | } |
150 | #else |
151 | size_t convertedChars = 0; |
152 | errno_t err = mbstowcs_s(&convertedChars, wcstring.data(), length + 1, cString, _TRUNCATE); |
153 | if (err != 0) |
154 | { |
155 | return Optional<std::vector<wchar_t>>::Invalid(); |
156 | } |
157 | #endif |
158 | return Optional<std::vector<wchar_t>>(wcstring); |
159 | } |
160 | |
161 | bool BeginsWith(const std::string &str, const std::string &prefix) |
162 | { |
163 | return strncmp(str.c_str(), prefix.c_str(), prefix.length()) == 0; |
164 | } |
165 | |
166 | bool BeginsWith(const std::string &str, const char *prefix) |
167 | { |
168 | return strncmp(str.c_str(), prefix, strlen(prefix)) == 0; |
169 | } |
170 | |
171 | bool BeginsWith(const char *str, const char *prefix) |
172 | { |
173 | return strncmp(str, prefix, strlen(prefix)) == 0; |
174 | } |
175 | |
176 | bool BeginsWith(const std::string &str, const std::string &prefix, const size_t prefixLength) |
177 | { |
178 | return strncmp(str.c_str(), prefix.c_str(), prefixLength) == 0; |
179 | } |
180 | |
181 | bool EndsWith(const std::string &str, const char *suffix) |
182 | { |
183 | const auto len = strlen(suffix); |
184 | if (len > str.size()) |
185 | return false; |
186 | |
187 | const char *end = str.c_str() + str.size() - len; |
188 | |
189 | return memcmp(end, suffix, len) == 0; |
190 | } |
191 | |
192 | void ToLower(std::string *str) |
193 | { |
194 | for (auto &ch : *str) |
195 | { |
196 | ch = static_cast<char>(::tolower(ch)); |
197 | } |
198 | } |
199 | |
200 | bool ReplaceSubstring(std::string *str, |
201 | const std::string &substring, |
202 | const std::string &replacement) |
203 | { |
204 | size_t replacePos = str->find(substring); |
205 | if (replacePos == std::string::npos) |
206 | { |
207 | return false; |
208 | } |
209 | str->replace(replacePos, substring.size(), replacement); |
210 | return true; |
211 | } |
212 | |
213 | } // namespace angle |
214 | |