blob: 8bca9dd739bff7a21dcfb7f2799a6d51f380a22a [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 Lesinskid5083f62017-01-16 15:07:21 -0800174 StringBuilder& Append(const android::StringPiece& str);
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700175 const std::string& ToString() const;
176 const std::string& Error() const;
Adam Lesinskiac6edc52017-03-02 19:31:28 -0800177 bool IsEmpty() const;
Adam Lesinski8c3f31f2016-09-07 13:45:13 -0700178
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700179 // When building StyledStrings, we need UTF-16 indices into the string,
180 // which is what the Java layer expects when dealing with java
181 // String.charAt().
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700182 size_t Utf16Len() const;
Adam Lesinski8c3f31f2016-09-07 13:45:13 -0700183
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700184 explicit operator bool() const;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800185
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700186 private:
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700187 std::string str_;
188 size_t utf16_len_ = 0;
189 bool quote_ = false;
190 bool trailing_space_ = false;
191 bool last_char_was_escape_ = false;
192 std::string error_;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800193};
194
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700195inline const std::string& StringBuilder::ToString() const { return str_; }
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800196
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700197inline const std::string& StringBuilder::Error() const { return error_; }
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800198
Adam Lesinskiac6edc52017-03-02 19:31:28 -0800199inline bool StringBuilder::IsEmpty() const { return str_.empty(); }
200
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700201inline size_t StringBuilder::Utf16Len() const { return utf16_len_; }
Adam Lesinski8c3f31f2016-09-07 13:45:13 -0700202
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700203inline StringBuilder::operator bool() const { return error_.empty(); }
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800204
205/**
206 * Converts a UTF8 string to a UTF16 string.
207 */
Adam Lesinskid5083f62017-01-16 15:07:21 -0800208std::u16string Utf8ToUtf16(const android::StringPiece& utf8);
209std::string Utf16ToUtf8(const android::StringPiece16& utf16);
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800210
211/**
212 * Writes the entire BigBuffer to the output stream.
213 */
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700214bool WriteAll(std::ostream& out, const BigBuffer& buffer);
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800215
216/*
217 * Copies the entire BigBuffer into a single buffer.
218 */
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700219std::unique_ptr<uint8_t[]> Copy(const BigBuffer& buffer);
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800220
221/**
222 * A Tokenizer implemented as an iterable collection. It does not allocate
223 * any memory on the heap nor use standard containers.
224 */
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800225class Tokenizer {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700226 public:
227 class iterator {
228 public:
229 iterator(const iterator&) = default;
230 iterator& operator=(const iterator&) = default;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800231
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700232 iterator& operator++();
Adam Lesinskid0f116b2016-07-08 15:00:32 -0700233
Adam Lesinskid5083f62017-01-16 15:07:21 -0800234 android::StringPiece operator*() { return token_; }
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700235 bool operator==(const iterator& rhs) const;
236 bool operator!=(const iterator& rhs) const;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800237
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700238 private:
239 friend class Tokenizer;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800240
Adam Lesinskid5083f62017-01-16 15:07:21 -0800241 iterator(android::StringPiece s, char sep, android::StringPiece tok, bool end);
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800242
Adam Lesinskid5083f62017-01-16 15:07:21 -0800243 android::StringPiece str_;
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700244 char separator_;
Adam Lesinskid5083f62017-01-16 15:07:21 -0800245 android::StringPiece token_;
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700246 bool end_;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700247 };
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800248
Adam Lesinskid5083f62017-01-16 15:07:21 -0800249 Tokenizer(android::StringPiece str, char sep);
Adam Lesinskid0f116b2016-07-08 15:00:32 -0700250
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700251 iterator begin() { return begin_; }
Adam Lesinskid0f116b2016-07-08 15:00:32 -0700252
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700253 iterator end() { return end_; }
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800254
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700255 private:
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700256 const iterator begin_;
257 const iterator end_;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800258};
259
Adam Lesinskid5083f62017-01-16 15:07:21 -0800260inline Tokenizer Tokenize(const android::StringPiece& str, char sep) { return Tokenizer(str, sep); }
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800261
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700262inline uint16_t HostToDevice16(uint16_t value) { return htods(value); }
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700263
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700264inline uint32_t HostToDevice32(uint32_t value) { return htodl(value); }
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700265
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700266inline uint16_t DeviceToHost16(uint16_t value) { return dtohs(value); }
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700267
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700268inline uint32_t DeviceToHost32(uint32_t value) { return dtohl(value); }
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700269
Adam Lesinski24aad162015-04-24 19:19:30 -0700270/**
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700271 * Given a path like: res/xml-sw600dp/foo.xml
272 *
273 * Extracts "res/xml-sw600dp/" into outPrefix.
274 * Extracts "foo" into outEntry.
275 * Extracts ".xml" into outSuffix.
276 *
277 * Returns true if successful.
278 */
Adam Lesinskid5083f62017-01-16 15:07:21 -0800279bool ExtractResFilePathParts(const android::StringPiece& path, android::StringPiece* out_prefix,
280 android::StringPiece* out_entry, android::StringPiece* out_suffix);
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700281
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700282} // namespace util
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800283
284/**
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700285 * Stream operator for functions. Calls the function with the stream as an
286 * argument.
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800287 * In the aapt namespace for lookup.
288 */
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700289inline ::std::ostream& operator<<(
290 ::std::ostream& out,
291 const ::std::function<::std::ostream&(::std::ostream&)>& f) {
292 return f(out);
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800293}
294
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700295} // namespace aapt
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800296
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700297#endif // AAPT_UTIL_H