blob: dfa92d79515435828efd876955473dbe7f07b8d6 [file] [log] [blame]
Adam Lesinski6f6ceb72014-11-14 14:48:12 -08001/*
2 * Copyright (C) 2015 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
Adam Lesinskice5e56e2016-10-21 17:56:45 -070017#include "util/Util.h"
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080018
19#include <algorithm>
20#include <ostream>
21#include <string>
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080022#include <vector>
23
Adam Lesinskid5083f62017-01-16 15:07:21 -080024#include "androidfw/StringPiece.h"
Adam Lesinski549e4372017-06-27 18:39:07 -070025#include "utils/Unicode.h"
Adam Lesinskid5083f62017-01-16 15:07:21 -080026
27#include "util/BigBuffer.h"
28#include "util/Maybe.h"
Adam Lesinski549e4372017-06-27 18:39:07 -070029#include "util/Utf8Iterator.h"
Adam Lesinskid5083f62017-01-16 15:07:21 -080030
Adam Lesinski549e4372017-06-27 18:39:07 -070031using ::android::StringPiece;
32using ::android::StringPiece16;
Adam Lesinskid5083f62017-01-16 15:07:21 -080033
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080034namespace aapt {
35namespace util {
36
Adam Lesinskice5e56e2016-10-21 17:56:45 -070037static std::vector<std::string> SplitAndTransform(
38 const StringPiece& str, char sep, const std::function<char(char)>& f) {
39 std::vector<std::string> parts;
40 const StringPiece::const_iterator end = std::end(str);
41 StringPiece::const_iterator start = std::begin(str);
42 StringPiece::const_iterator current;
43 do {
44 current = std::find(start, end, sep);
Adam Lesinskid5083f62017-01-16 15:07:21 -080045 parts.emplace_back(str.substr(start, current).to_string());
Adam Lesinskice5e56e2016-10-21 17:56:45 -070046 if (f) {
47 std::string& part = parts.back();
48 std::transform(part.begin(), part.end(), part.begin(), f);
49 }
50 start = current + 1;
51 } while (current != end);
52 return parts;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080053}
54
Adam Lesinskice5e56e2016-10-21 17:56:45 -070055std::vector<std::string> Split(const StringPiece& str, char sep) {
56 return SplitAndTransform(str, sep, nullptr);
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080057}
58
Adam Lesinskice5e56e2016-10-21 17:56:45 -070059std::vector<std::string> SplitAndLowercase(const StringPiece& str, char sep) {
60 return SplitAndTransform(str, sep, ::tolower);
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080061}
62
Adam Lesinskice5e56e2016-10-21 17:56:45 -070063bool StartsWith(const StringPiece& str, const StringPiece& prefix) {
64 if (str.size() < prefix.size()) {
65 return false;
66 }
67 return str.substr(0, prefix.size()) == prefix;
Adam Lesinskid0f116b2016-07-08 15:00:32 -070068}
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080069
Adam Lesinskice5e56e2016-10-21 17:56:45 -070070bool EndsWith(const StringPiece& str, const StringPiece& suffix) {
71 if (str.size() < suffix.size()) {
72 return false;
73 }
74 return str.substr(str.size() - suffix.size(), suffix.size()) == suffix;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080075}
76
Adam Lesinskice5e56e2016-10-21 17:56:45 -070077StringPiece TrimWhitespace(const StringPiece& str) {
78 if (str.size() == 0 || str.data() == nullptr) {
79 return str;
80 }
Adam Lesinski3b4cd942015-10-30 16:31:42 -070081
Adam Lesinskice5e56e2016-10-21 17:56:45 -070082 const char* start = str.data();
83 const char* end = str.data() + str.length();
Adam Lesinski3b4cd942015-10-30 16:31:42 -070084
Adam Lesinskice5e56e2016-10-21 17:56:45 -070085 while (start != end && isspace(*start)) {
86 start++;
87 }
Adam Lesinski3b4cd942015-10-30 16:31:42 -070088
Adam Lesinskice5e56e2016-10-21 17:56:45 -070089 while (end != start && isspace(*(end - 1))) {
90 end--;
91 }
Adam Lesinski3b4cd942015-10-30 16:31:42 -070092
Adam Lesinskice5e56e2016-10-21 17:56:45 -070093 return StringPiece(start, end - start);
Adam Lesinski3b4cd942015-10-30 16:31:42 -070094}
95
Adam Lesinskice5e56e2016-10-21 17:56:45 -070096StringPiece::const_iterator FindNonAlphaNumericAndNotInSet(
97 const StringPiece& str, const StringPiece& allowed_chars) {
98 const auto end_iter = str.end();
99 for (auto iter = str.begin(); iter != end_iter; ++iter) {
100 char c = *iter;
101 if ((c >= u'a' && c <= u'z') || (c >= u'A' && c <= u'Z') ||
102 (c >= u'0' && c <= u'9')) {
103 continue;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800104 }
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700105
106 bool match = false;
107 for (char i : allowed_chars) {
108 if (c == i) {
109 match = true;
110 break;
111 }
112 }
113
114 if (!match) {
115 return iter;
116 }
117 }
118 return end_iter;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800119}
120
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700121bool IsJavaClassName(const StringPiece& str) {
122 size_t pieces = 0;
123 for (const StringPiece& piece : Tokenize(str, '.')) {
124 pieces++;
125 if (piece.empty()) {
126 return false;
Adam Lesinskia1ad4a82015-06-08 11:41:09 -0700127 }
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700128
129 // Can't have starting or trailing $ character.
130 if (piece.data()[0] == '$' || piece.data()[piece.size() - 1] == '$') {
131 return false;
132 }
133
134 if (FindNonAlphaNumericAndNotInSet(piece, "$_") != piece.end()) {
135 return false;
136 }
137 }
138 return pieces >= 2;
Adam Lesinskia1ad4a82015-06-08 11:41:09 -0700139}
140
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700141bool IsJavaPackageName(const StringPiece& str) {
142 if (str.empty()) {
143 return false;
144 }
145
146 size_t pieces = 0;
147 for (const StringPiece& piece : Tokenize(str, '.')) {
148 pieces++;
149 if (piece.empty()) {
150 return false;
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700151 }
152
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700153 if (piece.data()[0] == '_' || piece.data()[piece.size() - 1] == '_') {
154 return false;
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700155 }
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700156
157 if (FindNonAlphaNumericAndNotInSet(piece, "_") != piece.end()) {
158 return false;
159 }
160 }
161 return pieces >= 1;
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700162}
163
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700164Maybe<std::string> GetFullyQualifiedClassName(const StringPiece& package,
165 const StringPiece& classname) {
166 if (classname.empty()) {
167 return {};
168 }
Adam Lesinskia1ad4a82015-06-08 11:41:09 -0700169
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700170 if (util::IsJavaClassName(classname)) {
Adam Lesinskid5083f62017-01-16 15:07:21 -0800171 return classname.to_string();
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700172 }
Adam Lesinskia1ad4a82015-06-08 11:41:09 -0700173
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700174 if (package.empty()) {
175 return {};
176 }
Adam Lesinskia1ad4a82015-06-08 11:41:09 -0700177
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700178 std::string result(package.data(), package.size());
179 if (classname.data()[0] != '.') {
180 result += '.';
181 }
Adam Lesinski52364f72016-01-11 13:10:24 -0800182
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700183 result.append(classname.data(), classname.size());
184 if (!IsJavaClassName(result)) {
185 return {};
186 }
187 return result;
Adam Lesinskia1ad4a82015-06-08 11:41:09 -0700188}
189
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700190static size_t ConsumeDigits(const char* start, const char* end) {
191 const char* c = start;
192 for (; c != end && *c >= '0' && *c <= '9'; c++) {
193 }
194 return static_cast<size_t>(c - start);
Adam Lesinskib23f1e02015-11-03 12:24:17 -0800195}
196
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700197bool VerifyJavaStringFormat(const StringPiece& str) {
198 const char* c = str.begin();
199 const char* const end = str.end();
Adam Lesinskib23f1e02015-11-03 12:24:17 -0800200
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700201 size_t arg_count = 0;
202 bool nonpositional = false;
203 while (c != end) {
204 if (*c == '%' && c + 1 < end) {
205 c++;
Adam Lesinskib23f1e02015-11-03 12:24:17 -0800206
Adam Lesinskib9f05482017-06-02 16:32:37 -0700207 if (*c == '%' || *c == 'n') {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700208 c++;
209 continue;
210 }
Adam Lesinskib23f1e02015-11-03 12:24:17 -0800211
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700212 arg_count++;
Adam Lesinskib23f1e02015-11-03 12:24:17 -0800213
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700214 size_t num_digits = ConsumeDigits(c, end);
215 if (num_digits > 0) {
216 c += num_digits;
217 if (c != end && *c != '$') {
218 // The digits were a size, but not a positional argument.
219 nonpositional = true;
Adam Lesinskib23f1e02015-11-03 12:24:17 -0800220 }
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700221 } else if (*c == '<') {
222 // Reusing last argument, bad idea since positions can be moved around
223 // during translation.
224 nonpositional = true;
Adam Lesinskib23f1e02015-11-03 12:24:17 -0800225
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700226 c++;
227
228 // Optionally we can have a $ after
229 if (c != end && *c == '$') {
230 c++;
Adam Lesinskib23f1e02015-11-03 12:24:17 -0800231 }
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700232 } else {
233 nonpositional = true;
234 }
235
236 // Ignore size, width, flags, etc.
237 while (c != end && (*c == '-' || *c == '#' || *c == '+' || *c == ' ' ||
238 *c == ',' || *c == '(' || (*c >= '0' && *c <= '9'))) {
239 c++;
240 }
241
242 /*
243 * This is a shortcut to detect strings that are going to Time.format()
244 * instead of String.format()
245 *
246 * Comparison of String.format() and Time.format() args:
247 *
248 * String: ABC E GH ST X abcdefgh nost x
249 * Time: DEFGHKMS W Za d hkm s w yz
250 *
251 * Therefore we know it's definitely Time if we have:
252 * DFKMWZkmwyz
253 */
254 if (c != end) {
255 switch (*c) {
256 case 'D':
257 case 'F':
258 case 'K':
259 case 'M':
260 case 'W':
261 case 'Z':
262 case 'k':
263 case 'm':
264 case 'w':
265 case 'y':
266 case 'z':
267 return true;
268 }
269 }
Adam Lesinskib23f1e02015-11-03 12:24:17 -0800270 }
271
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700272 if (c != end) {
273 c++;
Adam Lesinskib23f1e02015-11-03 12:24:17 -0800274 }
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700275 }
276
277 if (arg_count > 1 && nonpositional) {
278 // Multiple arguments were specified, but some or all were non positional.
279 // Translated
280 // strings may rearrange the order of the arguments, which will break the
281 // string.
282 return false;
283 }
284 return true;
Adam Lesinskib23f1e02015-11-03 12:24:17 -0800285}
286
Adam Lesinski549e4372017-06-27 18:39:07 -0700287static bool AppendCodepointToUtf8String(char32_t codepoint, std::string* output) {
288 ssize_t len = utf32_to_utf8_length(&codepoint, 1);
289 if (len < 0) {
290 return false;
291 }
292
293 const size_t start_append_pos = output->size();
294
295 // Make room for the next character.
296 output->resize(output->size() + len);
297
298 char* dst = &*(output->begin() + start_append_pos);
299 utf32_to_utf8(&codepoint, 1, dst, len + 1);
300 return true;
301}
302
303static bool AppendUnicodeCodepoint(Utf8Iterator* iter, std::string* output) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700304 char32_t code = 0;
Adam Lesinski549e4372017-06-27 18:39:07 -0700305 for (size_t i = 0; i < 4 && iter->HasNext(); i++) {
306 char32_t codepoint = iter->Next();
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700307 char32_t a;
Adam Lesinski549e4372017-06-27 18:39:07 -0700308 if (codepoint >= U'0' && codepoint <= U'9') {
309 a = codepoint - U'0';
310 } else if (codepoint >= U'a' && codepoint <= U'f') {
311 a = codepoint - U'a' + 10;
312 } else if (codepoint >= U'A' && codepoint <= U'F') {
313 a = codepoint - U'A' + 10;
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700314 } else {
315 return {};
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800316 }
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700317 code = (code << 4) | a;
318 }
Adam Lesinski549e4372017-06-27 18:39:07 -0700319 return AppendCodepointToUtf8String(code, output);
320}
Adam Lesinskid0f116b2016-07-08 15:00:32 -0700321
Adam Lesinski549e4372017-06-27 18:39:07 -0700322static bool IsCodepointSpace(char32_t codepoint) {
323 if (static_cast<uint32_t>(codepoint) & 0xffffff00u) {
324 return false;
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700325 }
Adam Lesinski549e4372017-06-27 18:39:07 -0700326 return isspace(static_cast<char>(codepoint));
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800327}
328
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700329StringBuilder& StringBuilder::Append(const StringPiece& str) {
330 if (!error_.empty()) {
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800331 return *this;
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700332 }
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800333
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700334 // Where the new data will be appended to.
Adam Lesinski549e4372017-06-27 18:39:07 -0700335 const size_t new_data_index = str_.size();
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800336
Adam Lesinski549e4372017-06-27 18:39:07 -0700337 Utf8Iterator iter(str);
338 while (iter.HasNext()) {
339 const char32_t codepoint = iter.Next();
340
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700341 if (last_char_was_escape_) {
Adam Lesinski549e4372017-06-27 18:39:07 -0700342 switch (codepoint) {
343 case U't':
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700344 str_ += '\t';
345 break;
Adam Lesinski549e4372017-06-27 18:39:07 -0700346
347 case U'n':
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700348 str_ += '\n';
349 break;
Adam Lesinski549e4372017-06-27 18:39:07 -0700350
351 case U'#':
352 case U'@':
353 case U'?':
354 case U'"':
355 case U'\'':
356 case U'\\':
357 str_ += static_cast<char>(codepoint);
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700358 break;
Adam Lesinski549e4372017-06-27 18:39:07 -0700359
360 case U'u':
361 if (!AppendUnicodeCodepoint(&iter, &str_)) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700362 error_ = "invalid unicode escape sequence";
363 return *this;
364 }
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700365 break;
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700366
367 default:
Adam Lesinski549e4372017-06-27 18:39:07 -0700368 // Ignore the escape character and just include the codepoint.
369 AppendCodepointToUtf8String(codepoint, &str_);
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700370 break;
371 }
372 last_char_was_escape_ = false;
Adam Lesinski549e4372017-06-27 18:39:07 -0700373
374 } else if (codepoint == U'"') {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700375 if (!quote_ && trailing_space_) {
376 // We found an opening quote, and we have
377 // trailing space, so we should append that
378 // space now.
379 if (trailing_space_) {
380 // We had trailing whitespace, so
381 // replace with a single space.
382 if (!str_.empty()) {
383 str_ += ' ';
384 }
385 trailing_space_ = false;
386 }
387 }
388 quote_ = !quote_;
Adam Lesinski549e4372017-06-27 18:39:07 -0700389
390 } else if (codepoint == U'\'' && !quote_) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700391 // This should be escaped.
392 error_ = "unescaped apostrophe";
393 return *this;
Adam Lesinski549e4372017-06-27 18:39:07 -0700394
395 } else if (codepoint == U'\\') {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700396 // This is an escape sequence, convert to the real value.
397 if (!quote_ && trailing_space_) {
398 // We had trailing whitespace, so
399 // replace with a single space.
400 if (!str_.empty()) {
401 str_ += ' ';
402 }
403 trailing_space_ = false;
404 }
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700405 last_char_was_escape_ = true;
Adam Lesinski549e4372017-06-27 18:39:07 -0700406 } else {
407 if (quote_) {
408 // Quotes mean everything is taken, including whitespace.
409 AppendCodepointToUtf8String(codepoint, &str_);
410 } else {
411 // This is not quoted text, so we will accumulate whitespace and only emit a single
412 // character of whitespace if it is followed by a non-whitespace character.
413 if (IsCodepointSpace(codepoint)) {
414 // We found whitespace.
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700415 trailing_space_ = true;
Adam Lesinski549e4372017-06-27 18:39:07 -0700416 } else {
417 if (trailing_space_) {
418 // We saw trailing space before, so replace all
419 // that trailing space with one space.
420 if (!str_.empty()) {
421 str_ += ' ';
422 }
423 trailing_space_ = false;
424 }
425 AppendCodepointToUtf8String(codepoint, &str_);
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700426 }
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700427 }
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800428 }
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700429 }
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700430
431 // Accumulate the added string's UTF-16 length.
Adam Lesinski549e4372017-06-27 18:39:07 -0700432 ssize_t len = utf8_to_utf16_length(reinterpret_cast<const uint8_t*>(str_.data()) + new_data_index,
433 str_.size() - new_data_index);
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700434 if (len < 0) {
435 error_ = "invalid unicode code point";
436 return *this;
437 }
438 utf16_len_ += len;
439 return *this;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800440}
441
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700442std::u16string Utf8ToUtf16(const StringPiece& utf8) {
443 ssize_t utf16_length = utf8_to_utf16_length(
444 reinterpret_cast<const uint8_t*>(utf8.data()), utf8.length());
445 if (utf16_length <= 0) {
446 return {};
447 }
448
449 std::u16string utf16;
450 utf16.resize(utf16_length);
451 utf8_to_utf16(reinterpret_cast<const uint8_t*>(utf8.data()), utf8.length(),
452 &*utf16.begin(), utf16_length + 1);
453 return utf16;
454}
455
456std::string Utf16ToUtf8(const StringPiece16& utf16) {
457 ssize_t utf8_length = utf16_to_utf8_length(utf16.data(), utf16.length());
458 if (utf8_length <= 0) {
459 return {};
460 }
461
462 std::string utf8;
463 utf8.resize(utf8_length);
464 utf16_to_utf8(utf16.data(), utf16.length(), &*utf8.begin(), utf8_length + 1);
465 return utf8;
466}
467
468bool WriteAll(std::ostream& out, const BigBuffer& buffer) {
469 for (const auto& b : buffer) {
470 if (!out.write(reinterpret_cast<const char*>(b.buffer.get()), b.size)) {
471 return false;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800472 }
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700473 }
474 return true;
475}
476
477std::unique_ptr<uint8_t[]> Copy(const BigBuffer& buffer) {
478 std::unique_ptr<uint8_t[]> data =
479 std::unique_ptr<uint8_t[]>(new uint8_t[buffer.size()]);
480 uint8_t* p = data.get();
481 for (const auto& block : buffer) {
482 memcpy(p, block.buffer.get(), block.size);
483 p += block.size;
484 }
485 return data;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800486}
487
Adam Lesinskid0f116b2016-07-08 15:00:32 -0700488typename Tokenizer::iterator& Tokenizer::iterator::operator++() {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700489 const char* start = token_.end();
490 const char* end = str_.end();
491 if (start == end) {
492 end_ = true;
493 token_.assign(token_.end(), 0);
Adam Lesinskid0f116b2016-07-08 15:00:32 -0700494 return *this;
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700495 }
496
497 start += 1;
498 const char* current = start;
499 while (current != end) {
500 if (*current == separator_) {
501 token_.assign(start, current - start);
502 return *this;
503 }
504 ++current;
505 }
506 token_.assign(start, end - start);
507 return *this;
Adam Lesinskid0f116b2016-07-08 15:00:32 -0700508}
509
510bool Tokenizer::iterator::operator==(const iterator& rhs) const {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700511 // We check equality here a bit differently.
512 // We need to know that the addresses are the same.
513 return token_.begin() == rhs.token_.begin() &&
514 token_.end() == rhs.token_.end() && end_ == rhs.end_;
Adam Lesinskid0f116b2016-07-08 15:00:32 -0700515}
516
517bool Tokenizer::iterator::operator!=(const iterator& rhs) const {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700518 return !(*this == rhs);
Adam Lesinskid0f116b2016-07-08 15:00:32 -0700519}
520
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700521Tokenizer::iterator::iterator(StringPiece s, char sep, StringPiece tok,
522 bool end)
523 : str_(s), separator_(sep), token_(tok), end_(end) {}
Adam Lesinskid0f116b2016-07-08 15:00:32 -0700524
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700525Tokenizer::Tokenizer(StringPiece str, char sep)
526 : begin_(++iterator(str, sep, StringPiece(str.begin() - 1, 0), false)),
527 end_(str, sep, StringPiece(str.end(), 0), true) {}
Adam Lesinskid0f116b2016-07-08 15:00:32 -0700528
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700529bool ExtractResFilePathParts(const StringPiece& path, StringPiece* out_prefix,
530 StringPiece* out_entry, StringPiece* out_suffix) {
531 const StringPiece res_prefix("res/");
532 if (!StartsWith(path, res_prefix)) {
533 return false;
534 }
535
536 StringPiece::const_iterator last_occurence = path.end();
537 for (auto iter = path.begin() + res_prefix.size(); iter != path.end();
538 ++iter) {
539 if (*iter == '/') {
540 last_occurence = iter;
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700541 }
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700542 }
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700543
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700544 if (last_occurence == path.end()) {
545 return false;
546 }
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700547
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700548 auto iter = std::find(last_occurence, path.end(), '.');
549 *out_suffix = StringPiece(iter, path.end() - iter);
550 *out_entry = StringPiece(last_occurence + 1, iter - last_occurence - 1);
551 *out_prefix = StringPiece(path.begin(), last_occurence - path.begin() + 1);
552 return true;
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700553}
554
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700555StringPiece16 GetString16(const android::ResStringPool& pool, size_t idx) {
556 size_t len;
557 const char16_t* str = pool.stringAt(idx, &len);
558 if (str != nullptr) {
559 return StringPiece16(str, len);
560 }
561 return StringPiece16();
Adam Lesinskid0f116b2016-07-08 15:00:32 -0700562}
563
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700564std::string GetString(const android::ResStringPool& pool, size_t idx) {
565 size_t len;
566 const char* str = pool.string8At(idx, &len);
567 if (str != nullptr) {
568 return std::string(str, len);
569 }
570 return Utf16ToUtf8(GetString16(pool, idx));
Adam Lesinskid0f116b2016-07-08 15:00:32 -0700571}
572
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700573} // namespace util
574} // namespace aapt