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