blob: 7210d21f64ab65b37e4ab3dcaedbb325ed6bdc0f [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 Lesinskiac6edc52017-03-02 19:31:28 -0800167 bool IsEmpty() const;
Adam Lesinski8c3f31f2016-09-07 13:45:13 -0700168
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700169 // When building StyledStrings, we need UTF-16 indices into the string,
170 // which is what the Java layer expects when dealing with java
171 // String.charAt().
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700172 size_t Utf16Len() const;
Adam Lesinski8c3f31f2016-09-07 13:45:13 -0700173
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700174 explicit operator bool() const;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800175
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700176 private:
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700177 std::string str_;
178 size_t utf16_len_ = 0;
179 bool quote_ = false;
180 bool trailing_space_ = false;
181 bool last_char_was_escape_ = false;
182 std::string error_;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800183};
184
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700185inline const std::string& StringBuilder::ToString() const { return str_; }
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800186
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700187inline const std::string& StringBuilder::Error() const { return error_; }
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800188
Adam Lesinskiac6edc52017-03-02 19:31:28 -0800189inline bool StringBuilder::IsEmpty() const { return str_.empty(); }
190
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700191inline size_t StringBuilder::Utf16Len() const { return utf16_len_; }
Adam Lesinski8c3f31f2016-09-07 13:45:13 -0700192
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700193inline StringBuilder::operator bool() const { return error_.empty(); }
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800194
195/**
196 * Converts a UTF8 string to a UTF16 string.
197 */
Adam Lesinskid5083f62017-01-16 15:07:21 -0800198std::u16string Utf8ToUtf16(const android::StringPiece& utf8);
199std::string Utf16ToUtf8(const android::StringPiece16& utf16);
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800200
201/**
202 * Writes the entire BigBuffer to the output stream.
203 */
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700204bool WriteAll(std::ostream& out, const BigBuffer& buffer);
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800205
206/*
207 * Copies the entire BigBuffer into a single buffer.
208 */
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700209std::unique_ptr<uint8_t[]> Copy(const BigBuffer& buffer);
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800210
211/**
212 * A Tokenizer implemented as an iterable collection. It does not allocate
213 * any memory on the heap nor use standard containers.
214 */
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800215class Tokenizer {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700216 public:
217 class iterator {
218 public:
219 iterator(const iterator&) = default;
220 iterator& operator=(const iterator&) = default;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800221
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700222 iterator& operator++();
Adam Lesinskid0f116b2016-07-08 15:00:32 -0700223
Adam Lesinskid5083f62017-01-16 15:07:21 -0800224 android::StringPiece operator*() { return token_; }
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700225 bool operator==(const iterator& rhs) const;
226 bool operator!=(const iterator& rhs) const;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800227
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700228 private:
229 friend class Tokenizer;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800230
Adam Lesinskid5083f62017-01-16 15:07:21 -0800231 iterator(android::StringPiece s, char sep, android::StringPiece tok, bool end);
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800232
Adam Lesinskid5083f62017-01-16 15:07:21 -0800233 android::StringPiece str_;
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700234 char separator_;
Adam Lesinskid5083f62017-01-16 15:07:21 -0800235 android::StringPiece token_;
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700236 bool end_;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700237 };
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800238
Adam Lesinskid5083f62017-01-16 15:07:21 -0800239 Tokenizer(android::StringPiece str, char sep);
Adam Lesinskid0f116b2016-07-08 15:00:32 -0700240
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700241 iterator begin() { return begin_; }
Adam Lesinskid0f116b2016-07-08 15:00:32 -0700242
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700243 iterator end() { return end_; }
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800244
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700245 private:
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700246 const iterator begin_;
247 const iterator end_;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800248};
249
Adam Lesinskid5083f62017-01-16 15:07:21 -0800250inline Tokenizer Tokenize(const android::StringPiece& str, char sep) { return Tokenizer(str, sep); }
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800251
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700252inline uint16_t HostToDevice16(uint16_t value) { return htods(value); }
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700253
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700254inline uint32_t HostToDevice32(uint32_t value) { return htodl(value); }
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700255
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700256inline uint16_t DeviceToHost16(uint16_t value) { return dtohs(value); }
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700257
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700258inline uint32_t DeviceToHost32(uint32_t value) { return dtohl(value); }
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700259
Adam Lesinski24aad162015-04-24 19:19:30 -0700260/**
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700261 * Given a path like: res/xml-sw600dp/foo.xml
262 *
263 * Extracts "res/xml-sw600dp/" into outPrefix.
264 * Extracts "foo" into outEntry.
265 * Extracts ".xml" into outSuffix.
266 *
267 * Returns true if successful.
268 */
Adam Lesinskid5083f62017-01-16 15:07:21 -0800269bool ExtractResFilePathParts(const android::StringPiece& path, android::StringPiece* out_prefix,
270 android::StringPiece* out_entry, android::StringPiece* out_suffix);
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700271
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700272} // namespace util
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800273
274/**
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700275 * Stream operator for functions. Calls the function with the stream as an
276 * argument.
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800277 * In the aapt namespace for lookup.
278 */
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700279inline ::std::ostream& operator<<(
280 ::std::ostream& out,
281 const ::std::function<::std::ostream&(::std::ostream&)>& f) {
282 return f(out);
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800283}
284
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700285} // namespace aapt
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800286
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700287#endif // AAPT_UTIL_H