blob: 410258c402095e4f37b9b0d275031addc35d381c [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;
Adam Lesinski5b7337f2017-06-26 14:57:22 -070051
52 typename std::enable_if<has_lte_op<const T, const T>::value, bool>::type Contains(
53 const T& t) const {
54 return start <= t && t < end;
55 }
Adam Lesinskic744ae82017-05-17 19:28:38 -070056};
57
Adam Lesinskid5083f62017-01-16 15:07:21 -080058std::vector<std::string> Split(const android::StringPiece& str, char sep);
59std::vector<std::string> SplitAndLowercase(const android::StringPiece& str, char sep);
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080060
61/**
Adam Lesinski4d3a9872015-04-09 19:53:22 -070062 * Returns true if the string starts with prefix.
63 */
Adam Lesinskid5083f62017-01-16 15:07:21 -080064bool StartsWith(const android::StringPiece& str, const android::StringPiece& prefix);
Adam Lesinski4d3a9872015-04-09 19:53:22 -070065
66/**
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080067 * Returns true if the string ends with suffix.
68 */
Adam Lesinskid5083f62017-01-16 15:07:21 -080069bool EndsWith(const android::StringPiece& str, const android::StringPiece& suffix);
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080070
71/**
72 * Creates a new StringPiece16 that points to a substring
73 * of the original string without leading or trailing whitespace.
74 */
Adam Lesinskid5083f62017-01-16 15:07:21 -080075android::StringPiece TrimWhitespace(const android::StringPiece& str);
Adam Lesinski3b4cd942015-10-30 16:31:42 -070076
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080077/**
78 * UTF-16 isspace(). It basically checks for lower range characters that are
79 * whitespace.
80 */
Adam Lesinskicacb28f2016-10-19 12:18:14 -070081inline bool isspace16(char16_t c) { return c < 0x0080 && isspace(c); }
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080082
83/**
84 * Returns an iterator to the first character that is not alpha-numeric and that
85 * is not in the allowedChars set.
86 */
Adam Lesinskid5083f62017-01-16 15:07:21 -080087android::StringPiece::const_iterator FindNonAlphaNumericAndNotInSet(
88 const android::StringPiece& str, const android::StringPiece& allowed_chars);
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080089
90/**
Adam Lesinskia1ad4a82015-06-08 11:41:09 -070091 * Tests that the string is a valid Java class name.
92 */
Adam Lesinskid5083f62017-01-16 15:07:21 -080093bool IsJavaClassName(const android::StringPiece& str);
Adam Lesinskia1ad4a82015-06-08 11:41:09 -070094
95/**
Adam Lesinski1ab598f2015-08-14 14:26:04 -070096 * Tests that the string is a valid Java package name.
97 */
Adam Lesinskid5083f62017-01-16 15:07:21 -080098bool IsJavaPackageName(const android::StringPiece& str);
Adam Lesinski1ab598f2015-08-14 14:26:04 -070099
100/**
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700101 * Converts the class name to a fully qualified class name from the given
102 * `package`. Ex:
Adam Lesinskia1ad4a82015-06-08 11:41:09 -0700103 *
104 * asdf --> package.asdf
105 * .asdf --> package.asdf
106 * .a.b --> package.a.b
107 * asdf.adsf --> asdf.adsf
108 */
Adam Lesinskid5083f62017-01-16 15:07:21 -0800109Maybe<std::string> GetFullyQualifiedClassName(const android::StringPiece& package,
110 const android::StringPiece& class_name);
Adam Lesinskia1ad4a82015-06-08 11:41:09 -0700111
112/**
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700113 * Makes a std::unique_ptr<> with the template parameter inferred by the compiler.
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800114 * This will be present in C++14 and can be removed then.
115 */
116template <typename T, class... Args>
117std::unique_ptr<T> make_unique(Args&&... args) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700118 return std::unique_ptr<T>(new T{std::forward<Args>(args)...});
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800119}
120
121/**
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700122 * Writes a set of items to the std::ostream, joining the times with the
123 * provided
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800124 * separator.
125 */
Adam Lesinski36c73a52016-08-11 13:39:24 -0700126template <typename Container>
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700127::std::function<::std::ostream&(::std::ostream&)> Joiner(
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700128 const Container& container, const char* sep) {
129 using std::begin;
130 using std::end;
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700131 const auto begin_iter = begin(container);
132 const auto end_iter = end(container);
133 return [begin_iter, end_iter, sep](::std::ostream& out) -> ::std::ostream& {
134 for (auto iter = begin_iter; iter != end_iter; ++iter) {
135 if (iter != begin_iter) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700136 out << sep;
137 }
138 out << *iter;
139 }
140 return out;
141 };
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800142}
143
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800144/**
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700145 * Helper method to extract a UTF-16 string from a StringPool. If the string is
146 * stored as UTF-8,
Adam Lesinskid0f116b2016-07-08 15:00:32 -0700147 * the conversion to UTF-16 happens within ResStringPool.
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800148 */
Adam Lesinskid5083f62017-01-16 15:07:21 -0800149android::StringPiece16 GetString16(const android::ResStringPool& pool, size_t idx);
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800150
Adam Lesinskid0f116b2016-07-08 15:00:32 -0700151/**
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700152 * Helper method to extract a UTF-8 string from a StringPool. If the string is
153 * stored as UTF-16,
154 * the conversion from UTF-16 to UTF-8 does not happen in ResStringPool and is
155 * done by this method,
156 * which maintains no state or cache. This means we must return an std::string
157 * copy.
Adam Lesinskid0f116b2016-07-08 15:00:32 -0700158 */
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700159std::string GetString(const android::ResStringPool& pool, size_t idx);
Adam Lesinski28cacf02015-11-23 14:22:47 -0800160
Adam Lesinskib23f1e02015-11-03 12:24:17 -0800161/**
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700162 * Checks that the Java string format contains no non-positional arguments
163 * (arguments without
164 * explicitly specifying an index) when there are more than one argument. This
165 * is an error
166 * because translations may rearrange the order of the arguments in the string,
167 * which will
Adam Lesinskib23f1e02015-11-03 12:24:17 -0800168 * break the string interpolation.
169 */
Adam Lesinskid5083f62017-01-16 15:07:21 -0800170bool VerifyJavaStringFormat(const android::StringPiece& str);
Adam Lesinskib23f1e02015-11-03 12:24:17 -0800171
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800172class StringBuilder {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700173 public:
Adam Lesinskifba0cf22017-06-29 17:53:36 -0700174 explicit StringBuilder(bool preserve_spaces = false);
175
Adam Lesinskid5083f62017-01-16 15:07:21 -0800176 StringBuilder& Append(const android::StringPiece& str);
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700177 const std::string& ToString() const;
178 const std::string& Error() const;
Adam Lesinskiac6edc52017-03-02 19:31:28 -0800179 bool IsEmpty() const;
Adam Lesinski8c3f31f2016-09-07 13:45:13 -0700180
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700181 // When building StyledStrings, we need UTF-16 indices into the string,
182 // which is what the Java layer expects when dealing with java
183 // String.charAt().
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700184 size_t Utf16Len() const;
Adam Lesinski8c3f31f2016-09-07 13:45:13 -0700185
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700186 explicit operator bool() const;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800187
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700188 private:
Adam Lesinskifba0cf22017-06-29 17:53:36 -0700189 bool preserve_spaces_;
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700190 std::string str_;
191 size_t utf16_len_ = 0;
192 bool quote_ = false;
193 bool trailing_space_ = false;
194 bool last_char_was_escape_ = false;
195 std::string error_;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800196};
197
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700198inline const std::string& StringBuilder::ToString() const { return str_; }
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800199
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700200inline const std::string& StringBuilder::Error() const { return error_; }
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800201
Adam Lesinskiac6edc52017-03-02 19:31:28 -0800202inline bool StringBuilder::IsEmpty() const { return str_.empty(); }
203
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700204inline size_t StringBuilder::Utf16Len() const { return utf16_len_; }
Adam Lesinski8c3f31f2016-09-07 13:45:13 -0700205
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700206inline StringBuilder::operator bool() const { return error_.empty(); }
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800207
208/**
209 * Converts a UTF8 string to a UTF16 string.
210 */
Adam Lesinskid5083f62017-01-16 15:07:21 -0800211std::u16string Utf8ToUtf16(const android::StringPiece& utf8);
212std::string Utf16ToUtf8(const android::StringPiece16& utf16);
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800213
214/**
215 * Writes the entire BigBuffer to the output stream.
216 */
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700217bool WriteAll(std::ostream& out, const BigBuffer& buffer);
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800218
219/*
220 * Copies the entire BigBuffer into a single buffer.
221 */
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700222std::unique_ptr<uint8_t[]> Copy(const BigBuffer& buffer);
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800223
224/**
225 * A Tokenizer implemented as an iterable collection. It does not allocate
226 * any memory on the heap nor use standard containers.
227 */
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800228class Tokenizer {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700229 public:
230 class iterator {
231 public:
232 iterator(const iterator&) = default;
233 iterator& operator=(const iterator&) = default;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800234
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700235 iterator& operator++();
Adam Lesinskid0f116b2016-07-08 15:00:32 -0700236
Adam Lesinskid5083f62017-01-16 15:07:21 -0800237 android::StringPiece operator*() { return token_; }
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700238 bool operator==(const iterator& rhs) const;
239 bool operator!=(const iterator& rhs) const;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800240
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700241 private:
242 friend class Tokenizer;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800243
Adam Lesinskid5083f62017-01-16 15:07:21 -0800244 iterator(android::StringPiece s, char sep, android::StringPiece tok, bool end);
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800245
Adam Lesinskid5083f62017-01-16 15:07:21 -0800246 android::StringPiece str_;
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700247 char separator_;
Adam Lesinskid5083f62017-01-16 15:07:21 -0800248 android::StringPiece token_;
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700249 bool end_;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700250 };
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800251
Adam Lesinskid5083f62017-01-16 15:07:21 -0800252 Tokenizer(android::StringPiece str, char sep);
Adam Lesinskid0f116b2016-07-08 15:00:32 -0700253
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700254 iterator begin() { return begin_; }
Adam Lesinskid0f116b2016-07-08 15:00:32 -0700255
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700256 iterator end() { return end_; }
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800257
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700258 private:
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700259 const iterator begin_;
260 const iterator end_;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800261};
262
Adam Lesinskid5083f62017-01-16 15:07:21 -0800263inline Tokenizer Tokenize(const android::StringPiece& str, char sep) { return Tokenizer(str, sep); }
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800264
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700265inline uint16_t HostToDevice16(uint16_t value) { return htods(value); }
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700266
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700267inline uint32_t HostToDevice32(uint32_t value) { return htodl(value); }
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700268
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700269inline uint16_t DeviceToHost16(uint16_t value) { return dtohs(value); }
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700270
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700271inline uint32_t DeviceToHost32(uint32_t value) { return dtohl(value); }
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700272
Adam Lesinski24aad162015-04-24 19:19:30 -0700273/**
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700274 * Given a path like: res/xml-sw600dp/foo.xml
275 *
276 * Extracts "res/xml-sw600dp/" into outPrefix.
277 * Extracts "foo" into outEntry.
278 * Extracts ".xml" into outSuffix.
279 *
280 * Returns true if successful.
281 */
Adam Lesinskid5083f62017-01-16 15:07:21 -0800282bool ExtractResFilePathParts(const android::StringPiece& path, android::StringPiece* out_prefix,
283 android::StringPiece* out_entry, android::StringPiece* out_suffix);
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700284
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700285} // namespace util
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800286
287/**
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700288 * Stream operator for functions. Calls the function with the stream as an
289 * argument.
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800290 * In the aapt namespace for lookup.
291 */
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700292inline ::std::ostream& operator<<(
293 ::std::ostream& out,
294 const ::std::function<::std::ostream&(::std::ostream&)>& f) {
295 return f(out);
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800296}
297
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700298} // namespace aapt
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800299
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700300#endif // AAPT_UTIL_H