2019-09-12 16:51:55 +02:00
|
|
|
/*
|
|
|
|
* This program is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU General Public License
|
|
|
|
* as published by the Free Software Foundation; either version 2
|
|
|
|
* of the License, or (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program; if not, write to the Free Software Foundation,
|
|
|
|
* Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
|
|
|
*/
|
|
|
|
|
2020-08-07 09:50:34 +02:00
|
|
|
#pragma once
|
2019-09-13 21:12:26 +10:00
|
|
|
|
2019-09-12 16:51:55 +02:00
|
|
|
/** \file
|
|
|
|
* \ingroup bli
|
|
|
|
*
|
2020-06-09 10:27:24 +02:00
|
|
|
* A `blender::StringRef` references a const char array owned by someone else. It is just a pointer
|
|
|
|
* and a size. Since the memory is not owned, StringRef should not be used to transfer ownership of
|
|
|
|
* the string. The data referenced by a StringRef cannot be mutated through it.
|
2019-09-12 16:51:55 +02:00
|
|
|
*
|
2020-06-09 10:10:56 +02:00
|
|
|
* A StringRef is NOT null-terminated. This makes it much more powerful within C++, because we can
|
|
|
|
* also cut off parts of the end without creating a copy. When interfacing with C code that expects
|
2020-06-09 10:27:24 +02:00
|
|
|
* null-terminated strings, `blender::StringRefNull` can be used. It is essentially the same as
|
2020-06-09 10:10:56 +02:00
|
|
|
* StringRef, but with the restriction that the string has to be null-terminated.
|
|
|
|
*
|
|
|
|
* Whenever possible, string parameters should be of type StringRef and the string return type
|
|
|
|
* should be StringRefNull. Don't forget that the StringRefNull does not own the string, so don't
|
|
|
|
* return it when the string exists only in the scope of the function. This convention makes
|
|
|
|
* functions usable in the most contexts.
|
|
|
|
*
|
2020-06-09 10:27:24 +02:00
|
|
|
* blender::StringRef vs. std::string_view:
|
2020-06-09 10:10:56 +02:00
|
|
|
* Both types are certainly very similar. The main benefit of using StringRef in Blender is that
|
|
|
|
* this allows us to add convenience methods at any time. Especially, when doing a lot of string
|
|
|
|
* manipulation, this helps to keep the code clean. Furthermore, we need StringRefNull anyway,
|
|
|
|
* because there is a lot of C code that expects null-terminated strings. Once we use C++17,
|
|
|
|
* implicit conversions to and from string_view can be added.
|
2019-09-12 16:51:55 +02:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include <cstring>
|
|
|
|
#include <sstream>
|
2020-03-19 09:33:03 +01:00
|
|
|
#include <string>
|
2019-09-12 16:51:55 +02:00
|
|
|
|
2020-06-09 11:58:47 +02:00
|
|
|
#include "BLI_span.hh"
|
2020-03-19 09:33:03 +01:00
|
|
|
#include "BLI_utildefines.h"
|
2019-09-12 16:51:55 +02:00
|
|
|
|
2020-06-09 10:27:24 +02:00
|
|
|
namespace blender {
|
2019-09-12 16:51:55 +02:00
|
|
|
|
|
|
|
class StringRef;
|
|
|
|
|
2020-06-09 10:10:56 +02:00
|
|
|
/**
|
|
|
|
* A common base class for StringRef and StringRefNull. This should never be used in other files.
|
|
|
|
* It only exists to avoid some code duplication.
|
|
|
|
*/
|
2019-09-12 16:51:55 +02:00
|
|
|
class StringRefBase {
|
|
|
|
protected:
|
2020-07-03 14:15:05 +02:00
|
|
|
const char *data_;
|
2020-07-20 12:16:20 +02:00
|
|
|
int64_t size_;
|
2019-09-12 16:51:55 +02:00
|
|
|
|
2020-07-20 12:16:20 +02:00
|
|
|
StringRefBase(const char *data, const int64_t size) : data_(data), size_(size)
|
2019-09-12 16:51:55 +02:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
public:
|
|
|
|
/**
|
|
|
|
* Return the (byte-)length of the referenced string, without any null-terminator.
|
|
|
|
*/
|
2020-07-20 12:16:20 +02:00
|
|
|
int64_t size() const
|
2019-09-12 16:51:55 +02:00
|
|
|
{
|
2020-07-03 14:15:05 +02:00
|
|
|
return size_;
|
2019-09-12 16:51:55 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Return a pointer to the start of the string.
|
|
|
|
*/
|
|
|
|
const char *data() const
|
|
|
|
{
|
2020-07-03 14:15:05 +02:00
|
|
|
return data_;
|
2019-09-12 16:51:55 +02:00
|
|
|
}
|
|
|
|
|
2020-06-09 11:58:47 +02:00
|
|
|
operator Span<char>() const
|
2019-09-12 16:51:55 +02:00
|
|
|
{
|
2020-07-03 14:15:05 +02:00
|
|
|
return Span<char>(data_, size_);
|
2019-09-12 16:51:55 +02:00
|
|
|
}
|
|
|
|
|
2020-06-09 10:10:56 +02:00
|
|
|
/**
|
2020-06-30 20:54:31 +10:00
|
|
|
* Implicitly convert to std::string. This is convenient in most cases, but you have to be a bit
|
2020-06-09 10:10:56 +02:00
|
|
|
* careful not to convert to std::string accidentally.
|
|
|
|
*/
|
2019-09-12 16:51:55 +02:00
|
|
|
operator std::string() const
|
|
|
|
{
|
2020-07-20 12:16:20 +02:00
|
|
|
return std::string(data_, (size_t)size_);
|
2019-09-12 16:51:55 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
const char *begin() const
|
|
|
|
{
|
2020-07-03 14:15:05 +02:00
|
|
|
return data_;
|
2019-09-12 16:51:55 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
const char *end() const
|
|
|
|
{
|
2020-07-03 14:15:05 +02:00
|
|
|
return data_ + size_;
|
2019-09-12 16:51:55 +02:00
|
|
|
}
|
|
|
|
|
2020-06-09 10:10:56 +02:00
|
|
|
/**
|
|
|
|
* Copy the string into a buffer. The buffer has to be one byte larger than the size of the
|
|
|
|
* string, because the copied string will be null-terminated. Only use this when you are
|
|
|
|
* absolutely sure that the buffer is large enough.
|
|
|
|
*/
|
2020-04-24 23:35:17 +02:00
|
|
|
void unsafe_copy(char *dst) const
|
2019-09-12 16:51:55 +02:00
|
|
|
{
|
2020-07-20 12:16:20 +02:00
|
|
|
memcpy(dst, data_, (size_t)size_);
|
2020-07-03 14:15:05 +02:00
|
|
|
dst[size_] = '\0';
|
2019-09-12 16:51:55 +02:00
|
|
|
}
|
|
|
|
|
2020-06-09 10:10:56 +02:00
|
|
|
/**
|
|
|
|
* Copy the string into a buffer. The copied string will be null-terminated. This invokes
|
|
|
|
* undefined behavior when dst_size is too small. (Should we define the behavior?)
|
|
|
|
*/
|
2020-07-20 12:16:20 +02:00
|
|
|
void copy(char *dst, const int64_t dst_size) const
|
2020-04-24 23:35:17 +02:00
|
|
|
{
|
2020-07-03 14:15:05 +02:00
|
|
|
if (size_ < dst_size) {
|
2020-04-24 23:35:17 +02:00
|
|
|
this->unsafe_copy(dst);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
BLI_assert(false);
|
|
|
|
dst[0] = '\0';
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-06-09 10:10:56 +02:00
|
|
|
/**
|
|
|
|
* Copy the string into a char array. The copied string will be null-terminated. This invokes
|
|
|
|
* undefined behavior when dst is too small.
|
|
|
|
*/
|
2020-07-20 12:16:20 +02:00
|
|
|
template<size_t N> void copy(char (&dst)[N])
|
2020-04-24 23:35:17 +02:00
|
|
|
{
|
|
|
|
this->copy(dst, N);
|
|
|
|
}
|
|
|
|
|
2019-09-12 16:51:55 +02:00
|
|
|
/**
|
|
|
|
* Returns true when the string begins with the given prefix. Otherwise false.
|
|
|
|
*/
|
|
|
|
bool startswith(StringRef prefix) const;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns true when the string ends with the given suffix. Otherwise false.
|
|
|
|
*/
|
|
|
|
bool endswith(StringRef suffix) const;
|
2020-02-10 13:54:57 +01:00
|
|
|
|
2020-07-20 12:16:20 +02:00
|
|
|
StringRef substr(int64_t start, const int64_t size) const;
|
2019-09-12 16:51:55 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
2020-06-09 10:10:56 +02:00
|
|
|
* References a null-terminated const char array.
|
2019-09-12 16:51:55 +02:00
|
|
|
*/
|
|
|
|
class StringRefNull : public StringRefBase {
|
|
|
|
|
|
|
|
public:
|
|
|
|
StringRefNull() : StringRefBase("", 0)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2020-06-09 10:10:56 +02:00
|
|
|
/**
|
|
|
|
* Construct a StringRefNull from a null terminated c-string. The pointer must not point to NULL.
|
|
|
|
*/
|
2020-08-07 18:24:59 +02:00
|
|
|
StringRefNull(const char *str) : StringRefBase(str, static_cast<int64_t>(strlen(str)))
|
2019-09-12 16:51:55 +02:00
|
|
|
{
|
|
|
|
BLI_assert(str != NULL);
|
2020-07-03 14:15:05 +02:00
|
|
|
BLI_assert(data_[size_] == '\0');
|
2019-09-12 16:51:55 +02:00
|
|
|
}
|
|
|
|
|
2020-06-09 10:10:56 +02:00
|
|
|
/**
|
|
|
|
* Construct a StringRefNull from a null terminated c-string. This invokes undefined behavior
|
|
|
|
* when the given size is not the correct size of the string.
|
|
|
|
*/
|
2020-07-20 12:16:20 +02:00
|
|
|
StringRefNull(const char *str, const int64_t size) : StringRefBase(str, size)
|
2019-09-12 16:51:55 +02:00
|
|
|
{
|
2020-08-07 18:24:59 +02:00
|
|
|
BLI_assert(static_cast<int64_t>(strlen(str)) == size);
|
2019-09-12 16:51:55 +02:00
|
|
|
}
|
|
|
|
|
2020-06-09 10:10:56 +02:00
|
|
|
/**
|
|
|
|
* Reference a std::string. Remember that when the std::string is destructed, the StringRefNull
|
|
|
|
* will point to uninitialized memory.
|
|
|
|
*/
|
2020-07-17 12:38:15 +02:00
|
|
|
StringRefNull(const std::string &str) : StringRefNull(str.c_str())
|
2019-09-12 16:51:55 +02:00
|
|
|
{
|
|
|
|
}
|
2020-06-09 10:10:56 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Get the char at the given index.
|
|
|
|
*/
|
2020-07-20 12:16:20 +02:00
|
|
|
char operator[](const int64_t index) const
|
2020-06-09 10:10:56 +02:00
|
|
|
{
|
2020-07-20 12:16:20 +02:00
|
|
|
BLI_assert(index >= 0);
|
2020-06-09 10:10:56 +02:00
|
|
|
/* Use '<=' instead of just '<', so that the null character can be accessed as well. */
|
2020-07-03 14:15:05 +02:00
|
|
|
BLI_assert(index <= size_);
|
|
|
|
return data_[index];
|
2020-06-09 10:10:56 +02:00
|
|
|
}
|
2020-07-17 12:38:15 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns the beginning of a null-terminated char array.
|
|
|
|
*
|
|
|
|
* This is like ->data(), but can only be called on a StringRefNull.
|
|
|
|
*/
|
|
|
|
const char *c_str() const
|
|
|
|
{
|
|
|
|
return data_;
|
|
|
|
}
|
2019-09-12 16:51:55 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
2020-06-09 10:10:56 +02:00
|
|
|
* References a const char array. It might not be null terminated.
|
2019-09-12 16:51:55 +02:00
|
|
|
*/
|
|
|
|
class StringRef : public StringRefBase {
|
|
|
|
public:
|
|
|
|
StringRef() : StringRefBase(nullptr, 0)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2020-06-09 10:10:56 +02:00
|
|
|
/**
|
|
|
|
* StringRefNull can be converted into StringRef, but not the other way around.
|
|
|
|
*/
|
2019-09-12 16:51:55 +02:00
|
|
|
StringRef(StringRefNull other) : StringRefBase(other.data(), other.size())
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2020-06-09 10:10:56 +02:00
|
|
|
/**
|
|
|
|
* Create a StringRef from a null-terminated c-string.
|
|
|
|
*/
|
2020-08-07 18:24:59 +02:00
|
|
|
StringRef(const char *str) : StringRefBase(str, str ? static_cast<int64_t>(strlen(str)) : 0)
|
2019-09-12 16:51:55 +02:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2020-07-20 12:16:20 +02:00
|
|
|
StringRef(const char *str, const int64_t length) : StringRefBase(str, length)
|
2019-09-12 16:51:55 +02:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2020-06-10 18:26:11 +02:00
|
|
|
/**
|
|
|
|
* Create a StringRef from a start and end pointer. This invokes undefined behavior when the
|
|
|
|
* second point points to a smaller address than the first one.
|
|
|
|
*/
|
|
|
|
StringRef(const char *begin, const char *one_after_end)
|
2020-08-07 18:24:59 +02:00
|
|
|
: StringRefBase(begin, static_cast<int64_t>(one_after_end - begin))
|
2020-06-10 18:26:11 +02:00
|
|
|
{
|
|
|
|
BLI_assert(begin <= one_after_end);
|
|
|
|
}
|
|
|
|
|
2020-06-09 10:10:56 +02:00
|
|
|
/**
|
|
|
|
* Reference a std::string. Remember that when the std::string is destructed, the StringRef
|
|
|
|
* will point to uninitialized memory.
|
|
|
|
*/
|
2020-08-07 18:24:59 +02:00
|
|
|
StringRef(const std::string &str) : StringRefBase(str.data(), static_cast<int64_t>(str.size()))
|
2019-09-12 16:51:55 +02:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Return a new StringRef that does not contain the first n chars.
|
|
|
|
*/
|
2020-07-20 12:16:20 +02:00
|
|
|
StringRef drop_prefix(const int64_t n) const
|
2019-09-12 16:51:55 +02:00
|
|
|
{
|
2020-07-20 12:16:20 +02:00
|
|
|
BLI_assert(n >= 0);
|
2020-07-03 14:15:05 +02:00
|
|
|
BLI_assert(n <= size_);
|
|
|
|
return StringRef(data_ + n, size_ - n);
|
2019-09-12 16:51:55 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Return a new StringRef that with the given prefix being skipped.
|
|
|
|
* Asserts that the string begins with the given prefix.
|
|
|
|
*/
|
|
|
|
StringRef drop_prefix(StringRef prefix) const
|
|
|
|
{
|
|
|
|
BLI_assert(this->startswith(prefix));
|
|
|
|
return this->drop_prefix(prefix.size());
|
|
|
|
}
|
2020-06-09 10:10:56 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Get the char at the given index.
|
|
|
|
*/
|
2020-07-20 12:16:20 +02:00
|
|
|
char operator[](int64_t index) const
|
2020-06-09 10:10:56 +02:00
|
|
|
{
|
2020-07-20 12:16:20 +02:00
|
|
|
BLI_assert(index >= 0);
|
2020-07-03 14:15:05 +02:00
|
|
|
BLI_assert(index < size_);
|
|
|
|
return data_[index];
|
2020-06-09 10:10:56 +02:00
|
|
|
}
|
2019-09-12 16:51:55 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
/* More inline functions
|
|
|
|
***************************************/
|
|
|
|
|
|
|
|
inline std::ostream &operator<<(std::ostream &stream, StringRef ref)
|
|
|
|
{
|
|
|
|
stream << std::string(ref);
|
|
|
|
return stream;
|
|
|
|
}
|
|
|
|
|
|
|
|
inline std::ostream &operator<<(std::ostream &stream, StringRefNull ref)
|
|
|
|
{
|
2020-07-20 12:16:20 +02:00
|
|
|
stream << std::string(ref.data(), (size_t)ref.size());
|
2019-09-12 16:51:55 +02:00
|
|
|
return stream;
|
|
|
|
}
|
|
|
|
|
2020-06-09 10:10:56 +02:00
|
|
|
/**
|
2020-06-13 12:50:07 +10:00
|
|
|
* Adding two #StringRefs will allocate an std::string.
|
|
|
|
* This is not efficient, but convenient in most cases.
|
2020-06-09 10:10:56 +02:00
|
|
|
*/
|
2019-09-12 16:51:55 +02:00
|
|
|
inline std::string operator+(StringRef a, StringRef b)
|
|
|
|
{
|
|
|
|
return std::string(a) + std::string(b);
|
|
|
|
}
|
|
|
|
|
|
|
|
inline bool operator==(StringRef a, StringRef b)
|
|
|
|
{
|
|
|
|
if (a.size() != b.size()) {
|
|
|
|
return false;
|
|
|
|
}
|
2020-07-20 12:16:20 +02:00
|
|
|
return STREQLEN(a.data(), b.data(), (size_t)a.size());
|
2019-09-12 16:51:55 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
inline bool operator!=(StringRef a, StringRef b)
|
|
|
|
{
|
|
|
|
return !(a == b);
|
|
|
|
}
|
|
|
|
|
2020-06-09 10:10:56 +02:00
|
|
|
/**
|
|
|
|
* Return true when the string starts with the given prefix.
|
|
|
|
*/
|
2019-09-12 16:51:55 +02:00
|
|
|
inline bool StringRefBase::startswith(StringRef prefix) const
|
|
|
|
{
|
2020-07-03 14:15:05 +02:00
|
|
|
if (size_ < prefix.size_) {
|
2019-09-12 16:51:55 +02:00
|
|
|
return false;
|
|
|
|
}
|
2020-07-20 12:16:20 +02:00
|
|
|
for (int64_t i = 0; i < prefix.size_; i++) {
|
2020-07-03 14:15:05 +02:00
|
|
|
if (data_[i] != prefix.data_[i]) {
|
2019-09-12 16:51:55 +02:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2020-06-09 10:10:56 +02:00
|
|
|
/**
|
|
|
|
* Return true when the string ends with the given suffix.
|
|
|
|
*/
|
2019-09-12 16:51:55 +02:00
|
|
|
inline bool StringRefBase::endswith(StringRef suffix) const
|
|
|
|
{
|
2020-07-03 14:15:05 +02:00
|
|
|
if (size_ < suffix.size_) {
|
2019-09-12 16:51:55 +02:00
|
|
|
return false;
|
|
|
|
}
|
2020-07-20 12:16:20 +02:00
|
|
|
const int64_t offset = size_ - suffix.size_;
|
|
|
|
for (int64_t i = 0; i < suffix.size_; i++) {
|
2020-07-03 14:15:05 +02:00
|
|
|
if (data_[offset + i] != suffix.data_[i]) {
|
2019-09-12 16:51:55 +02:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2020-06-09 10:10:56 +02:00
|
|
|
/**
|
2020-06-13 12:50:07 +10:00
|
|
|
* Return a new #StringRef containing only a sub-string of the original string.
|
2020-06-09 10:10:56 +02:00
|
|
|
*/
|
2020-07-20 12:16:20 +02:00
|
|
|
inline StringRef StringRefBase::substr(const int64_t start, const int64_t size) const
|
2020-02-10 13:54:57 +01:00
|
|
|
{
|
2020-07-20 12:16:20 +02:00
|
|
|
BLI_assert(size >= 0);
|
|
|
|
BLI_assert(start >= 0);
|
2020-07-03 14:15:05 +02:00
|
|
|
BLI_assert(start + size <= size_);
|
|
|
|
return StringRef(data_ + start, size);
|
2020-02-10 13:54:57 +01:00
|
|
|
}
|
|
|
|
|
2020-06-09 10:27:24 +02:00
|
|
|
} // namespace blender
|