2020-02-28 03:50:58 -06:00
|
|
|
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4; fill-column: 100 -*- */
|
|
|
|
/*
|
|
|
|
* This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
|
|
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
|
|
|
|
*/
|
|
|
|
|
2020-04-18 03:39:50 -05:00
|
|
|
#pragma once
|
2020-02-28 03:50:58 -06:00
|
|
|
|
2022-03-29 20:37:57 -05:00
|
|
|
#include <cstring>
|
2020-02-28 03:50:58 -06:00
|
|
|
#include <string>
|
2020-06-01 21:46:49 -05:00
|
|
|
#include <utility>
|
2020-02-28 03:50:58 -06:00
|
|
|
#include <vector>
|
|
|
|
|
2020-02-28 07:51:22 -06:00
|
|
|
/**
|
|
|
|
* Stores an offset and a length into the single underlying string of StringVector.
|
|
|
|
*/
|
|
|
|
struct StringToken
|
|
|
|
{
|
2020-05-31 13:16:58 -05:00
|
|
|
std::size_t _index;
|
|
|
|
std::size_t _length;
|
2020-02-28 07:51:22 -06:00
|
|
|
|
|
|
|
StringToken() = default;
|
|
|
|
|
2020-05-31 13:16:58 -05:00
|
|
|
StringToken(std::size_t index, std::size_t length)
|
2020-02-28 07:51:22 -06:00
|
|
|
: _index(index),
|
|
|
|
_length(length)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2020-02-28 03:50:58 -06:00
|
|
|
/**
|
|
|
|
* Safe wrapper around an std::vector<std::string>. Gives you an empty string if you would read past
|
|
|
|
* the ends of the vector.
|
|
|
|
*/
|
|
|
|
class StringVector
|
|
|
|
{
|
2020-02-28 07:51:22 -06:00
|
|
|
/// All tokens are substrings of this string.
|
|
|
|
std::string _string;
|
|
|
|
std::vector<StringToken> _tokens;
|
2020-02-28 03:50:58 -06:00
|
|
|
|
|
|
|
public:
|
2020-06-01 21:22:42 -05:00
|
|
|
explicit StringVector() = default;
|
2020-02-28 03:50:58 -06:00
|
|
|
|
2020-06-01 21:46:49 -05:00
|
|
|
explicit StringVector(std::string string, std::vector<StringToken> tokens)
|
|
|
|
: _string(std::move(string))
|
|
|
|
, _tokens(std::move(tokens))
|
2020-06-01 21:22:42 -05:00
|
|
|
{
|
|
|
|
}
|
2020-02-28 03:50:58 -06:00
|
|
|
|
2022-03-29 20:37:57 -05:00
|
|
|
/// Tokenize delimited values until we hit new-line or the end.
|
|
|
|
static void tokenize(const char* data, const std::size_t size, const char delimiter,
|
|
|
|
std::vector<StringToken>& tokens)
|
|
|
|
{
|
|
|
|
if (size == 0 || data == nullptr || *data == '\0')
|
|
|
|
return;
|
|
|
|
|
|
|
|
tokens.reserve(16);
|
|
|
|
|
|
|
|
const char* start = data;
|
|
|
|
const char* end = data;
|
|
|
|
for (std::size_t i = 0; i < size && data[i] != '\n'; ++i, ++end)
|
|
|
|
{
|
|
|
|
if (data[i] == delimiter)
|
|
|
|
{
|
|
|
|
if (start != end && *start != delimiter)
|
|
|
|
tokens.emplace_back(start - data, end - start);
|
|
|
|
|
|
|
|
start = end;
|
|
|
|
}
|
|
|
|
else if (*start == delimiter)
|
|
|
|
++start;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (start != end && *start != delimiter && *start != '\n')
|
|
|
|
tokens.emplace_back(start - data, end - start);
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Tokenize single-char delimited values until we hit new-line or the end.
|
|
|
|
static StringVector tokenize(const char* data, const std::size_t size,
|
|
|
|
const char delimiter = ' ')
|
|
|
|
{
|
|
|
|
if (size == 0 || data == nullptr || *data == '\0')
|
|
|
|
return StringVector();
|
|
|
|
|
|
|
|
std::vector<StringToken> tokens;
|
|
|
|
tokenize(data, size, delimiter, tokens);
|
|
|
|
return StringVector(std::string(data, size), std::move(tokens));
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Tokenize single-char delimited values until we hit new-line or the end.
|
|
|
|
static StringVector tokenize(const std::string& s, const char delimiter = ' ')
|
|
|
|
{
|
|
|
|
if (s.empty())
|
|
|
|
return StringVector();
|
|
|
|
|
|
|
|
std::vector<StringToken> tokens;
|
|
|
|
tokenize(s.data(), s.size(), delimiter, tokens);
|
|
|
|
return StringVector(s, std::move(tokens));
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Tokenize by the delimiter string.
|
|
|
|
static StringVector tokenize(const std::string& s, const char* delimiter, int len = -1)
|
|
|
|
{
|
|
|
|
if (s.empty() || len == 0 || delimiter == nullptr || *delimiter == '\0')
|
|
|
|
return StringVector();
|
|
|
|
|
|
|
|
if (len < 0)
|
|
|
|
len = std::strlen(delimiter);
|
|
|
|
|
|
|
|
std::size_t start = 0;
|
|
|
|
std::size_t end = s.find(delimiter, start);
|
|
|
|
|
|
|
|
std::vector<StringToken> tokens;
|
|
|
|
tokens.reserve(16);
|
|
|
|
|
|
|
|
tokens.emplace_back(start, end - start);
|
|
|
|
start = end + len;
|
|
|
|
|
|
|
|
while (end != std::string::npos)
|
|
|
|
{
|
|
|
|
end = s.find(delimiter, start);
|
|
|
|
tokens.emplace_back(start, end - start);
|
|
|
|
start = end + len;
|
|
|
|
}
|
|
|
|
|
|
|
|
return StringVector(s, std::move(tokens));
|
|
|
|
}
|
|
|
|
|
|
|
|
template <std::size_t N>
|
|
|
|
static StringVector tokenize(const std::string& s, const char (&delimiter)[N])
|
|
|
|
{
|
|
|
|
return tokenize(s, delimiter, N - 1);
|
|
|
|
}
|
|
|
|
|
|
|
|
static StringVector tokenize(const std::string& s, const std::string& delimiter)
|
|
|
|
{
|
|
|
|
return tokenize(s, delimiter.data(), delimiter.size());
|
|
|
|
}
|
|
|
|
|
|
|
|
/** Tokenize based on any of the characters in 'delimiters'.
|
|
|
|
|
|
|
|
Ie. when there is '\n\r' in there, any of them means a delimiter.
|
|
|
|
In addition, trim the values so there are no leadiding or trailing spaces.
|
|
|
|
*/
|
|
|
|
static StringVector tokenizeAnyOf(const std::string& s, const char* delimiters,
|
|
|
|
const std::size_t delimitersLength);
|
|
|
|
|
|
|
|
template <std::size_t N>
|
|
|
|
static StringVector tokenizeAnyOf(const std::string& s, const char (&delimiters)[N])
|
|
|
|
{
|
|
|
|
return tokenizeAnyOf(s, delimiters, N - 1); // Exclude the null terminator.
|
|
|
|
}
|
|
|
|
|
|
|
|
static StringVector tokenizeAnyOf(const std::string& s, const char* delimiters)
|
|
|
|
{
|
|
|
|
return tokenizeAnyOf(s, delimiters, std::strlen(delimiters));
|
|
|
|
}
|
|
|
|
|
2020-02-28 03:50:58 -06:00
|
|
|
/// Unlike std::vector, gives an empty string if index is unexpected.
|
2020-06-01 21:22:42 -05:00
|
|
|
std::string operator[](std::size_t index) const
|
|
|
|
{
|
|
|
|
if (index >= _tokens.size())
|
|
|
|
{
|
|
|
|
return std::string();
|
|
|
|
}
|
|
|
|
|
|
|
|
const StringToken& token = _tokens[index];
|
|
|
|
return _string.substr(token._index, token._length);
|
|
|
|
}
|
2020-02-28 03:50:58 -06:00
|
|
|
|
2020-05-31 13:16:58 -05:00
|
|
|
std::size_t size() const { return _tokens.size(); }
|
2020-02-28 03:50:58 -06:00
|
|
|
|
2020-05-31 13:16:58 -05:00
|
|
|
bool empty() const { return _tokens.empty(); }
|
2020-02-28 03:50:58 -06:00
|
|
|
|
2020-05-31 13:16:58 -05:00
|
|
|
std::vector<StringToken>::const_iterator begin() const { return _tokens.begin(); }
|
2020-02-28 03:50:58 -06:00
|
|
|
|
2020-05-31 13:16:58 -05:00
|
|
|
std::vector<StringToken>::iterator begin() { return _tokens.begin(); }
|
2020-02-28 03:50:58 -06:00
|
|
|
|
2020-05-31 13:16:58 -05:00
|
|
|
std::vector<StringToken>::const_iterator end() const { return _tokens.end(); }
|
2020-02-28 03:50:58 -06:00
|
|
|
|
2020-05-31 13:16:58 -05:00
|
|
|
std::vector<StringToken>::iterator end() { return _tokens.end(); }
|
2020-02-28 03:50:58 -06:00
|
|
|
|
2020-05-31 13:16:58 -05:00
|
|
|
std::vector<StringToken>::iterator erase(std::vector<StringToken>::const_iterator it)
|
|
|
|
{
|
|
|
|
return _tokens.erase(it);
|
|
|
|
}
|
2020-02-28 03:50:58 -06:00
|
|
|
|
2020-05-31 13:16:58 -05:00
|
|
|
void push_back(const std::string& string)
|
|
|
|
{
|
|
|
|
_tokens.emplace_back(_string.size(), string.size());
|
|
|
|
_string += string;
|
|
|
|
}
|
2020-02-28 07:51:22 -06:00
|
|
|
|
|
|
|
/// Gets the underlying string of a single token.
|
2020-05-31 13:16:58 -05:00
|
|
|
std::string getParam(const StringToken& token) const
|
|
|
|
{
|
|
|
|
return _string.substr(token._index, token._length);
|
|
|
|
}
|
2020-02-28 07:51:22 -06:00
|
|
|
|
|
|
|
/// Concats tokens starting from begin, using separator as separator.
|
2020-05-31 13:16:58 -05:00
|
|
|
template <typename T> inline std::string cat(const T& separator, std::size_t offset) const
|
|
|
|
{
|
|
|
|
std::string ret;
|
|
|
|
|
|
|
|
if (offset >= _tokens.size())
|
|
|
|
{
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
ret.reserve(_string.size() * 2);
|
|
|
|
auto it = _tokens.begin() + offset;
|
|
|
|
ret = getParam(*it);
|
|
|
|
for (++it; it != _tokens.end(); ++it)
|
|
|
|
{
|
|
|
|
// Avoid temporary strings, append separately.
|
|
|
|
ret += separator;
|
|
|
|
ret += getParam(*it);
|
|
|
|
}
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
2020-03-09 03:05:30 -05:00
|
|
|
|
|
|
|
/// Compares the nth token with string.
|
2022-03-20 18:43:15 -05:00
|
|
|
template <typename T> bool equals(std::size_t index, const T& string) const
|
2020-05-31 13:16:58 -05:00
|
|
|
{
|
|
|
|
if (index >= _tokens.size())
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
const StringToken& token = _tokens[index];
|
|
|
|
return _string.compare(token._index, token._length, string) == 0;
|
|
|
|
}
|
2020-03-09 03:05:30 -05:00
|
|
|
|
2020-06-03 16:54:57 -05:00
|
|
|
/// Compares the nth token with string.
|
2022-03-20 18:43:15 -05:00
|
|
|
template <std::size_t N> bool equals(std::size_t index, const char (&string)[N]) const
|
2020-06-03 16:54:57 -05:00
|
|
|
{
|
|
|
|
if (index >= _tokens.size())
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
const StringToken& token = _tokens[index];
|
2022-03-20 18:43:15 -05:00
|
|
|
return _string.compare(token._index, token._length, string, N - 1) == 0;
|
2020-06-03 16:54:57 -05:00
|
|
|
}
|
|
|
|
|
2021-10-14 06:38:45 -05:00
|
|
|
// Checks if the token text at index starts with the given string
|
|
|
|
template <std::size_t N>
|
|
|
|
bool startsWith(std::size_t index, const char (&string)[N]) const
|
|
|
|
{
|
|
|
|
if (index >= _tokens.size())
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
const StringToken& token = _tokens[index];
|
2022-03-20 18:43:15 -05:00
|
|
|
constexpr auto len = N - 1; // we don't want to compare the '\0'
|
2021-10-14 06:38:45 -05:00
|
|
|
return token._length >= len && _string.compare(token._index, len, string) == 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Checks if the token text starts with the given string
|
|
|
|
template <std::size_t N>
|
|
|
|
bool startsWith(const StringToken& token, const char (&string)[N]) const
|
|
|
|
{
|
2021-10-15 08:35:02 -05:00
|
|
|
if (token._index >= _string.size())
|
2021-10-14 06:38:45 -05:00
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2022-03-29 20:37:57 -05:00
|
|
|
constexpr auto len = N - 1; // we don't want to compare the '\0'
|
2021-10-14 06:38:45 -05:00
|
|
|
return token._length >= len && _string.compare(token._index, len, string) == 0;
|
|
|
|
}
|
|
|
|
|
2020-10-03 11:38:25 -05:00
|
|
|
/// Compares the nth token with the mth token from another StringVector.
|
2020-05-31 13:16:58 -05:00
|
|
|
bool equals(std::size_t index, const StringVector& other, std::size_t otherIndex);
|
2021-04-27 02:52:46 -05:00
|
|
|
|
|
|
|
bool getUInt32(std::size_t index, const std::string& key, uint32_t& value) const;
|
2021-04-27 06:50:41 -05:00
|
|
|
bool getNameIntegerPair(std::size_t index, std::string& name, int& value) const;
|
2020-02-28 03:50:58 -06:00
|
|
|
};
|
|
|
|
|
|
|
|
/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|