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 | 66ea840 | 2017-06-28 11:44:11 -0700 | [diff] [blame^] | 27 | #include "text/Utf8Iterator.h" |
Adam Lesinski | d5083f6 | 2017-01-16 15:07:21 -0800 | [diff] [blame] | 28 | #include "util/BigBuffer.h" |
| 29 | #include "util/Maybe.h" |
| 30 | |
Adam Lesinski | 66ea840 | 2017-06-28 11:44:11 -0700 | [diff] [blame^] | 31 | using ::aapt::text::Utf8Iterator; |
Adam Lesinski | 549e437 | 2017-06-27 18:39:07 -0700 | [diff] [blame] | 32 | using ::android::StringPiece; |
| 33 | using ::android::StringPiece16; |
Adam Lesinski | d5083f6 | 2017-01-16 15:07:21 -0800 | [diff] [blame] | 34 | |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 35 | namespace aapt { |
| 36 | namespace util { |
| 37 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 38 | static 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 Lesinski | d5083f6 | 2017-01-16 15:07:21 -0800 | [diff] [blame] | 46 | parts.emplace_back(str.substr(start, current).to_string()); |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 47 | 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 Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 54 | } |
| 55 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 56 | std::vector<std::string> Split(const StringPiece& str, char sep) { |
| 57 | return SplitAndTransform(str, sep, nullptr); |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 58 | } |
| 59 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 60 | std::vector<std::string> SplitAndLowercase(const StringPiece& str, char sep) { |
| 61 | return SplitAndTransform(str, sep, ::tolower); |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 62 | } |
| 63 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 64 | bool 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 Lesinski | d0f116b | 2016-07-08 15:00:32 -0700 | [diff] [blame] | 69 | } |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 70 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 71 | bool 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 Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 76 | } |
| 77 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 78 | StringPiece TrimWhitespace(const StringPiece& str) { |
| 79 | if (str.size() == 0 || str.data() == nullptr) { |
| 80 | return str; |
| 81 | } |
Adam Lesinski | 3b4cd94 | 2015-10-30 16:31:42 -0700 | [diff] [blame] | 82 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 83 | const char* start = str.data(); |
| 84 | const char* end = str.data() + str.length(); |
Adam Lesinski | 3b4cd94 | 2015-10-30 16:31:42 -0700 | [diff] [blame] | 85 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 86 | while (start != end && isspace(*start)) { |
| 87 | start++; |
| 88 | } |
Adam Lesinski | 3b4cd94 | 2015-10-30 16:31:42 -0700 | [diff] [blame] | 89 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 90 | while (end != start && isspace(*(end - 1))) { |
| 91 | end--; |
| 92 | } |
Adam Lesinski | 3b4cd94 | 2015-10-30 16:31:42 -0700 | [diff] [blame] | 93 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 94 | return StringPiece(start, end - start); |
Adam Lesinski | 3b4cd94 | 2015-10-30 16:31:42 -0700 | [diff] [blame] | 95 | } |
| 96 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 97 | StringPiece::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 Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 105 | } |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 106 | |
| 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 Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 120 | } |
| 121 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 122 | bool 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 Lesinski | a1ad4a8 | 2015-06-08 11:41:09 -0700 | [diff] [blame] | 128 | } |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 129 | |
| 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 Lesinski | a1ad4a8 | 2015-06-08 11:41:09 -0700 | [diff] [blame] | 140 | } |
| 141 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 142 | bool 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 Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 152 | } |
| 153 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 154 | if (piece.data()[0] == '_' || piece.data()[piece.size() - 1] == '_') { |
| 155 | return false; |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 156 | } |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 157 | |
| 158 | if (FindNonAlphaNumericAndNotInSet(piece, "_") != piece.end()) { |
| 159 | return false; |
| 160 | } |
| 161 | } |
| 162 | return pieces >= 1; |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 163 | } |
| 164 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 165 | Maybe<std::string> GetFullyQualifiedClassName(const StringPiece& package, |
| 166 | const StringPiece& classname) { |
| 167 | if (classname.empty()) { |
| 168 | return {}; |
| 169 | } |
Adam Lesinski | a1ad4a8 | 2015-06-08 11:41:09 -0700 | [diff] [blame] | 170 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 171 | if (util::IsJavaClassName(classname)) { |
Adam Lesinski | d5083f6 | 2017-01-16 15:07:21 -0800 | [diff] [blame] | 172 | return classname.to_string(); |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 173 | } |
Adam Lesinski | a1ad4a8 | 2015-06-08 11:41:09 -0700 | [diff] [blame] | 174 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 175 | if (package.empty()) { |
| 176 | return {}; |
| 177 | } |
Adam Lesinski | a1ad4a8 | 2015-06-08 11:41:09 -0700 | [diff] [blame] | 178 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 179 | std::string result(package.data(), package.size()); |
| 180 | if (classname.data()[0] != '.') { |
| 181 | result += '.'; |
| 182 | } |
Adam Lesinski | 52364f7 | 2016-01-11 13:10:24 -0800 | [diff] [blame] | 183 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 184 | result.append(classname.data(), classname.size()); |
| 185 | if (!IsJavaClassName(result)) { |
| 186 | return {}; |
| 187 | } |
| 188 | return result; |
Adam Lesinski | a1ad4a8 | 2015-06-08 11:41:09 -0700 | [diff] [blame] | 189 | } |
| 190 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 191 | static 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 Lesinski | b23f1e0 | 2015-11-03 12:24:17 -0800 | [diff] [blame] | 196 | } |
| 197 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 198 | bool VerifyJavaStringFormat(const StringPiece& str) { |
| 199 | const char* c = str.begin(); |
| 200 | const char* const end = str.end(); |
Adam Lesinski | b23f1e0 | 2015-11-03 12:24:17 -0800 | [diff] [blame] | 201 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 202 | size_t arg_count = 0; |
| 203 | bool nonpositional = false; |
| 204 | while (c != end) { |
| 205 | if (*c == '%' && c + 1 < end) { |
| 206 | c++; |
Adam Lesinski | b23f1e0 | 2015-11-03 12:24:17 -0800 | [diff] [blame] | 207 | |
Adam Lesinski | b9f0548 | 2017-06-02 16:32:37 -0700 | [diff] [blame] | 208 | if (*c == '%' || *c == 'n') { |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 209 | c++; |
| 210 | continue; |
| 211 | } |
Adam Lesinski | b23f1e0 | 2015-11-03 12:24:17 -0800 | [diff] [blame] | 212 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 213 | arg_count++; |
Adam Lesinski | b23f1e0 | 2015-11-03 12:24:17 -0800 | [diff] [blame] | 214 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 215 | 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 Lesinski | b23f1e0 | 2015-11-03 12:24:17 -0800 | [diff] [blame] | 221 | } |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 222 | } else if (*c == '<') { |
| 223 | // Reusing last argument, bad idea since positions can be moved around |
| 224 | // during translation. |
| 225 | nonpositional = true; |
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 | c++; |
| 228 | |
| 229 | // Optionally we can have a $ after |
| 230 | if (c != end && *c == '$') { |
| 231 | c++; |
Adam Lesinski | b23f1e0 | 2015-11-03 12:24:17 -0800 | [diff] [blame] | 232 | } |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 233 | } 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 Lesinski | b23f1e0 | 2015-11-03 12:24:17 -0800 | [diff] [blame] | 271 | } |
| 272 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 273 | if (c != end) { |
| 274 | c++; |
Adam Lesinski | b23f1e0 | 2015-11-03 12:24:17 -0800 | [diff] [blame] | 275 | } |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 276 | } |
| 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 Lesinski | b23f1e0 | 2015-11-03 12:24:17 -0800 | [diff] [blame] | 286 | } |
| 287 | |
Adam Lesinski | 549e437 | 2017-06-27 18:39:07 -0700 | [diff] [blame] | 288 | static 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 | |
| 304 | static bool AppendUnicodeCodepoint(Utf8Iterator* iter, std::string* output) { |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 305 | char32_t code = 0; |
Adam Lesinski | 549e437 | 2017-06-27 18:39:07 -0700 | [diff] [blame] | 306 | for (size_t i = 0; i < 4 && iter->HasNext(); i++) { |
| 307 | char32_t codepoint = iter->Next(); |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 308 | char32_t a; |
Adam Lesinski | 549e437 | 2017-06-27 18:39:07 -0700 | [diff] [blame] | 309 | 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 Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 315 | } else { |
| 316 | return {}; |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 317 | } |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 318 | code = (code << 4) | a; |
| 319 | } |
Adam Lesinski | 549e437 | 2017-06-27 18:39:07 -0700 | [diff] [blame] | 320 | return AppendCodepointToUtf8String(code, output); |
| 321 | } |
Adam Lesinski | d0f116b | 2016-07-08 15:00:32 -0700 | [diff] [blame] | 322 | |
Adam Lesinski | 549e437 | 2017-06-27 18:39:07 -0700 | [diff] [blame] | 323 | static bool IsCodepointSpace(char32_t codepoint) { |
| 324 | if (static_cast<uint32_t>(codepoint) & 0xffffff00u) { |
| 325 | return false; |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 326 | } |
Adam Lesinski | 549e437 | 2017-06-27 18:39:07 -0700 | [diff] [blame] | 327 | return isspace(static_cast<char>(codepoint)); |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 328 | } |
| 329 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 330 | StringBuilder& StringBuilder::Append(const StringPiece& str) { |
| 331 | if (!error_.empty()) { |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 332 | return *this; |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 333 | } |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 334 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 335 | // Where the new data will be appended to. |
Adam Lesinski | 549e437 | 2017-06-27 18:39:07 -0700 | [diff] [blame] | 336 | const size_t new_data_index = str_.size(); |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 337 | |
Adam Lesinski | 549e437 | 2017-06-27 18:39:07 -0700 | [diff] [blame] | 338 | Utf8Iterator iter(str); |
| 339 | while (iter.HasNext()) { |
| 340 | const char32_t codepoint = iter.Next(); |
| 341 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 342 | if (last_char_was_escape_) { |
Adam Lesinski | 549e437 | 2017-06-27 18:39:07 -0700 | [diff] [blame] | 343 | switch (codepoint) { |
| 344 | case U't': |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 345 | str_ += '\t'; |
| 346 | break; |
Adam Lesinski | 549e437 | 2017-06-27 18:39:07 -0700 | [diff] [blame] | 347 | |
| 348 | case U'n': |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 349 | str_ += '\n'; |
| 350 | break; |
Adam Lesinski | 549e437 | 2017-06-27 18:39:07 -0700 | [diff] [blame] | 351 | |
| 352 | case U'#': |
| 353 | case U'@': |
| 354 | case U'?': |
| 355 | case U'"': |
| 356 | case U'\'': |
| 357 | case U'\\': |
| 358 | str_ += static_cast<char>(codepoint); |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 359 | break; |
Adam Lesinski | 549e437 | 2017-06-27 18:39:07 -0700 | [diff] [blame] | 360 | |
| 361 | case U'u': |
| 362 | if (!AppendUnicodeCodepoint(&iter, &str_)) { |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 363 | error_ = "invalid unicode escape sequence"; |
| 364 | return *this; |
| 365 | } |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 366 | break; |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 367 | |
| 368 | default: |
Adam Lesinski | 549e437 | 2017-06-27 18:39:07 -0700 | [diff] [blame] | 369 | // Ignore the escape character and just include the codepoint. |
| 370 | AppendCodepointToUtf8String(codepoint, &str_); |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 371 | break; |
| 372 | } |
| 373 | last_char_was_escape_ = false; |
Adam Lesinski | 549e437 | 2017-06-27 18:39:07 -0700 | [diff] [blame] | 374 | |
| 375 | } else if (codepoint == U'"') { |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 376 | if (!quote_ && trailing_space_) { |
| 377 | // We found an opening quote, and we have |
| 378 | // trailing space, so we should append that |
| 379 | // space now. |
| 380 | if (trailing_space_) { |
| 381 | // We had trailing whitespace, so |
| 382 | // replace with a single space. |
| 383 | if (!str_.empty()) { |
| 384 | str_ += ' '; |
| 385 | } |
| 386 | trailing_space_ = false; |
| 387 | } |
| 388 | } |
| 389 | quote_ = !quote_; |
Adam Lesinski | 549e437 | 2017-06-27 18:39:07 -0700 | [diff] [blame] | 390 | |
| 391 | } else if (codepoint == U'\'' && !quote_) { |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 392 | // This should be escaped. |
| 393 | error_ = "unescaped apostrophe"; |
| 394 | return *this; |
Adam Lesinski | 549e437 | 2017-06-27 18:39:07 -0700 | [diff] [blame] | 395 | |
| 396 | } else if (codepoint == U'\\') { |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 397 | // This is an escape sequence, convert to the real value. |
| 398 | if (!quote_ && trailing_space_) { |
| 399 | // We had trailing whitespace, so |
| 400 | // replace with a single space. |
| 401 | if (!str_.empty()) { |
| 402 | str_ += ' '; |
| 403 | } |
| 404 | trailing_space_ = false; |
| 405 | } |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 406 | last_char_was_escape_ = true; |
Adam Lesinski | 549e437 | 2017-06-27 18:39:07 -0700 | [diff] [blame] | 407 | } else { |
| 408 | if (quote_) { |
| 409 | // Quotes mean everything is taken, including whitespace. |
| 410 | AppendCodepointToUtf8String(codepoint, &str_); |
| 411 | } else { |
| 412 | // This is not quoted text, so we will accumulate whitespace and only emit a single |
| 413 | // character of whitespace if it is followed by a non-whitespace character. |
| 414 | if (IsCodepointSpace(codepoint)) { |
| 415 | // We found whitespace. |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 416 | trailing_space_ = true; |
Adam Lesinski | 549e437 | 2017-06-27 18:39:07 -0700 | [diff] [blame] | 417 | } else { |
| 418 | if (trailing_space_) { |
| 419 | // We saw trailing space before, so replace all |
| 420 | // that trailing space with one space. |
| 421 | if (!str_.empty()) { |
| 422 | str_ += ' '; |
| 423 | } |
| 424 | trailing_space_ = false; |
| 425 | } |
| 426 | AppendCodepointToUtf8String(codepoint, &str_); |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 427 | } |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 428 | } |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 429 | } |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 430 | } |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 431 | |
| 432 | // Accumulate the added string's UTF-16 length. |
Adam Lesinski | 549e437 | 2017-06-27 18:39:07 -0700 | [diff] [blame] | 433 | ssize_t len = utf8_to_utf16_length(reinterpret_cast<const uint8_t*>(str_.data()) + new_data_index, |
| 434 | str_.size() - new_data_index); |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 435 | if (len < 0) { |
| 436 | error_ = "invalid unicode code point"; |
| 437 | return *this; |
| 438 | } |
| 439 | utf16_len_ += len; |
| 440 | return *this; |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 441 | } |
| 442 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 443 | std::u16string Utf8ToUtf16(const StringPiece& utf8) { |
| 444 | ssize_t utf16_length = utf8_to_utf16_length( |
| 445 | reinterpret_cast<const uint8_t*>(utf8.data()), utf8.length()); |
| 446 | if (utf16_length <= 0) { |
| 447 | return {}; |
| 448 | } |
| 449 | |
| 450 | std::u16string utf16; |
| 451 | utf16.resize(utf16_length); |
| 452 | utf8_to_utf16(reinterpret_cast<const uint8_t*>(utf8.data()), utf8.length(), |
| 453 | &*utf16.begin(), utf16_length + 1); |
| 454 | return utf16; |
| 455 | } |
| 456 | |
| 457 | std::string Utf16ToUtf8(const StringPiece16& utf16) { |
| 458 | ssize_t utf8_length = utf16_to_utf8_length(utf16.data(), utf16.length()); |
| 459 | if (utf8_length <= 0) { |
| 460 | return {}; |
| 461 | } |
| 462 | |
| 463 | std::string utf8; |
| 464 | utf8.resize(utf8_length); |
| 465 | utf16_to_utf8(utf16.data(), utf16.length(), &*utf8.begin(), utf8_length + 1); |
| 466 | return utf8; |
| 467 | } |
| 468 | |
| 469 | bool WriteAll(std::ostream& out, const BigBuffer& buffer) { |
| 470 | for (const auto& b : buffer) { |
| 471 | if (!out.write(reinterpret_cast<const char*>(b.buffer.get()), b.size)) { |
| 472 | return false; |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 473 | } |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 474 | } |
| 475 | return true; |
| 476 | } |
| 477 | |
| 478 | std::unique_ptr<uint8_t[]> Copy(const BigBuffer& buffer) { |
| 479 | std::unique_ptr<uint8_t[]> data = |
| 480 | std::unique_ptr<uint8_t[]>(new uint8_t[buffer.size()]); |
| 481 | uint8_t* p = data.get(); |
| 482 | for (const auto& block : buffer) { |
| 483 | memcpy(p, block.buffer.get(), block.size); |
| 484 | p += block.size; |
| 485 | } |
| 486 | return data; |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 487 | } |
| 488 | |
Adam Lesinski | d0f116b | 2016-07-08 15:00:32 -0700 | [diff] [blame] | 489 | typename Tokenizer::iterator& Tokenizer::iterator::operator++() { |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 490 | const char* start = token_.end(); |
| 491 | const char* end = str_.end(); |
| 492 | if (start == end) { |
| 493 | end_ = true; |
| 494 | token_.assign(token_.end(), 0); |
Adam Lesinski | d0f116b | 2016-07-08 15:00:32 -0700 | [diff] [blame] | 495 | return *this; |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 496 | } |
| 497 | |
| 498 | start += 1; |
| 499 | const char* current = start; |
| 500 | while (current != end) { |
| 501 | if (*current == separator_) { |
| 502 | token_.assign(start, current - start); |
| 503 | return *this; |
| 504 | } |
| 505 | ++current; |
| 506 | } |
| 507 | token_.assign(start, end - start); |
| 508 | return *this; |
Adam Lesinski | d0f116b | 2016-07-08 15:00:32 -0700 | [diff] [blame] | 509 | } |
| 510 | |
| 511 | bool Tokenizer::iterator::operator==(const iterator& rhs) const { |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 512 | // We check equality here a bit differently. |
| 513 | // We need to know that the addresses are the same. |
| 514 | return token_.begin() == rhs.token_.begin() && |
| 515 | token_.end() == rhs.token_.end() && end_ == rhs.end_; |
Adam Lesinski | d0f116b | 2016-07-08 15:00:32 -0700 | [diff] [blame] | 516 | } |
| 517 | |
| 518 | bool Tokenizer::iterator::operator!=(const iterator& rhs) const { |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 519 | return !(*this == rhs); |
Adam Lesinski | d0f116b | 2016-07-08 15:00:32 -0700 | [diff] [blame] | 520 | } |
| 521 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 522 | Tokenizer::iterator::iterator(StringPiece s, char sep, StringPiece tok, |
| 523 | bool end) |
| 524 | : str_(s), separator_(sep), token_(tok), end_(end) {} |
Adam Lesinski | d0f116b | 2016-07-08 15:00:32 -0700 | [diff] [blame] | 525 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 526 | Tokenizer::Tokenizer(StringPiece str, char sep) |
| 527 | : begin_(++iterator(str, sep, StringPiece(str.begin() - 1, 0), false)), |
| 528 | end_(str, sep, StringPiece(str.end(), 0), true) {} |
Adam Lesinski | d0f116b | 2016-07-08 15:00:32 -0700 | [diff] [blame] | 529 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 530 | bool 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 Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 542 | } |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 543 | } |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 544 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 545 | if (last_occurence == path.end()) { |
| 546 | return false; |
| 547 | } |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 548 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 549 | 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 Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 554 | } |
| 555 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 556 | StringPiece16 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 Lesinski | d0f116b | 2016-07-08 15:00:32 -0700 | [diff] [blame] | 563 | } |
| 564 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 565 | std::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 Lesinski | d0f116b | 2016-07-08 15:00:32 -0700 | [diff] [blame] | 572 | } |
| 573 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 574 | } // namespace util |
| 575 | } // namespace aapt |