784b7dc39d
StringVector is heavily used for tokenization and benefits from inlining of small functions. Also, cat doesn't need to be slower than necessary. Change-Id: I4ab2ff1b1f1a81092049d2cde64b6df10b34b5f7 Reviewed-on: https://gerrit.libreoffice.org/c/online/+/95287 Reviewed-by: Michael Meeks <michael.meeks@collabora.com> Tested-by: Jenkins
118 lines
3.2 KiB
C++
118 lines
3.2 KiB
C++
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4; fill-column: 100 -*- */
|
|
/*
|
|
* This file is part of the LibreOffice project.
|
|
*
|
|
* 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/.
|
|
*/
|
|
|
|
#pragma once
|
|
|
|
#include <string>
|
|
#include <vector>
|
|
|
|
/**
|
|
* Stores an offset and a length into the single underlying string of StringVector.
|
|
*/
|
|
struct StringToken
|
|
{
|
|
std::size_t _index;
|
|
std::size_t _length;
|
|
|
|
StringToken() = default;
|
|
|
|
StringToken(std::size_t index, std::size_t length)
|
|
: _index(index),
|
|
_length(length)
|
|
{
|
|
}
|
|
};
|
|
|
|
/**
|
|
* 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
|
|
{
|
|
/// All tokens are substrings of this string.
|
|
std::string _string;
|
|
std::vector<StringToken> _tokens;
|
|
|
|
public:
|
|
explicit StringVector();
|
|
|
|
explicit StringVector(const std::string& string, const std::vector<StringToken>& tokens);
|
|
|
|
/// Unlike std::vector, gives an empty string if index is unexpected.
|
|
std::string operator[](std::size_t index) const;
|
|
|
|
std::size_t size() const { return _tokens.size(); }
|
|
|
|
bool empty() const { return _tokens.empty(); }
|
|
|
|
std::vector<StringToken>::const_iterator begin() const { return _tokens.begin(); }
|
|
|
|
std::vector<StringToken>::iterator begin() { return _tokens.begin(); }
|
|
|
|
std::vector<StringToken>::const_iterator end() const { return _tokens.end(); }
|
|
|
|
std::vector<StringToken>::iterator end() { return _tokens.end(); }
|
|
|
|
std::vector<StringToken>::iterator erase(std::vector<StringToken>::const_iterator it)
|
|
{
|
|
return _tokens.erase(it);
|
|
}
|
|
|
|
void push_back(const std::string& string)
|
|
{
|
|
_tokens.emplace_back(_string.size(), string.size());
|
|
_string += string;
|
|
}
|
|
|
|
/// Gets the underlying string of a single token.
|
|
std::string getParam(const StringToken& token) const
|
|
{
|
|
return _string.substr(token._index, token._length);
|
|
}
|
|
|
|
/// Concats tokens starting from begin, using separator as separator.
|
|
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;
|
|
}
|
|
|
|
/// Compares the nth token with string.
|
|
bool equals(std::size_t index, const char* string) const
|
|
{
|
|
if (index >= _tokens.size())
|
|
{
|
|
return false;
|
|
}
|
|
|
|
const StringToken& token = _tokens[index];
|
|
return _string.compare(token._index, token._length, string) == 0;
|
|
}
|
|
|
|
/// Compares the nth token with the mth token from an other StringVector.
|
|
bool equals(std::size_t index, const StringVector& other, std::size_t otherIndex);
|
|
};
|
|
|
|
/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|