blob: e42145dff47eab246fd66fe889512da569676e2a [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 Lesinski96ea08f2017-11-06 10:44:46 -080027#include "text/Unicode.h"
Adam Lesinski66ea8402017-06-28 11:44:11 -070028#include "text/Utf8Iterator.h"
Adam Lesinskid5083f62017-01-16 15:07:21 -080029#include "util/BigBuffer.h"
30#include "util/Maybe.h"
31
Adam Lesinski66ea8402017-06-28 11:44:11 -070032using ::aapt::text::Utf8Iterator;
Adam Lesinski549e4372017-06-27 18:39:07 -070033using ::android::StringPiece;
34using ::android::StringPiece16;
Adam Lesinskid5083f62017-01-16 15:07:21 -080035
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080036namespace aapt {
37namespace util {
38
Adam Lesinskice5e56e2016-10-21 17:56:45 -070039static std::vector<std::string> SplitAndTransform(
40 const StringPiece& str, char sep, const std::function<char(char)>& f) {
41 std::vector<std::string> parts;
42 const StringPiece::const_iterator end = std::end(str);
43 StringPiece::const_iterator start = std::begin(str);
44 StringPiece::const_iterator current;
45 do {
46 current = std::find(start, end, sep);
Adam Lesinskid5083f62017-01-16 15:07:21 -080047 parts.emplace_back(str.substr(start, current).to_string());
Adam Lesinskice5e56e2016-10-21 17:56:45 -070048 if (f) {
49 std::string& part = parts.back();
50 std::transform(part.begin(), part.end(), part.begin(), f);
51 }
52 start = current + 1;
53 } while (current != end);
54 return parts;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080055}
56
Adam Lesinskice5e56e2016-10-21 17:56:45 -070057std::vector<std::string> Split(const StringPiece& str, char sep) {
58 return SplitAndTransform(str, sep, nullptr);
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080059}
60
Adam Lesinskice5e56e2016-10-21 17:56:45 -070061std::vector<std::string> SplitAndLowercase(const StringPiece& str, char sep) {
62 return SplitAndTransform(str, sep, ::tolower);
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080063}
64
Adam Lesinskice5e56e2016-10-21 17:56:45 -070065bool StartsWith(const StringPiece& str, const StringPiece& prefix) {
66 if (str.size() < prefix.size()) {
67 return false;
68 }
69 return str.substr(0, prefix.size()) == prefix;
Adam Lesinskid0f116b2016-07-08 15:00:32 -070070}
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080071
Adam Lesinskice5e56e2016-10-21 17:56:45 -070072bool EndsWith(const StringPiece& str, const StringPiece& suffix) {
73 if (str.size() < suffix.size()) {
74 return false;
75 }
76 return str.substr(str.size() - suffix.size(), suffix.size()) == suffix;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080077}
78
Adam Lesinskice5e56e2016-10-21 17:56:45 -070079StringPiece TrimWhitespace(const StringPiece& str) {
80 if (str.size() == 0 || str.data() == nullptr) {
81 return str;
82 }
Adam Lesinski3b4cd942015-10-30 16:31:42 -070083
Adam Lesinskice5e56e2016-10-21 17:56:45 -070084 const char* start = str.data();
85 const char* end = str.data() + str.length();
Adam Lesinski3b4cd942015-10-30 16:31:42 -070086
Adam Lesinskice5e56e2016-10-21 17:56:45 -070087 while (start != end && isspace(*start)) {
88 start++;
89 }
Adam Lesinski3b4cd942015-10-30 16:31:42 -070090
Adam Lesinskice5e56e2016-10-21 17:56:45 -070091 while (end != start && isspace(*(end - 1))) {
92 end--;
93 }
Adam Lesinski3b4cd942015-10-30 16:31:42 -070094
Adam Lesinskice5e56e2016-10-21 17:56:45 -070095 return StringPiece(start, end - start);
Adam Lesinski3b4cd942015-10-30 16:31:42 -070096}
97
Adam Lesinski96ea08f2017-11-06 10:44:46 -080098static int IsJavaNameImpl(const StringPiece& str) {
99 int pieces = 0;
100 for (const StringPiece& piece : Tokenize(str, '.')) {
101 pieces++;
102 if (!text::IsJavaIdentifier(piece)) {
103 return -1;
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700104 }
105 }
Adam Lesinski96ea08f2017-11-06 10:44:46 -0800106 return pieces;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800107}
108
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700109bool IsJavaClassName(const StringPiece& str) {
Adam Lesinski96ea08f2017-11-06 10:44:46 -0800110 return IsJavaNameImpl(str) >= 2;
Adam Lesinskia1ad4a82015-06-08 11:41:09 -0700111}
112
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700113bool IsJavaPackageName(const StringPiece& str) {
Adam Lesinski96ea08f2017-11-06 10:44:46 -0800114 return IsJavaNameImpl(str) >= 1;
115}
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700116
Adam Lesinski96ea08f2017-11-06 10:44:46 -0800117static int IsAndroidNameImpl(const StringPiece& str) {
118 int pieces = 0;
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700119 for (const StringPiece& piece : Tokenize(str, '.')) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700120 if (piece.empty()) {
Adam Lesinski96ea08f2017-11-06 10:44:46 -0800121 return -1;
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700122 }
123
Adam Lesinski96ea08f2017-11-06 10:44:46 -0800124 const char first_character = piece.data()[0];
125 if (!::isalpha(first_character)) {
126 return -1;
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700127 }
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700128
Adam Lesinski96ea08f2017-11-06 10:44:46 -0800129 bool valid = std::all_of(piece.begin() + 1, piece.end(), [](const char c) -> bool {
130 return ::isalnum(c) || c == '_';
131 });
132
133 if (!valid) {
134 return -1;
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700135 }
Adam Lesinski96ea08f2017-11-06 10:44:46 -0800136 pieces++;
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700137 }
Adam Lesinski96ea08f2017-11-06 10:44:46 -0800138 return pieces;
139}
140
141bool IsAndroidPackageName(const StringPiece& str) {
142 return IsAndroidNameImpl(str) > 1 || str == "android";
143}
144
145bool IsAndroidSplitName(const StringPiece& str) {
146 return IsAndroidNameImpl(str) > 0;
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700147}
148
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700149Maybe<std::string> GetFullyQualifiedClassName(const StringPiece& package,
150 const StringPiece& classname) {
151 if (classname.empty()) {
152 return {};
153 }
Adam Lesinskia1ad4a82015-06-08 11:41:09 -0700154
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700155 if (util::IsJavaClassName(classname)) {
Adam Lesinskid5083f62017-01-16 15:07:21 -0800156 return classname.to_string();
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700157 }
Adam Lesinskia1ad4a82015-06-08 11:41:09 -0700158
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700159 if (package.empty()) {
160 return {};
161 }
Adam Lesinskia1ad4a82015-06-08 11:41:09 -0700162
Adam Lesinski96ea08f2017-11-06 10:44:46 -0800163 std::string result = package.to_string();
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700164 if (classname.data()[0] != '.') {
165 result += '.';
166 }
Adam Lesinski52364f72016-01-11 13:10:24 -0800167
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700168 result.append(classname.data(), classname.size());
169 if (!IsJavaClassName(result)) {
170 return {};
171 }
172 return result;
Adam Lesinskia1ad4a82015-06-08 11:41:09 -0700173}
174
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700175static size_t ConsumeDigits(const char* start, const char* end) {
176 const char* c = start;
177 for (; c != end && *c >= '0' && *c <= '9'; c++) {
178 }
179 return static_cast<size_t>(c - start);
Adam Lesinskib23f1e02015-11-03 12:24:17 -0800180}
181
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700182bool VerifyJavaStringFormat(const StringPiece& str) {
183 const char* c = str.begin();
184 const char* const end = str.end();
Adam Lesinskib23f1e02015-11-03 12:24:17 -0800185
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700186 size_t arg_count = 0;
187 bool nonpositional = false;
188 while (c != end) {
189 if (*c == '%' && c + 1 < end) {
190 c++;
Adam Lesinskib23f1e02015-11-03 12:24:17 -0800191
Adam Lesinskib9f05482017-06-02 16:32:37 -0700192 if (*c == '%' || *c == 'n') {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700193 c++;
194 continue;
195 }
Adam Lesinskib23f1e02015-11-03 12:24:17 -0800196
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700197 arg_count++;
Adam Lesinskib23f1e02015-11-03 12:24:17 -0800198
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700199 size_t num_digits = ConsumeDigits(c, end);
200 if (num_digits > 0) {
201 c += num_digits;
202 if (c != end && *c != '$') {
203 // The digits were a size, but not a positional argument.
204 nonpositional = true;
Adam Lesinskib23f1e02015-11-03 12:24:17 -0800205 }
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700206 } else if (*c == '<') {
207 // Reusing last argument, bad idea since positions can be moved around
208 // during translation.
209 nonpositional = true;
Adam Lesinskib23f1e02015-11-03 12:24:17 -0800210
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700211 c++;
212
213 // Optionally we can have a $ after
214 if (c != end && *c == '$') {
215 c++;
Adam Lesinskib23f1e02015-11-03 12:24:17 -0800216 }
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700217 } else {
218 nonpositional = true;
219 }
220
221 // Ignore size, width, flags, etc.
222 while (c != end && (*c == '-' || *c == '#' || *c == '+' || *c == ' ' ||
223 *c == ',' || *c == '(' || (*c >= '0' && *c <= '9'))) {
224 c++;
225 }
226
227 /*
228 * This is a shortcut to detect strings that are going to Time.format()
229 * instead of String.format()
230 *
231 * Comparison of String.format() and Time.format() args:
232 *
233 * String: ABC E GH ST X abcdefgh nost x
234 * Time: DEFGHKMS W Za d hkm s w yz
235 *
236 * Therefore we know it's definitely Time if we have:
237 * DFKMWZkmwyz
238 */
239 if (c != end) {
240 switch (*c) {
241 case 'D':
242 case 'F':
243 case 'K':
244 case 'M':
245 case 'W':
246 case 'Z':
247 case 'k':
248 case 'm':
249 case 'w':
250 case 'y':
251 case 'z':
252 return true;
253 }
254 }
Adam Lesinskib23f1e02015-11-03 12:24:17 -0800255 }
256
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700257 if (c != end) {
258 c++;
Adam Lesinskib23f1e02015-11-03 12:24:17 -0800259 }
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700260 }
261
262 if (arg_count > 1 && nonpositional) {
263 // Multiple arguments were specified, but some or all were non positional.
264 // Translated
265 // strings may rearrange the order of the arguments, which will break the
266 // string.
267 return false;
268 }
269 return true;
Adam Lesinskib23f1e02015-11-03 12:24:17 -0800270}
271
Adam Lesinski549e4372017-06-27 18:39:07 -0700272static bool AppendCodepointToUtf8String(char32_t codepoint, std::string* output) {
273 ssize_t len = utf32_to_utf8_length(&codepoint, 1);
274 if (len < 0) {
275 return false;
276 }
277
278 const size_t start_append_pos = output->size();
279
280 // Make room for the next character.
281 output->resize(output->size() + len);
282
283 char* dst = &*(output->begin() + start_append_pos);
284 utf32_to_utf8(&codepoint, 1, dst, len + 1);
285 return true;
286}
287
288static bool AppendUnicodeCodepoint(Utf8Iterator* iter, std::string* output) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700289 char32_t code = 0;
Adam Lesinski549e4372017-06-27 18:39:07 -0700290 for (size_t i = 0; i < 4 && iter->HasNext(); i++) {
291 char32_t codepoint = iter->Next();
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700292 char32_t a;
Adam Lesinski549e4372017-06-27 18:39:07 -0700293 if (codepoint >= U'0' && codepoint <= U'9') {
294 a = codepoint - U'0';
295 } else if (codepoint >= U'a' && codepoint <= U'f') {
296 a = codepoint - U'a' + 10;
297 } else if (codepoint >= U'A' && codepoint <= U'F') {
298 a = codepoint - U'A' + 10;
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700299 } else {
300 return {};
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800301 }
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700302 code = (code << 4) | a;
303 }
Adam Lesinski549e4372017-06-27 18:39:07 -0700304 return AppendCodepointToUtf8String(code, output);
305}
Adam Lesinskid0f116b2016-07-08 15:00:32 -0700306
Adam Lesinski549e4372017-06-27 18:39:07 -0700307static bool IsCodepointSpace(char32_t codepoint) {
308 if (static_cast<uint32_t>(codepoint) & 0xffffff00u) {
309 return false;
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700310 }
Adam Lesinski549e4372017-06-27 18:39:07 -0700311 return isspace(static_cast<char>(codepoint));
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800312}
313
Adam Lesinskifba0cf22017-06-29 17:53:36 -0700314StringBuilder::StringBuilder(bool preserve_spaces) : preserve_spaces_(preserve_spaces) {
315}
316
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700317StringBuilder& StringBuilder::Append(const StringPiece& str) {
318 if (!error_.empty()) {
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800319 return *this;
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700320 }
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800321
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700322 // Where the new data will be appended to.
Adam Lesinski549e4372017-06-27 18:39:07 -0700323 const size_t new_data_index = str_.size();
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800324
Adam Lesinski549e4372017-06-27 18:39:07 -0700325 Utf8Iterator iter(str);
326 while (iter.HasNext()) {
327 const char32_t codepoint = iter.Next();
328
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700329 if (last_char_was_escape_) {
Adam Lesinski549e4372017-06-27 18:39:07 -0700330 switch (codepoint) {
331 case U't':
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700332 str_ += '\t';
333 break;
Adam Lesinski549e4372017-06-27 18:39:07 -0700334
335 case U'n':
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700336 str_ += '\n';
337 break;
Adam Lesinski549e4372017-06-27 18:39:07 -0700338
339 case U'#':
340 case U'@':
341 case U'?':
342 case U'"':
343 case U'\'':
344 case U'\\':
345 str_ += static_cast<char>(codepoint);
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700346 break;
Adam Lesinski549e4372017-06-27 18:39:07 -0700347
348 case U'u':
349 if (!AppendUnicodeCodepoint(&iter, &str_)) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700350 error_ = "invalid unicode escape sequence";
351 return *this;
352 }
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700353 break;
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700354
355 default:
Adam Lesinski549e4372017-06-27 18:39:07 -0700356 // Ignore the escape character and just include the codepoint.
357 AppendCodepointToUtf8String(codepoint, &str_);
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700358 break;
359 }
360 last_char_was_escape_ = false;
Adam Lesinski549e4372017-06-27 18:39:07 -0700361
Adam Lesinskifba0cf22017-06-29 17:53:36 -0700362 } else if (!preserve_spaces_ && codepoint == U'"') {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700363 if (!quote_ && trailing_space_) {
Adam Lesinskifba0cf22017-06-29 17:53:36 -0700364 // We found an opening quote, and we have trailing space, so we should append that
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700365 // space now.
366 if (trailing_space_) {
Adam Lesinskifba0cf22017-06-29 17:53:36 -0700367 // We had trailing whitespace, so replace with a single space.
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700368 if (!str_.empty()) {
369 str_ += ' ';
370 }
371 trailing_space_ = false;
372 }
373 }
374 quote_ = !quote_;
Adam Lesinski549e4372017-06-27 18:39:07 -0700375
Adam Lesinskifba0cf22017-06-29 17:53:36 -0700376 } else if (!preserve_spaces_ && codepoint == U'\'' && !quote_) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700377 // This should be escaped.
378 error_ = "unescaped apostrophe";
379 return *this;
Adam Lesinski549e4372017-06-27 18:39:07 -0700380
381 } else if (codepoint == U'\\') {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700382 // This is an escape sequence, convert to the real value.
383 if (!quote_ && trailing_space_) {
384 // We had trailing whitespace, so
385 // replace with a single space.
386 if (!str_.empty()) {
387 str_ += ' ';
388 }
389 trailing_space_ = false;
390 }
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700391 last_char_was_escape_ = true;
Adam Lesinski549e4372017-06-27 18:39:07 -0700392 } else {
Adam Lesinskifba0cf22017-06-29 17:53:36 -0700393 if (preserve_spaces_ || quote_) {
Adam Lesinski549e4372017-06-27 18:39:07 -0700394 // Quotes mean everything is taken, including whitespace.
395 AppendCodepointToUtf8String(codepoint, &str_);
396 } else {
397 // This is not quoted text, so we will accumulate whitespace and only emit a single
398 // character of whitespace if it is followed by a non-whitespace character.
399 if (IsCodepointSpace(codepoint)) {
400 // We found whitespace.
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700401 trailing_space_ = true;
Adam Lesinski549e4372017-06-27 18:39:07 -0700402 } else {
403 if (trailing_space_) {
404 // We saw trailing space before, so replace all
405 // that trailing space with one space.
406 if (!str_.empty()) {
407 str_ += ' ';
408 }
409 trailing_space_ = false;
410 }
411 AppendCodepointToUtf8String(codepoint, &str_);
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700412 }
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700413 }
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800414 }
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700415 }
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700416
417 // Accumulate the added string's UTF-16 length.
Adam Lesinski549e4372017-06-27 18:39:07 -0700418 ssize_t len = utf8_to_utf16_length(reinterpret_cast<const uint8_t*>(str_.data()) + new_data_index,
419 str_.size() - new_data_index);
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700420 if (len < 0) {
421 error_ = "invalid unicode code point";
422 return *this;
423 }
424 utf16_len_ += len;
425 return *this;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800426}
427
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700428std::u16string Utf8ToUtf16(const StringPiece& utf8) {
429 ssize_t utf16_length = utf8_to_utf16_length(
430 reinterpret_cast<const uint8_t*>(utf8.data()), utf8.length());
431 if (utf16_length <= 0) {
432 return {};
433 }
434
435 std::u16string utf16;
436 utf16.resize(utf16_length);
437 utf8_to_utf16(reinterpret_cast<const uint8_t*>(utf8.data()), utf8.length(),
438 &*utf16.begin(), utf16_length + 1);
439 return utf16;
440}
441
442std::string Utf16ToUtf8(const StringPiece16& utf16) {
443 ssize_t utf8_length = utf16_to_utf8_length(utf16.data(), utf16.length());
444 if (utf8_length <= 0) {
445 return {};
446 }
447
448 std::string utf8;
449 utf8.resize(utf8_length);
450 utf16_to_utf8(utf16.data(), utf16.length(), &*utf8.begin(), utf8_length + 1);
451 return utf8;
452}
453
454bool WriteAll(std::ostream& out, const BigBuffer& buffer) {
455 for (const auto& b : buffer) {
456 if (!out.write(reinterpret_cast<const char*>(b.buffer.get()), b.size)) {
457 return false;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800458 }
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700459 }
460 return true;
461}
462
463std::unique_ptr<uint8_t[]> Copy(const BigBuffer& buffer) {
464 std::unique_ptr<uint8_t[]> data =
465 std::unique_ptr<uint8_t[]>(new uint8_t[buffer.size()]);
466 uint8_t* p = data.get();
467 for (const auto& block : buffer) {
468 memcpy(p, block.buffer.get(), block.size);
469 p += block.size;
470 }
471 return data;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800472}
473
Adam Lesinskid0f116b2016-07-08 15:00:32 -0700474typename Tokenizer::iterator& Tokenizer::iterator::operator++() {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700475 const char* start = token_.end();
476 const char* end = str_.end();
477 if (start == end) {
478 end_ = true;
479 token_.assign(token_.end(), 0);
Adam Lesinskid0f116b2016-07-08 15:00:32 -0700480 return *this;
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700481 }
482
483 start += 1;
484 const char* current = start;
485 while (current != end) {
486 if (*current == separator_) {
487 token_.assign(start, current - start);
488 return *this;
489 }
490 ++current;
491 }
492 token_.assign(start, end - start);
493 return *this;
Adam Lesinskid0f116b2016-07-08 15:00:32 -0700494}
495
496bool Tokenizer::iterator::operator==(const iterator& rhs) const {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700497 // We check equality here a bit differently.
498 // We need to know that the addresses are the same.
499 return token_.begin() == rhs.token_.begin() &&
500 token_.end() == rhs.token_.end() && end_ == rhs.end_;
Adam Lesinskid0f116b2016-07-08 15:00:32 -0700501}
502
503bool Tokenizer::iterator::operator!=(const iterator& rhs) const {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700504 return !(*this == rhs);
Adam Lesinskid0f116b2016-07-08 15:00:32 -0700505}
506
Chih-Hung Hsieh4dc58122017-08-03 16:28:10 -0700507Tokenizer::iterator::iterator(const StringPiece& s, char sep, const StringPiece& tok, bool end)
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700508 : str_(s), separator_(sep), token_(tok), end_(end) {}
Adam Lesinskid0f116b2016-07-08 15:00:32 -0700509
Chih-Hung Hsieh4dc58122017-08-03 16:28:10 -0700510Tokenizer::Tokenizer(const StringPiece& str, char sep)
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700511 : begin_(++iterator(str, sep, StringPiece(str.begin() - 1, 0), false)),
512 end_(str, sep, StringPiece(str.end(), 0), true) {}
Adam Lesinskid0f116b2016-07-08 15:00:32 -0700513
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700514bool ExtractResFilePathParts(const StringPiece& path, StringPiece* out_prefix,
515 StringPiece* out_entry, StringPiece* out_suffix) {
516 const StringPiece res_prefix("res/");
517 if (!StartsWith(path, res_prefix)) {
518 return false;
519 }
520
521 StringPiece::const_iterator last_occurence = path.end();
522 for (auto iter = path.begin() + res_prefix.size(); iter != path.end();
523 ++iter) {
524 if (*iter == '/') {
525 last_occurence = iter;
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700526 }
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700527 }
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700528
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700529 if (last_occurence == path.end()) {
530 return false;
531 }
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700532
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700533 auto iter = std::find(last_occurence, path.end(), '.');
534 *out_suffix = StringPiece(iter, path.end() - iter);
535 *out_entry = StringPiece(last_occurence + 1, iter - last_occurence - 1);
536 *out_prefix = StringPiece(path.begin(), last_occurence - path.begin() + 1);
537 return true;
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700538}
539
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700540StringPiece16 GetString16(const android::ResStringPool& pool, size_t idx) {
541 size_t len;
542 const char16_t* str = pool.stringAt(idx, &len);
543 if (str != nullptr) {
544 return StringPiece16(str, len);
545 }
546 return StringPiece16();
Adam Lesinskid0f116b2016-07-08 15:00:32 -0700547}
548
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700549std::string GetString(const android::ResStringPool& pool, size_t idx) {
550 size_t len;
551 const char* str = pool.string8At(idx, &len);
552 if (str != nullptr) {
553 return std::string(str, len);
554 }
555 return Utf16ToUtf8(GetString16(pool, idx));
Adam Lesinskid0f116b2016-07-08 15:00:32 -0700556}
557
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700558} // namespace util
559} // namespace aapt