blob: 30b9af61d8962d03df7fd3da25825467ee24b2ae [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 Lesinskid5083f62017-01-16 15:07:21 -080047std::vector<std::string> Split(const android::StringPiece& str, char sep);
48std::vector<std::string> SplitAndLowercase(const android::StringPiece& str, char sep);
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080049
50/**
Adam Lesinski4d3a9872015-04-09 19:53:22 -070051 * Returns true if the string starts with prefix.
52 */
Adam Lesinskid5083f62017-01-16 15:07:21 -080053bool StartsWith(const android::StringPiece& str, const android::StringPiece& prefix);
Adam Lesinski4d3a9872015-04-09 19:53:22 -070054
55/**
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080056 * Returns true if the string ends with suffix.
57 */
Adam Lesinskid5083f62017-01-16 15:07:21 -080058bool EndsWith(const android::StringPiece& str, const android::StringPiece& suffix);
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080059
60/**
61 * Creates a new StringPiece16 that points to a substring
62 * of the original string without leading or trailing whitespace.
63 */
Adam Lesinskid5083f62017-01-16 15:07:21 -080064android::StringPiece TrimWhitespace(const android::StringPiece& str);
Adam Lesinski3b4cd942015-10-30 16:31:42 -070065
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080066/**
67 * UTF-16 isspace(). It basically checks for lower range characters that are
68 * whitespace.
69 */
Adam Lesinskicacb28f2016-10-19 12:18:14 -070070inline bool isspace16(char16_t c) { return c < 0x0080 && isspace(c); }
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080071
72/**
73 * 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
101/**
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700102 * Makes a std::unique_ptr<> with the template parameter inferred by the compiler.
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800103 * This will be present in C++14 and can be removed then.
104 */
105template <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
110/**
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700111 * Writes a set of items to the std::ostream, joining the times with the
112 * provided
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800113 * separator.
114 */
Adam Lesinski36c73a52016-08-11 13:39:24 -0700115template <typename Container>
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700116::std::function<::std::ostream&(::std::ostream&)> Joiner(
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700117 const Container& container, const char* sep) {
118 using std::begin;
119 using std::end;
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700120 const auto begin_iter = begin(container);
121 const auto end_iter = end(container);
122 return [begin_iter, end_iter, sep](::std::ostream& out) -> ::std::ostream& {
123 for (auto iter = begin_iter; iter != end_iter; ++iter) {
124 if (iter != begin_iter) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700125 out << sep;
126 }
127 out << *iter;
128 }
129 return out;
130 };
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800131}
132
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800133/**
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700134 * Helper method to extract a UTF-16 string from a StringPool. If the string is
135 * stored as UTF-8,
Adam Lesinskid0f116b2016-07-08 15:00:32 -0700136 * the conversion to UTF-16 happens within ResStringPool.
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800137 */
Adam Lesinskid5083f62017-01-16 15:07:21 -0800138android::StringPiece16 GetString16(const android::ResStringPool& pool, size_t idx);
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800139
Adam Lesinskid0f116b2016-07-08 15:00:32 -0700140/**
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700141 * Helper method to extract a UTF-8 string from a StringPool. If the string is
142 * stored as UTF-16,
143 * the conversion from UTF-16 to UTF-8 does not happen in ResStringPool and is
144 * done by this method,
145 * which maintains no state or cache. This means we must return an std::string
146 * copy.
Adam Lesinskid0f116b2016-07-08 15:00:32 -0700147 */
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700148std::string GetString(const android::ResStringPool& pool, size_t idx);
Adam Lesinski28cacf02015-11-23 14:22:47 -0800149
Adam Lesinskib23f1e02015-11-03 12:24:17 -0800150/**
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700151 * Checks that the Java string format contains no non-positional arguments
152 * (arguments without
153 * explicitly specifying an index) when there are more than one argument. This
154 * is an error
155 * because translations may rearrange the order of the arguments in the string,
156 * which will
Adam Lesinskib23f1e02015-11-03 12:24:17 -0800157 * break the string interpolation.
158 */
Adam Lesinskid5083f62017-01-16 15:07:21 -0800159bool VerifyJavaStringFormat(const android::StringPiece& str);
Adam Lesinskib23f1e02015-11-03 12:24:17 -0800160
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800161class StringBuilder {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700162 public:
Adam Lesinskid5083f62017-01-16 15:07:21 -0800163 StringBuilder& Append(const android::StringPiece& str);
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700164 const std::string& ToString() const;
165 const std::string& Error() const;
Adam Lesinskiac6edc52017-03-02 19:31:28 -0800166 bool IsEmpty() const;
Adam Lesinski8c3f31f2016-09-07 13:45:13 -0700167
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700168 // When building StyledStrings, we need UTF-16 indices into the string,
169 // which is what the Java layer expects when dealing with java
170 // String.charAt().
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700171 size_t Utf16Len() const;
Adam Lesinski8c3f31f2016-09-07 13:45:13 -0700172
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700173 explicit operator bool() const;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800174
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700175 private:
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700176 std::string str_;
177 size_t utf16_len_ = 0;
178 bool quote_ = false;
179 bool trailing_space_ = false;
180 bool last_char_was_escape_ = false;
181 std::string error_;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800182};
183
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700184inline const std::string& StringBuilder::ToString() const { return str_; }
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800185
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700186inline const std::string& StringBuilder::Error() const { return error_; }
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800187
Adam Lesinskiac6edc52017-03-02 19:31:28 -0800188inline bool StringBuilder::IsEmpty() const { return str_.empty(); }
189
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700190inline size_t StringBuilder::Utf16Len() const { return utf16_len_; }
Adam Lesinski8c3f31f2016-09-07 13:45:13 -0700191
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700192inline StringBuilder::operator bool() const { return error_.empty(); }
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800193
194/**
195 * Converts a UTF8 string to a UTF16 string.
196 */
Adam Lesinskid5083f62017-01-16 15:07:21 -0800197std::u16string Utf8ToUtf16(const android::StringPiece& utf8);
198std::string Utf16ToUtf8(const android::StringPiece16& utf16);
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800199
200/**
201 * Writes the entire BigBuffer to the output stream.
202 */
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700203bool WriteAll(std::ostream& out, const BigBuffer& buffer);
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800204
205/*
206 * Copies the entire BigBuffer into a single buffer.
207 */
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700208std::unique_ptr<uint8_t[]> Copy(const BigBuffer& buffer);
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800209
210/**
211 * A Tokenizer implemented as an iterable collection. It does not allocate
212 * any memory on the heap nor use standard containers.
213 */
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800214class Tokenizer {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700215 public:
216 class iterator {
217 public:
218 iterator(const iterator&) = default;
219 iterator& operator=(const iterator&) = default;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800220
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700221 iterator& operator++();
Adam Lesinskid0f116b2016-07-08 15:00:32 -0700222
Adam Lesinskid5083f62017-01-16 15:07:21 -0800223 android::StringPiece operator*() { return token_; }
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700224 bool operator==(const iterator& rhs) const;
225 bool operator!=(const iterator& rhs) const;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800226
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700227 private:
228 friend class Tokenizer;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800229
Adam Lesinskid5083f62017-01-16 15:07:21 -0800230 iterator(android::StringPiece s, char sep, android::StringPiece tok, bool end);
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800231
Adam Lesinskid5083f62017-01-16 15:07:21 -0800232 android::StringPiece str_;
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700233 char separator_;
Adam Lesinskid5083f62017-01-16 15:07:21 -0800234 android::StringPiece token_;
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700235 bool end_;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700236 };
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800237
Adam Lesinskid5083f62017-01-16 15:07:21 -0800238 Tokenizer(android::StringPiece str, char sep);
Adam Lesinskid0f116b2016-07-08 15:00:32 -0700239
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700240 iterator begin() { return begin_; }
Adam Lesinskid0f116b2016-07-08 15:00:32 -0700241
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700242 iterator end() { return end_; }
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800243
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700244 private:
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700245 const iterator begin_;
246 const iterator end_;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800247};
248
Adam Lesinskid5083f62017-01-16 15:07:21 -0800249inline Tokenizer Tokenize(const android::StringPiece& str, char sep) { return Tokenizer(str, sep); }
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800250
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700251inline uint16_t HostToDevice16(uint16_t value) { return htods(value); }
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700252
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700253inline uint32_t HostToDevice32(uint32_t value) { return htodl(value); }
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700254
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700255inline uint16_t DeviceToHost16(uint16_t value) { return dtohs(value); }
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700256
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700257inline uint32_t DeviceToHost32(uint32_t value) { return dtohl(value); }
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700258
Adam Lesinski24aad162015-04-24 19:19:30 -0700259/**
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700260 * Given a path like: res/xml-sw600dp/foo.xml
261 *
262 * Extracts "res/xml-sw600dp/" into outPrefix.
263 * Extracts "foo" into outEntry.
264 * Extracts ".xml" into outSuffix.
265 *
266 * Returns true if successful.
267 */
Adam Lesinskid5083f62017-01-16 15:07:21 -0800268bool ExtractResFilePathParts(const android::StringPiece& path, android::StringPiece* out_prefix,
269 android::StringPiece* out_entry, android::StringPiece* out_suffix);
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700270
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700271} // namespace util
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800272
273/**
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700274 * Stream operator for functions. Calls the function with the stream as an
275 * argument.
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800276 * In the aapt namespace for lookup.
277 */
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700278inline ::std::ostream& operator<<(
279 ::std::ostream& out,
280 const ::std::function<::std::ostream&(::std::ostream&)>& f) {
281 return f(out);
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800282}
283
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700284} // namespace aapt
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800285
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700286#endif // AAPT_UTIL_H