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 | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 79 | StringPiece TrimWhitespace(const StringPiece& str) { |
| 80 | if (str.size() == 0 || str.data() == nullptr) { |
| 81 | return str; |
| 82 | } |
Adam Lesinski | 3b4cd94 | 2015-10-30 16:31:42 -0700 | [diff] [blame] | 83 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 84 | const char* start = str.data(); |
| 85 | const char* end = str.data() + str.length(); |
Adam Lesinski | 3b4cd94 | 2015-10-30 16:31:42 -0700 | [diff] [blame] | 86 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 87 | while (start != end && isspace(*start)) { |
| 88 | start++; |
| 89 | } |
Adam Lesinski | 3b4cd94 | 2015-10-30 16:31:42 -0700 | [diff] [blame] | 90 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 91 | while (end != start && isspace(*(end - 1))) { |
| 92 | end--; |
| 93 | } |
Adam Lesinski | 3b4cd94 | 2015-10-30 16:31:42 -0700 | [diff] [blame] | 94 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 95 | return StringPiece(start, end - start); |
Adam Lesinski | 3b4cd94 | 2015-10-30 16:31:42 -0700 | [diff] [blame] | 96 | } |
| 97 | |
Adam Lesinski | 96ea08f | 2017-11-06 10:44:46 -0800 | [diff] [blame] | 98 | static 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 Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 104 | } |
| 105 | } |
Adam Lesinski | 96ea08f | 2017-11-06 10:44:46 -0800 | [diff] [blame] | 106 | return pieces; |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 107 | } |
| 108 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 109 | bool IsJavaClassName(const StringPiece& str) { |
Adam Lesinski | 96ea08f | 2017-11-06 10:44:46 -0800 | [diff] [blame] | 110 | return IsJavaNameImpl(str) >= 2; |
Adam Lesinski | a1ad4a8 | 2015-06-08 11:41:09 -0700 | [diff] [blame] | 111 | } |
| 112 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 113 | bool IsJavaPackageName(const StringPiece& str) { |
Adam Lesinski | 96ea08f | 2017-11-06 10:44:46 -0800 | [diff] [blame] | 114 | return IsJavaNameImpl(str) >= 1; |
| 115 | } |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 116 | |
Adam Lesinski | 96ea08f | 2017-11-06 10:44:46 -0800 | [diff] [blame] | 117 | static int IsAndroidNameImpl(const StringPiece& str) { |
| 118 | int pieces = 0; |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 119 | for (const StringPiece& piece : Tokenize(str, '.')) { |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 120 | if (piece.empty()) { |
Adam Lesinski | 96ea08f | 2017-11-06 10:44:46 -0800 | [diff] [blame] | 121 | return -1; |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 122 | } |
| 123 | |
Adam Lesinski | 96ea08f | 2017-11-06 10:44:46 -0800 | [diff] [blame] | 124 | const char first_character = piece.data()[0]; |
| 125 | if (!::isalpha(first_character)) { |
| 126 | return -1; |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 127 | } |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 128 | |
Adam Lesinski | 96ea08f | 2017-11-06 10:44:46 -0800 | [diff] [blame] | 129 | 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 Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 135 | } |
Adam Lesinski | 96ea08f | 2017-11-06 10:44:46 -0800 | [diff] [blame] | 136 | pieces++; |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 137 | } |
Adam Lesinski | 96ea08f | 2017-11-06 10:44:46 -0800 | [diff] [blame] | 138 | return pieces; |
| 139 | } |
| 140 | |
| 141 | bool IsAndroidPackageName(const StringPiece& str) { |
| 142 | return IsAndroidNameImpl(str) > 1 || str == "android"; |
| 143 | } |
| 144 | |
| 145 | bool IsAndroidSplitName(const StringPiece& str) { |
| 146 | return IsAndroidNameImpl(str) > 0; |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 147 | } |
| 148 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 149 | Maybe<std::string> GetFullyQualifiedClassName(const StringPiece& package, |
| 150 | const StringPiece& classname) { |
| 151 | if (classname.empty()) { |
| 152 | return {}; |
| 153 | } |
Adam Lesinski | a1ad4a8 | 2015-06-08 11:41:09 -0700 | [diff] [blame] | 154 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 155 | if (util::IsJavaClassName(classname)) { |
Adam Lesinski | d5083f6 | 2017-01-16 15:07:21 -0800 | [diff] [blame] | 156 | return classname.to_string(); |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 157 | } |
Adam Lesinski | a1ad4a8 | 2015-06-08 11:41:09 -0700 | [diff] [blame] | 158 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 159 | if (package.empty()) { |
| 160 | return {}; |
| 161 | } |
Adam Lesinski | a1ad4a8 | 2015-06-08 11:41:09 -0700 | [diff] [blame] | 162 | |
Adam Lesinski | 96ea08f | 2017-11-06 10:44:46 -0800 | [diff] [blame] | 163 | std::string result = package.to_string(); |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 164 | if (classname.data()[0] != '.') { |
| 165 | result += '.'; |
| 166 | } |
Adam Lesinski | 52364f7 | 2016-01-11 13:10:24 -0800 | [diff] [blame] | 167 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 168 | result.append(classname.data(), classname.size()); |
| 169 | if (!IsJavaClassName(result)) { |
| 170 | return {}; |
| 171 | } |
| 172 | return result; |
Adam Lesinski | a1ad4a8 | 2015-06-08 11:41:09 -0700 | [diff] [blame] | 173 | } |
| 174 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 175 | static 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 Lesinski | b23f1e0 | 2015-11-03 12:24:17 -0800 | [diff] [blame] | 180 | } |
| 181 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 182 | bool VerifyJavaStringFormat(const StringPiece& str) { |
| 183 | const char* c = str.begin(); |
| 184 | const char* const end = str.end(); |
Adam Lesinski | b23f1e0 | 2015-11-03 12:24:17 -0800 | [diff] [blame] | 185 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 186 | size_t arg_count = 0; |
| 187 | bool nonpositional = false; |
| 188 | while (c != end) { |
| 189 | if (*c == '%' && c + 1 < end) { |
| 190 | c++; |
Adam Lesinski | b23f1e0 | 2015-11-03 12:24:17 -0800 | [diff] [blame] | 191 | |
Adam Lesinski | b9f0548 | 2017-06-02 16:32:37 -0700 | [diff] [blame] | 192 | if (*c == '%' || *c == 'n') { |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 193 | c++; |
| 194 | continue; |
| 195 | } |
Adam Lesinski | b23f1e0 | 2015-11-03 12:24:17 -0800 | [diff] [blame] | 196 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 197 | arg_count++; |
Adam Lesinski | b23f1e0 | 2015-11-03 12:24:17 -0800 | [diff] [blame] | 198 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 199 | 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 Lesinski | b23f1e0 | 2015-11-03 12:24:17 -0800 | [diff] [blame] | 205 | } |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 206 | } else if (*c == '<') { |
| 207 | // Reusing last argument, bad idea since positions can be moved around |
| 208 | // during translation. |
| 209 | nonpositional = true; |
Adam Lesinski | b23f1e0 | 2015-11-03 12:24:17 -0800 | [diff] [blame] | 210 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 211 | c++; |
| 212 | |
| 213 | // Optionally we can have a $ after |
| 214 | if (c != end && *c == '$') { |
| 215 | c++; |
Adam Lesinski | b23f1e0 | 2015-11-03 12:24:17 -0800 | [diff] [blame] | 216 | } |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 217 | } 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 Lesinski | b23f1e0 | 2015-11-03 12:24:17 -0800 | [diff] [blame] | 255 | } |
| 256 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 257 | if (c != end) { |
| 258 | c++; |
Adam Lesinski | b23f1e0 | 2015-11-03 12:24:17 -0800 | [diff] [blame] | 259 | } |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 260 | } |
| 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 Lesinski | b23f1e0 | 2015-11-03 12:24:17 -0800 | [diff] [blame] | 270 | } |
| 271 | |
Adam Lesinski | 549e437 | 2017-06-27 18:39:07 -0700 | [diff] [blame] | 272 | static 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 | |
| 288 | static bool AppendUnicodeCodepoint(Utf8Iterator* iter, std::string* output) { |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 289 | char32_t code = 0; |
Adam Lesinski | 549e437 | 2017-06-27 18:39:07 -0700 | [diff] [blame] | 290 | for (size_t i = 0; i < 4 && iter->HasNext(); i++) { |
| 291 | char32_t codepoint = iter->Next(); |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 292 | char32_t a; |
Adam Lesinski | 549e437 | 2017-06-27 18:39:07 -0700 | [diff] [blame] | 293 | 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 Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 299 | } else { |
| 300 | return {}; |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 301 | } |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 302 | code = (code << 4) | a; |
| 303 | } |
Adam Lesinski | 549e437 | 2017-06-27 18:39:07 -0700 | [diff] [blame] | 304 | return AppendCodepointToUtf8String(code, output); |
| 305 | } |
Adam Lesinski | d0f116b | 2016-07-08 15:00:32 -0700 | [diff] [blame] | 306 | |
Adam Lesinski | 549e437 | 2017-06-27 18:39:07 -0700 | [diff] [blame] | 307 | static bool IsCodepointSpace(char32_t codepoint) { |
| 308 | if (static_cast<uint32_t>(codepoint) & 0xffffff00u) { |
| 309 | return false; |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 310 | } |
Adam Lesinski | 549e437 | 2017-06-27 18:39:07 -0700 | [diff] [blame] | 311 | return isspace(static_cast<char>(codepoint)); |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 312 | } |
| 313 | |
Adam Lesinski | fba0cf2 | 2017-06-29 17:53:36 -0700 | [diff] [blame] | 314 | StringBuilder::StringBuilder(bool preserve_spaces) : preserve_spaces_(preserve_spaces) { |
| 315 | } |
| 316 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 317 | StringBuilder& StringBuilder::Append(const StringPiece& str) { |
| 318 | if (!error_.empty()) { |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 319 | return *this; |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 320 | } |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 321 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 322 | // Where the new data will be appended to. |
Adam Lesinski | 549e437 | 2017-06-27 18:39:07 -0700 | [diff] [blame] | 323 | const size_t new_data_index = str_.size(); |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 324 | |
Adam Lesinski | 549e437 | 2017-06-27 18:39:07 -0700 | [diff] [blame] | 325 | Utf8Iterator iter(str); |
| 326 | while (iter.HasNext()) { |
| 327 | const char32_t codepoint = iter.Next(); |
| 328 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 329 | if (last_char_was_escape_) { |
Adam Lesinski | 549e437 | 2017-06-27 18:39:07 -0700 | [diff] [blame] | 330 | switch (codepoint) { |
| 331 | case U't': |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 332 | str_ += '\t'; |
| 333 | break; |
Adam Lesinski | 549e437 | 2017-06-27 18:39:07 -0700 | [diff] [blame] | 334 | |
| 335 | case U'n': |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 336 | str_ += '\n'; |
| 337 | break; |
Adam Lesinski | 549e437 | 2017-06-27 18:39:07 -0700 | [diff] [blame] | 338 | |
| 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 Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 346 | break; |
Adam Lesinski | 549e437 | 2017-06-27 18:39:07 -0700 | [diff] [blame] | 347 | |
| 348 | case U'u': |
| 349 | if (!AppendUnicodeCodepoint(&iter, &str_)) { |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 350 | error_ = "invalid unicode escape sequence"; |
| 351 | return *this; |
| 352 | } |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 353 | break; |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 354 | |
| 355 | default: |
Adam Lesinski | 549e437 | 2017-06-27 18:39:07 -0700 | [diff] [blame] | 356 | // Ignore the escape character and just include the codepoint. |
| 357 | AppendCodepointToUtf8String(codepoint, &str_); |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 358 | break; |
| 359 | } |
| 360 | last_char_was_escape_ = false; |
Adam Lesinski | 549e437 | 2017-06-27 18:39:07 -0700 | [diff] [blame] | 361 | |
Adam Lesinski | fba0cf2 | 2017-06-29 17:53:36 -0700 | [diff] [blame] | 362 | } else if (!preserve_spaces_ && codepoint == U'"') { |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 363 | if (!quote_ && trailing_space_) { |
Adam Lesinski | fba0cf2 | 2017-06-29 17:53:36 -0700 | [diff] [blame] | 364 | // We found an opening quote, and we have trailing space, so we should append that |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 365 | // space now. |
| 366 | if (trailing_space_) { |
Adam Lesinski | fba0cf2 | 2017-06-29 17:53:36 -0700 | [diff] [blame] | 367 | // We had trailing whitespace, so replace with a single space. |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 368 | if (!str_.empty()) { |
| 369 | str_ += ' '; |
| 370 | } |
| 371 | trailing_space_ = false; |
| 372 | } |
| 373 | } |
| 374 | quote_ = !quote_; |
Adam Lesinski | 549e437 | 2017-06-27 18:39:07 -0700 | [diff] [blame] | 375 | |
Adam Lesinski | fba0cf2 | 2017-06-29 17:53:36 -0700 | [diff] [blame] | 376 | } else if (!preserve_spaces_ && codepoint == U'\'' && !quote_) { |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 377 | // This should be escaped. |
| 378 | error_ = "unescaped apostrophe"; |
| 379 | return *this; |
Adam Lesinski | 549e437 | 2017-06-27 18:39:07 -0700 | [diff] [blame] | 380 | |
| 381 | } else if (codepoint == U'\\') { |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 382 | // 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 Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 391 | last_char_was_escape_ = true; |
Adam Lesinski | 549e437 | 2017-06-27 18:39:07 -0700 | [diff] [blame] | 392 | } else { |
Adam Lesinski | fba0cf2 | 2017-06-29 17:53:36 -0700 | [diff] [blame] | 393 | if (preserve_spaces_ || quote_) { |
Adam Lesinski | 549e437 | 2017-06-27 18:39:07 -0700 | [diff] [blame] | 394 | // 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 Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 401 | trailing_space_ = true; |
Adam Lesinski | 549e437 | 2017-06-27 18:39:07 -0700 | [diff] [blame] | 402 | } 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 Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 412 | } |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 413 | } |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 414 | } |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 415 | } |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 416 | |
| 417 | // Accumulate the added string's UTF-16 length. |
Adam Lesinski | 549e437 | 2017-06-27 18:39:07 -0700 | [diff] [blame] | 418 | ssize_t len = utf8_to_utf16_length(reinterpret_cast<const uint8_t*>(str_.data()) + new_data_index, |
| 419 | str_.size() - new_data_index); |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 420 | if (len < 0) { |
| 421 | error_ = "invalid unicode code point"; |
| 422 | return *this; |
| 423 | } |
| 424 | utf16_len_ += len; |
| 425 | return *this; |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 426 | } |
| 427 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 428 | std::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 | |
| 442 | std::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 | |
| 454 | bool 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 Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 458 | } |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 459 | } |
| 460 | return true; |
| 461 | } |
| 462 | |
| 463 | std::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 Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 472 | } |
| 473 | |
Adam Lesinski | d0f116b | 2016-07-08 15:00:32 -0700 | [diff] [blame] | 474 | typename Tokenizer::iterator& Tokenizer::iterator::operator++() { |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 475 | 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 Lesinski | d0f116b | 2016-07-08 15:00:32 -0700 | [diff] [blame] | 480 | return *this; |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 481 | } |
| 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 Lesinski | d0f116b | 2016-07-08 15:00:32 -0700 | [diff] [blame] | 494 | } |
| 495 | |
| 496 | bool Tokenizer::iterator::operator==(const iterator& rhs) const { |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 497 | // 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 Lesinski | d0f116b | 2016-07-08 15:00:32 -0700 | [diff] [blame] | 501 | } |
| 502 | |
| 503 | bool Tokenizer::iterator::operator!=(const iterator& rhs) const { |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 504 | return !(*this == rhs); |
Adam Lesinski | d0f116b | 2016-07-08 15:00:32 -0700 | [diff] [blame] | 505 | } |
| 506 | |
Chih-Hung Hsieh | 4dc5812 | 2017-08-03 16:28:10 -0700 | [diff] [blame] | 507 | 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] | 508 | : str_(s), separator_(sep), token_(tok), end_(end) {} |
Adam Lesinski | d0f116b | 2016-07-08 15:00:32 -0700 | [diff] [blame] | 509 | |
Chih-Hung Hsieh | 4dc5812 | 2017-08-03 16:28:10 -0700 | [diff] [blame] | 510 | Tokenizer::Tokenizer(const StringPiece& str, char sep) |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 511 | : begin_(++iterator(str, sep, StringPiece(str.begin() - 1, 0), false)), |
| 512 | end_(str, sep, StringPiece(str.end(), 0), true) {} |
Adam Lesinski | d0f116b | 2016-07-08 15:00:32 -0700 | [diff] [blame] | 513 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 514 | bool 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 Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 526 | } |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 527 | } |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 528 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 529 | if (last_occurence == path.end()) { |
| 530 | return false; |
| 531 | } |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 532 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 533 | 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 Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 538 | } |
| 539 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 540 | StringPiece16 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 Lesinski | d0f116b | 2016-07-08 15:00:32 -0700 | [diff] [blame] | 547 | } |
| 548 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 549 | std::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 Lesinski | d0f116b | 2016-07-08 15:00:32 -0700 | [diff] [blame] | 556 | } |
| 557 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 558 | } // namespace util |
| 559 | } // namespace aapt |