blob: a9b49d9d99042c5a82a03c3782cb6d939b5ec3e4 [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
Adam Lesinski66ea8402017-06-28 11:44:11 -070027#include "text/Utf8Iterator.h"
Adam Lesinskid5083f62017-01-16 15:07:21 -080028#include "util/BigBuffer.h"
29#include "util/Maybe.h"
30
Adam Lesinski66ea8402017-06-28 11:44:11 -070031using ::aapt::text::Utf8Iterator;
Adam Lesinski549e4372017-06-27 18:39:07 -070032using ::android::StringPiece;
33using ::android::StringPiece16;
Adam Lesinskid5083f62017-01-16 15:07:21 -080034
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080035namespace aapt {
36namespace util {
37
Adam Lesinskice5e56e2016-10-21 17:56:45 -070038static std::vector<std::string> SplitAndTransform(
39 const StringPiece& str, char sep, const std::function<char(char)>& f) {
40 std::vector<std::string> parts;
41 const StringPiece::const_iterator end = std::end(str);
42 StringPiece::const_iterator start = std::begin(str);
43 StringPiece::const_iterator current;
44 do {
45 current = std::find(start, end, sep);
Adam Lesinskid5083f62017-01-16 15:07:21 -080046 parts.emplace_back(str.substr(start, current).to_string());
Adam Lesinskice5e56e2016-10-21 17:56:45 -070047 if (f) {
48 std::string& part = parts.back();
49 std::transform(part.begin(), part.end(), part.begin(), f);
50 }
51 start = current + 1;
52 } while (current != end);
53 return parts;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080054}
55
Adam Lesinskice5e56e2016-10-21 17:56:45 -070056std::vector<std::string> Split(const StringPiece& str, char sep) {
57 return SplitAndTransform(str, sep, nullptr);
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080058}
59
Adam Lesinskice5e56e2016-10-21 17:56:45 -070060std::vector<std::string> SplitAndLowercase(const StringPiece& str, char sep) {
61 return SplitAndTransform(str, sep, ::tolower);
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080062}
63
Adam Lesinskice5e56e2016-10-21 17:56:45 -070064bool StartsWith(const StringPiece& str, const StringPiece& prefix) {
65 if (str.size() < prefix.size()) {
66 return false;
67 }
68 return str.substr(0, prefix.size()) == prefix;
Adam Lesinskid0f116b2016-07-08 15:00:32 -070069}
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080070
Adam Lesinskice5e56e2016-10-21 17:56:45 -070071bool EndsWith(const StringPiece& str, const StringPiece& suffix) {
72 if (str.size() < suffix.size()) {
73 return false;
74 }
75 return str.substr(str.size() - suffix.size(), suffix.size()) == suffix;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080076}
77
Adam Lesinskice5e56e2016-10-21 17:56:45 -070078StringPiece TrimWhitespace(const StringPiece& str) {
79 if (str.size() == 0 || str.data() == nullptr) {
80 return str;
81 }
Adam Lesinski3b4cd942015-10-30 16:31:42 -070082
Adam Lesinskice5e56e2016-10-21 17:56:45 -070083 const char* start = str.data();
84 const char* end = str.data() + str.length();
Adam Lesinski3b4cd942015-10-30 16:31:42 -070085
Adam Lesinskice5e56e2016-10-21 17:56:45 -070086 while (start != end && isspace(*start)) {
87 start++;
88 }
Adam Lesinski3b4cd942015-10-30 16:31:42 -070089
Adam Lesinskice5e56e2016-10-21 17:56:45 -070090 while (end != start && isspace(*(end - 1))) {
91 end--;
92 }
Adam Lesinski3b4cd942015-10-30 16:31:42 -070093
Adam Lesinskice5e56e2016-10-21 17:56:45 -070094 return StringPiece(start, end - start);
Adam Lesinski3b4cd942015-10-30 16:31:42 -070095}
96
Adam Lesinskice5e56e2016-10-21 17:56:45 -070097StringPiece::const_iterator FindNonAlphaNumericAndNotInSet(
98 const StringPiece& str, const StringPiece& allowed_chars) {
99 const auto end_iter = str.end();
100 for (auto iter = str.begin(); iter != end_iter; ++iter) {
101 char c = *iter;
102 if ((c >= u'a' && c <= u'z') || (c >= u'A' && c <= u'Z') ||
103 (c >= u'0' && c <= u'9')) {
104 continue;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800105 }
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700106
107 bool match = false;
108 for (char i : allowed_chars) {
109 if (c == i) {
110 match = true;
111 break;
112 }
113 }
114
115 if (!match) {
116 return iter;
117 }
118 }
119 return end_iter;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800120}
121
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700122bool IsJavaClassName(const StringPiece& str) {
123 size_t pieces = 0;
124 for (const StringPiece& piece : Tokenize(str, '.')) {
125 pieces++;
126 if (piece.empty()) {
127 return false;
Adam Lesinskia1ad4a82015-06-08 11:41:09 -0700128 }
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700129
130 // Can't have starting or trailing $ character.
131 if (piece.data()[0] == '$' || piece.data()[piece.size() - 1] == '$') {
132 return false;
133 }
134
135 if (FindNonAlphaNumericAndNotInSet(piece, "$_") != piece.end()) {
136 return false;
137 }
138 }
139 return pieces >= 2;
Adam Lesinskia1ad4a82015-06-08 11:41:09 -0700140}
141
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700142bool IsJavaPackageName(const StringPiece& str) {
143 if (str.empty()) {
144 return false;
145 }
146
147 size_t pieces = 0;
148 for (const StringPiece& piece : Tokenize(str, '.')) {
149 pieces++;
150 if (piece.empty()) {
151 return false;
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700152 }
153
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700154 if (piece.data()[0] == '_' || piece.data()[piece.size() - 1] == '_') {
155 return false;
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700156 }
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700157
158 if (FindNonAlphaNumericAndNotInSet(piece, "_") != piece.end()) {
159 return false;
160 }
161 }
162 return pieces >= 1;
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700163}
164
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700165Maybe<std::string> GetFullyQualifiedClassName(const StringPiece& package,
166 const StringPiece& classname) {
167 if (classname.empty()) {
168 return {};
169 }
Adam Lesinskia1ad4a82015-06-08 11:41:09 -0700170
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700171 if (util::IsJavaClassName(classname)) {
Adam Lesinskid5083f62017-01-16 15:07:21 -0800172 return classname.to_string();
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700173 }
Adam Lesinskia1ad4a82015-06-08 11:41:09 -0700174
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700175 if (package.empty()) {
176 return {};
177 }
Adam Lesinskia1ad4a82015-06-08 11:41:09 -0700178
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700179 std::string result(package.data(), package.size());
180 if (classname.data()[0] != '.') {
181 result += '.';
182 }
Adam Lesinski52364f72016-01-11 13:10:24 -0800183
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700184 result.append(classname.data(), classname.size());
185 if (!IsJavaClassName(result)) {
186 return {};
187 }
188 return result;
Adam Lesinskia1ad4a82015-06-08 11:41:09 -0700189}
190
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700191static size_t ConsumeDigits(const char* start, const char* end) {
192 const char* c = start;
193 for (; c != end && *c >= '0' && *c <= '9'; c++) {
194 }
195 return static_cast<size_t>(c - start);
Adam Lesinskib23f1e02015-11-03 12:24:17 -0800196}
197
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700198bool VerifyJavaStringFormat(const StringPiece& str) {
199 const char* c = str.begin();
200 const char* const end = str.end();
Adam Lesinskib23f1e02015-11-03 12:24:17 -0800201
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700202 size_t arg_count = 0;
203 bool nonpositional = false;
204 while (c != end) {
205 if (*c == '%' && c + 1 < end) {
206 c++;
Adam Lesinskib23f1e02015-11-03 12:24:17 -0800207
Adam Lesinskib9f05482017-06-02 16:32:37 -0700208 if (*c == '%' || *c == 'n') {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700209 c++;
210 continue;
211 }
Adam Lesinskib23f1e02015-11-03 12:24:17 -0800212
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700213 arg_count++;
Adam Lesinskib23f1e02015-11-03 12:24:17 -0800214
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700215 size_t num_digits = ConsumeDigits(c, end);
216 if (num_digits > 0) {
217 c += num_digits;
218 if (c != end && *c != '$') {
219 // The digits were a size, but not a positional argument.
220 nonpositional = true;
Adam Lesinskib23f1e02015-11-03 12:24:17 -0800221 }
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700222 } else if (*c == '<') {
223 // Reusing last argument, bad idea since positions can be moved around
224 // during translation.
225 nonpositional = true;
Adam Lesinskib23f1e02015-11-03 12:24:17 -0800226
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700227 c++;
228
229 // Optionally we can have a $ after
230 if (c != end && *c == '$') {
231 c++;
Adam Lesinskib23f1e02015-11-03 12:24:17 -0800232 }
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700233 } else {
234 nonpositional = true;
235 }
236
237 // Ignore size, width, flags, etc.
238 while (c != end && (*c == '-' || *c == '#' || *c == '+' || *c == ' ' ||
239 *c == ',' || *c == '(' || (*c >= '0' && *c <= '9'))) {
240 c++;
241 }
242
243 /*
244 * This is a shortcut to detect strings that are going to Time.format()
245 * instead of String.format()
246 *
247 * Comparison of String.format() and Time.format() args:
248 *
249 * String: ABC E GH ST X abcdefgh nost x
250 * Time: DEFGHKMS W Za d hkm s w yz
251 *
252 * Therefore we know it's definitely Time if we have:
253 * DFKMWZkmwyz
254 */
255 if (c != end) {
256 switch (*c) {
257 case 'D':
258 case 'F':
259 case 'K':
260 case 'M':
261 case 'W':
262 case 'Z':
263 case 'k':
264 case 'm':
265 case 'w':
266 case 'y':
267 case 'z':
268 return true;
269 }
270 }
Adam Lesinskib23f1e02015-11-03 12:24:17 -0800271 }
272
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700273 if (c != end) {
274 c++;
Adam Lesinskib23f1e02015-11-03 12:24:17 -0800275 }
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700276 }
277
278 if (arg_count > 1 && nonpositional) {
279 // Multiple arguments were specified, but some or all were non positional.
280 // Translated
281 // strings may rearrange the order of the arguments, which will break the
282 // string.
283 return false;
284 }
285 return true;
Adam Lesinskib23f1e02015-11-03 12:24:17 -0800286}
287
Adam Lesinski549e4372017-06-27 18:39:07 -0700288static bool AppendCodepointToUtf8String(char32_t codepoint, std::string* output) {
289 ssize_t len = utf32_to_utf8_length(&codepoint, 1);
290 if (len < 0) {
291 return false;
292 }
293
294 const size_t start_append_pos = output->size();
295
296 // Make room for the next character.
297 output->resize(output->size() + len);
298
299 char* dst = &*(output->begin() + start_append_pos);
300 utf32_to_utf8(&codepoint, 1, dst, len + 1);
301 return true;
302}
303
304static bool AppendUnicodeCodepoint(Utf8Iterator* iter, std::string* output) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700305 char32_t code = 0;
Adam Lesinski549e4372017-06-27 18:39:07 -0700306 for (size_t i = 0; i < 4 && iter->HasNext(); i++) {
307 char32_t codepoint = iter->Next();
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700308 char32_t a;
Adam Lesinski549e4372017-06-27 18:39:07 -0700309 if (codepoint >= U'0' && codepoint <= U'9') {
310 a = codepoint - U'0';
311 } else if (codepoint >= U'a' && codepoint <= U'f') {
312 a = codepoint - U'a' + 10;
313 } else if (codepoint >= U'A' && codepoint <= U'F') {
314 a = codepoint - U'A' + 10;
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700315 } else {
316 return {};
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800317 }
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700318 code = (code << 4) | a;
319 }
Adam Lesinski549e4372017-06-27 18:39:07 -0700320 return AppendCodepointToUtf8String(code, output);
321}
Adam Lesinskid0f116b2016-07-08 15:00:32 -0700322
Adam Lesinski549e4372017-06-27 18:39:07 -0700323static bool IsCodepointSpace(char32_t codepoint) {
324 if (static_cast<uint32_t>(codepoint) & 0xffffff00u) {
325 return false;
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700326 }
Adam Lesinski549e4372017-06-27 18:39:07 -0700327 return isspace(static_cast<char>(codepoint));
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800328}
329
Adam Lesinskifba0cf22017-06-29 17:53:36 -0700330StringBuilder::StringBuilder(bool preserve_spaces) : preserve_spaces_(preserve_spaces) {
331}
332
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700333StringBuilder& StringBuilder::Append(const StringPiece& str) {
334 if (!error_.empty()) {
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800335 return *this;
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700336 }
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800337
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700338 // Where the new data will be appended to.
Adam Lesinski549e4372017-06-27 18:39:07 -0700339 const size_t new_data_index = str_.size();
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800340
Adam Lesinski549e4372017-06-27 18:39:07 -0700341 Utf8Iterator iter(str);
342 while (iter.HasNext()) {
343 const char32_t codepoint = iter.Next();
344
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700345 if (last_char_was_escape_) {
Adam Lesinski549e4372017-06-27 18:39:07 -0700346 switch (codepoint) {
347 case U't':
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700348 str_ += '\t';
349 break;
Adam Lesinski549e4372017-06-27 18:39:07 -0700350
351 case U'n':
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700352 str_ += '\n';
353 break;
Adam Lesinski549e4372017-06-27 18:39:07 -0700354
355 case U'#':
356 case U'@':
357 case U'?':
358 case U'"':
359 case U'\'':
360 case U'\\':
361 str_ += static_cast<char>(codepoint);
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700362 break;
Adam Lesinski549e4372017-06-27 18:39:07 -0700363
364 case U'u':
365 if (!AppendUnicodeCodepoint(&iter, &str_)) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700366 error_ = "invalid unicode escape sequence";
367 return *this;
368 }
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700369 break;
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700370
371 default:
Adam Lesinski549e4372017-06-27 18:39:07 -0700372 // Ignore the escape character and just include the codepoint.
373 AppendCodepointToUtf8String(codepoint, &str_);
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700374 break;
375 }
376 last_char_was_escape_ = false;
Adam Lesinski549e4372017-06-27 18:39:07 -0700377
Adam Lesinskifba0cf22017-06-29 17:53:36 -0700378 } else if (!preserve_spaces_ && codepoint == U'"') {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700379 if (!quote_ && trailing_space_) {
Adam Lesinskifba0cf22017-06-29 17:53:36 -0700380 // We found an opening quote, and we have trailing space, so we should append that
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700381 // space now.
382 if (trailing_space_) {
Adam Lesinskifba0cf22017-06-29 17:53:36 -0700383 // We had trailing whitespace, so replace with a single space.
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700384 if (!str_.empty()) {
385 str_ += ' ';
386 }
387 trailing_space_ = false;
388 }
389 }
390 quote_ = !quote_;
Adam Lesinski549e4372017-06-27 18:39:07 -0700391
Adam Lesinskifba0cf22017-06-29 17:53:36 -0700392 } else if (!preserve_spaces_ && codepoint == U'\'' && !quote_) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700393 // This should be escaped.
394 error_ = "unescaped apostrophe";
395 return *this;
Adam Lesinski549e4372017-06-27 18:39:07 -0700396
397 } else if (codepoint == U'\\') {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700398 // This is an escape sequence, convert to the real value.
399 if (!quote_ && trailing_space_) {
400 // We had trailing whitespace, so
401 // replace with a single space.
402 if (!str_.empty()) {
403 str_ += ' ';
404 }
405 trailing_space_ = false;
406 }
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700407 last_char_was_escape_ = true;
Adam Lesinski549e4372017-06-27 18:39:07 -0700408 } else {
Adam Lesinskifba0cf22017-06-29 17:53:36 -0700409 if (preserve_spaces_ || quote_) {
Adam Lesinski549e4372017-06-27 18:39:07 -0700410 // Quotes mean everything is taken, including whitespace.
411 AppendCodepointToUtf8String(codepoint, &str_);
412 } else {
413 // This is not quoted text, so we will accumulate whitespace and only emit a single
414 // character of whitespace if it is followed by a non-whitespace character.
415 if (IsCodepointSpace(codepoint)) {
416 // We found whitespace.
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700417 trailing_space_ = true;
Adam Lesinski549e4372017-06-27 18:39:07 -0700418 } else {
419 if (trailing_space_) {
420 // We saw trailing space before, so replace all
421 // that trailing space with one space.
422 if (!str_.empty()) {
423 str_ += ' ';
424 }
425 trailing_space_ = false;
426 }
427 AppendCodepointToUtf8String(codepoint, &str_);
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700428 }
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700429 }
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800430 }
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700431 }
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700432
433 // Accumulate the added string's UTF-16 length.
Adam Lesinski549e4372017-06-27 18:39:07 -0700434 ssize_t len = utf8_to_utf16_length(reinterpret_cast<const uint8_t*>(str_.data()) + new_data_index,
435 str_.size() - new_data_index);
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700436 if (len < 0) {
437 error_ = "invalid unicode code point";
438 return *this;
439 }
440 utf16_len_ += len;
441 return *this;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800442}
443
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700444std::u16string Utf8ToUtf16(const StringPiece& utf8) {
445 ssize_t utf16_length = utf8_to_utf16_length(
446 reinterpret_cast<const uint8_t*>(utf8.data()), utf8.length());
447 if (utf16_length <= 0) {
448 return {};
449 }
450
451 std::u16string utf16;
452 utf16.resize(utf16_length);
453 utf8_to_utf16(reinterpret_cast<const uint8_t*>(utf8.data()), utf8.length(),
454 &*utf16.begin(), utf16_length + 1);
455 return utf16;
456}
457
458std::string Utf16ToUtf8(const StringPiece16& utf16) {
459 ssize_t utf8_length = utf16_to_utf8_length(utf16.data(), utf16.length());
460 if (utf8_length <= 0) {
461 return {};
462 }
463
464 std::string utf8;
465 utf8.resize(utf8_length);
466 utf16_to_utf8(utf16.data(), utf16.length(), &*utf8.begin(), utf8_length + 1);
467 return utf8;
468}
469
470bool WriteAll(std::ostream& out, const BigBuffer& buffer) {
471 for (const auto& b : buffer) {
472 if (!out.write(reinterpret_cast<const char*>(b.buffer.get()), b.size)) {
473 return false;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800474 }
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700475 }
476 return true;
477}
478
479std::unique_ptr<uint8_t[]> Copy(const BigBuffer& buffer) {
480 std::unique_ptr<uint8_t[]> data =
481 std::unique_ptr<uint8_t[]>(new uint8_t[buffer.size()]);
482 uint8_t* p = data.get();
483 for (const auto& block : buffer) {
484 memcpy(p, block.buffer.get(), block.size);
485 p += block.size;
486 }
487 return data;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800488}
489
Adam Lesinskid0f116b2016-07-08 15:00:32 -0700490typename Tokenizer::iterator& Tokenizer::iterator::operator++() {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700491 const char* start = token_.end();
492 const char* end = str_.end();
493 if (start == end) {
494 end_ = true;
495 token_.assign(token_.end(), 0);
Adam Lesinskid0f116b2016-07-08 15:00:32 -0700496 return *this;
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700497 }
498
499 start += 1;
500 const char* current = start;
501 while (current != end) {
502 if (*current == separator_) {
503 token_.assign(start, current - start);
504 return *this;
505 }
506 ++current;
507 }
508 token_.assign(start, end - start);
509 return *this;
Adam Lesinskid0f116b2016-07-08 15:00:32 -0700510}
511
512bool Tokenizer::iterator::operator==(const iterator& rhs) const {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700513 // We check equality here a bit differently.
514 // We need to know that the addresses are the same.
515 return token_.begin() == rhs.token_.begin() &&
516 token_.end() == rhs.token_.end() && end_ == rhs.end_;
Adam Lesinskid0f116b2016-07-08 15:00:32 -0700517}
518
519bool Tokenizer::iterator::operator!=(const iterator& rhs) const {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700520 return !(*this == rhs);
Adam Lesinskid0f116b2016-07-08 15:00:32 -0700521}
522
Chih-Hung Hsieh4dc58122017-08-03 16:28:10 -0700523Tokenizer::iterator::iterator(const StringPiece& s, char sep, const StringPiece& tok, bool end)
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700524 : str_(s), separator_(sep), token_(tok), end_(end) {}
Adam Lesinskid0f116b2016-07-08 15:00:32 -0700525
Chih-Hung Hsieh4dc58122017-08-03 16:28:10 -0700526Tokenizer::Tokenizer(const StringPiece& str, char sep)
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700527 : begin_(++iterator(str, sep, StringPiece(str.begin() - 1, 0), false)),
528 end_(str, sep, StringPiece(str.end(), 0), true) {}
Adam Lesinskid0f116b2016-07-08 15:00:32 -0700529
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700530bool ExtractResFilePathParts(const StringPiece& path, StringPiece* out_prefix,
531 StringPiece* out_entry, StringPiece* out_suffix) {
532 const StringPiece res_prefix("res/");
533 if (!StartsWith(path, res_prefix)) {
534 return false;
535 }
536
537 StringPiece::const_iterator last_occurence = path.end();
538 for (auto iter = path.begin() + res_prefix.size(); iter != path.end();
539 ++iter) {
540 if (*iter == '/') {
541 last_occurence = iter;
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700542 }
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700543 }
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700544
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700545 if (last_occurence == path.end()) {
546 return false;
547 }
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700548
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700549 auto iter = std::find(last_occurence, path.end(), '.');
550 *out_suffix = StringPiece(iter, path.end() - iter);
551 *out_entry = StringPiece(last_occurence + 1, iter - last_occurence - 1);
552 *out_prefix = StringPiece(path.begin(), last_occurence - path.begin() + 1);
553 return true;
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700554}
555
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700556StringPiece16 GetString16(const android::ResStringPool& pool, size_t idx) {
557 size_t len;
558 const char16_t* str = pool.stringAt(idx, &len);
559 if (str != nullptr) {
560 return StringPiece16(str, len);
561 }
562 return StringPiece16();
Adam Lesinskid0f116b2016-07-08 15:00:32 -0700563}
564
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700565std::string GetString(const android::ResStringPool& pool, size_t idx) {
566 size_t len;
567 const char* str = pool.string8At(idx, &len);
568 if (str != nullptr) {
569 return std::string(str, len);
570 }
571 return Utf16ToUtf8(GetString16(pool, idx));
Adam Lesinskid0f116b2016-07-08 15:00:32 -0700572}
573
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700574} // namespace util
575} // namespace aapt