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 | |
| 17 | #ifndef AAPT_UTIL_H |
| 18 | #define AAPT_UTIL_H |
| 19 | |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 20 | #include "util/BigBuffer.h" |
| 21 | #include "util/Maybe.h" |
| 22 | #include "util/StringPiece.h" |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 23 | |
| 24 | #include <androidfw/ResourceTypes.h> |
| 25 | #include <functional> |
| 26 | #include <memory> |
| 27 | #include <ostream> |
| 28 | #include <string> |
| 29 | #include <vector> |
| 30 | |
| 31 | namespace aapt { |
| 32 | namespace util { |
| 33 | |
| 34 | std::vector<std::string> split(const StringPiece& str, char sep); |
| 35 | std::vector<std::string> splitAndLowercase(const StringPiece& str, char sep); |
| 36 | |
| 37 | /** |
Adam Lesinski | 4d3a987 | 2015-04-09 19:53:22 -0700 | [diff] [blame] | 38 | * Returns true if the string starts with prefix. |
| 39 | */ |
| 40 | template <typename T> |
| 41 | bool stringStartsWith(const BasicStringPiece<T>& str, const BasicStringPiece<T>& prefix) { |
| 42 | if (str.size() < prefix.size()) { |
| 43 | return false; |
| 44 | } |
| 45 | return str.substr(0, prefix.size()) == prefix; |
| 46 | } |
| 47 | |
| 48 | /** |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 49 | * Returns true if the string ends with suffix. |
| 50 | */ |
Adam Lesinski | 4d3a987 | 2015-04-09 19:53:22 -0700 | [diff] [blame] | 51 | template <typename T> |
| 52 | bool stringEndsWith(const BasicStringPiece<T>& str, const BasicStringPiece<T>& suffix) { |
| 53 | if (str.size() < suffix.size()) { |
| 54 | return false; |
| 55 | } |
| 56 | return str.substr(str.size() - suffix.size(), suffix.size()) == suffix; |
| 57 | } |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 58 | |
| 59 | /** |
| 60 | * Creates a new StringPiece16 that points to a substring |
| 61 | * of the original string without leading or trailing whitespace. |
| 62 | */ |
| 63 | StringPiece16 trimWhitespace(const StringPiece16& str); |
| 64 | |
Adam Lesinski | 3b4cd94 | 2015-10-30 16:31:42 -0700 | [diff] [blame] | 65 | StringPiece trimWhitespace(const StringPiece& str); |
| 66 | |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 67 | /** |
| 68 | * UTF-16 isspace(). It basically checks for lower range characters that are |
| 69 | * whitespace. |
| 70 | */ |
| 71 | inline bool isspace16(char16_t c) { |
| 72 | return c < 0x0080 && isspace(c); |
| 73 | } |
| 74 | |
| 75 | /** |
| 76 | * Returns an iterator to the first character that is not alpha-numeric and that |
| 77 | * is not in the allowedChars set. |
| 78 | */ |
| 79 | StringPiece16::const_iterator findNonAlphaNumericAndNotInSet(const StringPiece16& str, |
| 80 | const StringPiece16& allowedChars); |
| 81 | |
| 82 | /** |
Adam Lesinski | a1ad4a8 | 2015-06-08 11:41:09 -0700 | [diff] [blame] | 83 | * Tests that the string is a valid Java class name. |
| 84 | */ |
| 85 | bool isJavaClassName(const StringPiece16& str); |
| 86 | |
| 87 | /** |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 88 | * Tests that the string is a valid Java package name. |
| 89 | */ |
| 90 | bool isJavaPackageName(const StringPiece16& str); |
| 91 | |
| 92 | /** |
Adam Lesinski | a1ad4a8 | 2015-06-08 11:41:09 -0700 | [diff] [blame] | 93 | * Converts the class name to a fully qualified class name from the given `package`. Ex: |
| 94 | * |
| 95 | * asdf --> package.asdf |
| 96 | * .asdf --> package.asdf |
| 97 | * .a.b --> package.a.b |
| 98 | * asdf.adsf --> asdf.adsf |
| 99 | */ |
| 100 | Maybe<std::u16string> getFullyQualifiedClassName(const StringPiece16& package, |
| 101 | const StringPiece16& className); |
| 102 | |
| 103 | |
| 104 | /** |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 105 | * Makes a std::unique_ptr<> with the template parameter inferred by the compiler. |
| 106 | * This will be present in C++14 and can be removed then. |
| 107 | */ |
| 108 | template <typename T, class... Args> |
| 109 | std::unique_ptr<T> make_unique(Args&&... args) { |
| 110 | return std::unique_ptr<T>(new T{std::forward<Args>(args)...}); |
| 111 | } |
| 112 | |
| 113 | /** |
| 114 | * Writes a set of items to the std::ostream, joining the times with the provided |
| 115 | * separator. |
| 116 | */ |
| 117 | template <typename Iterator> |
| 118 | ::std::function<::std::ostream&(::std::ostream&)> joiner(Iterator begin, Iterator end, |
| 119 | const char* sep) { |
| 120 | return [begin, end, sep](::std::ostream& out) -> ::std::ostream& { |
| 121 | for (auto iter = begin; iter != end; ++iter) { |
| 122 | if (iter != begin) { |
| 123 | out << sep; |
| 124 | } |
| 125 | out << *iter; |
| 126 | } |
| 127 | return out; |
| 128 | }; |
| 129 | } |
| 130 | |
| 131 | inline ::std::function<::std::ostream&(::std::ostream&)> formatSize(size_t size) { |
| 132 | return [size](::std::ostream& out) -> ::std::ostream& { |
Adam Lesinski | ca2fc35 | 2015-04-03 12:08:26 -0700 | [diff] [blame] | 133 | constexpr size_t K = 1024u; |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 134 | constexpr size_t M = K * K; |
Greg Hackmann | 1fce4f9 | 2015-04-02 20:23:22 -0700 | [diff] [blame] | 135 | constexpr size_t G = M * K; |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 136 | if (size < K) { |
| 137 | out << size << "B"; |
| 138 | } else if (size < M) { |
| 139 | out << (double(size) / K) << " KiB"; |
| 140 | } else if (size < G) { |
| 141 | out << (double(size) / M) << " MiB"; |
| 142 | } else { |
| 143 | out << (double(size) / G) << " GiB"; |
| 144 | } |
| 145 | return out; |
| 146 | }; |
| 147 | } |
| 148 | |
| 149 | /** |
| 150 | * Helper method to extract a string from a StringPool. |
| 151 | */ |
| 152 | inline StringPiece16 getString(const android::ResStringPool& pool, size_t idx) { |
| 153 | size_t len; |
| 154 | const char16_t* str = pool.stringAt(idx, &len); |
| 155 | if (str != nullptr) { |
| 156 | return StringPiece16(str, len); |
| 157 | } |
| 158 | return StringPiece16(); |
| 159 | } |
| 160 | |
Adam Lesinski | b23f1e0 | 2015-11-03 12:24:17 -0800 | [diff] [blame] | 161 | /** |
| 162 | * Checks that the Java string format contains no non-positional arguments (arguments without |
| 163 | * explicitly specifying an index) when there are more than one argument. This is an error |
| 164 | * because translations may rearrange the order of the arguments in the string, which will |
| 165 | * break the string interpolation. |
| 166 | */ |
| 167 | bool verifyJavaStringFormat(const StringPiece16& str); |
| 168 | |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 169 | class StringBuilder { |
| 170 | public: |
| 171 | StringBuilder& append(const StringPiece16& str); |
| 172 | const std::u16string& str() const; |
| 173 | const std::string& error() const; |
| 174 | operator bool() const; |
| 175 | |
| 176 | private: |
| 177 | std::u16string mStr; |
| 178 | bool mQuote = false; |
| 179 | bool mTrailingSpace = false; |
Adam Lesinski | 9095988 | 2015-07-06 18:09:18 -0700 | [diff] [blame] | 180 | bool mLastCharWasEscape = false; |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 181 | std::string mError; |
| 182 | }; |
| 183 | |
| 184 | inline const std::u16string& StringBuilder::str() const { |
| 185 | return mStr; |
| 186 | } |
| 187 | |
| 188 | inline const std::string& StringBuilder::error() const { |
| 189 | return mError; |
| 190 | } |
| 191 | |
| 192 | inline StringBuilder::operator bool() const { |
| 193 | return mError.empty(); |
| 194 | } |
| 195 | |
| 196 | /** |
| 197 | * Converts a UTF8 string to a UTF16 string. |
| 198 | */ |
| 199 | std::u16string utf8ToUtf16(const StringPiece& utf8); |
| 200 | std::string utf16ToUtf8(const StringPiece16& utf8); |
| 201 | |
| 202 | /** |
| 203 | * Writes the entire BigBuffer to the output stream. |
| 204 | */ |
| 205 | bool writeAll(std::ostream& out, const BigBuffer& buffer); |
| 206 | |
| 207 | /* |
| 208 | * Copies the entire BigBuffer into a single buffer. |
| 209 | */ |
| 210 | std::unique_ptr<uint8_t[]> copy(const BigBuffer& buffer); |
| 211 | |
| 212 | /** |
| 213 | * A Tokenizer implemented as an iterable collection. It does not allocate |
| 214 | * any memory on the heap nor use standard containers. |
| 215 | */ |
| 216 | template <typename Char> |
| 217 | class Tokenizer { |
| 218 | public: |
| 219 | class iterator { |
| 220 | public: |
| 221 | iterator(const iterator&) = default; |
| 222 | iterator& operator=(const iterator&) = default; |
| 223 | |
| 224 | iterator& operator++(); |
| 225 | BasicStringPiece<Char> operator*(); |
| 226 | bool operator==(const iterator& rhs) const; |
| 227 | bool operator!=(const iterator& rhs) const; |
| 228 | |
| 229 | private: |
| 230 | friend class Tokenizer<Char>; |
| 231 | |
Adam Lesinski | cf95a58 | 2015-11-16 15:37:30 -0800 | [diff] [blame^] | 232 | iterator(BasicStringPiece<Char> s, Char sep, BasicStringPiece<Char> tok, bool end); |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 233 | |
Adam Lesinski | cf95a58 | 2015-11-16 15:37:30 -0800 | [diff] [blame^] | 234 | BasicStringPiece<Char> mStr; |
| 235 | Char mSeparator; |
| 236 | BasicStringPiece<Char> mToken; |
| 237 | bool mEnd; |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 238 | }; |
| 239 | |
| 240 | Tokenizer(BasicStringPiece<Char> str, Char sep); |
| 241 | iterator begin(); |
| 242 | iterator end(); |
| 243 | |
| 244 | private: |
| 245 | const iterator mBegin; |
| 246 | const iterator mEnd; |
| 247 | }; |
| 248 | |
| 249 | template <typename Char> |
| 250 | inline Tokenizer<Char> tokenize(BasicStringPiece<Char> str, Char sep) { |
| 251 | return Tokenizer<Char>(str, sep); |
| 252 | } |
| 253 | |
| 254 | template <typename Char> |
| 255 | typename Tokenizer<Char>::iterator& Tokenizer<Char>::iterator::operator++() { |
Adam Lesinski | cf95a58 | 2015-11-16 15:37:30 -0800 | [diff] [blame^] | 256 | const Char* start = mToken.end(); |
| 257 | const Char* end = mStr.end(); |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 258 | if (start == end) { |
Adam Lesinski | cf95a58 | 2015-11-16 15:37:30 -0800 | [diff] [blame^] | 259 | mEnd = true; |
| 260 | mToken.assign(mToken.end(), 0); |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 261 | return *this; |
| 262 | } |
| 263 | |
| 264 | start += 1; |
| 265 | const Char* current = start; |
| 266 | while (current != end) { |
Adam Lesinski | cf95a58 | 2015-11-16 15:37:30 -0800 | [diff] [blame^] | 267 | if (*current == mSeparator) { |
| 268 | mToken.assign(start, current - start); |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 269 | return *this; |
| 270 | } |
| 271 | ++current; |
| 272 | } |
Adam Lesinski | cf95a58 | 2015-11-16 15:37:30 -0800 | [diff] [blame^] | 273 | mToken.assign(start, end - start); |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 274 | return *this; |
| 275 | } |
| 276 | |
| 277 | template <typename Char> |
| 278 | inline BasicStringPiece<Char> Tokenizer<Char>::iterator::operator*() { |
Adam Lesinski | cf95a58 | 2015-11-16 15:37:30 -0800 | [diff] [blame^] | 279 | return mToken; |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 280 | } |
| 281 | |
| 282 | template <typename Char> |
| 283 | inline bool Tokenizer<Char>::iterator::operator==(const iterator& rhs) const { |
| 284 | // We check equality here a bit differently. |
| 285 | // We need to know that the addresses are the same. |
Adam Lesinski | cf95a58 | 2015-11-16 15:37:30 -0800 | [diff] [blame^] | 286 | return mToken.begin() == rhs.mToken.begin() && mToken.end() == rhs.mToken.end() && |
| 287 | mEnd == rhs.mEnd; |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 288 | } |
| 289 | |
| 290 | template <typename Char> |
| 291 | inline bool Tokenizer<Char>::iterator::operator!=(const iterator& rhs) const { |
| 292 | return !(*this == rhs); |
| 293 | } |
| 294 | |
| 295 | template <typename Char> |
| 296 | inline Tokenizer<Char>::iterator::iterator(BasicStringPiece<Char> s, Char sep, |
Adam Lesinski | cf95a58 | 2015-11-16 15:37:30 -0800 | [diff] [blame^] | 297 | BasicStringPiece<Char> tok, bool end) : |
| 298 | mStr(s), mSeparator(sep), mToken(tok), mEnd(end) { |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 299 | } |
| 300 | |
| 301 | template <typename Char> |
| 302 | inline typename Tokenizer<Char>::iterator Tokenizer<Char>::begin() { |
| 303 | return mBegin; |
| 304 | } |
| 305 | |
| 306 | template <typename Char> |
| 307 | inline typename Tokenizer<Char>::iterator Tokenizer<Char>::end() { |
| 308 | return mEnd; |
| 309 | } |
| 310 | |
| 311 | template <typename Char> |
| 312 | inline Tokenizer<Char>::Tokenizer(BasicStringPiece<Char> str, Char sep) : |
Adam Lesinski | cf95a58 | 2015-11-16 15:37:30 -0800 | [diff] [blame^] | 313 | mBegin(++iterator(str, sep, BasicStringPiece<Char>(str.begin() - 1, 0), false)), |
| 314 | mEnd(str, sep, BasicStringPiece<Char>(str.end(), 0), true) { |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 315 | } |
| 316 | |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 317 | inline uint16_t hostToDevice16(uint16_t value) { |
| 318 | return htods(value); |
| 319 | } |
| 320 | |
| 321 | inline uint32_t hostToDevice32(uint32_t value) { |
| 322 | return htodl(value); |
| 323 | } |
| 324 | |
| 325 | inline uint16_t deviceToHost16(uint16_t value) { |
| 326 | return dtohs(value); |
| 327 | } |
| 328 | |
| 329 | inline uint32_t deviceToHost32(uint32_t value) { |
| 330 | return dtohl(value); |
| 331 | } |
| 332 | |
Adam Lesinski | 24aad16 | 2015-04-24 19:19:30 -0700 | [diff] [blame] | 333 | /** |
| 334 | * Returns a package name if the namespace URI is of the form: |
| 335 | * http://schemas.android.com/apk/res/<package> |
| 336 | * |
| 337 | * Special case: if namespaceUri is http://schemas.android.com/apk/res-auto, |
| 338 | * returns an empty package name. |
| 339 | */ |
| 340 | Maybe<std::u16string> extractPackageFromNamespace(const std::u16string& namespaceUri); |
| 341 | |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 342 | /** |
| 343 | * Given a path like: res/xml-sw600dp/foo.xml |
| 344 | * |
| 345 | * Extracts "res/xml-sw600dp/" into outPrefix. |
| 346 | * Extracts "foo" into outEntry. |
| 347 | * Extracts ".xml" into outSuffix. |
| 348 | * |
| 349 | * Returns true if successful. |
| 350 | */ |
| 351 | bool extractResFilePathParts(const StringPiece16& path, StringPiece16* outPrefix, |
| 352 | StringPiece16* outEntry, StringPiece16* outSuffix); |
| 353 | |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 354 | } // namespace util |
| 355 | |
| 356 | /** |
| 357 | * Stream operator for functions. Calls the function with the stream as an argument. |
| 358 | * In the aapt namespace for lookup. |
| 359 | */ |
| 360 | inline ::std::ostream& operator<<(::std::ostream& out, |
| 361 | ::std::function<::std::ostream&(::std::ostream&)> f) { |
| 362 | return f(out); |
| 363 | } |
| 364 | |
| 365 | } // namespace aapt |
| 366 | |
| 367 | #endif // AAPT_UTIL_H |