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