blob: b9ada7704a26de05276f46f7f8fcd258b08a2eee [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;
51};
52
Adam Lesinskid5083f62017-01-16 15:07:21 -080053std::vector<std::string> Split(const android::StringPiece& str, char sep);
54std::vector<std::string> SplitAndLowercase(const android::StringPiece& str, char sep);
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080055
56/**
Adam Lesinski4d3a9872015-04-09 19:53:22 -070057 * Returns true if the string starts with prefix.
58 */
Adam Lesinskid5083f62017-01-16 15:07:21 -080059bool StartsWith(const android::StringPiece& str, const android::StringPiece& prefix);
Adam Lesinski4d3a9872015-04-09 19:53:22 -070060
61/**
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080062 * Returns true if the string ends with suffix.
63 */
Adam Lesinskid5083f62017-01-16 15:07:21 -080064bool EndsWith(const android::StringPiece& str, const android::StringPiece& suffix);
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080065
66/**
67 * Creates a new StringPiece16 that points to a substring
68 * of the original string without leading or trailing whitespace.
69 */
Adam Lesinskid5083f62017-01-16 15:07:21 -080070android::StringPiece TrimWhitespace(const android::StringPiece& str);
Adam Lesinski3b4cd942015-10-30 16:31:42 -070071
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080072/**
73 * UTF-16 isspace(). It basically checks for lower range characters that are
74 * whitespace.
75 */
Adam Lesinskicacb28f2016-10-19 12:18:14 -070076inline bool isspace16(char16_t c) { return c < 0x0080 && isspace(c); }
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080077
78/**
79 * Returns an iterator to the first character that is not alpha-numeric and that
80 * is not in the allowedChars set.
81 */
Adam Lesinskid5083f62017-01-16 15:07:21 -080082android::StringPiece::const_iterator FindNonAlphaNumericAndNotInSet(
83 const android::StringPiece& str, const android::StringPiece& allowed_chars);
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080084
85/**
Adam Lesinskia1ad4a82015-06-08 11:41:09 -070086 * Tests that the string is a valid Java class name.
87 */
Adam Lesinskid5083f62017-01-16 15:07:21 -080088bool IsJavaClassName(const android::StringPiece& str);
Adam Lesinskia1ad4a82015-06-08 11:41:09 -070089
90/**
Adam Lesinski1ab598f2015-08-14 14:26:04 -070091 * Tests that the string is a valid Java package name.
92 */
Adam Lesinskid5083f62017-01-16 15:07:21 -080093bool IsJavaPackageName(const android::StringPiece& str);
Adam Lesinski1ab598f2015-08-14 14:26:04 -070094
95/**
Adam Lesinskicacb28f2016-10-19 12:18:14 -070096 * Converts the class name to a fully qualified class name from the given
97 * `package`. Ex:
Adam Lesinskia1ad4a82015-06-08 11:41:09 -070098 *
99 * asdf --> package.asdf
100 * .asdf --> package.asdf
101 * .a.b --> package.a.b
102 * asdf.adsf --> asdf.adsf
103 */
Adam Lesinskid5083f62017-01-16 15:07:21 -0800104Maybe<std::string> GetFullyQualifiedClassName(const android::StringPiece& package,
105 const android::StringPiece& class_name);
Adam Lesinskia1ad4a82015-06-08 11:41:09 -0700106
107/**
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700108 * Makes a std::unique_ptr<> with the template parameter inferred by the compiler.
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800109 * This will be present in C++14 and can be removed then.
110 */
111template <typename T, class... Args>
112std::unique_ptr<T> make_unique(Args&&... args) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700113 return std::unique_ptr<T>(new T{std::forward<Args>(args)...});
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800114}
115
116/**
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700117 * Writes a set of items to the std::ostream, joining the times with the
118 * provided
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800119 * separator.
120 */
Adam Lesinski36c73a52016-08-11 13:39:24 -0700121template <typename Container>
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700122::std::function<::std::ostream&(::std::ostream&)> Joiner(
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700123 const Container& container, const char* sep) {
124 using std::begin;
125 using std::end;
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700126 const auto begin_iter = begin(container);
127 const auto end_iter = end(container);
128 return [begin_iter, end_iter, sep](::std::ostream& out) -> ::std::ostream& {
129 for (auto iter = begin_iter; iter != end_iter; ++iter) {
130 if (iter != begin_iter) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700131 out << sep;
132 }
133 out << *iter;
134 }
135 return out;
136 };
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800137}
138
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800139/**
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700140 * Helper method to extract a UTF-16 string from a StringPool. If the string is
141 * stored as UTF-8,
Adam Lesinskid0f116b2016-07-08 15:00:32 -0700142 * the conversion to UTF-16 happens within ResStringPool.
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800143 */
Adam Lesinskid5083f62017-01-16 15:07:21 -0800144android::StringPiece16 GetString16(const android::ResStringPool& pool, size_t idx);
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800145
Adam Lesinskid0f116b2016-07-08 15:00:32 -0700146/**
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700147 * Helper method to extract a UTF-8 string from a StringPool. If the string is
148 * stored as UTF-16,
149 * the conversion from UTF-16 to UTF-8 does not happen in ResStringPool and is
150 * done by this method,
151 * which maintains no state or cache. This means we must return an std::string
152 * copy.
Adam Lesinskid0f116b2016-07-08 15:00:32 -0700153 */
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700154std::string GetString(const android::ResStringPool& pool, size_t idx);
Adam Lesinski28cacf02015-11-23 14:22:47 -0800155
Adam Lesinskib23f1e02015-11-03 12:24:17 -0800156/**
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700157 * Checks that the Java string format contains no non-positional arguments
158 * (arguments without
159 * explicitly specifying an index) when there are more than one argument. This
160 * is an error
161 * because translations may rearrange the order of the arguments in the string,
162 * which will
Adam Lesinskib23f1e02015-11-03 12:24:17 -0800163 * break the string interpolation.
164 */
Adam Lesinskid5083f62017-01-16 15:07:21 -0800165bool VerifyJavaStringFormat(const android::StringPiece& str);
Adam Lesinskib23f1e02015-11-03 12:24:17 -0800166
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800167class StringBuilder {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700168 public:
Adam Lesinskic8956882017-06-29 17:53:36 -0700169 explicit StringBuilder(bool preserve_spaces = false);
170
Adam Lesinskid5083f62017-01-16 15:07:21 -0800171 StringBuilder& Append(const android::StringPiece& str);
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700172 const std::string& ToString() const;
173 const std::string& Error() const;
Adam Lesinskiac6edc52017-03-02 19:31:28 -0800174 bool IsEmpty() const;
Adam Lesinski8c3f31f2016-09-07 13:45:13 -0700175
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700176 // When building StyledStrings, we need UTF-16 indices into the string,
177 // which is what the Java layer expects when dealing with java
178 // String.charAt().
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700179 size_t Utf16Len() const;
Adam Lesinski8c3f31f2016-09-07 13:45:13 -0700180
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700181 explicit operator bool() const;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800182
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700183 private:
Adam Lesinskic8956882017-06-29 17:53:36 -0700184 bool preserve_spaces_;
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700185 std::string str_;
186 size_t utf16_len_ = 0;
187 bool quote_ = false;
188 bool trailing_space_ = false;
189 bool last_char_was_escape_ = false;
190 std::string error_;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800191};
192
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700193inline const std::string& StringBuilder::ToString() const { return str_; }
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800194
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700195inline const std::string& StringBuilder::Error() const { return error_; }
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800196
Adam Lesinskiac6edc52017-03-02 19:31:28 -0800197inline bool StringBuilder::IsEmpty() const { return str_.empty(); }
198
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700199inline size_t StringBuilder::Utf16Len() const { return utf16_len_; }
Adam Lesinski8c3f31f2016-09-07 13:45:13 -0700200
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700201inline StringBuilder::operator bool() const { return error_.empty(); }
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800202
203/**
204 * Converts a UTF8 string to a UTF16 string.
205 */
Adam Lesinskid5083f62017-01-16 15:07:21 -0800206std::u16string Utf8ToUtf16(const android::StringPiece& utf8);
207std::string Utf16ToUtf8(const android::StringPiece16& utf16);
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800208
209/**
210 * Writes the entire BigBuffer to the output stream.
211 */
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700212bool WriteAll(std::ostream& out, const BigBuffer& buffer);
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800213
214/*
215 * Copies the entire BigBuffer into a single buffer.
216 */
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700217std::unique_ptr<uint8_t[]> Copy(const BigBuffer& buffer);
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800218
219/**
220 * A Tokenizer implemented as an iterable collection. It does not allocate
221 * any memory on the heap nor use standard containers.
222 */
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800223class Tokenizer {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700224 public:
225 class iterator {
226 public:
227 iterator(const iterator&) = default;
228 iterator& operator=(const iterator&) = default;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800229
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700230 iterator& operator++();
Adam Lesinskid0f116b2016-07-08 15:00:32 -0700231
Adam Lesinskid5083f62017-01-16 15:07:21 -0800232 android::StringPiece operator*() { return token_; }
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700233 bool operator==(const iterator& rhs) const;
234 bool operator!=(const iterator& rhs) const;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800235
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700236 private:
237 friend class Tokenizer;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800238
Adam Lesinskid5083f62017-01-16 15:07:21 -0800239 iterator(android::StringPiece s, char sep, android::StringPiece tok, bool end);
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800240
Adam Lesinskid5083f62017-01-16 15:07:21 -0800241 android::StringPiece str_;
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700242 char separator_;
Adam Lesinskid5083f62017-01-16 15:07:21 -0800243 android::StringPiece token_;
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700244 bool end_;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700245 };
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800246
Adam Lesinskid5083f62017-01-16 15:07:21 -0800247 Tokenizer(android::StringPiece str, char sep);
Adam Lesinskid0f116b2016-07-08 15:00:32 -0700248
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700249 iterator begin() { return begin_; }
Adam Lesinskid0f116b2016-07-08 15:00:32 -0700250
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700251 iterator end() { return end_; }
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800252
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700253 private:
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700254 const iterator begin_;
255 const iterator end_;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800256};
257
Adam Lesinskid5083f62017-01-16 15:07:21 -0800258inline Tokenizer Tokenize(const android::StringPiece& str, char sep) { return Tokenizer(str, sep); }
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800259
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700260inline uint16_t HostToDevice16(uint16_t value) { return htods(value); }
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700261
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700262inline uint32_t HostToDevice32(uint32_t value) { return htodl(value); }
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700263
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700264inline uint16_t DeviceToHost16(uint16_t value) { return dtohs(value); }
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700265
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700266inline uint32_t DeviceToHost32(uint32_t value) { return dtohl(value); }
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700267
Adam Lesinski24aad162015-04-24 19:19:30 -0700268/**
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700269 * Given a path like: res/xml-sw600dp/foo.xml
270 *
271 * Extracts "res/xml-sw600dp/" into outPrefix.
272 * Extracts "foo" into outEntry.
273 * Extracts ".xml" into outSuffix.
274 *
275 * Returns true if successful.
276 */
Adam Lesinskid5083f62017-01-16 15:07:21 -0800277bool ExtractResFilePathParts(const android::StringPiece& path, android::StringPiece* out_prefix,
278 android::StringPiece* out_entry, android::StringPiece* out_suffix);
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700279
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700280} // namespace util
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800281
282/**
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700283 * Stream operator for functions. Calls the function with the stream as an
284 * argument.
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800285 * In the aapt namespace for lookup.
286 */
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700287inline ::std::ostream& operator<<(
288 ::std::ostream& out,
289 const ::std::function<::std::ostream&(::std::ostream&)>& f) {
290 return f(out);
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800291}
292
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700293} // namespace aapt
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800294
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700295#endif // AAPT_UTIL_H