Files
online/common/StringVector.cpp
Miklos Vajna 547f9ea731 Rework StringVector to have a single underlying string
This is meant to reduce lots of small allocations and instead have
pointers into the single string for the various tokens instead.

This has a few requirements, though:

1) It's no longer OK to modify the tokens, changing their length would
invalidate the start/length of other tokens. Rework
DocumentBroker::load() to avoid such mutation.

2) The iterators no longer expose zero-terminated strings, so
Poco::cat() doesn't work anymore: add an own cat() instead and use that
in e.g. ChildSession. The own cat() has the benefit that it won't read
past the end of the array if the begin index is out of bounds to add
more safety.

(This nicely works towards killing Poco usage in general.)

3) If zero-terminated strings for all individual tokens is needed, a
copy has to be made, as done in spawnProcess().

(For all of these requirements, the build fails if there are problems.)

Change-Id: Iea40e4400e630b2d669f5c72aea85cb40edf9a2c
Reviewed-on: https://gerrit.libreoffice.org/c/online/+/89711
Reviewed-by: Michael Meeks <michael.meeks@collabora.com>
Tested-by: Jenkins CollaboraOffice <jenkinscollaboraoffice@gmail.com>
2020-02-28 18:31:37 +01:00

90 lines
2.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/.
*/
#include "StringVector.hpp"
StringVector::StringVector() = default;
StringVector::StringVector(const std::string& string, const std::vector<StringToken>& tokens)
: _string(string),
_tokens(tokens)
{
}
std::string StringVector::operator[](size_t index) const
{
if (index >= _tokens.size())
{
return std::string();
}
const StringToken& token = _tokens[index];
return _string.substr(token._index, token._length);
}
size_t StringVector::size() const { return _tokens.size(); }
bool StringVector::empty() const { return _tokens.empty(); }
std::vector<StringToken>::const_iterator StringVector::begin() const { return _tokens.begin(); }
std::vector<StringToken>::iterator StringVector::begin() { return _tokens.begin(); }
std::vector<StringToken>::const_iterator StringVector::end() const { return _tokens.end(); }
std::vector<StringToken>::iterator StringVector::end() { return _tokens.end(); }
std::vector<StringToken>::iterator StringVector::erase(std::vector<StringToken>::const_iterator it)
{
return _tokens.erase(it);
}
void StringVector::push_back(const std::string& string)
{
StringToken token;
token._index = _string.length();
token._length = string.length();
_tokens.push_back(token);
_string += string;
}
std::string StringVector::getParam(const StringToken& token) const
{
return _string.substr(token._index, token._length);
}
std::string StringVector::cat(const std::string& separator, size_t begin) const
{
std::string ret;
bool first = true;
if (begin >= _tokens.size())
{
return ret;
}
for (auto it = _tokens.begin() + begin; it != _tokens.end(); ++it)
{
if (first)
{
first = false;
}
else
{
ret += separator;
}
ret += getParam(*it);
}
return ret;
}
/* vim:set shiftwidth=4 softtabstop=4 expandtab: */