blob: 05e9cc5d28ca266d10bfeed80a476e8ea2d68f78 [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
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080020#include <functional>
21#include <memory>
22#include <ostream>
23#include <string>
24#include <vector>
25
Adam Lesinskice5e56e2016-10-21 17:56:45 -070026#include "androidfw/ResourceTypes.h"
27#include "utils/ByteOrder.h"
28
29#include "util/BigBuffer.h"
30#include "util/Maybe.h"
31#include "util/StringPiece.h"
32
33#ifdef _WIN32
34// TODO(adamlesinski): remove once http://b/32447322 is resolved.
35// utils/ByteOrder.h includes winsock2.h on WIN32,
36// which will pull in the ERROR definition. This conflicts
37// with android-base/logging.h, which takes care of undefining
38// ERROR, but it gets included too early (before winsock2.h).
39#ifdef ERROR
40#undef ERROR
41#endif
42#endif
43
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080044namespace aapt {
45namespace util {
46
Adam Lesinskice5e56e2016-10-21 17:56:45 -070047std::vector<std::string> Split(const StringPiece& str, char sep);
48std::vector<std::string> SplitAndLowercase(const StringPiece& str, char sep);
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080049
50/**
Adam Lesinski4d3a9872015-04-09 19:53:22 -070051 * Returns true if the string starts with prefix.
52 */
Adam Lesinskice5e56e2016-10-21 17:56:45 -070053bool StartsWith(const StringPiece& str, const StringPiece& prefix);
Adam Lesinski4d3a9872015-04-09 19:53:22 -070054
55/**
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080056 * Returns true if the string ends with suffix.
57 */
Adam Lesinskice5e56e2016-10-21 17:56:45 -070058bool EndsWith(const StringPiece& str, const StringPiece& suffix);
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080059
60/**
61 * Creates a new StringPiece16 that points to a substring
62 * of the original string without leading or trailing whitespace.
63 */
Adam Lesinskice5e56e2016-10-21 17:56:45 -070064StringPiece TrimWhitespace(const StringPiece& str);
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080065
Adam Lesinskice5e56e2016-10-21 17:56:45 -070066StringPiece TrimWhitespace(const StringPiece& str);
Adam Lesinski3b4cd942015-10-30 16:31:42 -070067
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080068/**
69 * UTF-16 isspace(). It basically checks for lower range characters that are
70 * whitespace.
71 */
Adam Lesinskicacb28f2016-10-19 12:18:14 -070072inline bool isspace16(char16_t c) { return c < 0x0080 && isspace(c); }
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080073
74/**
75 * Returns an iterator to the first character that is not alpha-numeric and that
76 * is not in the allowedChars set.
77 */
Adam Lesinskice5e56e2016-10-21 17:56:45 -070078StringPiece::const_iterator FindNonAlphaNumericAndNotInSet(
79 const StringPiece& str, const StringPiece& allowed_chars);
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080080
81/**
Adam Lesinskia1ad4a82015-06-08 11:41:09 -070082 * Tests that the string is a valid Java class name.
83 */
Adam Lesinskice5e56e2016-10-21 17:56:45 -070084bool IsJavaClassName(const StringPiece& str);
Adam Lesinskia1ad4a82015-06-08 11:41:09 -070085
86/**
Adam Lesinski1ab598f2015-08-14 14:26:04 -070087 * Tests that the string is a valid Java package name.
88 */
Adam Lesinskice5e56e2016-10-21 17:56:45 -070089bool IsJavaPackageName(const StringPiece& str);
Adam Lesinski1ab598f2015-08-14 14:26:04 -070090
91/**
Adam Lesinskicacb28f2016-10-19 12:18:14 -070092 * Converts the class name to a fully qualified class name from the given
93 * `package`. Ex:
Adam Lesinskia1ad4a82015-06-08 11:41:09 -070094 *
95 * asdf --> package.asdf
96 * .asdf --> package.asdf
97 * .a.b --> package.a.b
98 * asdf.adsf --> asdf.adsf
99 */
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700100Maybe<std::string> GetFullyQualifiedClassName(const StringPiece& package,
101 const StringPiece& class_name);
Adam Lesinskia1ad4a82015-06-08 11:41:09 -0700102
103/**
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700104 * Makes a std::unique_ptr<> with the template parameter inferred by the
105 * compiler.
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800106 * This will be present in C++14 and can be removed then.
107 */
108template <typename T, class... Args>
109std::unique_ptr<T> make_unique(Args&&... args) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700110 return std::unique_ptr<T>(new T{std::forward<Args>(args)...});
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800111}
112
113/**
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700114 * Writes a set of items to the std::ostream, joining the times with the
115 * provided
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800116 * separator.
117 */
Adam Lesinski36c73a52016-08-11 13:39:24 -0700118template <typename Container>
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700119::std::function<::std::ostream&(::std::ostream&)> Joiner(
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700120 const Container& container, const char* sep) {
121 using std::begin;
122 using std::end;
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700123 const auto begin_iter = begin(container);
124 const auto end_iter = end(container);
125 return [begin_iter, end_iter, sep](::std::ostream& out) -> ::std::ostream& {
126 for (auto iter = begin_iter; iter != end_iter; ++iter) {
127 if (iter != begin_iter) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700128 out << sep;
129 }
130 out << *iter;
131 }
132 return out;
133 };
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800134}
135
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800136/**
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700137 * Helper method to extract a UTF-16 string from a StringPool. If the string is
138 * stored as UTF-8,
Adam Lesinskid0f116b2016-07-08 15:00:32 -0700139 * the conversion to UTF-16 happens within ResStringPool.
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800140 */
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700141StringPiece16 GetString16(const android::ResStringPool& pool, size_t idx);
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800142
Adam Lesinskid0f116b2016-07-08 15:00:32 -0700143/**
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700144 * Helper method to extract a UTF-8 string from a StringPool. If the string is
145 * stored as UTF-16,
146 * the conversion from UTF-16 to UTF-8 does not happen in ResStringPool and is
147 * done by this method,
148 * which maintains no state or cache. This means we must return an std::string
149 * copy.
Adam Lesinskid0f116b2016-07-08 15:00:32 -0700150 */
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700151std::string GetString(const android::ResStringPool& pool, size_t idx);
Adam Lesinski28cacf02015-11-23 14:22:47 -0800152
Adam Lesinskib23f1e02015-11-03 12:24:17 -0800153/**
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700154 * Checks that the Java string format contains no non-positional arguments
155 * (arguments without
156 * explicitly specifying an index) when there are more than one argument. This
157 * is an error
158 * because translations may rearrange the order of the arguments in the string,
159 * which will
Adam Lesinskib23f1e02015-11-03 12:24:17 -0800160 * break the string interpolation.
161 */
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700162bool VerifyJavaStringFormat(const StringPiece& str);
Adam Lesinskib23f1e02015-11-03 12:24:17 -0800163
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800164class StringBuilder {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700165 public:
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700166 StringBuilder& Append(const StringPiece& str);
167 const std::string& ToString() const;
168 const std::string& Error() const;
Adam Lesinski8c3f31f2016-09-07 13:45:13 -0700169
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700170 // When building StyledStrings, we need UTF-16 indices into the string,
171 // which is what the Java layer expects when dealing with java
172 // String.charAt().
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700173 size_t Utf16Len() const;
Adam Lesinski8c3f31f2016-09-07 13:45:13 -0700174
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700175 explicit operator bool() const;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800176
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700177 private:
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700178 std::string str_;
179 size_t utf16_len_ = 0;
180 bool quote_ = false;
181 bool trailing_space_ = false;
182 bool last_char_was_escape_ = false;
183 std::string error_;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800184};
185
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700186inline const std::string& StringBuilder::ToString() const { return str_; }
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800187
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700188inline const std::string& StringBuilder::Error() const { return error_; }
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800189
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700190inline size_t StringBuilder::Utf16Len() const { return utf16_len_; }
Adam Lesinski8c3f31f2016-09-07 13:45:13 -0700191
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700192inline StringBuilder::operator bool() const { return error_.empty(); }
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800193
194/**
195 * Converts a UTF8 string to a UTF16 string.
196 */
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700197std::u16string Utf8ToUtf16(const StringPiece& utf8);
198std::string Utf16ToUtf8(const StringPiece16& utf16);
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800199
200/**
201 * Writes the entire BigBuffer to the output stream.
202 */
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700203bool WriteAll(std::ostream& out, const BigBuffer& buffer);
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800204
205/*
206 * Copies the entire BigBuffer into a single buffer.
207 */
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700208std::unique_ptr<uint8_t[]> Copy(const BigBuffer& buffer);
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800209
210/**
211 * A Tokenizer implemented as an iterable collection. It does not allocate
212 * any memory on the heap nor use standard containers.
213 */
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800214class Tokenizer {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700215 public:
216 class iterator {
217 public:
218 iterator(const iterator&) = default;
219 iterator& operator=(const iterator&) = default;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800220
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700221 iterator& operator++();
Adam Lesinskid0f116b2016-07-08 15:00:32 -0700222
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700223 StringPiece operator*() { return token_; }
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700224 bool operator==(const iterator& rhs) const;
225 bool operator!=(const iterator& rhs) const;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800226
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700227 private:
228 friend class Tokenizer;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800229
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700230 iterator(StringPiece s, char sep, StringPiece tok, bool end);
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800231
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700232 StringPiece str_;
233 char separator_;
234 StringPiece token_;
235 bool end_;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700236 };
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800237
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700238 Tokenizer(StringPiece str, char sep);
Adam Lesinskid0f116b2016-07-08 15:00:32 -0700239
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700240 iterator begin() { return begin_; }
Adam Lesinskid0f116b2016-07-08 15:00:32 -0700241
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700242 iterator end() { return end_; }
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800243
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700244 private:
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700245 const iterator begin_;
246 const iterator end_;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800247};
248
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700249inline Tokenizer Tokenize(const StringPiece& str, char sep) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700250 return Tokenizer(str, sep);
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800251}
252
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700253inline uint16_t HostToDevice16(uint16_t value) { return htods(value); }
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700254
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700255inline uint32_t HostToDevice32(uint32_t value) { return htodl(value); }
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700256
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700257inline uint16_t DeviceToHost16(uint16_t value) { return dtohs(value); }
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700258
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700259inline uint32_t DeviceToHost32(uint32_t value) { return dtohl(value); }
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700260
Adam Lesinski24aad162015-04-24 19:19:30 -0700261/**
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700262 * Given a path like: res/xml-sw600dp/foo.xml
263 *
264 * Extracts "res/xml-sw600dp/" into outPrefix.
265 * Extracts "foo" into outEntry.
266 * Extracts ".xml" into outSuffix.
267 *
268 * Returns true if successful.
269 */
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700270bool ExtractResFilePathParts(const StringPiece& path, StringPiece* out_prefix,
271 StringPiece* out_entry, StringPiece* out_suffix);
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700272
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700273} // namespace util
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800274
275/**
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700276 * Stream operator for functions. Calls the function with the stream as an
277 * argument.
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800278 * In the aapt namespace for lookup.
279 */
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700280inline ::std::ostream& operator<<(
281 ::std::ostream& out,
282 const ::std::function<::std::ostream&(::std::ostream&)>& f) {
283 return f(out);
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800284}
285
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700286} // namespace aapt
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800287
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700288#endif // AAPT_UTIL_H