547f9ea731
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>
76 lines
2 KiB
C++
76 lines
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/.
|
|
*/
|
|
|
|
#ifndef INCLUDED_STRINGVECTOR_HPP
|
|
#define INCLUDED_STRINGVECTOR_HPP
|
|
|
|
#include <string>
|
|
#include <vector>
|
|
|
|
/**
|
|
* Stores an offset and a length into the single underlying string of StringVector.
|
|
*/
|
|
struct StringToken
|
|
{
|
|
size_t _index;
|
|
size_t _length;
|
|
|
|
StringToken() = default;
|
|
|
|
StringToken(size_t index, 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[](size_t index) const;
|
|
|
|
size_t size() const;
|
|
|
|
bool empty() const;
|
|
|
|
std::vector<StringToken>::const_iterator begin() const;
|
|
|
|
std::vector<StringToken>::iterator begin();
|
|
|
|
std::vector<StringToken>::const_iterator end() const;
|
|
|
|
std::vector<StringToken>::iterator end();
|
|
|
|
std::vector<StringToken>::iterator erase(std::vector<StringToken>::const_iterator it);
|
|
|
|
void push_back(const std::string& string);
|
|
|
|
/// Gets the underlying string of a single token.
|
|
std::string getParam(const StringToken& token) const;
|
|
|
|
/// Concats tokens starting from begin, using separator as separator.
|
|
std::string cat(const std::string& separator, size_t begin) const;
|
|
};
|
|
|
|
#endif
|
|
|
|
/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|