|
| 1 | +/* |
| 2 | + * Copyright 2019 WebAssembly Community Group participants |
| 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 | +// |
| 18 | +// String helpers. |
| 19 | +// |
| 20 | + |
| 21 | +#ifndef wasm_support_string_h |
| 22 | +#define wasm_support_string_h |
| 23 | + |
| 24 | +#include <string> |
| 25 | +#include <vector> |
| 26 | + |
| 27 | +namespace wasm { |
| 28 | + |
| 29 | +namespace String { |
| 30 | + |
| 31 | +// Creates a vector of the split parts of a string, by a delimiter. |
| 32 | +class Split : public std::vector<std::string> { |
| 33 | +public: |
| 34 | + Split(const std::string& input, const std::string& delim) { |
| 35 | + size_t lastEnd = 0; |
| 36 | + while (lastEnd < input.size()) { |
| 37 | + auto nextDelim = input.find(delim, lastEnd); |
| 38 | + if (nextDelim == std::string::npos) { |
| 39 | + nextDelim = input.size(); |
| 40 | + } |
| 41 | + (*this).push_back(input.substr(lastEnd, nextDelim - lastEnd)); |
| 42 | + lastEnd = nextDelim + delim.size(); |
| 43 | + } |
| 44 | + } |
| 45 | +}; |
| 46 | + |
| 47 | +// Does a simple wildcard match between a pattern and a value. Currently |
| 48 | +// supports a '*' at the end of the pattern. |
| 49 | +inline bool wildcardMatch(const std::string& pattern, |
| 50 | + const std::string& value) { |
| 51 | + for (size_t i = 0; i < pattern.size(); i++) { |
| 52 | + if (i >= value.size()) { |
| 53 | + return false; |
| 54 | + } |
| 55 | + if (pattern[i] == '*') { |
| 56 | + return true; |
| 57 | + } |
| 58 | + if (pattern[i] != value[i]) { |
| 59 | + return false; |
| 60 | + } |
| 61 | + } |
| 62 | + return value.size() == pattern.size(); |
| 63 | +} |
| 64 | + |
| 65 | +} // namespace String |
| 66 | + |
| 67 | +} // namespace wasm |
| 68 | + |
| 69 | +#endif // wasm_support_string_h |
0 commit comments