blob: 0eb35d18c06e085b2de934d84823b2fb0a55d1c3 [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
Adam Lesinski96ea08f2017-11-06 10:44:46 -080056// Returns true if the string starts with prefix.
Adam Lesinskid5083f62017-01-16 15:07:21 -080057bool StartsWith(const android::StringPiece& str, const android::StringPiece& prefix);
Adam Lesinski4d3a9872015-04-09 19:53:22 -070058
Adam Lesinski96ea08f2017-11-06 10:44:46 -080059// Returns true if the string ends with suffix.
Adam Lesinskid5083f62017-01-16 15:07:21 -080060bool EndsWith(const android::StringPiece& str, const android::StringPiece& suffix);
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080061
Adam Lesinski2eed52e2018-02-21 15:55:58 -080062// Creates a new StringPiece that points to a substring of the original string without leading
63// whitespace.
64android::StringPiece TrimLeadingWhitespace(const android::StringPiece& str);
65
66// Creates a new StringPiece that points to a substring of the original string without trailing
67// whitespace.
68android::StringPiece TrimTrailingWhitespace(const android::StringPiece& str);
69
70// Creates a new StringPiece that points to a substring of the original string without leading or
Adam Lesinski96ea08f2017-11-06 10:44:46 -080071// trailing whitespace.
Adam Lesinskid5083f62017-01-16 15:07:21 -080072android::StringPiece TrimWhitespace(const android::StringPiece& str);
Adam Lesinski3b4cd942015-10-30 16:31:42 -070073
Adam Lesinski96ea08f2017-11-06 10:44:46 -080074// Tests that the string is a valid Java class name.
Adam Lesinskid5083f62017-01-16 15:07:21 -080075bool IsJavaClassName(const android::StringPiece& str);
Adam Lesinskia1ad4a82015-06-08 11:41:09 -070076
Adam Lesinski96ea08f2017-11-06 10:44:46 -080077// Tests that the string is a valid Java package name.
Adam Lesinskid5083f62017-01-16 15:07:21 -080078bool IsJavaPackageName(const android::StringPiece& str);
Adam Lesinski1ab598f2015-08-14 14:26:04 -070079
Adam Lesinski96ea08f2017-11-06 10:44:46 -080080// Tests that the string is a valid Android package name. More strict than a Java package name.
81// - First character of each component (separated by '.') must be an ASCII letter.
82// - Subsequent characters of a component can be ASCII alphanumeric or an underscore.
83// - Package must contain at least two components, unless it is 'android'.
84bool IsAndroidPackageName(const android::StringPiece& str);
85
86// Tests that the string is a valid Android split name.
87// - First character of each component (separated by '.') must be an ASCII letter.
88// - Subsequent characters of a component can be ASCII alphanumeric or an underscore.
89bool IsAndroidSplitName(const android::StringPiece& str);
90
91// Converts the class name to a fully qualified class name from the given
92// `package`. Ex:
93//
94// asdf --> package.asdf
95// .asdf --> package.asdf
96// .a.b --> package.a.b
97// asdf.adsf --> asdf.adsf
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 Lesinski060b53d2017-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 Lesinski96ea08f2017-11-06 10:44:46 -0800111// Makes a std::unique_ptr<> with the template parameter inferred by the compiler.
112// This will be present in C++14 and can be removed then.
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800113template <typename T, class... Args>
114std::unique_ptr<T> make_unique(Args&&... args) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700115 return std::unique_ptr<T>(new T{std::forward<Args>(args)...});
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800116}
117
Adam Lesinski96ea08f2017-11-06 10:44:46 -0800118// Writes a set of items to the std::ostream, joining the times with the provided separator.
Adam Lesinski36c73a52016-08-11 13:39:24 -0700119template <typename Container>
Adam Lesinski96ea08f2017-11-06 10:44:46 -0800120::std::function<::std::ostream&(::std::ostream&)> Joiner(const Container& container,
121 const char* sep) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700122 using std::begin;
123 using std::end;
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700124 const auto begin_iter = begin(container);
125 const auto end_iter = end(container);
126 return [begin_iter, end_iter, sep](::std::ostream& out) -> ::std::ostream& {
127 for (auto iter = begin_iter; iter != end_iter; ++iter) {
128 if (iter != begin_iter) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700129 out << sep;
130 }
131 out << *iter;
132 }
133 return out;
134 };
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800135}
136
Adam Lesinski96ea08f2017-11-06 10:44:46 -0800137// Helper method to extract a UTF-16 string from a StringPool. If the string is stored as UTF-8,
138// the conversion to UTF-16 happens within ResStringPool.
Adam Lesinskid5083f62017-01-16 15:07:21 -0800139android::StringPiece16 GetString16(const android::ResStringPool& pool, size_t idx);
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800140
Adam Lesinski96ea08f2017-11-06 10:44:46 -0800141// Helper method to extract a UTF-8 string from a StringPool. If the string is stored as UTF-16,
142// the conversion from UTF-16 to UTF-8 does not happen in ResStringPool and is done by this method,
143// which maintains no state or cache. This means we must return an std::string copy.
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700144std::string GetString(const android::ResStringPool& pool, size_t idx);
Adam Lesinski28cacf02015-11-23 14:22:47 -0800145
Adam Lesinski96ea08f2017-11-06 10:44:46 -0800146// Checks that the Java string format contains no non-positional arguments (arguments without
147// explicitly specifying an index) when there are more than one argument. This is an error
148// because translations may rearrange the order of the arguments in the string, which will
149// break the string interpolation.
Adam Lesinskid5083f62017-01-16 15:07:21 -0800150bool VerifyJavaStringFormat(const android::StringPiece& str);
Adam Lesinskib23f1e02015-11-03 12:24:17 -0800151
Adam Lesinski2eed52e2018-02-21 15:55:58 -0800152bool AppendStyledString(const android::StringPiece& input, bool preserve_spaces,
153 std::string* out_str, std::string* out_error);
154
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800155class StringBuilder {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700156 public:
Adam Lesinski2eed52e2018-02-21 15:55:58 -0800157 StringBuilder() = default;
Adam Lesinskifba0cf22017-06-29 17:53:36 -0700158
Adam Lesinskid5083f62017-01-16 15:07:21 -0800159 StringBuilder& Append(const android::StringPiece& str);
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700160 const std::string& ToString() const;
161 const std::string& Error() const;
Adam Lesinskiac6edc52017-03-02 19:31:28 -0800162 bool IsEmpty() const;
Adam Lesinski8c3f31f2016-09-07 13:45:13 -0700163
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700164 // When building StyledStrings, we need UTF-16 indices into the string,
165 // which is what the Java layer expects when dealing with java
166 // String.charAt().
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700167 size_t Utf16Len() const;
Adam Lesinski8c3f31f2016-09-07 13:45:13 -0700168
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700169 explicit operator bool() const;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800170
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700171 private:
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700172 std::string str_;
173 size_t utf16_len_ = 0;
174 bool quote_ = false;
175 bool trailing_space_ = false;
176 bool last_char_was_escape_ = false;
177 std::string error_;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800178};
179
Adam Lesinski96ea08f2017-11-06 10:44:46 -0800180inline const std::string& StringBuilder::ToString() const {
181 return str_;
182}
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800183
Adam Lesinski96ea08f2017-11-06 10:44:46 -0800184inline const std::string& StringBuilder::Error() const {
185 return error_;
186}
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800187
Adam Lesinski96ea08f2017-11-06 10:44:46 -0800188inline bool StringBuilder::IsEmpty() const {
189 return str_.empty();
190}
Adam Lesinskiac6edc52017-03-02 19:31:28 -0800191
Adam Lesinski96ea08f2017-11-06 10:44:46 -0800192inline size_t StringBuilder::Utf16Len() const {
193 return utf16_len_;
194}
Adam Lesinski8c3f31f2016-09-07 13:45:13 -0700195
Adam Lesinski96ea08f2017-11-06 10:44:46 -0800196inline StringBuilder::operator bool() const {
197 return error_.empty();
198}
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800199
Adam Lesinski96ea08f2017-11-06 10:44:46 -0800200// Converts a UTF8 string to a UTF16 string.
Adam Lesinskid5083f62017-01-16 15:07:21 -0800201std::u16string Utf8ToUtf16(const android::StringPiece& utf8);
202std::string Utf16ToUtf8(const android::StringPiece16& utf16);
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800203
Adam Lesinski96ea08f2017-11-06 10:44:46 -0800204// Writes the entire BigBuffer to the output stream.
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700205bool WriteAll(std::ostream& out, const BigBuffer& buffer);
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800206
Adam Lesinski96ea08f2017-11-06 10:44:46 -0800207// Copies the entire BigBuffer into a single buffer.
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700208std::unique_ptr<uint8_t[]> Copy(const BigBuffer& buffer);
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800209
Adam Lesinski96ea08f2017-11-06 10:44:46 -0800210// A Tokenizer implemented as an iterable collection. It does not allocate any memory on the heap
211// nor use standard containers.
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800212class Tokenizer {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700213 public:
214 class iterator {
215 public:
Adam Lesinski448a15c2017-07-25 10:59:26 -0700216 using reference = android::StringPiece&;
217 using value_type = android::StringPiece;
218 using difference_type = size_t;
219 using pointer = android::StringPiece*;
220 using iterator_category = std::forward_iterator_tag;
221
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700222 iterator(const iterator&) = default;
223 iterator& operator=(const iterator&) = default;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800224
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700225 iterator& operator++();
Adam Lesinskid0f116b2016-07-08 15:00:32 -0700226
Adam Lesinskid5083f62017-01-16 15:07:21 -0800227 android::StringPiece operator*() { return token_; }
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700228 bool operator==(const iterator& rhs) const;
229 bool operator!=(const iterator& rhs) const;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800230
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700231 private:
232 friend class Tokenizer;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800233
Chih-Hung Hsieh4dc58122017-08-03 16:28:10 -0700234 iterator(const android::StringPiece& s, char sep, const android::StringPiece& tok, bool end);
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800235
Adam Lesinskid5083f62017-01-16 15:07:21 -0800236 android::StringPiece str_;
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700237 char separator_;
Adam Lesinskid5083f62017-01-16 15:07:21 -0800238 android::StringPiece token_;
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700239 bool end_;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700240 };
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800241
Chih-Hung Hsieh4dc58122017-08-03 16:28:10 -0700242 Tokenizer(const android::StringPiece& str, char sep);
Adam Lesinskid0f116b2016-07-08 15:00:32 -0700243
Adam Lesinski448a15c2017-07-25 10:59:26 -0700244 iterator begin() const {
245 return begin_;
246 }
Adam Lesinskid0f116b2016-07-08 15:00:32 -0700247
Adam Lesinski448a15c2017-07-25 10:59:26 -0700248 iterator end() const {
249 return end_;
250 }
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800251
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700252 private:
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700253 const iterator begin_;
254 const iterator end_;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800255};
256
Adam Lesinski96ea08f2017-11-06 10:44:46 -0800257inline Tokenizer Tokenize(const android::StringPiece& str, char sep) {
258 return Tokenizer(str, sep);
259}
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800260
Adam Lesinski96ea08f2017-11-06 10:44:46 -0800261inline uint16_t HostToDevice16(uint16_t value) {
262 return htods(value);
263}
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700264
Adam Lesinski96ea08f2017-11-06 10:44:46 -0800265inline uint32_t HostToDevice32(uint32_t value) {
266 return htodl(value);
267}
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700268
Adam Lesinski96ea08f2017-11-06 10:44:46 -0800269inline uint16_t DeviceToHost16(uint16_t value) {
270 return dtohs(value);
271}
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700272
Adam Lesinski96ea08f2017-11-06 10:44:46 -0800273inline uint32_t DeviceToHost32(uint32_t value) {
274 return dtohl(value);
275}
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700276
Adam Lesinski96ea08f2017-11-06 10:44:46 -0800277// Given a path like: res/xml-sw600dp/foo.xml
278//
279// Extracts "res/xml-sw600dp/" into outPrefix.
280// Extracts "foo" into outEntry.
281// Extracts ".xml" into outSuffix.
282//
283// Returns true if successful.
Adam Lesinskid5083f62017-01-16 15:07:21 -0800284bool ExtractResFilePathParts(const android::StringPiece& path, android::StringPiece* out_prefix,
285 android::StringPiece* out_entry, android::StringPiece* out_suffix);
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700286
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700287} // namespace util
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800288
Adam Lesinski96ea08f2017-11-06 10:44:46 -0800289// Stream operator for functions. Calls the function with the stream as an argument.
290// In the aapt namespace for lookup.
291inline ::std::ostream& operator<<(::std::ostream& out,
292 const ::std::function<::std::ostream&(::std::ostream&)>& f) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700293 return f(out);
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800294}
295
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700296} // namespace aapt
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800297
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700298#endif // AAPT_UTIL_H