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 | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 20 | #include <functional> |
| 21 | #include <memory> |
| 22 | #include <ostream> |
| 23 | #include <string> |
| 24 | #include <vector> |
| 25 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 26 | #include "androidfw/ResourceTypes.h" |
Adam Lesinski | d5083f6 | 2017-01-16 15:07:21 -0800 | [diff] [blame] | 27 | #include "androidfw/StringPiece.h" |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 28 | #include "utils/ByteOrder.h" |
| 29 | |
| 30 | #include "util/BigBuffer.h" |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 31 | |
| 32 | #ifdef _WIN32 |
| 33 | // TODO(adamlesinski): remove once http://b/32447322 is resolved. |
| 34 | // utils/ByteOrder.h includes winsock2.h on WIN32, |
| 35 | // which will pull in the ERROR definition. This conflicts |
| 36 | // with android-base/logging.h, which takes care of undefining |
| 37 | // ERROR, but it gets included too early (before winsock2.h). |
| 38 | #ifdef ERROR |
| 39 | #undef ERROR |
| 40 | #endif |
| 41 | #endif |
| 42 | |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 43 | namespace aapt { |
| 44 | namespace util { |
| 45 | |
Adam Lesinski | c744ae8 | 2017-05-17 19:28:38 -0700 | [diff] [blame] | 46 | template <typename T> |
| 47 | struct Range { |
| 48 | T start; |
| 49 | T end; |
| 50 | }; |
| 51 | |
Adam Lesinski | d5083f6 | 2017-01-16 15:07:21 -0800 | [diff] [blame] | 52 | std::vector<std::string> Split(const android::StringPiece& str, char sep); |
| 53 | std::vector<std::string> SplitAndLowercase(const android::StringPiece& str, char sep); |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 54 | |
Adam Lesinski | 96ea08f | 2017-11-06 10:44:46 -0800 | [diff] [blame] | 55 | // Returns true if the string starts with prefix. |
Adam Lesinski | d5083f6 | 2017-01-16 15:07:21 -0800 | [diff] [blame] | 56 | bool StartsWith(const android::StringPiece& str, const android::StringPiece& prefix); |
Adam Lesinski | 4d3a987 | 2015-04-09 19:53:22 -0700 | [diff] [blame] | 57 | |
Adam Lesinski | 96ea08f | 2017-11-06 10:44:46 -0800 | [diff] [blame] | 58 | // Returns true if the string ends with suffix. |
Adam Lesinski | d5083f6 | 2017-01-16 15:07:21 -0800 | [diff] [blame] | 59 | bool EndsWith(const android::StringPiece& str, const android::StringPiece& suffix); |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 60 | |
Adam Lesinski | 2eed52e | 2018-02-21 15:55:58 -0800 | [diff] [blame] | 61 | // Creates a new StringPiece that points to a substring of the original string without leading |
| 62 | // whitespace. |
| 63 | android::StringPiece TrimLeadingWhitespace(const android::StringPiece& str); |
| 64 | |
| 65 | // Creates a new StringPiece that points to a substring of the original string without trailing |
| 66 | // whitespace. |
| 67 | android::StringPiece TrimTrailingWhitespace(const android::StringPiece& str); |
| 68 | |
| 69 | // Creates a new StringPiece that points to a substring of the original string without leading or |
Adam Lesinski | 96ea08f | 2017-11-06 10:44:46 -0800 | [diff] [blame] | 70 | // trailing whitespace. |
Adam Lesinski | d5083f6 | 2017-01-16 15:07:21 -0800 | [diff] [blame] | 71 | android::StringPiece TrimWhitespace(const android::StringPiece& str); |
Adam Lesinski | 3b4cd94 | 2015-10-30 16:31:42 -0700 | [diff] [blame] | 72 | |
Adam Lesinski | 96ea08f | 2017-11-06 10:44:46 -0800 | [diff] [blame] | 73 | // Tests that the string is a valid Java class name. |
Adam Lesinski | d5083f6 | 2017-01-16 15:07:21 -0800 | [diff] [blame] | 74 | bool IsJavaClassName(const android::StringPiece& str); |
Adam Lesinski | a1ad4a8 | 2015-06-08 11:41:09 -0700 | [diff] [blame] | 75 | |
Adam Lesinski | 96ea08f | 2017-11-06 10:44:46 -0800 | [diff] [blame] | 76 | // Tests that the string is a valid Java package name. |
Adam Lesinski | d5083f6 | 2017-01-16 15:07:21 -0800 | [diff] [blame] | 77 | bool IsJavaPackageName(const android::StringPiece& str); |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 78 | |
Adam Lesinski | 96ea08f | 2017-11-06 10:44:46 -0800 | [diff] [blame] | 79 | // Tests that the string is a valid Android package name. More strict than a Java package name. |
| 80 | // - First character of each component (separated by '.') must be an ASCII letter. |
| 81 | // - Subsequent characters of a component can be ASCII alphanumeric or an underscore. |
| 82 | // - Package must contain at least two components, unless it is 'android'. |
Rhed Jao | 2c43442 | 2020-11-12 10:48:03 +0800 | [diff] [blame] | 83 | // - The maximum package name length is 223. |
Adam Lesinski | 96ea08f | 2017-11-06 10:44:46 -0800 | [diff] [blame] | 84 | bool IsAndroidPackageName(const android::StringPiece& str); |
| 85 | |
| 86 | // Tests that the string is a valid Android split name. |
| 87 | // - First character of each component (separated by '.') must be an ASCII letter. |
| 88 | // - Subsequent characters of a component can be ASCII alphanumeric or an underscore. |
| 89 | bool IsAndroidSplitName(const android::StringPiece& str); |
| 90 | |
Rhed Jao | 2c43442 | 2020-11-12 10:48:03 +0800 | [diff] [blame] | 91 | // Tests that the string is a valid Android shared user id. |
| 92 | // - First character of each component (separated by '.') must be an ASCII letter. |
| 93 | // - Subsequent characters of a component can be ASCII alphanumeric or an underscore. |
| 94 | // - Must contain at least two components, unless package name is 'android'. |
| 95 | // - The maximum shared user id length is 223. |
| 96 | // - Treat empty string as valid, it's the case of no shared user id. |
| 97 | bool IsAndroidSharedUserId(const android::StringPiece& package_name, |
| 98 | const android::StringPiece& shared_user_id); |
| 99 | |
Adam Lesinski | 96ea08f | 2017-11-06 10:44:46 -0800 | [diff] [blame] | 100 | // Converts the class name to a fully qualified class name from the given |
| 101 | // `package`. Ex: |
| 102 | // |
| 103 | // asdf --> package.asdf |
| 104 | // .asdf --> package.asdf |
| 105 | // .a.b --> package.a.b |
| 106 | // asdf.adsf --> asdf.adsf |
Ryan Mitchell | 4382e44 | 2021-07-14 12:53:01 -0700 | [diff] [blame] | 107 | std::optional<std::string> GetFullyQualifiedClassName(const android::StringPiece& package, |
| 108 | const android::StringPiece& class_name); |
Adam Lesinski | a1ad4a8 | 2015-06-08 11:41:09 -0700 | [diff] [blame] | 109 | |
Ryan Mitchell | 34039b2 | 2019-03-18 08:57:47 -0700 | [diff] [blame] | 110 | // Retrieves the formatted name of aapt2. |
| 111 | const char* GetToolName(); |
| 112 | |
| 113 | // Retrieves the build fingerprint of aapt2. |
| 114 | std::string GetToolFingerprint(); |
| 115 | |
Adam Lesinski | 060b53d | 2017-07-28 17:10:35 -0700 | [diff] [blame] | 116 | template <typename T> |
| 117 | typename std::enable_if<std::is_arithmetic<T>::value, int>::type compare(const T& a, const T& b) { |
| 118 | if (a < b) { |
| 119 | return -1; |
| 120 | } else if (a > b) { |
| 121 | return 1; |
| 122 | } |
| 123 | return 0; |
| 124 | } |
| 125 | |
Adam Lesinski | 96ea08f | 2017-11-06 10:44:46 -0800 | [diff] [blame] | 126 | // Makes a std::unique_ptr<> with the template parameter inferred by the compiler. |
| 127 | // This will be present in C++14 and can be removed then. |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 128 | template <typename T, class... Args> |
| 129 | std::unique_ptr<T> make_unique(Args&&... args) { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 130 | return std::unique_ptr<T>(new T{std::forward<Args>(args)...}); |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 131 | } |
| 132 | |
Adam Lesinski | 96ea08f | 2017-11-06 10:44:46 -0800 | [diff] [blame] | 133 | // Writes a set of items to the std::ostream, joining the times with the provided separator. |
Adam Lesinski | 36c73a5 | 2016-08-11 13:39:24 -0700 | [diff] [blame] | 134 | template <typename Container> |
Adam Lesinski | 96ea08f | 2017-11-06 10:44:46 -0800 | [diff] [blame] | 135 | ::std::function<::std::ostream&(::std::ostream&)> Joiner(const Container& container, |
| 136 | const char* sep) { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 137 | using std::begin; |
| 138 | using std::end; |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 139 | const auto begin_iter = begin(container); |
| 140 | const auto end_iter = end(container); |
| 141 | return [begin_iter, end_iter, sep](::std::ostream& out) -> ::std::ostream& { |
| 142 | for (auto iter = begin_iter; iter != end_iter; ++iter) { |
| 143 | if (iter != begin_iter) { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 144 | out << sep; |
| 145 | } |
| 146 | out << *iter; |
| 147 | } |
| 148 | return out; |
| 149 | }; |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 150 | } |
| 151 | |
Adam Lesinski | 96ea08f | 2017-11-06 10:44:46 -0800 | [diff] [blame] | 152 | // Helper method to extract a UTF-16 string from a StringPool. If the string is stored as UTF-8, |
| 153 | // the conversion to UTF-16 happens within ResStringPool. |
Adam Lesinski | d5083f6 | 2017-01-16 15:07:21 -0800 | [diff] [blame] | 154 | android::StringPiece16 GetString16(const android::ResStringPool& pool, size_t idx); |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 155 | |
Adam Lesinski | 96ea08f | 2017-11-06 10:44:46 -0800 | [diff] [blame] | 156 | // Helper method to extract a UTF-8 string from a StringPool. If the string is stored as UTF-16, |
| 157 | // the conversion from UTF-16 to UTF-8 does not happen in ResStringPool and is done by this method, |
| 158 | // which maintains no state or cache. This means we must return an std::string copy. |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 159 | std::string GetString(const android::ResStringPool& pool, size_t idx); |
Adam Lesinski | 28cacf0 | 2015-11-23 14:22:47 -0800 | [diff] [blame] | 160 | |
Adam Lesinski | 96ea08f | 2017-11-06 10:44:46 -0800 | [diff] [blame] | 161 | // Checks that the Java string format contains no non-positional arguments (arguments without |
| 162 | // explicitly specifying an index) when there are more than one argument. This is an error |
| 163 | // because translations may rearrange the order of the arguments in the string, which will |
| 164 | // break the string interpolation. |
Adam Lesinski | d5083f6 | 2017-01-16 15:07:21 -0800 | [diff] [blame] | 165 | bool VerifyJavaStringFormat(const android::StringPiece& str); |
Adam Lesinski | b23f1e0 | 2015-11-03 12:24:17 -0800 | [diff] [blame] | 166 | |
Adam Lesinski | 2eed52e | 2018-02-21 15:55:58 -0800 | [diff] [blame] | 167 | bool AppendStyledString(const android::StringPiece& input, bool preserve_spaces, |
| 168 | std::string* out_str, std::string* out_error); |
| 169 | |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 170 | class StringBuilder { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 171 | public: |
Adam Lesinski | 2eed52e | 2018-02-21 15:55:58 -0800 | [diff] [blame] | 172 | StringBuilder() = default; |
Adam Lesinski | fba0cf2 | 2017-06-29 17:53:36 -0700 | [diff] [blame] | 173 | |
Adam Lesinski | d5083f6 | 2017-01-16 15:07:21 -0800 | [diff] [blame] | 174 | StringBuilder& Append(const android::StringPiece& str); |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 175 | const std::string& ToString() const; |
| 176 | const std::string& Error() const; |
Adam Lesinski | ac6edc5 | 2017-03-02 19:31:28 -0800 | [diff] [blame] | 177 | bool IsEmpty() const; |
Adam Lesinski | 8c3f31f | 2016-09-07 13:45:13 -0700 | [diff] [blame] | 178 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 179 | // When building StyledStrings, we need UTF-16 indices into the string, |
| 180 | // which is what the Java layer expects when dealing with java |
| 181 | // String.charAt(). |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 182 | size_t Utf16Len() const; |
Adam Lesinski | 8c3f31f | 2016-09-07 13:45:13 -0700 | [diff] [blame] | 183 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 184 | explicit operator bool() const; |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 185 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 186 | private: |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 187 | std::string str_; |
| 188 | size_t utf16_len_ = 0; |
| 189 | bool quote_ = false; |
| 190 | bool trailing_space_ = false; |
| 191 | bool last_char_was_escape_ = false; |
| 192 | std::string error_; |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 193 | }; |
| 194 | |
Adam Lesinski | 96ea08f | 2017-11-06 10:44:46 -0800 | [diff] [blame] | 195 | inline const std::string& StringBuilder::ToString() const { |
| 196 | return str_; |
| 197 | } |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 198 | |
Adam Lesinski | 96ea08f | 2017-11-06 10:44:46 -0800 | [diff] [blame] | 199 | inline const std::string& StringBuilder::Error() const { |
| 200 | return error_; |
| 201 | } |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 202 | |
Adam Lesinski | 96ea08f | 2017-11-06 10:44:46 -0800 | [diff] [blame] | 203 | inline bool StringBuilder::IsEmpty() const { |
| 204 | return str_.empty(); |
| 205 | } |
Adam Lesinski | ac6edc5 | 2017-03-02 19:31:28 -0800 | [diff] [blame] | 206 | |
Adam Lesinski | 96ea08f | 2017-11-06 10:44:46 -0800 | [diff] [blame] | 207 | inline size_t StringBuilder::Utf16Len() const { |
| 208 | return utf16_len_; |
| 209 | } |
Adam Lesinski | 8c3f31f | 2016-09-07 13:45:13 -0700 | [diff] [blame] | 210 | |
Adam Lesinski | 96ea08f | 2017-11-06 10:44:46 -0800 | [diff] [blame] | 211 | inline StringBuilder::operator bool() const { |
| 212 | return error_.empty(); |
| 213 | } |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 214 | |
Ryan Mitchell | d86ea58 | 2018-06-27 11:57:18 -0700 | [diff] [blame] | 215 | // Converts a UTF8 string into Modified UTF8 |
| 216 | std::string Utf8ToModifiedUtf8(const std::string& utf8); |
Ryan Mitchell | 4353d61b | 2018-09-10 17:09:12 -0700 | [diff] [blame] | 217 | std::string ModifiedUtf8ToUtf8(const std::string& modified_utf8); |
Ryan Mitchell | d86ea58 | 2018-06-27 11:57:18 -0700 | [diff] [blame] | 218 | |
Adam Lesinski | 96ea08f | 2017-11-06 10:44:46 -0800 | [diff] [blame] | 219 | // Converts a UTF8 string to a UTF16 string. |
Adam Lesinski | d5083f6 | 2017-01-16 15:07:21 -0800 | [diff] [blame] | 220 | std::u16string Utf8ToUtf16(const android::StringPiece& utf8); |
| 221 | std::string Utf16ToUtf8(const android::StringPiece16& utf16); |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 222 | |
Adam Lesinski | 96ea08f | 2017-11-06 10:44:46 -0800 | [diff] [blame] | 223 | // Writes the entire BigBuffer to the output stream. |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 224 | bool WriteAll(std::ostream& out, const BigBuffer& buffer); |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 225 | |
Adam Lesinski | 96ea08f | 2017-11-06 10:44:46 -0800 | [diff] [blame] | 226 | // Copies the entire BigBuffer into a single buffer. |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 227 | std::unique_ptr<uint8_t[]> Copy(const BigBuffer& buffer); |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 228 | |
Adam Lesinski | 96ea08f | 2017-11-06 10:44:46 -0800 | [diff] [blame] | 229 | // A Tokenizer implemented as an iterable collection. It does not allocate any memory on the heap |
| 230 | // nor use standard containers. |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 231 | class Tokenizer { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 232 | public: |
| 233 | class iterator { |
| 234 | public: |
Adam Lesinski | 448a15c | 2017-07-25 10:59:26 -0700 | [diff] [blame] | 235 | using reference = android::StringPiece&; |
| 236 | using value_type = android::StringPiece; |
| 237 | using difference_type = size_t; |
| 238 | using pointer = android::StringPiece*; |
| 239 | using iterator_category = std::forward_iterator_tag; |
| 240 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 241 | iterator(const iterator&) = default; |
| 242 | iterator& operator=(const iterator&) = default; |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 243 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 244 | iterator& operator++(); |
Adam Lesinski | d0f116b | 2016-07-08 15:00:32 -0700 | [diff] [blame] | 245 | |
Adam Lesinski | d5083f6 | 2017-01-16 15:07:21 -0800 | [diff] [blame] | 246 | android::StringPiece operator*() { return token_; } |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 247 | bool operator==(const iterator& rhs) const; |
| 248 | bool operator!=(const iterator& rhs) const; |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 249 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 250 | private: |
| 251 | friend class Tokenizer; |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 252 | |
Chih-Hung Hsieh | 4dc5812 | 2017-08-03 16:28:10 -0700 | [diff] [blame] | 253 | iterator(const android::StringPiece& s, char sep, const android::StringPiece& tok, bool end); |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 254 | |
Adam Lesinski | d5083f6 | 2017-01-16 15:07:21 -0800 | [diff] [blame] | 255 | android::StringPiece str_; |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 256 | char separator_; |
Adam Lesinski | d5083f6 | 2017-01-16 15:07:21 -0800 | [diff] [blame] | 257 | android::StringPiece token_; |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 258 | bool end_; |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 259 | }; |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 260 | |
Chih-Hung Hsieh | 4dc5812 | 2017-08-03 16:28:10 -0700 | [diff] [blame] | 261 | Tokenizer(const android::StringPiece& str, char sep); |
Adam Lesinski | d0f116b | 2016-07-08 15:00:32 -0700 | [diff] [blame] | 262 | |
Adam Lesinski | 448a15c | 2017-07-25 10:59:26 -0700 | [diff] [blame] | 263 | iterator begin() const { |
| 264 | return begin_; |
| 265 | } |
Adam Lesinski | d0f116b | 2016-07-08 15:00:32 -0700 | [diff] [blame] | 266 | |
Adam Lesinski | 448a15c | 2017-07-25 10:59:26 -0700 | [diff] [blame] | 267 | iterator end() const { |
| 268 | return end_; |
| 269 | } |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 270 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 271 | private: |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 272 | const iterator begin_; |
| 273 | const iterator end_; |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 274 | }; |
| 275 | |
Adam Lesinski | 96ea08f | 2017-11-06 10:44:46 -0800 | [diff] [blame] | 276 | inline Tokenizer Tokenize(const android::StringPiece& str, char sep) { |
| 277 | return Tokenizer(str, sep); |
| 278 | } |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 279 | |
Adam Lesinski | 96ea08f | 2017-11-06 10:44:46 -0800 | [diff] [blame] | 280 | inline uint16_t HostToDevice16(uint16_t value) { |
| 281 | return htods(value); |
| 282 | } |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 283 | |
Adam Lesinski | 96ea08f | 2017-11-06 10:44:46 -0800 | [diff] [blame] | 284 | inline uint32_t HostToDevice32(uint32_t value) { |
| 285 | return htodl(value); |
| 286 | } |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 287 | |
Adam Lesinski | 96ea08f | 2017-11-06 10:44:46 -0800 | [diff] [blame] | 288 | inline uint16_t DeviceToHost16(uint16_t value) { |
| 289 | return dtohs(value); |
| 290 | } |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 291 | |
Adam Lesinski | 96ea08f | 2017-11-06 10:44:46 -0800 | [diff] [blame] | 292 | inline uint32_t DeviceToHost32(uint32_t value) { |
| 293 | return dtohl(value); |
| 294 | } |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 295 | |
Adam Lesinski | 96ea08f | 2017-11-06 10:44:46 -0800 | [diff] [blame] | 296 | // Given a path like: res/xml-sw600dp/foo.xml |
| 297 | // |
| 298 | // Extracts "res/xml-sw600dp/" into outPrefix. |
| 299 | // Extracts "foo" into outEntry. |
| 300 | // Extracts ".xml" into outSuffix. |
| 301 | // |
| 302 | // Returns true if successful. |
Adam Lesinski | d5083f6 | 2017-01-16 15:07:21 -0800 | [diff] [blame] | 303 | bool ExtractResFilePathParts(const android::StringPiece& path, android::StringPiece* out_prefix, |
| 304 | android::StringPiece* out_entry, android::StringPiece* out_suffix); |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 305 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 306 | } // namespace util |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 307 | |
Adam Lesinski | 96ea08f | 2017-11-06 10:44:46 -0800 | [diff] [blame] | 308 | // Stream operator for functions. Calls the function with the stream as an argument. |
| 309 | // In the aapt namespace for lookup. |
| 310 | inline ::std::ostream& operator<<(::std::ostream& out, |
| 311 | const ::std::function<::std::ostream&(::std::ostream&)>& f) { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 312 | return f(out); |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 313 | } |
| 314 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 315 | } // namespace aapt |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 316 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 317 | #endif // AAPT_UTIL_H |