blob: f8fa80ed7d66b5b9bf0ac15b53b31b40fa0edf8c [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 Lesinskicacb28f2016-10-19 12:18:14 -0700102 * Makes a std::unique_ptr<> with the template parameter inferred by the
103 * compiler.
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800104 * This will be present in C++14 and can be removed then.
105 */
106template <typename T, class... Args>
107std::unique_ptr<T> make_unique(Args&&... args) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700108 return std::unique_ptr<T>(new T{std::forward<Args>(args)...});
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800109}
110
111/**
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700112 * Writes a set of items to the std::ostream, joining the times with the
113 * provided
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800114 * separator.
115 */
Adam Lesinski36c73a52016-08-11 13:39:24 -0700116template <typename Container>
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700117::std::function<::std::ostream&(::std::ostream&)> Joiner(
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700118 const Container& container, const char* sep) {
119 using std::begin;
120 using std::end;
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700121 const auto begin_iter = begin(container);
122 const auto end_iter = end(container);
123 return [begin_iter, end_iter, sep](::std::ostream& out) -> ::std::ostream& {
124 for (auto iter = begin_iter; iter != end_iter; ++iter) {
125 if (iter != begin_iter) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700126 out << sep;
127 }
128 out << *iter;
129 }
130 return out;
131 };
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800132}
133
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800134/**
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700135 * Helper method to extract a UTF-16 string from a StringPool. If the string is
136 * stored as UTF-8,
Adam Lesinskid0f116b2016-07-08 15:00:32 -0700137 * the conversion to UTF-16 happens within ResStringPool.
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800138 */
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 Lesinskid0f116b2016-07-08 15:00:32 -0700141/**
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700142 * Helper method to extract a UTF-8 string from a StringPool. If the string is
143 * stored as UTF-16,
144 * the conversion from UTF-16 to UTF-8 does not happen in ResStringPool and is
145 * done by this method,
146 * which maintains no state or cache. This means we must return an std::string
147 * copy.
Adam Lesinskid0f116b2016-07-08 15:00:32 -0700148 */
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700149std::string GetString(const android::ResStringPool& pool, size_t idx);
Adam Lesinski28cacf02015-11-23 14:22:47 -0800150
Adam Lesinskib23f1e02015-11-03 12:24:17 -0800151/**
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700152 * Checks that the Java string format contains no non-positional arguments
153 * (arguments without
154 * explicitly specifying an index) when there are more than one argument. This
155 * is an error
156 * because translations may rearrange the order of the arguments in the string,
157 * which will
Adam Lesinskib23f1e02015-11-03 12:24:17 -0800158 * break the string interpolation.
159 */
Adam Lesinskid5083f62017-01-16 15:07:21 -0800160bool VerifyJavaStringFormat(const android::StringPiece& str);
Adam Lesinskib23f1e02015-11-03 12:24:17 -0800161
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800162class StringBuilder {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700163 public:
Adam Lesinskid5083f62017-01-16 15:07:21 -0800164 StringBuilder& Append(const android::StringPiece& str);
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700165 const std::string& ToString() const;
166 const std::string& Error() 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 Lesinskice5e56e2016-10-21 17:56:45 -0700188inline size_t StringBuilder::Utf16Len() const { return utf16_len_; }
Adam Lesinski8c3f31f2016-09-07 13:45:13 -0700189
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700190inline StringBuilder::operator bool() const { return error_.empty(); }
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800191
192/**
193 * Converts a UTF8 string to a UTF16 string.
194 */
Adam Lesinskid5083f62017-01-16 15:07:21 -0800195std::u16string Utf8ToUtf16(const android::StringPiece& utf8);
196std::string Utf16ToUtf8(const android::StringPiece16& utf16);
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800197
198/**
199 * Writes the entire BigBuffer to the output stream.
200 */
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700201bool WriteAll(std::ostream& out, const BigBuffer& buffer);
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800202
203/*
204 * Copies the entire BigBuffer into a single buffer.
205 */
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700206std::unique_ptr<uint8_t[]> Copy(const BigBuffer& buffer);
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800207
208/**
209 * A Tokenizer implemented as an iterable collection. It does not allocate
210 * any memory on the heap nor use standard containers.
211 */
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800212class Tokenizer {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700213 public:
214 class iterator {
215 public:
216 iterator(const iterator&) = default;
217 iterator& operator=(const iterator&) = default;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800218
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700219 iterator& operator++();
Adam Lesinskid0f116b2016-07-08 15:00:32 -0700220
Adam Lesinskid5083f62017-01-16 15:07:21 -0800221 android::StringPiece operator*() { return token_; }
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700222 bool operator==(const iterator& rhs) const;
223 bool operator!=(const iterator& rhs) const;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800224
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700225 private:
226 friend class Tokenizer;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800227
Adam Lesinskid5083f62017-01-16 15:07:21 -0800228 iterator(android::StringPiece s, char sep, android::StringPiece tok, bool end);
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800229
Adam Lesinskid5083f62017-01-16 15:07:21 -0800230 android::StringPiece str_;
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700231 char separator_;
Adam Lesinskid5083f62017-01-16 15:07:21 -0800232 android::StringPiece token_;
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700233 bool end_;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700234 };
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800235
Adam Lesinskid5083f62017-01-16 15:07:21 -0800236 Tokenizer(android::StringPiece str, char sep);
Adam Lesinskid0f116b2016-07-08 15:00:32 -0700237
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700238 iterator begin() { return begin_; }
Adam Lesinskid0f116b2016-07-08 15:00:32 -0700239
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700240 iterator end() { return end_; }
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 Lesinskid5083f62017-01-16 15:07:21 -0800247inline Tokenizer Tokenize(const android::StringPiece& str, char sep) { return Tokenizer(str, sep); }
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800248
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700249inline uint16_t HostToDevice16(uint16_t value) { return htods(value); }
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700250
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700251inline uint32_t HostToDevice32(uint32_t value) { return htodl(value); }
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700252
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700253inline uint16_t DeviceToHost16(uint16_t value) { return dtohs(value); }
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700254
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700255inline uint32_t DeviceToHost32(uint32_t value) { return dtohl(value); }
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700256
Adam Lesinski24aad162015-04-24 19:19:30 -0700257/**
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700258 * Given a path like: res/xml-sw600dp/foo.xml
259 *
260 * Extracts "res/xml-sw600dp/" into outPrefix.
261 * Extracts "foo" into outEntry.
262 * Extracts ".xml" into outSuffix.
263 *
264 * Returns true if successful.
265 */
Adam Lesinskid5083f62017-01-16 15:07:21 -0800266bool ExtractResFilePathParts(const android::StringPiece& path, android::StringPiece* out_prefix,
267 android::StringPiece* out_entry, android::StringPiece* out_suffix);
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700268
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700269} // namespace util
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800270
271/**
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700272 * Stream operator for functions. Calls the function with the stream as an
273 * argument.
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800274 * In the aapt namespace for lookup.
275 */
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700276inline ::std::ostream& operator<<(
277 ::std::ostream& out,
278 const ::std::function<::std::ostream&(::std::ostream&)>& f) {
279 return f(out);
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800280}
281
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700282} // namespace aapt
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800283
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700284#endif // AAPT_UTIL_H