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