blob: 40ff5b633d978980325c177f135a15b436934a69 [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
Jeremy Meyer56f36e82022-05-20 20:35:42 +000026#include "androidfw/BigBuffer.h"
Adam Lesinskice5e56e2016-10-21 17:56:45 -070027#include "androidfw/ResourceTypes.h"
Adam Lesinskid5083f62017-01-16 15:07:21 -080028#include "androidfw/StringPiece.h"
Adam Lesinskice5e56e2016-10-21 17:56:45 -070029#include "utils/ByteOrder.h"
30
Adam Lesinskice5e56e2016-10-21 17:56:45 -070031#ifdef _WIN32
32// TODO(adamlesinski): remove once http://b/32447322 is resolved.
33// utils/ByteOrder.h includes winsock2.h on WIN32,
34// which will pull in the ERROR definition. This conflicts
35// with android-base/logging.h, which takes care of undefining
36// ERROR, but it gets included too early (before winsock2.h).
37#ifdef ERROR
38#undef ERROR
39#endif
40#endif
41
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080042namespace aapt {
43namespace util {
44
Adam Lesinskic744ae82017-05-17 19:28:38 -070045template <typename T>
46struct Range {
47 T start;
48 T end;
49};
50
Yurii Zubrytskyia5775142022-11-02 17:49:49 -070051std::vector<std::string> Split(android::StringPiece str, char sep);
52std::vector<std::string> SplitAndLowercase(android::StringPiece str, char sep);
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080053
Adam Lesinski96ea08f2017-11-06 10:44:46 -080054// Returns true if the string starts with prefix.
Yurii Zubrytskyia5775142022-11-02 17:49:49 -070055bool StartsWith(android::StringPiece str, android::StringPiece prefix);
Adam Lesinski4d3a9872015-04-09 19:53:22 -070056
Adam Lesinski96ea08f2017-11-06 10:44:46 -080057// Returns true if the string ends with suffix.
Yurii Zubrytskyia5775142022-11-02 17:49:49 -070058bool EndsWith(android::StringPiece str, android::StringPiece suffix);
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080059
Adam Lesinski2eed52e2018-02-21 15:55:58 -080060// Creates a new StringPiece that points to a substring of the original string without leading
61// whitespace.
Yurii Zubrytskyia5775142022-11-02 17:49:49 -070062android::StringPiece TrimLeadingWhitespace(android::StringPiece str);
Adam Lesinski2eed52e2018-02-21 15:55:58 -080063
64// Creates a new StringPiece that points to a substring of the original string without trailing
65// whitespace.
Yurii Zubrytskyia5775142022-11-02 17:49:49 -070066android::StringPiece TrimTrailingWhitespace(android::StringPiece str);
Adam Lesinski2eed52e2018-02-21 15:55:58 -080067
68// Creates a new StringPiece that points to a substring of the original string without leading or
Adam Lesinski96ea08f2017-11-06 10:44:46 -080069// trailing whitespace.
Yurii Zubrytskyia5775142022-11-02 17:49:49 -070070android::StringPiece TrimWhitespace(android::StringPiece str);
Adam Lesinski3b4cd942015-10-30 16:31:42 -070071
Adam Lesinski96ea08f2017-11-06 10:44:46 -080072// Tests that the string is a valid Java class name.
Yurii Zubrytskyia5775142022-11-02 17:49:49 -070073bool IsJavaClassName(android::StringPiece str);
Adam Lesinskia1ad4a82015-06-08 11:41:09 -070074
Adam Lesinski96ea08f2017-11-06 10:44:46 -080075// Tests that the string is a valid Java package name.
Yurii Zubrytskyia5775142022-11-02 17:49:49 -070076bool IsJavaPackageName(android::StringPiece str);
Adam Lesinski1ab598f2015-08-14 14:26:04 -070077
Adam Lesinski96ea08f2017-11-06 10:44:46 -080078// Tests that the string is a valid Android package name. More strict than a Java package name.
79// - First character of each component (separated by '.') must be an ASCII letter.
80// - Subsequent characters of a component can be ASCII alphanumeric or an underscore.
81// - Package must contain at least two components, unless it is 'android'.
Rhed Jao2c434422020-11-12 10:48:03 +080082// - The maximum package name length is 223.
Yurii Zubrytskyia5775142022-11-02 17:49:49 -070083bool IsAndroidPackageName(android::StringPiece str);
Adam Lesinski96ea08f2017-11-06 10:44:46 -080084
85// Tests that the string is a valid Android split name.
86// - First character of each component (separated by '.') must be an ASCII letter.
87// - Subsequent characters of a component can be ASCII alphanumeric or an underscore.
Yurii Zubrytskyia5775142022-11-02 17:49:49 -070088bool IsAndroidSplitName(android::StringPiece str);
Adam Lesinski96ea08f2017-11-06 10:44:46 -080089
Rhed Jao2c434422020-11-12 10:48:03 +080090// Tests that the string is a valid Android shared user id.
91// - First character of each component (separated by '.') must be an ASCII letter.
92// - Subsequent characters of a component can be ASCII alphanumeric or an underscore.
93// - Must contain at least two components, unless package name is 'android'.
94// - The maximum shared user id length is 223.
95// - Treat empty string as valid, it's the case of no shared user id.
Yurii Zubrytskyia5775142022-11-02 17:49:49 -070096bool IsAndroidSharedUserId(android::StringPiece package_name, android::StringPiece shared_user_id);
Rhed Jao2c434422020-11-12 10:48:03 +080097
Adam Lesinski96ea08f2017-11-06 10:44:46 -080098// Converts the class name to a fully qualified class name from the given
99// `package`. Ex:
100//
101// asdf --> package.asdf
102// .asdf --> package.asdf
103// .a.b --> package.a.b
104// asdf.adsf --> asdf.adsf
Yurii Zubrytskyia5775142022-11-02 17:49:49 -0700105std::optional<std::string> GetFullyQualifiedClassName(android::StringPiece package,
106 android::StringPiece class_name);
Adam Lesinskia1ad4a82015-06-08 11:41:09 -0700107
Ryan Mitchell34039b22019-03-18 08:57:47 -0700108// Retrieves the formatted name of aapt2.
109const char* GetToolName();
110
111// Retrieves the build fingerprint of aapt2.
112std::string GetToolFingerprint();
113
Adam Lesinski060b53d2017-07-28 17:10:35 -0700114template <typename T>
115typename std::enable_if<std::is_arithmetic<T>::value, int>::type compare(const T& a, const T& b) {
116 if (a < b) {
117 return -1;
118 } else if (a > b) {
119 return 1;
120 }
121 return 0;
122}
123
Adam Lesinski96ea08f2017-11-06 10:44:46 -0800124// Makes a std::unique_ptr<> with the template parameter inferred by the compiler.
125// This will be present in C++14 and can be removed then.
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800126template <typename T, class... Args>
127std::unique_ptr<T> make_unique(Args&&... args) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700128 return std::unique_ptr<T>(new T{std::forward<Args>(args)...});
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800129}
130
Adam Lesinski96ea08f2017-11-06 10:44:46 -0800131// Writes a set of items to the std::ostream, joining the times with the provided separator.
Adam Lesinski36c73a52016-08-11 13:39:24 -0700132template <typename Container>
Adam Lesinski96ea08f2017-11-06 10:44:46 -0800133::std::function<::std::ostream&(::std::ostream&)> Joiner(const Container& container,
134 const char* sep) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700135 using std::begin;
136 using std::end;
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700137 const auto begin_iter = begin(container);
138 const auto end_iter = end(container);
139 return [begin_iter, end_iter, sep](::std::ostream& out) -> ::std::ostream& {
140 for (auto iter = begin_iter; iter != end_iter; ++iter) {
141 if (iter != begin_iter) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700142 out << sep;
143 }
144 out << *iter;
145 }
146 return out;
147 };
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800148}
149
Adam Lesinski96ea08f2017-11-06 10:44:46 -0800150// Checks that the Java string format contains no non-positional arguments (arguments without
151// explicitly specifying an index) when there are more than one argument. This is an error
152// because translations may rearrange the order of the arguments in the string, which will
153// break the string interpolation.
Yurii Zubrytskyia5775142022-11-02 17:49:49 -0700154bool VerifyJavaStringFormat(android::StringPiece str);
Adam Lesinskib23f1e02015-11-03 12:24:17 -0800155
Yurii Zubrytskyia5775142022-11-02 17:49:49 -0700156bool AppendStyledString(android::StringPiece input, bool preserve_spaces, std::string* out_str,
157 std::string* out_error);
Adam Lesinski2eed52e2018-02-21 15:55:58 -0800158
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800159class StringBuilder {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700160 public:
Adam Lesinski2eed52e2018-02-21 15:55:58 -0800161 StringBuilder() = default;
Adam Lesinskifba0cf22017-06-29 17:53:36 -0700162
Yurii Zubrytskyia5775142022-11-02 17:49:49 -0700163 StringBuilder& Append(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 Lesinski96ea08f2017-11-06 10:44:46 -0800184inline const std::string& StringBuilder::ToString() const {
185 return str_;
186}
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800187
Adam Lesinski96ea08f2017-11-06 10:44:46 -0800188inline const std::string& StringBuilder::Error() const {
189 return error_;
190}
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800191
Adam Lesinski96ea08f2017-11-06 10:44:46 -0800192inline bool StringBuilder::IsEmpty() const {
193 return str_.empty();
194}
Adam Lesinskiac6edc52017-03-02 19:31:28 -0800195
Adam Lesinski96ea08f2017-11-06 10:44:46 -0800196inline size_t StringBuilder::Utf16Len() const {
197 return utf16_len_;
198}
Adam Lesinski8c3f31f2016-09-07 13:45:13 -0700199
Adam Lesinski96ea08f2017-11-06 10:44:46 -0800200inline StringBuilder::operator bool() const {
201 return error_.empty();
202}
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800203
Adam Lesinski96ea08f2017-11-06 10:44:46 -0800204// Writes the entire BigBuffer to the output stream.
Jeremy Meyer56f36e82022-05-20 20:35:42 +0000205bool WriteAll(std::ostream& out, const android::BigBuffer& buffer);
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800206
Adam Lesinski96ea08f2017-11-06 10:44:46 -0800207// A Tokenizer implemented as an iterable collection. It does not allocate any memory on the heap
208// nor use standard containers.
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800209class Tokenizer {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700210 public:
211 class iterator {
212 public:
Adam Lesinski448a15c2017-07-25 10:59:26 -0700213 using reference = android::StringPiece&;
214 using value_type = android::StringPiece;
215 using difference_type = size_t;
216 using pointer = android::StringPiece*;
217 using iterator_category = std::forward_iterator_tag;
218
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700219 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
Yurii Zubrytskyia5775142022-11-02 17:49:49 -0700231 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
Yurii Zubrytskyia5775142022-11-02 17:49:49 -0700239 Tokenizer(android::StringPiece str, char sep);
Adam Lesinskid0f116b2016-07-08 15:00:32 -0700240
Adam Lesinski448a15c2017-07-25 10:59:26 -0700241 iterator begin() const {
242 return begin_;
243 }
Adam Lesinskid0f116b2016-07-08 15:00:32 -0700244
Adam Lesinski448a15c2017-07-25 10:59:26 -0700245 iterator end() const {
246 return end_;
247 }
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800248
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700249 private:
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700250 const iterator begin_;
251 const iterator end_;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800252};
253
Yurii Zubrytskyia5775142022-11-02 17:49:49 -0700254inline Tokenizer Tokenize(android::StringPiece str, char sep) {
Adam Lesinski96ea08f2017-11-06 10:44:46 -0800255 return Tokenizer(str, sep);
256}
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800257
Adam Lesinski96ea08f2017-11-06 10:44:46 -0800258// 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.
Yurii Zubrytskyia5775142022-11-02 17:49:49 -0700265bool ExtractResFilePathParts(android::StringPiece path, android::StringPiece* out_prefix,
Adam Lesinskid5083f62017-01-16 15:07:21 -0800266 android::StringPiece* out_entry, android::StringPiece* out_suffix);
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700267
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700268} // namespace util
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800269
Jeremy Meyer56f36e82022-05-20 20:35:42 +0000270} // namespace aapt
271
272namespace std {
Adam Lesinski96ea08f2017-11-06 10:44:46 -0800273// Stream operator for functions. Calls the function with the stream as an argument.
274// In the aapt namespace for lookup.
275inline ::std::ostream& operator<<(::std::ostream& out,
276 const ::std::function<::std::ostream&(::std::ostream&)>& f) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700277 return f(out);
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800278}
Jeremy Meyer56f36e82022-05-20 20:35:42 +0000279} // namespace std
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800280
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700281#endif // AAPT_UTIL_H