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