2023-02-11 20:49:42 +00:00
|
|
|
// This file Copyright © 2022-2023 Mnemosyne LLC.
|
2022-08-08 18:05:39 +00:00
|
|
|
// It may be used under GPLv2 (SPDX: GPL-2.0-only), GPLv3 (SPDX: GPL-3.0-only),
|
2022-03-24 21:41:29 +00:00
|
|
|
// or any future license endorsed by Mnemosyne LLC.
|
|
|
|
// License text can be found in the licenses/ folder.
|
|
|
|
|
|
|
|
#pragma once
|
|
|
|
|
2022-04-25 01:49:52 +00:00
|
|
|
#include <cstddef>
|
2022-03-24 21:41:29 +00:00
|
|
|
#include <string_view>
|
2022-08-17 16:08:36 +00:00
|
|
|
#include <utility>
|
2022-03-24 21:41:29 +00:00
|
|
|
|
2023-04-16 20:34:19 +00:00
|
|
|
#include <fmt/core.h>
|
2022-03-24 21:41:29 +00:00
|
|
|
|
|
|
|
/**
|
2022-03-28 02:55:30 +00:00
|
|
|
* A memory buffer which uses a builtin array of N bytes, using heap
|
|
|
|
* memory only if its string gets too big. Its main use case is building
|
|
|
|
* temporary strings in stack memory.
|
2022-03-24 21:41:29 +00:00
|
|
|
*
|
2022-04-21 14:28:38 +00:00
|
|
|
* It is a convenience wrapper around `fmt::basic_memory_buffer`.
|
2022-03-24 21:41:29 +00:00
|
|
|
*/
|
2022-03-28 02:55:30 +00:00
|
|
|
template<typename Char, size_t N>
|
2022-03-24 21:41:29 +00:00
|
|
|
class tr_strbuf
|
|
|
|
{
|
|
|
|
private:
|
2022-03-28 02:55:30 +00:00
|
|
|
fmt::basic_memory_buffer<Char, N> buffer_;
|
2022-03-24 21:41:29 +00:00
|
|
|
|
|
|
|
public:
|
2022-03-28 02:55:30 +00:00
|
|
|
using value_type = Char;
|
|
|
|
using const_reference = const Char&;
|
2022-03-24 21:41:29 +00:00
|
|
|
|
2022-03-30 17:10:51 +00:00
|
|
|
tr_strbuf()
|
|
|
|
{
|
|
|
|
ensure_sz();
|
|
|
|
}
|
|
|
|
|
2022-03-28 02:55:30 +00:00
|
|
|
tr_strbuf(tr_strbuf const& other) = delete;
|
|
|
|
tr_strbuf& operator=(tr_strbuf const& other) = delete;
|
2022-03-24 21:41:29 +00:00
|
|
|
|
2022-03-30 17:10:51 +00:00
|
|
|
tr_strbuf(tr_strbuf&& other)
|
|
|
|
: buffer_{ std::move(other.buffer_) }
|
2022-03-28 02:55:30 +00:00
|
|
|
{
|
2022-03-30 17:10:51 +00:00
|
|
|
ensure_sz();
|
2022-03-28 02:55:30 +00:00
|
|
|
}
|
|
|
|
|
2022-03-30 17:10:51 +00:00
|
|
|
tr_strbuf& operator=(tr_strbuf&& other)
|
2022-03-24 21:41:29 +00:00
|
|
|
{
|
|
|
|
buffer_ = std::move(other.buffer_);
|
2022-03-30 17:10:51 +00:00
|
|
|
ensure_sz();
|
2022-03-24 21:41:29 +00:00
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
2022-03-25 21:16:26 +00:00
|
|
|
template<typename... Args>
|
2022-03-29 10:57:04 +00:00
|
|
|
explicit tr_strbuf(Args const&... args)
|
2022-03-24 21:41:29 +00:00
|
|
|
{
|
2022-03-25 21:16:26 +00:00
|
|
|
append(args...);
|
2022-03-24 21:41:29 +00:00
|
|
|
}
|
|
|
|
|
2022-03-28 02:55:30 +00:00
|
|
|
[[nodiscard]] constexpr auto begin() noexcept
|
2022-03-24 21:41:29 +00:00
|
|
|
{
|
|
|
|
return buffer_.begin();
|
|
|
|
}
|
|
|
|
|
2022-03-28 02:55:30 +00:00
|
|
|
[[nodiscard]] constexpr auto end() noexcept
|
2022-03-24 21:41:29 +00:00
|
|
|
{
|
|
|
|
return buffer_.end();
|
|
|
|
}
|
|
|
|
|
2022-03-28 02:55:30 +00:00
|
|
|
[[nodiscard]] constexpr auto begin() const noexcept
|
2022-03-24 21:41:29 +00:00
|
|
|
{
|
|
|
|
return buffer_.begin();
|
|
|
|
}
|
|
|
|
|
2022-03-28 02:55:30 +00:00
|
|
|
[[nodiscard]] constexpr auto end() const noexcept
|
2022-03-24 21:41:29 +00:00
|
|
|
{
|
|
|
|
return buffer_.end();
|
|
|
|
}
|
|
|
|
|
2022-03-28 02:55:30 +00:00
|
|
|
[[nodiscard]] constexpr Char& at(size_t pos) noexcept
|
2022-03-24 21:41:29 +00:00
|
|
|
{
|
|
|
|
return buffer_[pos];
|
|
|
|
}
|
|
|
|
|
2022-03-28 02:55:30 +00:00
|
|
|
[[nodiscard]] constexpr Char at(size_t pos) const noexcept
|
2022-03-24 21:41:29 +00:00
|
|
|
{
|
|
|
|
return buffer_[pos];
|
|
|
|
}
|
|
|
|
|
2022-03-28 02:55:30 +00:00
|
|
|
[[nodiscard]] constexpr auto size() const noexcept
|
2022-03-24 21:41:29 +00:00
|
|
|
{
|
|
|
|
return buffer_.size();
|
|
|
|
}
|
|
|
|
|
2022-03-28 02:55:30 +00:00
|
|
|
[[nodiscard]] constexpr bool empty() const noexcept
|
2022-03-24 21:41:29 +00:00
|
|
|
{
|
|
|
|
return size() == 0;
|
|
|
|
}
|
|
|
|
|
2022-03-28 02:55:30 +00:00
|
|
|
[[nodiscard]] constexpr auto* data() noexcept
|
2022-03-24 21:41:29 +00:00
|
|
|
{
|
|
|
|
return buffer_.data();
|
|
|
|
}
|
|
|
|
|
2022-03-28 02:55:30 +00:00
|
|
|
[[nodiscard]] constexpr auto const* data() const noexcept
|
2022-03-24 21:41:29 +00:00
|
|
|
{
|
|
|
|
return buffer_.data();
|
|
|
|
}
|
|
|
|
|
2022-03-28 02:55:30 +00:00
|
|
|
[[nodiscard]] constexpr auto const* c_str() const noexcept
|
2022-03-25 21:16:26 +00:00
|
|
|
{
|
|
|
|
return data();
|
|
|
|
}
|
|
|
|
|
2022-03-28 02:55:30 +00:00
|
|
|
[[nodiscard]] constexpr auto sv() const noexcept
|
2022-03-25 21:16:26 +00:00
|
|
|
{
|
2022-03-28 02:55:30 +00:00
|
|
|
return std::basic_string_view<Char>{ data(), size() };
|
2022-03-25 21:16:26 +00:00
|
|
|
}
|
|
|
|
|
2022-04-03 18:23:00 +00:00
|
|
|
template<typename ContiguousRange>
|
|
|
|
[[nodiscard]] constexpr auto operator==(ContiguousRange const& x) const noexcept
|
|
|
|
{
|
|
|
|
return sv() == x;
|
|
|
|
}
|
|
|
|
|
2022-03-24 21:41:29 +00:00
|
|
|
///
|
|
|
|
|
2022-03-28 02:55:30 +00:00
|
|
|
[[nodiscard]] constexpr bool ends_with(Char const& x) const noexcept
|
2022-03-24 21:41:29 +00:00
|
|
|
{
|
2022-03-25 21:16:26 +00:00
|
|
|
auto const n = size();
|
|
|
|
return n != 0 && data()[n - 1] == x;
|
2022-03-24 21:41:29 +00:00
|
|
|
}
|
|
|
|
|
2022-03-25 21:16:26 +00:00
|
|
|
template<typename ContiguousRange>
|
2022-03-28 02:55:30 +00:00
|
|
|
[[nodiscard]] constexpr bool ends_with(ContiguousRange const& x) const noexcept
|
2022-03-24 21:41:29 +00:00
|
|
|
{
|
2022-03-25 21:16:26 +00:00
|
|
|
auto const x_len = std::size(x);
|
|
|
|
auto const len = size();
|
|
|
|
return len >= x_len && this->sv().substr(len - x_len) == x;
|
2022-03-24 21:41:29 +00:00
|
|
|
}
|
|
|
|
|
2022-03-28 02:55:30 +00:00
|
|
|
[[nodiscard]] constexpr bool ends_with(Char const* x) const noexcept
|
2022-03-24 21:41:29 +00:00
|
|
|
{
|
2022-03-28 02:55:30 +00:00
|
|
|
return x != nullptr && ends_with(std::basic_string_view<Char>(x));
|
2022-03-25 21:16:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
///
|
|
|
|
|
2022-03-28 02:55:30 +00:00
|
|
|
[[nodiscard]] constexpr bool starts_with(Char const& x) const noexcept
|
2022-03-25 21:16:26 +00:00
|
|
|
{
|
|
|
|
return !empty() && *data() == x;
|
2022-03-24 21:41:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
template<typename ContiguousRange>
|
2022-03-28 02:55:30 +00:00
|
|
|
[[nodiscard]] constexpr bool starts_with(ContiguousRange const& x) const noexcept
|
2022-03-24 21:41:29 +00:00
|
|
|
{
|
2022-03-25 21:16:26 +00:00
|
|
|
auto const x_len = std::size(x);
|
|
|
|
return size() >= x_len && this->sv().substr(0, x_len) == x;
|
|
|
|
}
|
|
|
|
|
2022-03-28 02:55:30 +00:00
|
|
|
[[nodiscard]] constexpr bool starts_with(Char const* x) const noexcept
|
2022-03-25 21:16:26 +00:00
|
|
|
{
|
2022-03-28 02:55:30 +00:00
|
|
|
return x != nullptr && starts_with(std::basic_string_view<Char>(x));
|
2022-03-25 21:16:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
///
|
|
|
|
|
|
|
|
void clear()
|
|
|
|
{
|
|
|
|
buffer_.clear();
|
|
|
|
ensure_sz();
|
|
|
|
}
|
|
|
|
|
|
|
|
void resize(size_t n)
|
|
|
|
{
|
|
|
|
buffer_.resize(n);
|
|
|
|
ensure_sz();
|
|
|
|
}
|
|
|
|
|
|
|
|
///
|
|
|
|
|
2022-03-28 02:55:30 +00:00
|
|
|
void append(Char const& value)
|
2022-03-25 21:16:26 +00:00
|
|
|
{
|
|
|
|
buffer_.push_back(value);
|
|
|
|
ensure_sz();
|
2022-03-24 21:41:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
template<typename ContiguousRange>
|
2022-03-25 21:16:26 +00:00
|
|
|
void append(ContiguousRange const& args)
|
2022-03-24 21:41:29 +00:00
|
|
|
{
|
2022-03-25 21:16:26 +00:00
|
|
|
buffer_.append(std::data(args), std::data(args) + std::size(args));
|
|
|
|
ensure_sz();
|
|
|
|
}
|
|
|
|
|
2022-03-28 02:55:30 +00:00
|
|
|
void append(Char const* sz_value)
|
2022-03-25 21:16:26 +00:00
|
|
|
{
|
|
|
|
if (sz_value != nullptr)
|
|
|
|
{
|
2022-03-28 02:55:30 +00:00
|
|
|
append(std::basic_string_view<Char>{ sz_value });
|
2022-03-25 21:16:26 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
template<typename... Args>
|
|
|
|
void append(Args const&... args)
|
|
|
|
{
|
|
|
|
(append(args), ...);
|
|
|
|
}
|
|
|
|
|
|
|
|
template<typename Arg>
|
|
|
|
auto& operator+=(Arg const& arg)
|
|
|
|
{
|
|
|
|
append(arg);
|
2022-03-24 21:41:29 +00:00
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
2022-03-25 21:16:26 +00:00
|
|
|
///
|
|
|
|
|
|
|
|
template<typename... Args>
|
|
|
|
void assign(Args const&... args)
|
2022-03-24 21:41:29 +00:00
|
|
|
{
|
|
|
|
clear();
|
2022-03-25 21:16:26 +00:00
|
|
|
append(args...);
|
|
|
|
}
|
|
|
|
|
|
|
|
template<typename Arg>
|
|
|
|
auto& operator=(Arg const& arg)
|
|
|
|
{
|
|
|
|
assign(arg);
|
2022-03-24 21:41:29 +00:00
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
2022-03-28 02:55:30 +00:00
|
|
|
void push_back(Char const& value)
|
|
|
|
{
|
|
|
|
append(value);
|
|
|
|
}
|
|
|
|
|
2022-03-25 21:16:26 +00:00
|
|
|
///
|
|
|
|
|
|
|
|
template<typename... Args>
|
2022-03-28 02:55:30 +00:00
|
|
|
void join(Char delim, Args const&... args)
|
2022-03-24 21:41:29 +00:00
|
|
|
{
|
2022-03-25 21:16:26 +00:00
|
|
|
((append(args), append(delim)), ...);
|
2022-03-24 21:41:29 +00:00
|
|
|
resize(size() - 1);
|
|
|
|
}
|
|
|
|
|
2022-03-25 21:16:26 +00:00
|
|
|
template<typename ContiguousRange, typename... Args>
|
|
|
|
void join(ContiguousRange const& delim, Args const&... args)
|
|
|
|
{
|
|
|
|
((append(args), append(delim)), ...);
|
|
|
|
resize(size() - std::size(delim));
|
|
|
|
}
|
|
|
|
|
|
|
|
template<typename... Args>
|
2022-03-28 02:55:30 +00:00
|
|
|
void join(Char const* sz_delim, Args const&... args)
|
|
|
|
{
|
|
|
|
join(std::basic_string_view<Char>{ sz_delim }, args...);
|
|
|
|
}
|
|
|
|
|
|
|
|
[[nodiscard]] constexpr operator std::basic_string_view<Char>() const noexcept
|
2022-03-25 21:16:26 +00:00
|
|
|
{
|
2022-03-28 02:55:30 +00:00
|
|
|
return sv();
|
|
|
|
}
|
|
|
|
|
|
|
|
[[nodiscard]] constexpr operator auto() const noexcept
|
|
|
|
{
|
|
|
|
return c_str();
|
2022-03-25 21:16:26 +00:00
|
|
|
}
|
|
|
|
|
2022-05-22 01:17:00 +00:00
|
|
|
bool popdir() noexcept
|
|
|
|
{
|
2022-06-05 17:08:58 +00:00
|
|
|
std::string_view tr_sys_path_dirname(std::string_view path);
|
2022-05-22 01:17:00 +00:00
|
|
|
auto const parent = tr_sys_path_dirname(sv());
|
|
|
|
auto const changed = parent != sv();
|
|
|
|
|
|
|
|
if (changed)
|
|
|
|
{
|
|
|
|
if (std::data(parent) == std::data(*this))
|
|
|
|
{
|
|
|
|
resize(std::size(parent));
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
assign(parent);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return changed;
|
|
|
|
}
|
|
|
|
|
2022-03-25 21:16:26 +00:00
|
|
|
private:
|
2022-03-24 21:41:29 +00:00
|
|
|
/**
|
|
|
|
* Ensure that the buffer's string is zero-terminated, e.g. for
|
2023-01-23 16:26:11 +00:00
|
|
|
* external APIs that require `char*` strings.
|
2022-03-24 21:41:29 +00:00
|
|
|
*
|
2023-01-23 16:26:11 +00:00
|
|
|
* Note that the added trailing '\0' does not increment `size()`.
|
|
|
|
* This is to ensure that `strlen(buf.c_str()) == buf.size()`.
|
2022-03-24 21:41:29 +00:00
|
|
|
*/
|
|
|
|
void ensure_sz()
|
|
|
|
{
|
|
|
|
auto const n = size();
|
2022-03-25 21:16:26 +00:00
|
|
|
buffer_.reserve(n + 1);
|
2022-03-24 21:41:29 +00:00
|
|
|
buffer_[n] = '\0';
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2022-03-28 02:55:30 +00:00
|
|
|
template<typename Char, size_t N>
|
|
|
|
struct fmt::formatter<tr_strbuf<Char, N>> : formatter<std::basic_string_view<Char>, Char>
|
|
|
|
{
|
|
|
|
template<typename FormatContext>
|
|
|
|
constexpr auto format(tr_strbuf<Char, N> const& strbuf, FormatContext& ctx) const
|
|
|
|
{
|
|
|
|
return formatter<std::basic_string_view<Char>, Char>::format(strbuf.sv(), ctx);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2022-03-24 21:41:29 +00:00
|
|
|
/**
|
|
|
|
* Good for building short-term URLs.
|
|
|
|
* The initial size is big enough to avoid heap allocs in most cases,
|
|
|
|
* but that also makes it a poor choice for longer-term storage.
|
|
|
|
* https://stackoverflow.com/a/417184
|
|
|
|
*/
|
|
|
|
using tr_urlbuf = tr_strbuf<char, 2000>;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Good for building short-term filenames.
|
|
|
|
* The initial size is big enough to avoid heap allocs in most cases,
|
|
|
|
* but that also makes it a poor choice for longer-term storage.
|
|
|
|
* https://stackoverflow.com/a/65174437
|
|
|
|
*/
|
|
|
|
using tr_pathbuf = tr_strbuf<char, 4096>;
|