Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 1 | /* |
| 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 Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 17 | #include "util/Util.h" |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 18 | |
| 19 | #include <algorithm> |
| 20 | #include <ostream> |
| 21 | #include <string> |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 22 | #include <vector> |
| 23 | |
Adam Lesinski | d5083f6 | 2017-01-16 15:07:21 -0800 | [diff] [blame] | 24 | #include "androidfw/StringPiece.h" |
Adam Lesinski | 549e437 | 2017-06-27 18:39:07 -0700 | [diff] [blame] | 25 | #include "utils/Unicode.h" |
Adam Lesinski | d5083f6 | 2017-01-16 15:07:21 -0800 | [diff] [blame] | 26 | |
Adam Lesinski | 96ea08f | 2017-11-06 10:44:46 -0800 | [diff] [blame] | 27 | #include "text/Unicode.h" |
Adam Lesinski | 66ea840 | 2017-06-28 11:44:11 -0700 | [diff] [blame] | 28 | #include "text/Utf8Iterator.h" |
Adam Lesinski | d5083f6 | 2017-01-16 15:07:21 -0800 | [diff] [blame] | 29 | #include "util/BigBuffer.h" |
| 30 | #include "util/Maybe.h" |
| 31 | |
Adam Lesinski | 66ea840 | 2017-06-28 11:44:11 -0700 | [diff] [blame] | 32 | using ::aapt::text::Utf8Iterator; |
Adam Lesinski | 549e437 | 2017-06-27 18:39:07 -0700 | [diff] [blame] | 33 | using ::android::StringPiece; |
| 34 | using ::android::StringPiece16; |
Adam Lesinski | d5083f6 | 2017-01-16 15:07:21 -0800 | [diff] [blame] | 35 | |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 36 | namespace aapt { |
| 37 | namespace util { |
| 38 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 39 | static 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 Lesinski | d5083f6 | 2017-01-16 15:07:21 -0800 | [diff] [blame] | 47 | parts.emplace_back(str.substr(start, current).to_string()); |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 48 | 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 Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 55 | } |
| 56 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 57 | std::vector<std::string> Split(const StringPiece& str, char sep) { |
| 58 | return SplitAndTransform(str, sep, nullptr); |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 59 | } |
| 60 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 61 | std::vector<std::string> SplitAndLowercase(const StringPiece& str, char sep) { |
| 62 | return SplitAndTransform(str, sep, ::tolower); |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 63 | } |
| 64 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 65 | bool 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 Lesinski | d0f116b | 2016-07-08 15:00:32 -0700 | [diff] [blame] | 70 | } |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 71 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 72 | bool 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 Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 77 | } |
| 78 | |
Adam Lesinski | 2eed52e | 2018-02-21 15:55:58 -0800 | [diff] [blame^] | 79 | StringPiece 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 | |
| 93 | StringPiece 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 Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 107 | StringPiece TrimWhitespace(const StringPiece& str) { |
| 108 | if (str.size() == 0 || str.data() == nullptr) { |
| 109 | return str; |
| 110 | } |
Adam Lesinski | 3b4cd94 | 2015-10-30 16:31:42 -0700 | [diff] [blame] | 111 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 112 | const char* start = str.data(); |
| 113 | const char* end = str.data() + str.length(); |
Adam Lesinski | 3b4cd94 | 2015-10-30 16:31:42 -0700 | [diff] [blame] | 114 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 115 | while (start != end && isspace(*start)) { |
| 116 | start++; |
| 117 | } |
Adam Lesinski | 3b4cd94 | 2015-10-30 16:31:42 -0700 | [diff] [blame] | 118 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 119 | while (end != start && isspace(*(end - 1))) { |
| 120 | end--; |
| 121 | } |
Adam Lesinski | 3b4cd94 | 2015-10-30 16:31:42 -0700 | [diff] [blame] | 122 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 123 | return StringPiece(start, end - start); |
Adam Lesinski | 3b4cd94 | 2015-10-30 16:31:42 -0700 | [diff] [blame] | 124 | } |
| 125 | |
Adam Lesinski | 96ea08f | 2017-11-06 10:44:46 -0800 | [diff] [blame] | 126 | static 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 Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 132 | } |
| 133 | } |
Adam Lesinski | 96ea08f | 2017-11-06 10:44:46 -0800 | [diff] [blame] | 134 | return pieces; |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 135 | } |
| 136 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 137 | bool IsJavaClassName(const StringPiece& str) { |
Adam Lesinski | 96ea08f | 2017-11-06 10:44:46 -0800 | [diff] [blame] | 138 | return IsJavaNameImpl(str) >= 2; |
Adam Lesinski | a1ad4a8 | 2015-06-08 11:41:09 -0700 | [diff] [blame] | 139 | } |
| 140 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 141 | bool IsJavaPackageName(const StringPiece& str) { |
Adam Lesinski | 96ea08f | 2017-11-06 10:44:46 -0800 | [diff] [blame] | 142 | return IsJavaNameImpl(str) >= 1; |
| 143 | } |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 144 | |
Adam Lesinski | 96ea08f | 2017-11-06 10:44:46 -0800 | [diff] [blame] | 145 | static int IsAndroidNameImpl(const StringPiece& str) { |
| 146 | int pieces = 0; |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 147 | for (const StringPiece& piece : Tokenize(str, '.')) { |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 148 | if (piece.empty()) { |
Adam Lesinski | 96ea08f | 2017-11-06 10:44:46 -0800 | [diff] [blame] | 149 | return -1; |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 150 | } |
| 151 | |
Adam Lesinski | 96ea08f | 2017-11-06 10:44:46 -0800 | [diff] [blame] | 152 | const char first_character = piece.data()[0]; |
| 153 | if (!::isalpha(first_character)) { |
| 154 | return -1; |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 155 | } |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 156 | |
Adam Lesinski | 96ea08f | 2017-11-06 10:44:46 -0800 | [diff] [blame] | 157 | 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 Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 163 | } |
Adam Lesinski | 96ea08f | 2017-11-06 10:44:46 -0800 | [diff] [blame] | 164 | pieces++; |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 165 | } |
Adam Lesinski | 96ea08f | 2017-11-06 10:44:46 -0800 | [diff] [blame] | 166 | return pieces; |
| 167 | } |
| 168 | |
| 169 | bool IsAndroidPackageName(const StringPiece& str) { |
| 170 | return IsAndroidNameImpl(str) > 1 || str == "android"; |
| 171 | } |
| 172 | |
| 173 | bool IsAndroidSplitName(const StringPiece& str) { |
| 174 | return IsAndroidNameImpl(str) > 0; |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 175 | } |
| 176 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 177 | Maybe<std::string> GetFullyQualifiedClassName(const StringPiece& package, |
| 178 | const StringPiece& classname) { |
| 179 | if (classname.empty()) { |
| 180 | return {}; |
| 181 | } |
Adam Lesinski | a1ad4a8 | 2015-06-08 11:41:09 -0700 | [diff] [blame] | 182 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 183 | if (util::IsJavaClassName(classname)) { |
Adam Lesinski | d5083f6 | 2017-01-16 15:07:21 -0800 | [diff] [blame] | 184 | return classname.to_string(); |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 185 | } |
Adam Lesinski | a1ad4a8 | 2015-06-08 11:41:09 -0700 | [diff] [blame] | 186 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 187 | if (package.empty()) { |
| 188 | return {}; |
| 189 | } |
Adam Lesinski | a1ad4a8 | 2015-06-08 11:41:09 -0700 | [diff] [blame] | 190 | |
Adam Lesinski | 96ea08f | 2017-11-06 10:44:46 -0800 | [diff] [blame] | 191 | std::string result = package.to_string(); |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 192 | if (classname.data()[0] != '.') { |
| 193 | result += '.'; |
| 194 | } |
Adam Lesinski | 52364f7 | 2016-01-11 13:10:24 -0800 | [diff] [blame] | 195 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 196 | result.append(classname.data(), classname.size()); |
| 197 | if (!IsJavaClassName(result)) { |
| 198 | return {}; |
| 199 | } |
| 200 | return result; |
Adam Lesinski | a1ad4a8 | 2015-06-08 11:41:09 -0700 | [diff] [blame] | 201 | } |
| 202 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 203 | static 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 Lesinski | b23f1e0 | 2015-11-03 12:24:17 -0800 | [diff] [blame] | 208 | } |
| 209 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 210 | bool VerifyJavaStringFormat(const StringPiece& str) { |
| 211 | const char* c = str.begin(); |
| 212 | const char* const end = str.end(); |
Adam Lesinski | b23f1e0 | 2015-11-03 12:24:17 -0800 | [diff] [blame] | 213 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 214 | size_t arg_count = 0; |
| 215 | bool nonpositional = false; |
| 216 | while (c != end) { |
| 217 | if (*c == '%' && c + 1 < end) { |
| 218 | c++; |
Adam Lesinski | b23f1e0 | 2015-11-03 12:24:17 -0800 | [diff] [blame] | 219 | |
Adam Lesinski | b9f0548 | 2017-06-02 16:32:37 -0700 | [diff] [blame] | 220 | if (*c == '%' || *c == 'n') { |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 221 | c++; |
| 222 | continue; |
| 223 | } |
Adam Lesinski | b23f1e0 | 2015-11-03 12:24:17 -0800 | [diff] [blame] | 224 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 225 | arg_count++; |
Adam Lesinski | b23f1e0 | 2015-11-03 12:24:17 -0800 | [diff] [blame] | 226 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 227 | 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 Lesinski | b23f1e0 | 2015-11-03 12:24:17 -0800 | [diff] [blame] | 233 | } |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 234 | } else if (*c == '<') { |
| 235 | // Reusing last argument, bad idea since positions can be moved around |
| 236 | // during translation. |
| 237 | nonpositional = true; |
Adam Lesinski | b23f1e0 | 2015-11-03 12:24:17 -0800 | [diff] [blame] | 238 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 239 | c++; |
| 240 | |
| 241 | // Optionally we can have a $ after |
| 242 | if (c != end && *c == '$') { |
| 243 | c++; |
Adam Lesinski | b23f1e0 | 2015-11-03 12:24:17 -0800 | [diff] [blame] | 244 | } |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 245 | } 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 Lesinski | b23f1e0 | 2015-11-03 12:24:17 -0800 | [diff] [blame] | 283 | } |
| 284 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 285 | if (c != end) { |
| 286 | c++; |
Adam Lesinski | b23f1e0 | 2015-11-03 12:24:17 -0800 | [diff] [blame] | 287 | } |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 288 | } |
| 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 Lesinski | b23f1e0 | 2015-11-03 12:24:17 -0800 | [diff] [blame] | 298 | } |
| 299 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 300 | std::u16string Utf8ToUtf16(const StringPiece& utf8) { |
| 301 | ssize_t utf16_length = utf8_to_utf16_length( |
| 302 | reinterpret_cast<const uint8_t*>(utf8.data()), utf8.length()); |
| 303 | if (utf16_length <= 0) { |
| 304 | return {}; |
| 305 | } |
| 306 | |
| 307 | std::u16string utf16; |
| 308 | utf16.resize(utf16_length); |
| 309 | utf8_to_utf16(reinterpret_cast<const uint8_t*>(utf8.data()), utf8.length(), |
| 310 | &*utf16.begin(), utf16_length + 1); |
| 311 | return utf16; |
| 312 | } |
| 313 | |
| 314 | std::string Utf16ToUtf8(const StringPiece16& utf16) { |
| 315 | ssize_t utf8_length = utf16_to_utf8_length(utf16.data(), utf16.length()); |
| 316 | if (utf8_length <= 0) { |
| 317 | return {}; |
| 318 | } |
| 319 | |
| 320 | std::string utf8; |
| 321 | utf8.resize(utf8_length); |
| 322 | utf16_to_utf8(utf16.data(), utf16.length(), &*utf8.begin(), utf8_length + 1); |
| 323 | return utf8; |
| 324 | } |
| 325 | |
| 326 | bool WriteAll(std::ostream& out, const BigBuffer& buffer) { |
| 327 | for (const auto& b : buffer) { |
| 328 | if (!out.write(reinterpret_cast<const char*>(b.buffer.get()), b.size)) { |
| 329 | return false; |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 330 | } |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 331 | } |
| 332 | return true; |
| 333 | } |
| 334 | |
| 335 | std::unique_ptr<uint8_t[]> Copy(const BigBuffer& buffer) { |
| 336 | std::unique_ptr<uint8_t[]> data = |
| 337 | std::unique_ptr<uint8_t[]>(new uint8_t[buffer.size()]); |
| 338 | uint8_t* p = data.get(); |
| 339 | for (const auto& block : buffer) { |
| 340 | memcpy(p, block.buffer.get(), block.size); |
| 341 | p += block.size; |
| 342 | } |
| 343 | return data; |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 344 | } |
| 345 | |
Adam Lesinski | d0f116b | 2016-07-08 15:00:32 -0700 | [diff] [blame] | 346 | typename Tokenizer::iterator& Tokenizer::iterator::operator++() { |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 347 | const char* start = token_.end(); |
| 348 | const char* end = str_.end(); |
| 349 | if (start == end) { |
| 350 | end_ = true; |
| 351 | token_.assign(token_.end(), 0); |
Adam Lesinski | d0f116b | 2016-07-08 15:00:32 -0700 | [diff] [blame] | 352 | return *this; |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 353 | } |
| 354 | |
| 355 | start += 1; |
| 356 | const char* current = start; |
| 357 | while (current != end) { |
| 358 | if (*current == separator_) { |
| 359 | token_.assign(start, current - start); |
| 360 | return *this; |
| 361 | } |
| 362 | ++current; |
| 363 | } |
| 364 | token_.assign(start, end - start); |
| 365 | return *this; |
Adam Lesinski | d0f116b | 2016-07-08 15:00:32 -0700 | [diff] [blame] | 366 | } |
| 367 | |
| 368 | bool Tokenizer::iterator::operator==(const iterator& rhs) const { |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 369 | // We check equality here a bit differently. |
| 370 | // We need to know that the addresses are the same. |
| 371 | return token_.begin() == rhs.token_.begin() && |
| 372 | token_.end() == rhs.token_.end() && end_ == rhs.end_; |
Adam Lesinski | d0f116b | 2016-07-08 15:00:32 -0700 | [diff] [blame] | 373 | } |
| 374 | |
| 375 | bool Tokenizer::iterator::operator!=(const iterator& rhs) const { |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 376 | return !(*this == rhs); |
Adam Lesinski | d0f116b | 2016-07-08 15:00:32 -0700 | [diff] [blame] | 377 | } |
| 378 | |
Chih-Hung Hsieh | 4dc5812 | 2017-08-03 16:28:10 -0700 | [diff] [blame] | 379 | Tokenizer::iterator::iterator(const StringPiece& s, char sep, const StringPiece& tok, bool end) |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 380 | : str_(s), separator_(sep), token_(tok), end_(end) {} |
Adam Lesinski | d0f116b | 2016-07-08 15:00:32 -0700 | [diff] [blame] | 381 | |
Chih-Hung Hsieh | 4dc5812 | 2017-08-03 16:28:10 -0700 | [diff] [blame] | 382 | Tokenizer::Tokenizer(const StringPiece& str, char sep) |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 383 | : begin_(++iterator(str, sep, StringPiece(str.begin() - 1, 0), false)), |
| 384 | end_(str, sep, StringPiece(str.end(), 0), true) {} |
Adam Lesinski | d0f116b | 2016-07-08 15:00:32 -0700 | [diff] [blame] | 385 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 386 | bool ExtractResFilePathParts(const StringPiece& path, StringPiece* out_prefix, |
| 387 | StringPiece* out_entry, StringPiece* out_suffix) { |
| 388 | const StringPiece res_prefix("res/"); |
| 389 | if (!StartsWith(path, res_prefix)) { |
| 390 | return false; |
| 391 | } |
| 392 | |
| 393 | StringPiece::const_iterator last_occurence = path.end(); |
| 394 | for (auto iter = path.begin() + res_prefix.size(); iter != path.end(); |
| 395 | ++iter) { |
| 396 | if (*iter == '/') { |
| 397 | last_occurence = iter; |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 398 | } |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 399 | } |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 400 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 401 | if (last_occurence == path.end()) { |
| 402 | return false; |
| 403 | } |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 404 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 405 | auto iter = std::find(last_occurence, path.end(), '.'); |
| 406 | *out_suffix = StringPiece(iter, path.end() - iter); |
| 407 | *out_entry = StringPiece(last_occurence + 1, iter - last_occurence - 1); |
| 408 | *out_prefix = StringPiece(path.begin(), last_occurence - path.begin() + 1); |
| 409 | return true; |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 410 | } |
| 411 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 412 | StringPiece16 GetString16(const android::ResStringPool& pool, size_t idx) { |
| 413 | size_t len; |
| 414 | const char16_t* str = pool.stringAt(idx, &len); |
| 415 | if (str != nullptr) { |
| 416 | return StringPiece16(str, len); |
| 417 | } |
| 418 | return StringPiece16(); |
Adam Lesinski | d0f116b | 2016-07-08 15:00:32 -0700 | [diff] [blame] | 419 | } |
| 420 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 421 | std::string GetString(const android::ResStringPool& pool, size_t idx) { |
| 422 | size_t len; |
| 423 | const char* str = pool.string8At(idx, &len); |
| 424 | if (str != nullptr) { |
| 425 | return std::string(str, len); |
| 426 | } |
| 427 | return Utf16ToUtf8(GetString16(pool, idx)); |
Adam Lesinski | d0f116b | 2016-07-08 15:00:32 -0700 | [diff] [blame] | 428 | } |
| 429 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 430 | } // namespace util |
| 431 | } // namespace aapt |