namespace art { // A range of tokens to make token matching algorithms easier. // // We try really hard to avoid copying and store only a pointer and iterators to the // interiors of the vector, so a typical copy constructor never ends up doing a deep copy. // It is up to the user to play nice and not to mutate the strings in-place. // // Tokens are only copied if a mutating operation is performed (and even then only // if it *actually* mutates the token). struct TokenRange { // Short-hand for a vector of strings. A single string and a token is synonymous. using TokenList = std::vector<std::string>;
// Non-copying constructor. Retains reference to an existing list of tokens, with offset. explicit TokenRange(const std::shared_ptr<TokenList>& token_list)
: token_list_(token_list),
begin_(token_list_->begin()),
end_(token_list_->end())
{}
// Iterator type for begin() and end(). Guaranteed to be a RandomAccessIterator. using iterator = TokenList::const_iterator;
// Iterator type for const begin() and const end(). Guaranteed to be a RandomAccessIterator. using const_iterator = iterator;
// Create a token range by splitting a string. Each separator gets their own token. // Since the separator are retained as tokens, it might be useful to call // RemoveToken afterwards. static TokenRange Split(const std::string& string, std::initializer_list<char> separators) {
TokenList new_token_list;
std::string tok; for (auto&& c : string) { for (char sep : separators) { if (c == sep) { // We spotted a separator character. // Push back everything before the last separator as a new token. // Push back the separator as a token. if (!tok.empty()) {
new_token_list.push_back(tok);
tok = "";
}
new_token_list.push_back(std::string() + sep);
} else { // Build up the token with another character.
tok += c;
}
}
}
if (!tok.empty()) {
new_token_list.push_back(tok);
}
return TokenRange(std::move(new_token_list));
}
// A RandomAccessIterator to the first element in this range.
iterator begin() const { return begin_;
}
// A RandomAccessIterator to one past the last element in this range.
iterator end() const { return end_;
}
// The size of the range, i.e. how many tokens are in it.
size_t Size() const { return std::distance(begin_, end_);
}
// Are there 0 tokens in this range? bool IsEmpty() const { return Size() > 0;
}
// Look up a token by it's offset. const std::string& GetToken(size_t offset) const {
assert(offset < Size()); return *(begin_ + offset);
}
// Does this token range equal the other range? // Equality is defined as having both the same size, and // each corresponding token being equal. booloperator==(const TokenRange& other) const { if (this == &other) { returntrue;
}
// Remove all characters 'c' from each token, potentially copying the underlying tokens.
TokenRange RemoveCharacter(char c) const {
TokenList new_token_list(begin(), end());
bool changed = false; for (auto&& token : new_token_list) { auto it = std::remove_if(token.begin(), token.end(), [&](char ch) { if (ch == c) {
changed = true; returntrue;
} returnfalse;
});
token.erase(it, token.end());
}
if (!changed) { return *this;
}
return TokenRange(std::move(new_token_list));
}
// Remove all tokens matching this one, potentially copying the underlying tokens.
TokenRange RemoveToken(const std::string& token) { return RemoveIf([&](const std::string& tok) { return tok == token; });
}
// Create a non-copying subset of this range. // Length is trimmed so that the Slice does not go out of range.
TokenRange Slice(size_t offset, size_t length = std::string::npos) const {
assert(offset < Size());
// Try to match the string with tokens from this range. // Each token is used to match exactly once (after which the next token is used, and so on). // The matching happens from left-to-right in a non-greedy fashion. // If the currently-matched token is the wildcard, then the new outputted token will // contain as much as possible until the next token is matched. // // For example, if this == ["a:", "_", "b:] and "_" is the match string, then // MatchSubstrings on "a:foob:" will yield: ["a:", "foo", "b:"] // // Since the string matching can fail (e.g. ["foo"] against "bar"), then this // function can fail, in which cause it will return null.
std::unique_ptr<TokenRange> MatchSubstrings(const std::string& string, const std::string& wildcard) const {
TokenList new_token_list;
// Function to push all the characters matched as a wildcard so far // as a brand new token. It resets the wildcard matching. // Empty wildcards are possible and ok, but only if wildcard matching was on. auto maybe_push_wildcard_token = [&]() { if (wildcard_idx != std::string::npos) {
size_t wildcard_length = string_idx - wildcard_idx;
std::string wildcard_substr = string.substr(wildcard_idx, wildcard_length);
new_token_list.push_back(std::move(wildcard_substr));
wildcard_idx = std::string::npos;
}
};
for (iterator it = begin(); it != end(); ++it) { const std::string& tok = *it;
size_t next_token_idx = string.find(tok); if (next_token_idx == std::string::npos) { // Could not find token at all return nullptr;
} elseif (next_token_idx != string_idx && wildcard_idx == std::string::npos) { // Found the token at a non-starting location, and we weren't // trying to parse the wildcard. return nullptr;
}
size_t remaining = string.size() - string_idx; if (remaining > 0) { if (wildcard_idx == std::string::npos) { // Some characters were still remaining in the string, // but it wasn't trying to match a wildcard. return nullptr;
}
}
// If some characters are remaining, the rest must be a wildcard.
string_idx += remaining;
maybe_push_wildcard_token();
// Do a quick match token-by-token, and see if they match. // Any tokens with a wildcard in them are only matched up until the wildcard. // If this is true, then the wildcard matching later on can still fail, so this is not // a guarantee that the argument is correct, it's more of a strong hint that the // user-provided input *probably* was trying to match this argument. // // Returns how many tokens were either matched (or ignored because there was a // wildcard present). 0 means no match. If the size() tokens are returned.
size_t MaybeMatches(const TokenRange& token_list, const std::string& wildcard) const { auto token_it = token_list.begin(); auto token_end = token_list.end(); auto name_it = begin(); auto name_end = end();
size_t matched_tokens = 0;
while (token_it != token_end && name_it != name_end) { // Skip token matching when the corresponding name has a wildcard in it. const std::string& name = *name_it;
size_t wildcard_idx = name.find(wildcard); if (wildcard_idx == std::string::npos) { // No wildcard present // Did the definition token match the user token? if (name != *token_it) { return matched_tokens;
}
} else {
std::string name_prefix = name.substr(0, wildcard_idx);
// Did the user token start with the up-to-the-wildcard prefix? if (!StartsWith(*token_it, name_prefix)) { return matched_tokens;
}
}
++token_it;
++name_it;
++matched_tokens;
}
// If we got this far, it's either a full match or the token list was too short. return matched_tokens;
}
// Flatten the token range by joining every adjacent token with the separator character. // e.g. ["hello", "world"].join('$') == "hello$world"
std::string Join(char separator) const {
TokenList tmp(begin(), end()); return android::base::Join(tmp, separator); // TODO: Join should probably take an offset or iterators
}
template <typename TPredicate>
TokenRange RemoveIf(const TPredicate& predicate) const { // If any of the tokens in the token lists are empty, then // we need to remove them and compress the token list into a smaller one. bool remove = false; for (auto it = begin_; it != end_; ++it) { auto&& token = *it;
if (predicate(token)) {
remove = true; break;
}
}
// Actually copy the token list and remove the tokens that don't match our predicate. if (remove) { auto token_list = std::make_shared<TokenList>(begin(), end());
TokenList::iterator new_end =
std::remove_if(token_list->begin(), token_list->end(), predicate);
token_list->erase(new_end, token_list->end());
assert(token_list_->size() > token_list->size() && "Nothing was actually removed!");
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung und die Messung sind noch experimentell.