blob: ad3989ea430f54585133cfce8e844f85f7a34420 [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"
Adam Lesinskid5083f62017-01-16 15:07:21 -080027#include "androidfw/StringPiece.h"
Adam Lesinskice5e56e2016-10-21 17:56:45 -070028#include "utils/ByteOrder.h"
29
30#include "util/BigBuffer.h"
31#include "util/Maybe.h"
Adam Lesinskice5e56e2016-10-21 17:56:45 -070032
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 Lesinskic744ae82017-05-17 19:28:38 -070047template <typename T>
48struct Range {
49 T start;
50 T end;
51};
52
Adam Lesinskid5083f62017-01-16 15:07:21 -080053std::vector<std::string> Split(const android::StringPiece& str, char sep);
54std::vector<std::string> SplitAndLowercase(const android::StringPiece& str, char sep);
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080055
56/**
Adam Lesinski4d3a9872015-04-09 19:53:22 -070057 * Returns true if the string starts with prefix.
58 */
Adam Lesinskid5083f62017-01-16 15:07:21 -080059bool StartsWith(const android::StringPiece& str, const android::StringPiece& prefix);
Adam Lesinski4d3a9872015-04-09 19:53:22 -070060
61/**
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080062 * Returns true if the string ends with suffix.
63 */
Adam Lesinskid5083f62017-01-16 15:07:21 -080064bool EndsWith(const android::StringPiece& str, const android::StringPiece& suffix);
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080065
66/**
67 * Creates a new StringPiece16 that points to a substring
68 * of the original string without leading or trailing whitespace.
69 */
Adam Lesinskid5083f62017-01-16 15:07:21 -080070android::StringPiece TrimWhitespace(const android::StringPiece& str);
Adam Lesinski3b4cd942015-10-30 16:31:42 -070071
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080072/**
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080073 * Returns an iterator to the first character that is not alpha-numeric and that
74 * is not in the allowedChars set.
75 */
Adam Lesinskid5083f62017-01-16 15:07:21 -080076android::StringPiece::const_iterator FindNonAlphaNumericAndNotInSet(
77 const android::StringPiece& str, const android::StringPiece& allowed_chars);
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080078
79/**
Adam Lesinskia1ad4a82015-06-08 11:41:09 -070080 * Tests that the string is a valid Java class name.
81 */
Adam Lesinskid5083f62017-01-16 15:07:21 -080082bool IsJavaClassName(const android::StringPiece& str);
Adam Lesinskia1ad4a82015-06-08 11:41:09 -070083
84/**
Adam Lesinski1ab598f2015-08-14 14:26:04 -070085 * Tests that the string is a valid Java package name.
86 */
Adam Lesinskid5083f62017-01-16 15:07:21 -080087bool IsJavaPackageName(const android::StringPiece& str);
Adam Lesinski1ab598f2015-08-14 14:26:04 -070088
89/**
Adam Lesinskicacb28f2016-10-19 12:18:14 -070090 * Converts the class name to a fully qualified class name from the given
91 * `package`. Ex:
Adam Lesinskia1ad4a82015-06-08 11:41:09 -070092 *
93 * asdf --> package.asdf
94 * .asdf --> package.asdf
95 * .a.b --> package.a.b
96 * asdf.adsf --> asdf.adsf
97 */
Adam Lesinskid5083f62017-01-16 15:07:21 -080098Maybe<std::string> GetFullyQualifiedClassName(const android::StringPiece& package,
99 const android::StringPiece& class_name);
Adam Lesinskia1ad4a82015-06-08 11:41:09 -0700100
Adam Lesinski5b6ee112017-07-28 17:10:35 -0700101template <typename T>
102typename std::enable_if<std::is_arithmetic<T>::value, int>::type compare(const T& a, const T& b) {
103 if (a < b) {
104 return -1;
105 } else if (a > b) {
106 return 1;
107 }
108 return 0;
109}
110
Adam Lesinskia1ad4a82015-06-08 11:41:09 -0700111/**
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700112 * Makes a std::unique_ptr<> with the template parameter inferred by the compiler.
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800113 * This will be present in C++14 and can be removed then.
114 */
115template <typename T, class... Args>
116std::unique_ptr<T> make_unique(Args&&... args) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700117 return std::unique_ptr<T>(new T{std::forward<Args>(args)...});
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800118}
119
120/**
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700121 * Writes a set of items to the std::ostream, joining the times with the
122 * provided
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800123 * separator.
124 */
Adam Lesinski36c73a52016-08-11 13:39:24 -0700125template <typename Container>
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700126::std::function<::std::ostream&(::std::ostream&)> Joiner(
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700127 const Container& container, const char* sep) {
128 using std::begin;
129 using std::end;
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700130 const auto begin_iter = begin(container);
131 const auto end_iter = end(container);
132 return [begin_iter, end_iter, sep](::std::ostream& out) -> ::std::ostream& {
133 for (auto iter = begin_iter; iter != end_iter; ++iter) {
134 if (iter != begin_iter) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700135 out << sep;
136 }
137 out << *iter;
138 }
139 return out;
140 };
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800141}
142
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800143/**
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700144 * Helper method to extract a UTF-16 string from a StringPool. If the string is
145 * stored as UTF-8,
Adam Lesinskid0f116b2016-07-08 15:00:32 -0700146 * the conversion to UTF-16 happens within ResStringPool.
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800147 */
Adam Lesinskid5083f62017-01-16 15:07:21 -0800148android::StringPiece16 GetString16(const android::ResStringPool& pool, size_t idx);
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800149
Adam Lesinskid0f116b2016-07-08 15:00:32 -0700150/**
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700151 * Helper method to extract a UTF-8 string from a StringPool. If the string is
152 * stored as UTF-16,
153 * the conversion from UTF-16 to UTF-8 does not happen in ResStringPool and is
154 * done by this method,
155 * which maintains no state or cache. This means we must return an std::string
156 * copy.
Adam Lesinskid0f116b2016-07-08 15:00:32 -0700157 */
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700158std::string GetString(const android::ResStringPool& pool, size_t idx);
Adam Lesinski28cacf02015-11-23 14:22:47 -0800159
Adam Lesinskib23f1e02015-11-03 12:24:17 -0800160/**
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700161 * Checks that the Java string format contains no non-positional arguments
162 * (arguments without
163 * explicitly specifying an index) when there are more than one argument. This
164 * is an error
165 * because translations may rearrange the order of the arguments in the string,
166 * which will
Adam Lesinskib23f1e02015-11-03 12:24:17 -0800167 * break the string interpolation.
168 */
Adam Lesinskid5083f62017-01-16 15:07:21 -0800169bool VerifyJavaStringFormat(const android::StringPiece& str);
Adam Lesinskib23f1e02015-11-03 12:24:17 -0800170
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800171class StringBuilder {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700172 public:
Adam Lesinskifba0cf22017-06-29 17:53:36 -0700173 explicit StringBuilder(bool preserve_spaces = false);
174
Adam Lesinskid5083f62017-01-16 15:07:21 -0800175 StringBuilder& Append(const android::StringPiece& str);
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700176 const std::string& ToString() const;
177 const std::string& Error() const;
Adam Lesinskiac6edc52017-03-02 19:31:28 -0800178 bool IsEmpty() const;
Adam Lesinski8c3f31f2016-09-07 13:45:13 -0700179
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700180 // When building StyledStrings, we need UTF-16 indices into the string,
181 // which is what the Java layer expects when dealing with java
182 // String.charAt().
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700183 size_t Utf16Len() const;
Adam Lesinski8c3f31f2016-09-07 13:45:13 -0700184
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700185 explicit operator bool() const;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800186
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700187 private:
Adam Lesinskifba0cf22017-06-29 17:53:36 -0700188 bool preserve_spaces_;
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700189 std::string str_;
190 size_t utf16_len_ = 0;
191 bool quote_ = false;
192 bool trailing_space_ = false;
193 bool last_char_was_escape_ = false;
194 std::string error_;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800195};
196
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700197inline const std::string& StringBuilder::ToString() const { return str_; }
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800198
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700199inline const std::string& StringBuilder::Error() const { return error_; }
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800200
Adam Lesinskiac6edc52017-03-02 19:31:28 -0800201inline bool StringBuilder::IsEmpty() const { return str_.empty(); }
202
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700203inline size_t StringBuilder::Utf16Len() const { return utf16_len_; }
Adam Lesinski8c3f31f2016-09-07 13:45:13 -0700204
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700205inline StringBuilder::operator bool() const { return error_.empty(); }
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800206
207/**
208 * Converts a UTF8 string to a UTF16 string.
209 */
Adam Lesinskid5083f62017-01-16 15:07:21 -0800210std::u16string Utf8ToUtf16(const android::StringPiece& utf8);
211std::string Utf16ToUtf8(const android::StringPiece16& utf16);
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800212
213/**
214 * Writes the entire BigBuffer to the output stream.
215 */
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700216bool WriteAll(std::ostream& out, const BigBuffer& buffer);
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800217
218/*
219 * Copies the entire BigBuffer into a single buffer.
220 */
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700221std::unique_ptr<uint8_t[]> Copy(const BigBuffer& buffer);
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800222
223/**
224 * A Tokenizer implemented as an iterable collection. It does not allocate
225 * any memory on the heap nor use standard containers.
226 */
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800227class Tokenizer {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700228 public:
229 class iterator {
230 public:
231 iterator(const iterator&) = default;
232 iterator& operator=(const iterator&) = default;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800233
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700234 iterator& operator++();
Adam Lesinskid0f116b2016-07-08 15:00:32 -0700235
Adam Lesinskid5083f62017-01-16 15:07:21 -0800236 android::StringPiece operator*() { return token_; }
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700237 bool operator==(const iterator& rhs) const;
238 bool operator!=(const iterator& rhs) const;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800239
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700240 private:
241 friend class Tokenizer;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800242
Adam Lesinskid5083f62017-01-16 15:07:21 -0800243 iterator(android::StringPiece s, char sep, android::StringPiece tok, bool end);
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800244
Adam Lesinskid5083f62017-01-16 15:07:21 -0800245 android::StringPiece str_;
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700246 char separator_;
Adam Lesinskid5083f62017-01-16 15:07:21 -0800247 android::StringPiece token_;
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700248 bool end_;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700249 };
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800250
Adam Lesinskid5083f62017-01-16 15:07:21 -0800251 Tokenizer(android::StringPiece str, char sep);
Adam Lesinskid0f116b2016-07-08 15:00:32 -0700252
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700253 iterator begin() { return begin_; }
Adam Lesinskid0f116b2016-07-08 15:00:32 -0700254
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700255 iterator end() { return end_; }
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800256
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700257 private:
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700258 const iterator begin_;
259 const iterator end_;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800260};
261
Adam Lesinskid5083f62017-01-16 15:07:21 -0800262inline Tokenizer Tokenize(const android::StringPiece& str, char sep) { return Tokenizer(str, sep); }
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800263
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700264inline uint16_t HostToDevice16(uint16_t value) { return htods(value); }
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700265
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700266inline uint32_t HostToDevice32(uint32_t value) { return htodl(value); }
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700267
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700268inline uint16_t DeviceToHost16(uint16_t value) { return dtohs(value); }
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700269
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700270inline uint32_t DeviceToHost32(uint32_t value) { return dtohl(value); }
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700271
Adam Lesinski24aad162015-04-24 19:19:30 -0700272/**
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700273 * Given a path like: res/xml-sw600dp/foo.xml
274 *
275 * Extracts "res/xml-sw600dp/" into outPrefix.
276 * Extracts "foo" into outEntry.
277 * Extracts ".xml" into outSuffix.
278 *
279 * Returns true if successful.
280 */
Adam Lesinskid5083f62017-01-16 15:07:21 -0800281bool ExtractResFilePathParts(const android::StringPiece& path, android::StringPiece* out_prefix,
282 android::StringPiece* out_entry, android::StringPiece* out_suffix);
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700283
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700284} // namespace util
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800285
286/**
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700287 * Stream operator for functions. Calls the function with the stream as an
288 * argument.
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800289 * In the aapt namespace for lookup.
290 */
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700291inline ::std::ostream& operator<<(
292 ::std::ostream& out,
293 const ::std::function<::std::ostream&(::std::ostream&)>& f) {
294 return f(out);
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800295}
296
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700297} // namespace aapt
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800298
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700299#endif // AAPT_UTIL_H