2019-12-09 11:57:37 +00:00
|
|
|
/*
|
2021-10-04 20:59:10 +01:00
|
|
|
* Copyright (c) Atmosphère-NX
|
2019-12-09 11:57:37 +00:00
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or modify it
|
|
|
|
* under the terms and conditions of the GNU General Public License,
|
|
|
|
* version 2, as published by the Free Software Foundation.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope 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, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
#pragma once
|
|
|
|
#include <vapours.hpp>
|
|
|
|
|
|
|
|
namespace ams::kvdb {
|
|
|
|
|
|
|
|
/* Represents a string with a backing buffer of N bytes. */
|
|
|
|
template<size_t N>
|
|
|
|
class BoundedString {
|
|
|
|
static_assert(N > 0, "BoundedString requires non-zero backing buffer!");
|
|
|
|
private:
|
2021-10-10 08:14:06 +01:00
|
|
|
char m_buffer[N];
|
2019-12-09 11:57:37 +00:00
|
|
|
public:
|
|
|
|
/* Constructors. */
|
|
|
|
constexpr BoundedString() {
|
2021-10-10 08:14:06 +01:00
|
|
|
m_buffer[0] = 0;
|
2019-12-09 11:57:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
explicit constexpr BoundedString(const char *s) {
|
2022-03-19 03:11:26 +00:00
|
|
|
AMS_ABORT_UNLESS(static_cast<size_t>(util::Strnlen(s, N)) < N);
|
|
|
|
util::Strlcpy(m_buffer, s, N);
|
2019-12-09 11:57:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Static constructors. */
|
|
|
|
static constexpr BoundedString<N> Make(const char *s) {
|
|
|
|
return BoundedString<N>(s);
|
|
|
|
}
|
|
|
|
|
2022-03-19 03:11:26 +00:00
|
|
|
static BoundedString<N> MakeFormat(const char *format, ...) __attribute__((format (printf, 1, 2))) {
|
2019-12-09 11:57:37 +00:00
|
|
|
BoundedString<N> string;
|
|
|
|
|
2022-03-19 03:11:26 +00:00
|
|
|
std::va_list vl;
|
|
|
|
va_start(vl, format);
|
|
|
|
AMS_ABORT_UNLESS(static_cast<size_t>(util::VSNPrintf(string.m_buffer, N, format, vl)) < N);
|
|
|
|
va_end(vl);
|
2019-12-09 11:57:37 +00:00
|
|
|
|
|
|
|
return string;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Getters. */
|
2022-03-19 03:11:26 +00:00
|
|
|
constexpr size_t GetLength() const {
|
2021-10-10 08:14:06 +01:00
|
|
|
return util::Strnlen(m_buffer, N);
|
2019-12-09 11:57:37 +00:00
|
|
|
}
|
|
|
|
|
2022-03-19 03:11:26 +00:00
|
|
|
constexpr const char *Get() const {
|
2021-10-10 08:14:06 +01:00
|
|
|
return m_buffer;
|
2019-12-09 11:57:37 +00:00
|
|
|
}
|
|
|
|
|
2022-03-19 03:11:26 +00:00
|
|
|
constexpr operator const char *() const {
|
2021-10-10 08:14:06 +01:00
|
|
|
return m_buffer;
|
2019-12-09 11:57:37 +00:00
|
|
|
}
|
|
|
|
|
2022-03-19 03:11:26 +00:00
|
|
|
/* Assignment. */
|
|
|
|
constexpr BoundedString<N> &Assign(const char *s) {
|
|
|
|
AMS_ABORT_UNLESS(static_cast<size_t>(util::Strnlen(s, N)) < N);
|
|
|
|
util::Strlcpy(m_buffer, s, N);
|
|
|
|
|
|
|
|
return *this;
|
2019-12-09 11:57:37 +00:00
|
|
|
}
|
|
|
|
|
2022-03-19 03:11:26 +00:00
|
|
|
BoundedString<N> &AssignFormat(const char *format, ...) __attribute__((format (printf, 2, 3))) {
|
|
|
|
std::va_list vl;
|
|
|
|
va_start(vl, format);
|
|
|
|
AMS_ABORT_UNLESS(static_cast<size_t>(util::VSNPrintf(m_buffer, N, format, vl)) < N);
|
|
|
|
va_end(vl);
|
|
|
|
|
|
|
|
return *this;
|
2019-12-09 11:57:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Append to existing. */
|
2022-03-19 03:11:26 +00:00
|
|
|
BoundedString<N> &Append(const char *s) {
|
2019-12-09 11:57:37 +00:00
|
|
|
const size_t length = GetLength();
|
2022-03-19 03:11:26 +00:00
|
|
|
AMS_ABORT_UNLESS(length + static_cast<size_t>(util::Strnlen(s, N)) < N);
|
2021-10-10 08:14:06 +01:00
|
|
|
std::strncat(m_buffer, s, N - length - 1);
|
2022-03-19 03:11:26 +00:00
|
|
|
|
|
|
|
return *this;
|
2019-12-09 11:57:37 +00:00
|
|
|
}
|
|
|
|
|
2022-03-19 03:11:26 +00:00
|
|
|
BoundedString<N> &Append(char c) {
|
2019-12-09 11:57:37 +00:00
|
|
|
const size_t length = GetLength();
|
2022-03-19 03:11:26 +00:00
|
|
|
AMS_ABORT_UNLESS(length + 1 < N);
|
2021-10-10 08:14:06 +01:00
|
|
|
m_buffer[length] = c;
|
|
|
|
m_buffer[length + 1] = 0;
|
2022-03-19 03:11:26 +00:00
|
|
|
|
|
|
|
return *this;
|
2019-12-09 11:57:37 +00:00
|
|
|
}
|
|
|
|
|
2022-03-19 03:11:26 +00:00
|
|
|
BoundedString<N> &AppendFormat(const char *format, ...) __attribute__((format (printf, 2, 3))) {
|
2019-12-09 11:57:37 +00:00
|
|
|
const size_t length = GetLength();
|
2022-03-19 03:11:26 +00:00
|
|
|
|
|
|
|
std::va_list vl;
|
|
|
|
va_start(vl, format);
|
|
|
|
AMS_ABORT_UNLESS(static_cast<size_t>(util::VSNPrintf(m_buffer + length, N - length, format, vl)) < static_cast<size_t>(N - length));
|
|
|
|
va_end(vl);
|
|
|
|
|
|
|
|
return *this;
|
2019-12-09 11:57:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Substring utilities. */
|
2022-03-19 03:11:26 +00:00
|
|
|
void GetSubString(char *dst, size_t dst_size, size_t offset, size_t length) const {
|
2019-12-09 11:57:37 +00:00
|
|
|
/* Make sure output buffer can hold the substring. */
|
2020-02-23 07:05:14 +00:00
|
|
|
AMS_ABORT_UNLESS(offset + length <= GetLength());
|
|
|
|
AMS_ABORT_UNLESS(dst_size > length);
|
2022-03-19 03:11:26 +00:00
|
|
|
|
2019-12-09 11:57:37 +00:00
|
|
|
/* Copy substring to dst. */
|
2021-10-10 08:14:06 +01:00
|
|
|
std::strncpy(dst, m_buffer + offset, length);
|
2019-12-09 11:57:37 +00:00
|
|
|
dst[length] = 0;
|
|
|
|
}
|
|
|
|
|
2022-03-19 03:11:26 +00:00
|
|
|
BoundedString<N> MakeSubString(size_t offset, size_t length) const {
|
2019-12-09 11:57:37 +00:00
|
|
|
BoundedString<N> string;
|
2022-03-19 03:11:26 +00:00
|
|
|
GetSubString(string.m_buffer, N, offset, length);
|
2019-12-09 11:57:37 +00:00
|
|
|
return string;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Comparison. */
|
2022-03-19 03:11:26 +00:00
|
|
|
constexpr bool Equals(const char *s, size_t offset = 0) const {
|
|
|
|
if (std::is_constant_evaluated()) {
|
|
|
|
return util::Strncmp(m_buffer + offset, s, N - offset) == 0;
|
|
|
|
} else {
|
|
|
|
return std::strncmp(m_buffer + offset, s, N - offset) == 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-12-09 11:57:37 +00:00
|
|
|
constexpr bool operator==(const BoundedString<N> &rhs) const {
|
2022-03-19 03:11:26 +00:00
|
|
|
return this->Equals(rhs.m_buffer);
|
2019-12-09 11:57:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
constexpr bool operator!=(const BoundedString<N> &rhs) const {
|
|
|
|
return !(*this == rhs);
|
|
|
|
}
|
|
|
|
|
2022-03-19 03:11:26 +00:00
|
|
|
constexpr bool EqualsPostfix(const char *s) const {
|
2021-04-27 04:05:56 +01:00
|
|
|
const size_t suffix_length = util::Strnlen(s, N);
|
2019-12-09 11:57:37 +00:00
|
|
|
const size_t length = GetLength();
|
2022-03-19 03:11:26 +00:00
|
|
|
return suffix_length <= length && this->Equals(s, length - suffix_length);
|
2019-12-09 11:57:37 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
}
|