blob: 59b7fff0f9e3ebf5a35817fa183fb3a6c02ff938 [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 Lesinski2eed52e2018-02-21 15:55:58 -080079StringPiece TrimLeadingWhitespace(const StringPiece& str) {
80 if (str.size() == 0 || str.data() == nullptr) {
81 return str;
82 }
83
84 const char* start = str.data();
85 const char* end = start + str.length();
86
87 while (start != end && isspace(*start)) {
88 start++;
89 }
90 return StringPiece(start, end - start);
91}
92
93StringPiece TrimTrailingWhitespace(const StringPiece& str) {
94 if (str.size() == 0 || str.data() == nullptr) {
95 return str;
96 }
97
98 const char* start = str.data();
99 const char* end = start + str.length();
100
101 while (end != start && isspace(*(end - 1))) {
102 end--;
103 }
104 return StringPiece(start, end - start);
105}
106
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700107StringPiece TrimWhitespace(const StringPiece& str) {
108 if (str.size() == 0 || str.data() == nullptr) {
109 return str;
110 }
Adam Lesinski3b4cd942015-10-30 16:31:42 -0700111
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700112 const char* start = str.data();
113 const char* end = str.data() + str.length();
Adam Lesinski3b4cd942015-10-30 16:31:42 -0700114
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700115 while (start != end && isspace(*start)) {
116 start++;
117 }
Adam Lesinski3b4cd942015-10-30 16:31:42 -0700118
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700119 while (end != start && isspace(*(end - 1))) {
120 end--;
121 }
Adam Lesinski3b4cd942015-10-30 16:31:42 -0700122
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700123 return StringPiece(start, end - start);
Adam Lesinski3b4cd942015-10-30 16:31:42 -0700124}
125
Adam Lesinski96ea08f2017-11-06 10:44:46 -0800126static int IsJavaNameImpl(const StringPiece& str) {
127 int pieces = 0;
128 for (const StringPiece& piece : Tokenize(str, '.')) {
129 pieces++;
130 if (!text::IsJavaIdentifier(piece)) {
131 return -1;
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700132 }
133 }
Adam Lesinski96ea08f2017-11-06 10:44:46 -0800134 return pieces;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800135}
136
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700137bool IsJavaClassName(const StringPiece& str) {
Adam Lesinski96ea08f2017-11-06 10:44:46 -0800138 return IsJavaNameImpl(str) >= 2;
Adam Lesinskia1ad4a82015-06-08 11:41:09 -0700139}
140
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700141bool IsJavaPackageName(const StringPiece& str) {
Adam Lesinski96ea08f2017-11-06 10:44:46 -0800142 return IsJavaNameImpl(str) >= 1;
143}
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700144
Adam Lesinski96ea08f2017-11-06 10:44:46 -0800145static int IsAndroidNameImpl(const StringPiece& str) {
146 int pieces = 0;
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700147 for (const StringPiece& piece : Tokenize(str, '.')) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700148 if (piece.empty()) {
Adam Lesinski96ea08f2017-11-06 10:44:46 -0800149 return -1;
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700150 }
151
Adam Lesinski96ea08f2017-11-06 10:44:46 -0800152 const char first_character = piece.data()[0];
153 if (!::isalpha(first_character)) {
154 return -1;
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700155 }
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700156
Adam Lesinski96ea08f2017-11-06 10:44:46 -0800157 bool valid = std::all_of(piece.begin() + 1, piece.end(), [](const char c) -> bool {
158 return ::isalnum(c) || c == '_';
159 });
160
161 if (!valid) {
162 return -1;
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700163 }
Adam Lesinski96ea08f2017-11-06 10:44:46 -0800164 pieces++;
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700165 }
Adam Lesinski96ea08f2017-11-06 10:44:46 -0800166 return pieces;
167}
168
169bool IsAndroidPackageName(const StringPiece& str) {
170 return IsAndroidNameImpl(str) > 1 || str == "android";
171}
172
173bool IsAndroidSplitName(const StringPiece& str) {
174 return IsAndroidNameImpl(str) > 0;
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700175}
176
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700177Maybe<std::string> GetFullyQualifiedClassName(const StringPiece& package,
178 const StringPiece& classname) {
179 if (classname.empty()) {
180 return {};
181 }
Adam Lesinskia1ad4a82015-06-08 11:41:09 -0700182
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700183 if (util::IsJavaClassName(classname)) {
Adam Lesinskid5083f62017-01-16 15:07:21 -0800184 return classname.to_string();
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700185 }
Adam Lesinskia1ad4a82015-06-08 11:41:09 -0700186
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700187 if (package.empty()) {
188 return {};
189 }
Adam Lesinskia1ad4a82015-06-08 11:41:09 -0700190
Adam Lesinski96ea08f2017-11-06 10:44:46 -0800191 std::string result = package.to_string();
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700192 if (classname.data()[0] != '.') {
193 result += '.';
194 }
Adam Lesinski52364f72016-01-11 13:10:24 -0800195
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700196 result.append(classname.data(), classname.size());
197 if (!IsJavaClassName(result)) {
198 return {};
199 }
200 return result;
Adam Lesinskia1ad4a82015-06-08 11:41:09 -0700201}
202
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700203static size_t ConsumeDigits(const char* start, const char* end) {
204 const char* c = start;
205 for (; c != end && *c >= '0' && *c <= '9'; c++) {
206 }
207 return static_cast<size_t>(c - start);
Adam Lesinskib23f1e02015-11-03 12:24:17 -0800208}
209
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700210bool VerifyJavaStringFormat(const StringPiece& str) {
211 const char* c = str.begin();
212 const char* const end = str.end();
Adam Lesinskib23f1e02015-11-03 12:24:17 -0800213
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700214 size_t arg_count = 0;
215 bool nonpositional = false;
216 while (c != end) {
217 if (*c == '%' && c + 1 < end) {
218 c++;
Adam Lesinskib23f1e02015-11-03 12:24:17 -0800219
Adam Lesinskib9f05482017-06-02 16:32:37 -0700220 if (*c == '%' || *c == 'n') {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700221 c++;
222 continue;
223 }
Adam Lesinskib23f1e02015-11-03 12:24:17 -0800224
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700225 arg_count++;
Adam Lesinskib23f1e02015-11-03 12:24:17 -0800226
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700227 size_t num_digits = ConsumeDigits(c, end);
228 if (num_digits > 0) {
229 c += num_digits;
230 if (c != end && *c != '$') {
231 // The digits were a size, but not a positional argument.
232 nonpositional = true;
Adam Lesinskib23f1e02015-11-03 12:24:17 -0800233 }
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700234 } else if (*c == '<') {
235 // Reusing last argument, bad idea since positions can be moved around
236 // during translation.
237 nonpositional = true;
Adam Lesinskib23f1e02015-11-03 12:24:17 -0800238
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700239 c++;
240
241 // Optionally we can have a $ after
242 if (c != end && *c == '$') {
243 c++;
Adam Lesinskib23f1e02015-11-03 12:24:17 -0800244 }
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700245 } else {
246 nonpositional = true;
247 }
248
249 // Ignore size, width, flags, etc.
250 while (c != end && (*c == '-' || *c == '#' || *c == '+' || *c == ' ' ||
251 *c == ',' || *c == '(' || (*c >= '0' && *c <= '9'))) {
252 c++;
253 }
254
255 /*
256 * This is a shortcut to detect strings that are going to Time.format()
257 * instead of String.format()
258 *
259 * Comparison of String.format() and Time.format() args:
260 *
261 * String: ABC E GH ST X abcdefgh nost x
262 * Time: DEFGHKMS W Za d hkm s w yz
263 *
264 * Therefore we know it's definitely Time if we have:
265 * DFKMWZkmwyz
266 */
267 if (c != end) {
268 switch (*c) {
269 case 'D':
270 case 'F':
271 case 'K':
272 case 'M':
273 case 'W':
274 case 'Z':
275 case 'k':
276 case 'm':
277 case 'w':
278 case 'y':
279 case 'z':
280 return true;
281 }
282 }
Adam Lesinskib23f1e02015-11-03 12:24:17 -0800283 }
284
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700285 if (c != end) {
286 c++;
Adam Lesinskib23f1e02015-11-03 12:24:17 -0800287 }
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700288 }
289
290 if (arg_count > 1 && nonpositional) {
291 // Multiple arguments were specified, but some or all were non positional.
292 // Translated
293 // strings may rearrange the order of the arguments, which will break the
294 // string.
295 return false;
296 }
297 return true;
Adam Lesinskib23f1e02015-11-03 12:24:17 -0800298}
299
Ryan Mitchelld86ea582018-06-27 11:57:18 -0700300std::string Utf8ToModifiedUtf8(const std::string& utf8) {
301 // Java uses Modified UTF-8 which only supports the 1, 2, and 3 byte formats of UTF-8. To encode
302 // 4 byte UTF-8 codepoints, Modified UTF-8 allows the use of surrogate pairs in the same format
303 // of CESU-8 surrogate pairs. Calculate the size of the utf8 string with all 4 byte UTF-8
304 // codepoints replaced with 2 3 byte surrogate pairs
305 size_t modified_size = 0;
306 const size_t size = utf8.size();
307 for (size_t i = 0; i < size; i++) {
308 if (((uint8_t) utf8[i] >> 4) == 0xF) {
309 modified_size += 6;
310 i += 3;
311 } else {
312 modified_size++;
313 }
314 }
315
316 // Early out if no 4 byte codepoints are found
317 if (size == modified_size) {
318 return utf8;
319 }
320
321 std::string output;
322 output.reserve(modified_size);
323 for (size_t i = 0; i < size; i++) {
324 if (((uint8_t) utf8[i] >> 4) == 0xF) {
Ryan Mitchell4353d61b2018-09-10 17:09:12 -0700325 int32_t codepoint = utf32_from_utf8_at(utf8.data(), size, i, nullptr);
Ryan Mitchelld86ea582018-06-27 11:57:18 -0700326
327 // Calculate the high and low surrogates as UTF-16 would
Ryan Mitchell4353d61b2018-09-10 17:09:12 -0700328 int32_t high = ((codepoint - 0x10000) / 0x400) + 0xD800;
329 int32_t low = ((codepoint - 0x10000) % 0x400) + 0xDC00;
Ryan Mitchelld86ea582018-06-27 11:57:18 -0700330
331 // Encode each surrogate in UTF-8
332 output.push_back((char) (0xE4 | ((high >> 12) & 0xF)));
333 output.push_back((char) (0x80 | ((high >> 6) & 0x3F)));
334 output.push_back((char) (0x80 | (high & 0x3F)));
335 output.push_back((char) (0xE4 | ((low >> 12) & 0xF)));
336 output.push_back((char) (0x80 | ((low >> 6) & 0x3F)));
337 output.push_back((char) (0x80 | (low & 0x3F)));
338 i += 3;
339 } else {
340 output.push_back(utf8[i]);
341 }
342 }
343
344 return output;
345}
346
Ryan Mitchell4353d61b2018-09-10 17:09:12 -0700347std::string ModifiedUtf8ToUtf8(const std::string& modified_utf8) {
348 // The UTF-8 representation will have a byte length less than or equal to the Modified UTF-8
349 // representation.
350 std::string output;
351 output.reserve(modified_utf8.size());
352
353 size_t index = 0;
354 const size_t modified_size = modified_utf8.size();
355 while (index < modified_size) {
356 size_t next_index;
357 int32_t high_surrogate = utf32_from_utf8_at(modified_utf8.data(), modified_size, index,
358 &next_index);
359 if (high_surrogate < 0) {
360 return {};
361 }
362
363 // Check that the first codepoint is within the high surrogate range
364 if (high_surrogate >= 0xD800 && high_surrogate <= 0xDB7F) {
365 int32_t low_surrogate = utf32_from_utf8_at(modified_utf8.data(), modified_size, next_index,
366 &next_index);
367 if (low_surrogate < 0) {
368 return {};
369 }
370
371 // Check that the second codepoint is within the low surrogate range
372 if (low_surrogate >= 0xDC00 && low_surrogate <= 0xDFFF) {
373 const char32_t codepoint = (char32_t) (((high_surrogate - 0xD800) * 0x400)
374 + (low_surrogate - 0xDC00) + 0x10000);
375
376 // The decoded codepoint should represent a 4 byte, UTF-8 character
377 const size_t utf8_length = (size_t) utf32_to_utf8_length(&codepoint, 1);
378 if (utf8_length != 4) {
379 return {};
380 }
381
382 // Encode the UTF-8 representation of the codepoint into the string
383 char* start = &output[output.size()];
384 output.resize(output.size() + utf8_length);
385 utf32_to_utf8((char32_t*) &codepoint, 1, start, utf8_length + 1);
386
387 index = next_index;
388 continue;
389 }
390 }
391
392 // Append non-surrogate pairs to the output string
393 for (size_t i = index; i < next_index; i++) {
394 output.push_back(modified_utf8[i]);
395 }
396 index = next_index;
397 }
398 return output;
399}
400
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700401std::u16string Utf8ToUtf16(const StringPiece& utf8) {
402 ssize_t utf16_length = utf8_to_utf16_length(
403 reinterpret_cast<const uint8_t*>(utf8.data()), utf8.length());
404 if (utf16_length <= 0) {
405 return {};
406 }
407
408 std::u16string utf16;
409 utf16.resize(utf16_length);
410 utf8_to_utf16(reinterpret_cast<const uint8_t*>(utf8.data()), utf8.length(),
411 &*utf16.begin(), utf16_length + 1);
412 return utf16;
413}
414
415std::string Utf16ToUtf8(const StringPiece16& utf16) {
416 ssize_t utf8_length = utf16_to_utf8_length(utf16.data(), utf16.length());
417 if (utf8_length <= 0) {
418 return {};
419 }
420
421 std::string utf8;
422 utf8.resize(utf8_length);
423 utf16_to_utf8(utf16.data(), utf16.length(), &*utf8.begin(), utf8_length + 1);
424 return utf8;
425}
426
427bool WriteAll(std::ostream& out, const BigBuffer& buffer) {
428 for (const auto& b : buffer) {
429 if (!out.write(reinterpret_cast<const char*>(b.buffer.get()), b.size)) {
430 return false;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800431 }
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700432 }
433 return true;
434}
435
436std::unique_ptr<uint8_t[]> Copy(const BigBuffer& buffer) {
437 std::unique_ptr<uint8_t[]> data =
438 std::unique_ptr<uint8_t[]>(new uint8_t[buffer.size()]);
439 uint8_t* p = data.get();
440 for (const auto& block : buffer) {
441 memcpy(p, block.buffer.get(), block.size);
442 p += block.size;
443 }
444 return data;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800445}
446
Adam Lesinskid0f116b2016-07-08 15:00:32 -0700447typename Tokenizer::iterator& Tokenizer::iterator::operator++() {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700448 const char* start = token_.end();
449 const char* end = str_.end();
450 if (start == end) {
451 end_ = true;
452 token_.assign(token_.end(), 0);
Adam Lesinskid0f116b2016-07-08 15:00:32 -0700453 return *this;
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700454 }
455
456 start += 1;
457 const char* current = start;
458 while (current != end) {
459 if (*current == separator_) {
460 token_.assign(start, current - start);
461 return *this;
462 }
463 ++current;
464 }
465 token_.assign(start, end - start);
466 return *this;
Adam Lesinskid0f116b2016-07-08 15:00:32 -0700467}
468
469bool Tokenizer::iterator::operator==(const iterator& rhs) const {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700470 // We check equality here a bit differently.
471 // We need to know that the addresses are the same.
472 return token_.begin() == rhs.token_.begin() &&
473 token_.end() == rhs.token_.end() && end_ == rhs.end_;
Adam Lesinskid0f116b2016-07-08 15:00:32 -0700474}
475
476bool Tokenizer::iterator::operator!=(const iterator& rhs) const {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700477 return !(*this == rhs);
Adam Lesinskid0f116b2016-07-08 15:00:32 -0700478}
479
Chih-Hung Hsieh4dc58122017-08-03 16:28:10 -0700480Tokenizer::iterator::iterator(const StringPiece& s, char sep, const StringPiece& tok, bool end)
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700481 : str_(s), separator_(sep), token_(tok), end_(end) {}
Adam Lesinskid0f116b2016-07-08 15:00:32 -0700482
Chih-Hung Hsieh4dc58122017-08-03 16:28:10 -0700483Tokenizer::Tokenizer(const StringPiece& str, char sep)
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700484 : begin_(++iterator(str, sep, StringPiece(str.begin() - 1, 0), false)),
485 end_(str, sep, StringPiece(str.end(), 0), true) {}
Adam Lesinskid0f116b2016-07-08 15:00:32 -0700486
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700487bool ExtractResFilePathParts(const StringPiece& path, StringPiece* out_prefix,
488 StringPiece* out_entry, StringPiece* out_suffix) {
489 const StringPiece res_prefix("res/");
490 if (!StartsWith(path, res_prefix)) {
491 return false;
492 }
493
494 StringPiece::const_iterator last_occurence = path.end();
495 for (auto iter = path.begin() + res_prefix.size(); iter != path.end();
496 ++iter) {
497 if (*iter == '/') {
498 last_occurence = iter;
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700499 }
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700500 }
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700501
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700502 if (last_occurence == path.end()) {
503 return false;
504 }
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700505
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700506 auto iter = std::find(last_occurence, path.end(), '.');
507 *out_suffix = StringPiece(iter, path.end() - iter);
508 *out_entry = StringPiece(last_occurence + 1, iter - last_occurence - 1);
509 *out_prefix = StringPiece(path.begin(), last_occurence - path.begin() + 1);
510 return true;
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700511}
512
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700513StringPiece16 GetString16(const android::ResStringPool& pool, size_t idx) {
514 size_t len;
515 const char16_t* str = pool.stringAt(idx, &len);
516 if (str != nullptr) {
517 return StringPiece16(str, len);
518 }
519 return StringPiece16();
Adam Lesinskid0f116b2016-07-08 15:00:32 -0700520}
521
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700522std::string GetString(const android::ResStringPool& pool, size_t idx) {
523 size_t len;
524 const char* str = pool.string8At(idx, &len);
525 if (str != nullptr) {
Ryan Mitchell4353d61b2018-09-10 17:09:12 -0700526 return ModifiedUtf8ToUtf8(std::string(str, len));
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700527 }
528 return Utf16ToUtf8(GetString16(pool, idx));
Adam Lesinskid0f116b2016-07-08 15:00:32 -0700529}
530
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700531} // namespace util
532} // namespace aapt