blob: 7c949b90c10a0c274f22e3aa1e45c9f9db2f4728 [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 Lesinski96ea08f2017-11-06 10:44:46 -080062// Creates a new StringPiece16 that points to a substring of the original string without leading or
63// trailing whitespace.
Adam Lesinskid5083f62017-01-16 15:07:21 -080064android::StringPiece TrimWhitespace(const android::StringPiece& str);
Adam Lesinski3b4cd942015-10-30 16:31:42 -070065
Adam Lesinski96ea08f2017-11-06 10:44:46 -080066// Tests that the string is a valid Java class name.
Adam Lesinskid5083f62017-01-16 15:07:21 -080067bool IsJavaClassName(const android::StringPiece& str);
Adam Lesinskia1ad4a82015-06-08 11:41:09 -070068
Adam Lesinski96ea08f2017-11-06 10:44:46 -080069// Tests that the string is a valid Java package name.
Adam Lesinskid5083f62017-01-16 15:07:21 -080070bool IsJavaPackageName(const android::StringPiece& str);
Adam Lesinski1ab598f2015-08-14 14:26:04 -070071
Adam Lesinski96ea08f2017-11-06 10:44:46 -080072// Tests that the string is a valid Android package name. More strict than a Java package name.
73// - First character of each component (separated by '.') must be an ASCII letter.
74// - Subsequent characters of a component can be ASCII alphanumeric or an underscore.
75// - Package must contain at least two components, unless it is 'android'.
76bool IsAndroidPackageName(const android::StringPiece& str);
77
78// Tests that the string is a valid Android split name.
79// - First character of each component (separated by '.') must be an ASCII letter.
80// - Subsequent characters of a component can be ASCII alphanumeric or an underscore.
81bool IsAndroidSplitName(const android::StringPiece& str);
82
83// Converts the class name to a fully qualified class name from the given
84// `package`. Ex:
85//
86// asdf --> package.asdf
87// .asdf --> package.asdf
88// .a.b --> package.a.b
89// asdf.adsf --> asdf.adsf
Adam Lesinskid5083f62017-01-16 15:07:21 -080090Maybe<std::string> GetFullyQualifiedClassName(const android::StringPiece& package,
91 const android::StringPiece& class_name);
Adam Lesinskia1ad4a82015-06-08 11:41:09 -070092
Adam Lesinski060b53d2017-07-28 17:10:35 -070093template <typename T>
94typename std::enable_if<std::is_arithmetic<T>::value, int>::type compare(const T& a, const T& b) {
95 if (a < b) {
96 return -1;
97 } else if (a > b) {
98 return 1;
99 }
100 return 0;
101}
102
Adam Lesinski96ea08f2017-11-06 10:44:46 -0800103// Makes a std::unique_ptr<> with the template parameter inferred by the compiler.
104// This will be present in C++14 and can be removed then.
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800105template <typename T, class... Args>
106std::unique_ptr<T> make_unique(Args&&... args) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700107 return std::unique_ptr<T>(new T{std::forward<Args>(args)...});
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800108}
109
Adam Lesinski96ea08f2017-11-06 10:44:46 -0800110// Writes a set of items to the std::ostream, joining the times with the provided separator.
Adam Lesinski36c73a52016-08-11 13:39:24 -0700111template <typename Container>
Adam Lesinski96ea08f2017-11-06 10:44:46 -0800112::std::function<::std::ostream&(::std::ostream&)> Joiner(const Container& container,
113 const char* sep) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700114 using std::begin;
115 using std::end;
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700116 const auto begin_iter = begin(container);
117 const auto end_iter = end(container);
118 return [begin_iter, end_iter, sep](::std::ostream& out) -> ::std::ostream& {
119 for (auto iter = begin_iter; iter != end_iter; ++iter) {
120 if (iter != begin_iter) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700121 out << sep;
122 }
123 out << *iter;
124 }
125 return out;
126 };
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800127}
128
Adam Lesinski96ea08f2017-11-06 10:44:46 -0800129// Helper method to extract a UTF-16 string from a StringPool. If the string is stored as UTF-8,
130// the conversion to UTF-16 happens within ResStringPool.
Adam Lesinskid5083f62017-01-16 15:07:21 -0800131android::StringPiece16 GetString16(const android::ResStringPool& pool, size_t idx);
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800132
Adam Lesinski96ea08f2017-11-06 10:44:46 -0800133// Helper method to extract a UTF-8 string from a StringPool. If the string is stored as UTF-16,
134// the conversion from UTF-16 to UTF-8 does not happen in ResStringPool and is done by this method,
135// which maintains no state or cache. This means we must return an std::string copy.
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700136std::string GetString(const android::ResStringPool& pool, size_t idx);
Adam Lesinski28cacf02015-11-23 14:22:47 -0800137
Adam Lesinski96ea08f2017-11-06 10:44:46 -0800138// Checks that the Java string format contains no non-positional arguments (arguments without
139// explicitly specifying an index) when there are more than one argument. This is an error
140// because translations may rearrange the order of the arguments in the string, which will
141// break the string interpolation.
Adam Lesinskid5083f62017-01-16 15:07:21 -0800142bool VerifyJavaStringFormat(const android::StringPiece& str);
Adam Lesinskib23f1e02015-11-03 12:24:17 -0800143
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800144class StringBuilder {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700145 public:
Adam Lesinskifba0cf22017-06-29 17:53:36 -0700146 explicit StringBuilder(bool preserve_spaces = false);
147
Adam Lesinskid5083f62017-01-16 15:07:21 -0800148 StringBuilder& Append(const android::StringPiece& str);
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700149 const std::string& ToString() const;
150 const std::string& Error() const;
Adam Lesinskiac6edc52017-03-02 19:31:28 -0800151 bool IsEmpty() const;
Adam Lesinski8c3f31f2016-09-07 13:45:13 -0700152
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700153 // When building StyledStrings, we need UTF-16 indices into the string,
154 // which is what the Java layer expects when dealing with java
155 // String.charAt().
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700156 size_t Utf16Len() const;
Adam Lesinski8c3f31f2016-09-07 13:45:13 -0700157
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700158 explicit operator bool() const;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800159
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700160 private:
Adam Lesinskifba0cf22017-06-29 17:53:36 -0700161 bool preserve_spaces_;
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700162 std::string str_;
163 size_t utf16_len_ = 0;
164 bool quote_ = false;
165 bool trailing_space_ = false;
166 bool last_char_was_escape_ = false;
167 std::string error_;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800168};
169
Adam Lesinski96ea08f2017-11-06 10:44:46 -0800170inline const std::string& StringBuilder::ToString() const {
171 return str_;
172}
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800173
Adam Lesinski96ea08f2017-11-06 10:44:46 -0800174inline const std::string& StringBuilder::Error() const {
175 return error_;
176}
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800177
Adam Lesinski96ea08f2017-11-06 10:44:46 -0800178inline bool StringBuilder::IsEmpty() const {
179 return str_.empty();
180}
Adam Lesinskiac6edc52017-03-02 19:31:28 -0800181
Adam Lesinski96ea08f2017-11-06 10:44:46 -0800182inline size_t StringBuilder::Utf16Len() const {
183 return utf16_len_;
184}
Adam Lesinski8c3f31f2016-09-07 13:45:13 -0700185
Adam Lesinski96ea08f2017-11-06 10:44:46 -0800186inline StringBuilder::operator bool() const {
187 return error_.empty();
188}
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800189
Adam Lesinski96ea08f2017-11-06 10:44:46 -0800190// Converts a UTF8 string to a UTF16 string.
Adam Lesinskid5083f62017-01-16 15:07:21 -0800191std::u16string Utf8ToUtf16(const android::StringPiece& utf8);
192std::string Utf16ToUtf8(const android::StringPiece16& utf16);
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800193
Adam Lesinski96ea08f2017-11-06 10:44:46 -0800194// Writes the entire BigBuffer to the output stream.
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700195bool WriteAll(std::ostream& out, const BigBuffer& buffer);
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800196
Adam Lesinski96ea08f2017-11-06 10:44:46 -0800197// Copies the entire BigBuffer into a single buffer.
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700198std::unique_ptr<uint8_t[]> Copy(const BigBuffer& buffer);
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800199
Adam Lesinski96ea08f2017-11-06 10:44:46 -0800200// A Tokenizer implemented as an iterable collection. It does not allocate any memory on the heap
201// nor use standard containers.
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800202class Tokenizer {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700203 public:
204 class iterator {
205 public:
Adam Lesinski448a15c2017-07-25 10:59:26 -0700206 using reference = android::StringPiece&;
207 using value_type = android::StringPiece;
208 using difference_type = size_t;
209 using pointer = android::StringPiece*;
210 using iterator_category = std::forward_iterator_tag;
211
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700212 iterator(const iterator&) = default;
213 iterator& operator=(const iterator&) = default;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800214
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700215 iterator& operator++();
Adam Lesinskid0f116b2016-07-08 15:00:32 -0700216
Adam Lesinskid5083f62017-01-16 15:07:21 -0800217 android::StringPiece operator*() { return token_; }
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700218 bool operator==(const iterator& rhs) const;
219 bool operator!=(const iterator& rhs) const;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800220
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700221 private:
222 friend class Tokenizer;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800223
Chih-Hung Hsieh4dc58122017-08-03 16:28:10 -0700224 iterator(const android::StringPiece& s, char sep, const android::StringPiece& tok, bool end);
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800225
Adam Lesinskid5083f62017-01-16 15:07:21 -0800226 android::StringPiece str_;
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700227 char separator_;
Adam Lesinskid5083f62017-01-16 15:07:21 -0800228 android::StringPiece token_;
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700229 bool end_;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700230 };
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800231
Chih-Hung Hsieh4dc58122017-08-03 16:28:10 -0700232 Tokenizer(const android::StringPiece& str, char sep);
Adam Lesinskid0f116b2016-07-08 15:00:32 -0700233
Adam Lesinski448a15c2017-07-25 10:59:26 -0700234 iterator begin() const {
235 return begin_;
236 }
Adam Lesinskid0f116b2016-07-08 15:00:32 -0700237
Adam Lesinski448a15c2017-07-25 10:59:26 -0700238 iterator end() const {
239 return end_;
240 }
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800241
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700242 private:
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700243 const iterator begin_;
244 const iterator end_;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800245};
246
Adam Lesinski96ea08f2017-11-06 10:44:46 -0800247inline Tokenizer Tokenize(const android::StringPiece& str, char sep) {
248 return Tokenizer(str, sep);
249}
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800250
Adam Lesinski96ea08f2017-11-06 10:44:46 -0800251inline uint16_t HostToDevice16(uint16_t value) {
252 return htods(value);
253}
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700254
Adam Lesinski96ea08f2017-11-06 10:44:46 -0800255inline uint32_t HostToDevice32(uint32_t value) {
256 return htodl(value);
257}
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700258
Adam Lesinski96ea08f2017-11-06 10:44:46 -0800259inline uint16_t DeviceToHost16(uint16_t value) {
260 return dtohs(value);
261}
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700262
Adam Lesinski96ea08f2017-11-06 10:44:46 -0800263inline uint32_t DeviceToHost32(uint32_t value) {
264 return dtohl(value);
265}
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700266
Adam Lesinski96ea08f2017-11-06 10:44:46 -0800267// Given a path like: res/xml-sw600dp/foo.xml
268//
269// Extracts "res/xml-sw600dp/" into outPrefix.
270// Extracts "foo" into outEntry.
271// Extracts ".xml" into outSuffix.
272//
273// Returns true if successful.
Adam Lesinskid5083f62017-01-16 15:07:21 -0800274bool ExtractResFilePathParts(const android::StringPiece& path, android::StringPiece* out_prefix,
275 android::StringPiece* out_entry, android::StringPiece* out_suffix);
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700276
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700277} // namespace util
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800278
Adam Lesinski96ea08f2017-11-06 10:44:46 -0800279// Stream operator for functions. Calls the function with the stream as an argument.
280// In the aapt namespace for lookup.
281inline ::std::ostream& operator<<(::std::ostream& out,
282 const ::std::function<::std::ostream&(::std::ostream&)>& f) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700283 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