blob: 7adaf1ef6a92a001c2fd5beaaf3e0203ec27d394 [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#include "BigBuffer.h"
18#include "Maybe.h"
19#include "StringPiece.h"
20#include "Util.h"
21
22#include <algorithm>
23#include <ostream>
24#include <string>
25#include <utils/Unicode.h>
26#include <vector>
27
28namespace aapt {
29namespace util {
30
Adam Lesinski24aad162015-04-24 19:19:30 -070031constexpr const char16_t* kSchemaAuto = u"http://schemas.android.com/apk/res-auto";
32constexpr const char16_t* kSchemaPrefix = u"http://schemas.android.com/apk/res/";
33
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080034static std::vector<std::string> splitAndTransform(const StringPiece& str, char sep,
35 const std::function<char(char)>& f) {
36 std::vector<std::string> parts;
37 const StringPiece::const_iterator end = std::end(str);
38 StringPiece::const_iterator start = std::begin(str);
39 StringPiece::const_iterator current;
40 do {
41 current = std::find(start, end, sep);
42 parts.emplace_back(str.substr(start, current).toString());
43 if (f) {
44 std::string& part = parts.back();
45 std::transform(part.begin(), part.end(), part.begin(), f);
46 }
47 start = current + 1;
48 } while (current != end);
49 return parts;
50}
51
52std::vector<std::string> split(const StringPiece& str, char sep) {
53 return splitAndTransform(str, sep, nullptr);
54}
55
56std::vector<std::string> splitAndLowercase(const StringPiece& str, char sep) {
57 return splitAndTransform(str, sep, ::tolower);
58}
59
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080060StringPiece16 trimWhitespace(const StringPiece16& str) {
61 if (str.size() == 0 || str.data() == nullptr) {
62 return str;
63 }
64
65 const char16_t* start = str.data();
66 const char16_t* end = str.data() + str.length();
67
68 while (start != end && util::isspace16(*start)) {
69 start++;
70 }
71
72 while (end != start && util::isspace16(*(end - 1))) {
73 end--;
74 }
75
76 return StringPiece16(start, end - start);
77}
78
79StringPiece16::const_iterator findNonAlphaNumericAndNotInSet(const StringPiece16& str,
80 const StringPiece16& allowedChars) {
81 const auto endIter = str.end();
82 for (auto iter = str.begin(); iter != endIter; ++iter) {
83 char16_t c = *iter;
84 if ((c >= u'a' && c <= u'z') ||
85 (c >= u'A' && c <= u'Z') ||
86 (c >= u'0' && c <= u'9')) {
87 continue;
88 }
89
90 bool match = false;
91 for (char16_t i : allowedChars) {
92 if (c == i) {
93 match = true;
94 break;
95 }
96 }
97
98 if (!match) {
99 return iter;
100 }
101 }
102 return endIter;
103}
104
105static Maybe<char16_t> parseUnicodeCodepoint(const char16_t** start, const char16_t* end) {
106 char16_t code = 0;
107 for (size_t i = 0; i < 4 && *start != end; i++, (*start)++) {
108 char16_t c = **start;
109 int a;
110 if (c >= '0' && c <= '9') {
111 a = c - '0';
112 } else if (c >= 'a' && c <= 'f') {
113 a = c - 'a' + 10;
114 } else if (c >= 'A' && c <= 'F') {
115 a = c - 'A' + 10;
116 } else {
117 return make_nothing<char16_t>();
118 }
119 code = (code << 4) | a;
120 }
121 return make_value(code);
122}
123
124StringBuilder& StringBuilder::append(const StringPiece16& str) {
125 if (!mError.empty()) {
126 return *this;
127 }
128
129 const char16_t* const end = str.end();
130 const char16_t* start = str.begin();
131 const char16_t* current = start;
132 while (current != end) {
133 if (*current == u'"') {
134 if (!mQuote && mTrailingSpace) {
135 // We found an opening quote, and we have
136 // trailing space, so we should append that
137 // space now.
138 if (mTrailingSpace) {
139 // We had trailing whitespace, so
140 // replace with a single space.
141 if (!mStr.empty()) {
142 mStr += u' ';
143 }
144 mTrailingSpace = false;
145 }
146 }
147 mQuote = !mQuote;
148 mStr.append(start, current - start);
149 start = current + 1;
150 } else if (*current == u'\'' && !mQuote) {
151 // This should be escaped.
152 mError = "unescaped apostrophe";
153 return *this;
154 } else if (*current == u'\\') {
155 // This is an escape sequence, convert to the real value.
156 if (!mQuote && mTrailingSpace) {
157 // We had trailing whitespace, so
158 // replace with a single space.
159 if (!mStr.empty()) {
160 mStr += u' ';
161 }
162 mTrailingSpace = false;
163 }
164 mStr.append(start, current - start);
165 start = current + 1;
166
167 current++;
168 if (current != end) {
169 switch (*current) {
170 case u't':
171 mStr += u'\t';
172 break;
173 case u'n':
174 mStr += u'\n';
175 break;
176 case u'#':
177 mStr += u'#';
178 break;
179 case u'@':
180 mStr += u'@';
181 break;
182 case u'?':
183 mStr += u'?';
184 break;
185 case u'"':
186 mStr += u'"';
187 break;
188 case u'\'':
189 mStr += u'\'';
190 break;
191 case u'\\':
192 mStr += u'\\';
193 break;
194 case u'u': {
195 current++;
196 Maybe<char16_t> c = parseUnicodeCodepoint(&current, end);
197 if (!c) {
198 mError = "invalid unicode escape sequence";
199 return *this;
200 }
201 mStr += c.value();
202 current -= 1;
203 break;
204 }
205
206 default:
207 // Ignore.
208 break;
209 }
210 start = current + 1;
211 }
212 } else if (!mQuote) {
213 // This is not quoted text, so look for whitespace.
214 if (isspace16(*current)) {
215 // We found whitespace, see if we have seen some
216 // before.
217 if (!mTrailingSpace) {
218 // We didn't see a previous adjacent space,
219 // so mark that we did.
220 mTrailingSpace = true;
221 mStr.append(start, current - start);
222 }
223
224 // Keep skipping whitespace.
225 start = current + 1;
226 } else if (mTrailingSpace) {
227 // We saw trailing space before, so replace all
228 // that trailing space with one space.
229 if (!mStr.empty()) {
230 mStr += u' ';
231 }
232 mTrailingSpace = false;
233 }
234 }
235 current++;
236 }
237 mStr.append(start, end - start);
238 return *this;
239}
240
241std::u16string utf8ToUtf16(const StringPiece& utf8) {
242 ssize_t utf16Length = utf8_to_utf16_length(reinterpret_cast<const uint8_t*>(utf8.data()),
243 utf8.length());
244 if (utf16Length <= 0) {
245 return {};
246 }
247
248 std::u16string utf16;
249 utf16.resize(utf16Length);
250 utf8_to_utf16(reinterpret_cast<const uint8_t*>(utf8.data()), utf8.length(), &*utf16.begin());
251 return utf16;
252}
253
254std::string utf16ToUtf8(const StringPiece16& utf16) {
255 ssize_t utf8Length = utf16_to_utf8_length(utf16.data(), utf16.length());
256 if (utf8Length <= 0) {
257 return {};
258 }
259
260 std::string utf8;
261 utf8.resize(utf8Length);
262 utf16_to_utf8(utf16.data(), utf16.length(), &*utf8.begin());
263 return utf8;
264}
265
266bool writeAll(std::ostream& out, const BigBuffer& buffer) {
267 for (const auto& b : buffer) {
268 if (!out.write(reinterpret_cast<const char*>(b.buffer.get()), b.size)) {
269 return false;
270 }
271 }
272 return true;
273}
274
275std::unique_ptr<uint8_t[]> copy(const BigBuffer& buffer) {
276 std::unique_ptr<uint8_t[]> data = std::unique_ptr<uint8_t[]>(new uint8_t[buffer.size()]);
277 uint8_t* p = data.get();
278 for (const auto& block : buffer) {
279 memcpy(p, block.buffer.get(), block.size);
280 p += block.size;
281 }
282 return data;
283}
284
Adam Lesinski24aad162015-04-24 19:19:30 -0700285Maybe<std::u16string> extractPackageFromNamespace(const std::u16string& namespaceUri) {
286 if (stringStartsWith<char16_t>(namespaceUri, kSchemaPrefix)) {
287 StringPiece16 schemaPrefix = kSchemaPrefix;
288 StringPiece16 package = namespaceUri;
289 return package.substr(schemaPrefix.size(), package.size() - schemaPrefix.size())
290 .toString();
291 } else if (namespaceUri == kSchemaAuto) {
292 return std::u16string();
293 }
294 return {};
295}
296
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800297} // namespace util
298} // namespace aapt