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 | |
| 20 | #include "BigBuffer.h" |
| 21 | #include "StringPiece.h" |
| 22 | |
| 23 | #include <androidfw/ResourceTypes.h> |
| 24 | #include <functional> |
| 25 | #include <memory> |
| 26 | #include <ostream> |
| 27 | #include <string> |
| 28 | #include <vector> |
| 29 | |
| 30 | namespace aapt { |
| 31 | namespace util { |
| 32 | |
| 33 | std::vector<std::string> split(const StringPiece& str, char sep); |
| 34 | std::vector<std::string> splitAndLowercase(const StringPiece& str, char sep); |
| 35 | |
| 36 | /** |
| 37 | * Returns true if the string ends with suffix. |
| 38 | */ |
| 39 | bool stringEndsWith(const StringPiece& str, const StringPiece& suffix); |
| 40 | |
| 41 | /** |
| 42 | * Creates a new StringPiece16 that points to a substring |
| 43 | * of the original string without leading or trailing whitespace. |
| 44 | */ |
| 45 | StringPiece16 trimWhitespace(const StringPiece16& str); |
| 46 | |
| 47 | /** |
| 48 | * UTF-16 isspace(). It basically checks for lower range characters that are |
| 49 | * whitespace. |
| 50 | */ |
| 51 | inline bool isspace16(char16_t c) { |
| 52 | return c < 0x0080 && isspace(c); |
| 53 | } |
| 54 | |
| 55 | /** |
| 56 | * Returns an iterator to the first character that is not alpha-numeric and that |
| 57 | * is not in the allowedChars set. |
| 58 | */ |
| 59 | StringPiece16::const_iterator findNonAlphaNumericAndNotInSet(const StringPiece16& str, |
| 60 | const StringPiece16& allowedChars); |
| 61 | |
| 62 | /** |
| 63 | * Makes a std::unique_ptr<> with the template parameter inferred by the compiler. |
| 64 | * This will be present in C++14 and can be removed then. |
| 65 | */ |
| 66 | template <typename T, class... Args> |
| 67 | std::unique_ptr<T> make_unique(Args&&... args) { |
| 68 | return std::unique_ptr<T>(new T{std::forward<Args>(args)...}); |
| 69 | } |
| 70 | |
| 71 | /** |
| 72 | * Writes a set of items to the std::ostream, joining the times with the provided |
| 73 | * separator. |
| 74 | */ |
| 75 | template <typename Iterator> |
| 76 | ::std::function<::std::ostream&(::std::ostream&)> joiner(Iterator begin, Iterator end, |
| 77 | const char* sep) { |
| 78 | return [begin, end, sep](::std::ostream& out) -> ::std::ostream& { |
| 79 | for (auto iter = begin; iter != end; ++iter) { |
| 80 | if (iter != begin) { |
| 81 | out << sep; |
| 82 | } |
| 83 | out << *iter; |
| 84 | } |
| 85 | return out; |
| 86 | }; |
| 87 | } |
| 88 | |
| 89 | inline ::std::function<::std::ostream&(::std::ostream&)> formatSize(size_t size) { |
| 90 | return [size](::std::ostream& out) -> ::std::ostream& { |
Adam Lesinski | ca2fc35 | 2015-04-03 12:08:26 -0700 | [diff] [blame^] | 91 | constexpr size_t K = 1024u; |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 92 | constexpr size_t M = K * K; |
Greg Hackmann | 1fce4f9 | 2015-04-02 20:23:22 -0700 | [diff] [blame] | 93 | constexpr size_t G = M * K; |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 94 | if (size < K) { |
| 95 | out << size << "B"; |
| 96 | } else if (size < M) { |
| 97 | out << (double(size) / K) << " KiB"; |
| 98 | } else if (size < G) { |
| 99 | out << (double(size) / M) << " MiB"; |
| 100 | } else { |
| 101 | out << (double(size) / G) << " GiB"; |
| 102 | } |
| 103 | return out; |
| 104 | }; |
| 105 | } |
| 106 | |
| 107 | /** |
| 108 | * Helper method to extract a string from a StringPool. |
| 109 | */ |
| 110 | inline StringPiece16 getString(const android::ResStringPool& pool, size_t idx) { |
| 111 | size_t len; |
| 112 | const char16_t* str = pool.stringAt(idx, &len); |
| 113 | if (str != nullptr) { |
| 114 | return StringPiece16(str, len); |
| 115 | } |
| 116 | return StringPiece16(); |
| 117 | } |
| 118 | |
| 119 | class StringBuilder { |
| 120 | public: |
| 121 | StringBuilder& append(const StringPiece16& str); |
| 122 | const std::u16string& str() const; |
| 123 | const std::string& error() const; |
| 124 | operator bool() const; |
| 125 | |
| 126 | private: |
| 127 | std::u16string mStr; |
| 128 | bool mQuote = false; |
| 129 | bool mTrailingSpace = false; |
| 130 | std::string mError; |
| 131 | }; |
| 132 | |
| 133 | inline const std::u16string& StringBuilder::str() const { |
| 134 | return mStr; |
| 135 | } |
| 136 | |
| 137 | inline const std::string& StringBuilder::error() const { |
| 138 | return mError; |
| 139 | } |
| 140 | |
| 141 | inline StringBuilder::operator bool() const { |
| 142 | return mError.empty(); |
| 143 | } |
| 144 | |
| 145 | /** |
| 146 | * Converts a UTF8 string to a UTF16 string. |
| 147 | */ |
| 148 | std::u16string utf8ToUtf16(const StringPiece& utf8); |
| 149 | std::string utf16ToUtf8(const StringPiece16& utf8); |
| 150 | |
| 151 | /** |
| 152 | * Writes the entire BigBuffer to the output stream. |
| 153 | */ |
| 154 | bool writeAll(std::ostream& out, const BigBuffer& buffer); |
| 155 | |
| 156 | /* |
| 157 | * Copies the entire BigBuffer into a single buffer. |
| 158 | */ |
| 159 | std::unique_ptr<uint8_t[]> copy(const BigBuffer& buffer); |
| 160 | |
| 161 | /** |
| 162 | * A Tokenizer implemented as an iterable collection. It does not allocate |
| 163 | * any memory on the heap nor use standard containers. |
| 164 | */ |
| 165 | template <typename Char> |
| 166 | class Tokenizer { |
| 167 | public: |
| 168 | class iterator { |
| 169 | public: |
| 170 | iterator(const iterator&) = default; |
| 171 | iterator& operator=(const iterator&) = default; |
| 172 | |
| 173 | iterator& operator++(); |
| 174 | BasicStringPiece<Char> operator*(); |
| 175 | bool operator==(const iterator& rhs) const; |
| 176 | bool operator!=(const iterator& rhs) const; |
| 177 | |
| 178 | private: |
| 179 | friend class Tokenizer<Char>; |
| 180 | |
| 181 | iterator(BasicStringPiece<Char> s, Char sep, BasicStringPiece<Char> tok); |
| 182 | |
| 183 | BasicStringPiece<Char> str; |
| 184 | Char separator; |
| 185 | BasicStringPiece<Char> token; |
| 186 | }; |
| 187 | |
| 188 | Tokenizer(BasicStringPiece<Char> str, Char sep); |
| 189 | iterator begin(); |
| 190 | iterator end(); |
| 191 | |
| 192 | private: |
| 193 | const iterator mBegin; |
| 194 | const iterator mEnd; |
| 195 | }; |
| 196 | |
| 197 | template <typename Char> |
| 198 | inline Tokenizer<Char> tokenize(BasicStringPiece<Char> str, Char sep) { |
| 199 | return Tokenizer<Char>(str, sep); |
| 200 | } |
| 201 | |
| 202 | template <typename Char> |
| 203 | typename Tokenizer<Char>::iterator& Tokenizer<Char>::iterator::operator++() { |
| 204 | const Char* start = token.end(); |
| 205 | const Char* end = str.end(); |
| 206 | if (start == end) { |
| 207 | token.assign(token.end(), 0); |
| 208 | return *this; |
| 209 | } |
| 210 | |
| 211 | start += 1; |
| 212 | const Char* current = start; |
| 213 | while (current != end) { |
| 214 | if (*current == separator) { |
| 215 | token.assign(start, current - start); |
| 216 | return *this; |
| 217 | } |
| 218 | ++current; |
| 219 | } |
| 220 | token.assign(start, end - start); |
| 221 | return *this; |
| 222 | } |
| 223 | |
| 224 | template <typename Char> |
| 225 | inline BasicStringPiece<Char> Tokenizer<Char>::iterator::operator*() { |
| 226 | return token; |
| 227 | } |
| 228 | |
| 229 | template <typename Char> |
| 230 | inline bool Tokenizer<Char>::iterator::operator==(const iterator& rhs) const { |
| 231 | // We check equality here a bit differently. |
| 232 | // We need to know that the addresses are the same. |
| 233 | return token.begin() == rhs.token.begin() && token.end() == rhs.token.end(); |
| 234 | } |
| 235 | |
| 236 | template <typename Char> |
| 237 | inline bool Tokenizer<Char>::iterator::operator!=(const iterator& rhs) const { |
| 238 | return !(*this == rhs); |
| 239 | } |
| 240 | |
| 241 | template <typename Char> |
| 242 | inline Tokenizer<Char>::iterator::iterator(BasicStringPiece<Char> s, Char sep, |
| 243 | BasicStringPiece<Char> tok) : |
| 244 | str(s), separator(sep), token(tok) { |
| 245 | } |
| 246 | |
| 247 | template <typename Char> |
| 248 | inline typename Tokenizer<Char>::iterator Tokenizer<Char>::begin() { |
| 249 | return mBegin; |
| 250 | } |
| 251 | |
| 252 | template <typename Char> |
| 253 | inline typename Tokenizer<Char>::iterator Tokenizer<Char>::end() { |
| 254 | return mEnd; |
| 255 | } |
| 256 | |
| 257 | template <typename Char> |
| 258 | inline Tokenizer<Char>::Tokenizer(BasicStringPiece<Char> str, Char sep) : |
| 259 | mBegin(++iterator(str, sep, BasicStringPiece<Char>(str.begin() - 1, 0))), |
| 260 | mEnd(str, sep, BasicStringPiece<Char>(str.end(), 0)) { |
| 261 | } |
| 262 | |
| 263 | } // namespace util |
| 264 | |
| 265 | /** |
| 266 | * Stream operator for functions. Calls the function with the stream as an argument. |
| 267 | * In the aapt namespace for lookup. |
| 268 | */ |
| 269 | inline ::std::ostream& operator<<(::std::ostream& out, |
| 270 | ::std::function<::std::ostream&(::std::ostream&)> f) { |
| 271 | return f(out); |
| 272 | } |
| 273 | |
| 274 | } // namespace aapt |
| 275 | |
| 276 | #endif // AAPT_UTIL_H |