| 1 |
/* SPDX-License-Identifier: BSL-1.0 OR BSD-3-Clause */ |
| 2 |
|
| 3 |
#ifndef MPT_STRING_BUFFER_HPP |
| 4 |
#define MPT_STRING_BUFFER_HPP |
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
#include "mpt/base/detect.hpp" |
| 9 |
#include "mpt/base/namespace.hpp" |
| 10 |
#include "mpt/base/saturate_cast.hpp" |
| 11 |
#include "mpt/detect/mfc.hpp" |
| 12 |
#include "mpt/string/types.hpp" |
| 13 |
#include "mpt/string/utility.hpp" |
| 14 |
|
| 15 |
#include <algorithm> |
| 16 |
#include <array> |
| 17 |
#include <iterator> |
| 18 |
#include <string> |
| 19 |
#include <string_view> |
| 20 |
#include <type_traits> |
| 21 |
|
| 22 |
#include <cassert> |
| 23 |
#include <cstddef> |
| 24 |
|
| 25 |
#if MPT_DETECTED_MFC |
| 26 |
// cppcheck-suppress missingInclude |
| 27 |
#include <afx.h> |
| 28 |
#endif // MPT_DETECTED_MFC |
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
namespace mpt { |
| 33 |
inline namespace MPT_INLINE_NS { |
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
template <typename Tstring, typename Tchar> |
| 39 |
class StringBufRefImpl { |
| 40 |
private: |
| 41 |
Tchar * buf; |
| 42 |
std::size_t size; |
| 43 |
|
| 44 |
public: |
| 45 |
// cppcheck false-positive |
| 46 |
// cppcheck-suppress uninitMemberVar |
| 47 |
explicit StringBufRefImpl(Tchar * buf_, std::size_t size_) |
| 48 |
: buf(buf_) |
| 49 |
, size(size_) { |
| 50 |
static_assert(sizeof(Tchar) == sizeof(typename Tstring::value_type)); |
| 51 |
assert(size > 0); |
| 52 |
} |
| 53 |
StringBufRefImpl(const StringBufRefImpl &) = delete; |
| 54 |
StringBufRefImpl(StringBufRefImpl &&) = default; |
| 55 |
StringBufRefImpl & operator=(const StringBufRefImpl &) = delete; |
| 56 |
StringBufRefImpl & operator=(StringBufRefImpl &&) = delete; |
| 57 |
operator Tstring() const { |
| 58 |
std::size_t len = std::find(buf, buf + size, char_constants<Tchar>::null) - buf; // terminate at \0 |
| 59 |
return Tstring(buf, buf + len); |
| 60 |
} |
| 61 |
explicit operator std::basic_string_view<Tchar>() const { |
| 62 |
std::size_t len = std::find(buf, buf + size, char_constants<Tchar>::null) - buf; // terminate at \0 |
| 63 |
return std::basic_string_view<Tchar>(buf, buf + len); |
| 64 |
} |
| 65 |
bool empty() const { |
| 66 |
return buf[0] == char_constants<Tchar>::null; |
| 67 |
} |
| 68 |
StringBufRefImpl & operator=(const Tstring & str) { |
| 69 |
std::copy(str.data(), str.data() + std::min(str.length(), size - 1), buf); |
| 70 |
std::fill(buf + std::min(str.length(), size - 1), buf + size, char_constants<Tchar>::null); |
| 71 |
return *this; |
| 72 |
} |
| 73 |
}; |
| 74 |
|
| 75 |
template <typename Tstring, typename Tchar> |
| 76 |
class StringBufRefImpl<Tstring, const Tchar> { |
| 77 |
private: |
| 78 |
const Tchar * buf; |
| 79 |
std::size_t size; |
| 80 |
|
| 81 |
public: |
| 82 |
// cppcheck false-positive |
| 83 |
// cppcheck-suppress uninitMemberVar |
| 84 |
explicit StringBufRefImpl(const Tchar * buf_, std::size_t size_) |
| 85 |
: buf(buf_) |
| 86 |
, size(size_) { |
| 87 |
static_assert(sizeof(Tchar) == sizeof(typename Tstring::value_type)); |
| 88 |
assert(size > 0); |
| 89 |
} |
| 90 |
StringBufRefImpl(const StringBufRefImpl &) = delete; |
| 91 |
StringBufRefImpl(StringBufRefImpl &&) = default; |
| 92 |
StringBufRefImpl & operator=(const StringBufRefImpl &) = delete; |
| 93 |
StringBufRefImpl & operator=(StringBufRefImpl &&) = delete; |
| 94 |
operator Tstring() const { |
| 95 |
std::size_t len = std::find(buf, buf + size, char_constants<Tchar>::null) - buf; // terminate at \0 |
| 96 |
return Tstring(buf, buf + len); |
| 97 |
} |
| 98 |
explicit operator std::basic_string_view<Tchar>() const { |
| 99 |
std::size_t len = std::find(buf, buf + size, char_constants<Tchar>::null) - buf; // terminate at \0 |
| 100 |
return std::basic_string_view<Tchar>(buf, len); |
| 101 |
} |
| 102 |
bool empty() const { |
| 103 |
return buf[0] == char_constants<Tchar>::null; |
| 104 |
} |
| 105 |
}; |
| 106 |
|
| 107 |
template <typename Tstring, typename Tchar> |
| 108 |
struct make_string_type<StringBufRefImpl<Tstring, Tchar>> { |
| 109 |
using type = Tstring; |
| 110 |
}; |
| 111 |
|
| 112 |
template <typename Tstring, typename Tchar> |
| 113 |
struct make_string_view_type<StringBufRefImpl<Tstring, Tchar>> { |
| 114 |
using type = typename mpt::make_string_view_type<Tstring>::type; |
| 115 |
}; |
| 116 |
|
| 117 |
|
| 118 |
|
| 119 |
template <typename Tstring, typename Tchar, std::size_t size> |
| 120 |
inline StringBufRefImpl<Tstring, typename std::add_const<Tchar>::type> ReadTypedBuf(const std::array<Tchar, size> & buf) { |
| 121 |
return StringBufRefImpl<Tstring, typename std::add_const<Tchar>::type>(buf.data(), size); |
| 122 |
} |
| 123 |
template <typename Tstring, typename Tchar, std::size_t size> |
| 124 |
inline StringBufRefImpl<Tstring, typename std::add_const<Tchar>::type> ReadTypedBuf(const Tchar (&buf)[size]) { |
| 125 |
return StringBufRefImpl<Tstring, typename std::add_const<Tchar>::type>(buf, size); |
| 126 |
} |
| 127 |
template <typename Tstring, typename Tchar> |
| 128 |
inline StringBufRefImpl<Tstring, typename std::add_const<Tchar>::type> ReadTypedBuf(const Tchar * buf, std::size_t size) { |
| 129 |
return StringBufRefImpl<Tstring, typename std::add_const<Tchar>::type>(buf, size); |
| 130 |
} |
| 131 |
template <typename Tstring, typename Tchar, std::size_t size> |
| 132 |
inline StringBufRefImpl<Tstring, Tchar> WriteTypedBuf(std::array<Tchar, size> & buf) { |
| 133 |
return StringBufRefImpl<Tstring, Tchar>(buf.data(), size); |
| 134 |
} |
| 135 |
template <typename Tstring, typename Tchar, std::size_t size> |
| 136 |
inline StringBufRefImpl<Tstring, Tchar> WriteTypedBuf(Tchar (&buf)[size]) { |
| 137 |
return StringBufRefImpl<Tstring, Tchar>(buf, size); |
| 138 |
} |
| 139 |
template <typename Tstring, typename Tchar> |
| 140 |
inline StringBufRefImpl<Tstring, Tchar> WriteTypedBuf(Tchar * buf, std::size_t size) { |
| 141 |
return StringBufRefImpl<Tstring, Tchar>(buf, size); |
| 142 |
} |
| 143 |
|
| 144 |
|
| 145 |
|
| 146 |
template <typename Tchar, std::size_t size> |
| 147 |
inline StringBufRefImpl<typename std::basic_string<typename std::remove_const<Tchar>::type>, typename std::add_const<Tchar>::type> ReadAutoBuf(const std::array<Tchar, size> & buf) { |
| 148 |
return StringBufRefImpl<typename std::basic_string<typename std::remove_const<Tchar>::type>, typename std::add_const<Tchar>::type>(buf.data(), size); |
| 149 |
} |
| 150 |
template <typename Tchar, std::size_t size> |
| 151 |
inline StringBufRefImpl<typename std::basic_string<typename std::remove_const<Tchar>::type>, typename std::add_const<Tchar>::type> ReadAutoBuf(const Tchar (&buf)[size]) { |
| 152 |
return StringBufRefImpl<typename std::basic_string<typename std::remove_const<Tchar>::type>, typename std::add_const<Tchar>::type>(buf, size); |
| 153 |
} |
| 154 |
template <typename Tchar> |
| 155 |
inline StringBufRefImpl<typename std::basic_string<typename std::remove_const<Tchar>::type>, typename std::add_const<Tchar>::type> ReadAutoBuf(const Tchar * buf, std::size_t size) { |
| 156 |
return StringBufRefImpl<typename std::basic_string<typename std::remove_const<Tchar>::type>, typename std::add_const<Tchar>::type>(buf, size); |
| 157 |
} |
| 158 |
template <typename Tchar, std::size_t size> |
| 159 |
inline StringBufRefImpl<typename std::basic_string<typename std::remove_const<Tchar>::type>, Tchar> WriteAutoBuf(std::array<Tchar, size> & buf) { |
| 160 |
return StringBufRefImpl<typename std::basic_string<typename std::remove_const<Tchar>::type>, Tchar>(buf.data(), size); |
| 161 |
} |
| 162 |
template <typename Tchar, std::size_t size> |
| 163 |
inline StringBufRefImpl<typename std::basic_string<typename std::remove_const<Tchar>::type>, Tchar> WriteAutoBuf(Tchar (&buf)[size]) { |
| 164 |
return StringBufRefImpl<typename std::basic_string<typename std::remove_const<Tchar>::type>, Tchar>(buf, size); |
| 165 |
} |
| 166 |
template <typename Tchar> |
| 167 |
inline StringBufRefImpl<typename std::basic_string<typename std::remove_const<Tchar>::type>, Tchar> WriteAutoBuf(Tchar * buf, std::size_t size) { |
| 168 |
return StringBufRefImpl<typename std::basic_string<typename std::remove_const<Tchar>::type>, Tchar>(buf, size); |
| 169 |
} |
| 170 |
|
| 171 |
|
| 172 |
|
| 173 |
#if MPT_OS_WINDOWS |
| 174 |
|
| 175 |
template <typename Tchar, std::size_t size> |
| 176 |
inline StringBufRefImpl<typename mpt::windows_char_traits<typename std::remove_const<Tchar>::type>::string_type, typename std::add_const<Tchar>::type> ReadWinBuf(const std::array<Tchar, size> & buf) { |
| 177 |
return StringBufRefImpl<typename mpt::windows_char_traits<typename std::remove_const<Tchar>::type>::string_type, typename std::add_const<Tchar>::type>(buf.data(), size); |
| 178 |
} |
| 179 |
template <typename Tchar, std::size_t size> |
| 180 |
inline StringBufRefImpl<typename mpt::windows_char_traits<typename std::remove_const<Tchar>::type>::string_type, typename std::add_const<Tchar>::type> ReadWinBuf(const Tchar (&buf)[size]) { |
| 181 |
return StringBufRefImpl<typename mpt::windows_char_traits<typename std::remove_const<Tchar>::type>::string_type, typename std::add_const<Tchar>::type>(buf, size); |
| 182 |
} |
| 183 |
template <typename Tchar> |
| 184 |
inline StringBufRefImpl<typename mpt::windows_char_traits<typename std::remove_const<Tchar>::type>::string_type, typename std::add_const<Tchar>::type> ReadWinBuf(const Tchar * buf, std::size_t size) { |
| 185 |
return StringBufRefImpl<typename mpt::windows_char_traits<typename std::remove_const<Tchar>::type>::string_type, typename std::add_const<Tchar>::type>(buf, size); |
| 186 |
} |
| 187 |
template <typename Tchar, std::size_t size> |
| 188 |
inline StringBufRefImpl<typename mpt::windows_char_traits<typename std::remove_const<Tchar>::type>::string_type, Tchar> WriteWinBuf(std::array<Tchar, size> & buf) { |
| 189 |
return StringBufRefImpl<typename mpt::windows_char_traits<typename std::remove_const<Tchar>::type>::string_type, Tchar>(buf.data(), size); |
| 190 |
} |
| 191 |
template <typename Tchar, std::size_t size> |
| 192 |
inline StringBufRefImpl<typename mpt::windows_char_traits<typename std::remove_const<Tchar>::type>::string_type, Tchar> WriteWinBuf(Tchar (&buf)[size]) { |
| 193 |
return StringBufRefImpl<typename mpt::windows_char_traits<typename std::remove_const<Tchar>::type>::string_type, Tchar>(buf, size); |
| 194 |
} |
| 195 |
template <typename Tchar> |
| 196 |
inline StringBufRefImpl<typename mpt::windows_char_traits<typename std::remove_const<Tchar>::type>::string_type, Tchar> WriteWinBuf(Tchar * buf, std::size_t size) { |
| 197 |
return StringBufRefImpl<typename mpt::windows_char_traits<typename std::remove_const<Tchar>::type>::string_type, Tchar>(buf, size); |
| 198 |
} |
| 199 |
|
| 200 |
#endif // MPT_OS_WINDOWS |
| 201 |
|
| 202 |
|
| 203 |
|
| 204 |
#if MPT_DETECTED_MFC |
| 205 |
|
| 206 |
template <typename Tchar> |
| 207 |
class CStringBufRefImpl { |
| 208 |
private: |
| 209 |
Tchar * buf; |
| 210 |
std::size_t size; |
| 211 |
|
| 212 |
public: |
| 213 |
// cppcheck false-positive |
| 214 |
// cppcheck-suppress uninitMemberVar |
| 215 |
explicit CStringBufRefImpl(Tchar * buf_, std::size_t size_) |
| 216 |
: buf(buf_) |
| 217 |
, size(size_) { |
| 218 |
assert(size > 0); |
| 219 |
} |
| 220 |
CStringBufRefImpl(const CStringBufRefImpl &) = delete; |
| 221 |
CStringBufRefImpl(CStringBufRefImpl &&) = default; |
| 222 |
CStringBufRefImpl & operator=(const CStringBufRefImpl &) = delete; |
| 223 |
CStringBufRefImpl & operator=(CStringBufRefImpl &&) = delete; |
| 224 |
operator CString() const { |
| 225 |
std::size_t len = std::find(buf, buf + size, char_constants<Tchar>::null) - buf; // terminate at \0 |
| 226 |
return CString(buf, mpt::saturate_cast<int>(len)); |
| 227 |
} |
| 228 |
CStringBufRefImpl & operator=(const CString & str) { |
| 229 |
std::copy(str.GetString(), str.GetString() + std::min(static_cast<std::size_t>(str.GetLength()), size - 1), buf); |
| 230 |
std::fill(buf + std::min(static_cast<std::size_t>(str.GetLength()), size - 1), buf + size, char_constants<Tchar>::null); |
| 231 |
return *this; |
| 232 |
} |
| 233 |
}; |
| 234 |
|
| 235 |
template <typename Tchar> |
| 236 |
class CStringBufRefImpl<const Tchar> { |
| 237 |
private: |
| 238 |
const Tchar * buf; |
| 239 |
std::size_t size; |
| 240 |
|
| 241 |
public: |
| 242 |
// cppcheck false-positive |
| 243 |
// cppcheck-suppress uninitMemberVar |
| 244 |
explicit CStringBufRefImpl(const Tchar * buf_, std::size_t size_) |
| 245 |
: buf(buf_) |
| 246 |
, size(size_) { |
| 247 |
assert(size > 0); |
| 248 |
} |
| 249 |
CStringBufRefImpl(const CStringBufRefImpl &) = delete; |
| 250 |
CStringBufRefImpl(CStringBufRefImpl &&) = default; |
| 251 |
CStringBufRefImpl & operator=(const CStringBufRefImpl &) = delete; |
| 252 |
CStringBufRefImpl & operator=(CStringBufRefImpl &&) = delete; |
| 253 |
operator CString() const { |
| 254 |
std::size_t len = std::find(buf, buf + size, char_constants<Tchar>::null) - buf; // terminate at \0 |
| 255 |
return CString(buf, mpt::saturate_cast<int>(len)); |
| 256 |
} |
| 257 |
}; |
| 258 |
|
| 259 |
template <typename Tchar> |
| 260 |
struct make_string_type<CStringBufRefImpl<Tchar>> { |
| 261 |
using type = CString; |
| 262 |
}; |
| 263 |
|
| 264 |
template <typename Tchar> |
| 265 |
struct make_string_view_type<CStringBufRefImpl<Tchar>> { |
| 266 |
using type = CString; |
| 267 |
}; |
| 268 |
|
| 269 |
template <typename Tchar, std::size_t size> |
| 270 |
inline CStringBufRefImpl<typename std::add_const<Tchar>::type> ReadCStringBuf(const std::array<Tchar, size> & buf) { |
| 271 |
return CStringBufRefImpl<typename std::add_const<Tchar>::type>(buf.data(), size); |
| 272 |
} |
| 273 |
template <typename Tchar, std::size_t size> |
| 274 |
inline CStringBufRefImpl<typename std::add_const<Tchar>::type> ReadCStringBuf(const Tchar (&buf)[size]) { |
| 275 |
return CStringBufRefImpl<typename std::add_const<Tchar>::type>(buf, size); |
| 276 |
} |
| 277 |
template <typename Tchar> |
| 278 |
inline CStringBufRefImpl<typename std::add_const<Tchar>::type> ReadCStringBuf(const Tchar * buf, std::size_t size) { |
| 279 |
return CStringBufRefImpl<typename std::add_const<Tchar>::type>(buf, size); |
| 280 |
} |
| 281 |
template <typename Tchar, std::size_t size> |
| 282 |
inline CStringBufRefImpl<Tchar> WriteCStringBuf(std::array<Tchar, size> & buf) { |
| 283 |
return CStringBufRefImpl<Tchar>(buf.data(), size); |
| 284 |
} |
| 285 |
template <typename Tchar, std::size_t size> |
| 286 |
inline CStringBufRefImpl<Tchar> WriteCStringBuf(Tchar (&buf)[size]) { |
| 287 |
return CStringBufRefImpl<Tchar>(buf, size); |
| 288 |
} |
| 289 |
template <typename Tchar> |
| 290 |
inline CStringBufRefImpl<Tchar> WriteCStringBuf(Tchar * buf, std::size_t size) { |
| 291 |
return CStringBufRefImpl<Tchar>(buf, size); |
| 292 |
} |
| 293 |
|
| 294 |
#endif // MPT_DETECTED_MFC |
| 295 |
|
| 296 |
|
| 297 |
|
| 298 |
template <std::size_t len> |
| 299 |
struct charbuf { |
| 300 |
public: |
| 301 |
using Tchar = char; |
| 302 |
using char_type = Tchar; |
| 303 |
using string_type = std::basic_string<Tchar>; |
| 304 |
using string_view_type = std::basic_string_view<Tchar>; |
| 305 |
constexpr std::size_t static_length() const { |
| 306 |
return len; |
| 307 |
} |
| 308 |
|
| 309 |
public: |
| 310 |
Tchar buf[len]{}; |
| 311 |
|
| 312 |
public: |
| 313 |
constexpr charbuf() { |
| 314 |
for (std::size_t i = 0; i < len; ++i) { |
| 315 |
buf[i] = char_constants<Tchar>::null; |
| 316 |
} |
| 317 |
} |
| 318 |
constexpr charbuf(const charbuf &) = default; |
| 319 |
constexpr charbuf(charbuf &&) = default; |
| 320 |
constexpr charbuf & operator=(const charbuf &) = default; |
| 321 |
constexpr charbuf & operator=(charbuf &&) = default; |
| 322 |
const Tchar & operator[](std::size_t i) const { |
| 323 |
return buf[i]; |
| 324 |
} |
| 325 |
std::string str() const { |
| 326 |
return static_cast<std::string>(*this); |
| 327 |
} |
| 328 |
operator string_type() const { |
| 329 |
return mpt::ReadAutoBuf(buf); |
| 330 |
} |
| 331 |
explicit operator string_view_type() const { |
| 332 |
return static_cast<string_view_type>(mpt::ReadAutoBuf(buf)); |
| 333 |
} |
| 334 |
bool empty() const { |
| 335 |
return mpt::ReadAutoBuf(buf).empty(); |
| 336 |
} |
| 337 |
charbuf & operator=(const string_type & str) { |
| 338 |
mpt::WriteAutoBuf(buf) = str; |
| 339 |
return *this; |
| 340 |
} |
| 341 |
|
| 342 |
public: |
| 343 |
friend bool operator!=(const charbuf & a, const charbuf & b) { |
| 344 |
return static_cast<string_view_type>(a) != static_cast<string_view_type>(b); |
| 345 |
} |
| 346 |
friend bool operator!=(const std::string & a, const charbuf & b) { |
| 347 |
return a != static_cast<string_view_type>(b); |
| 348 |
} |
| 349 |
friend bool operator!=(const charbuf & a, const std::string & b) { |
| 350 |
return static_cast<string_view_type>(a) != b; |
| 351 |
} |
| 352 |
friend bool operator==(const charbuf & a, const charbuf & b) { |
| 353 |
return static_cast<string_view_type>(a) == static_cast<string_view_type>(b); |
| 354 |
} |
| 355 |
friend bool operator==(const std::string & a, const charbuf & b) { |
| 356 |
return a == static_cast<string_view_type>(b); |
| 357 |
} |
| 358 |
friend bool operator==(const charbuf & a, const std::string & b) { |
| 359 |
return static_cast<string_view_type>(a) == b; |
| 360 |
} |
| 361 |
}; |
| 362 |
|
| 363 |
template <std::size_t len> |
| 364 |
struct make_string_type<charbuf<len>> { |
| 365 |
using type = std::string; |
| 366 |
}; |
| 367 |
|
| 368 |
template <std::size_t len> |
| 369 |
struct make_string_view_type<charbuf<len>> { |
| 370 |
using type = std::string_view; |
| 371 |
}; |
| 372 |
|
| 373 |
|
| 374 |
} // namespace MPT_INLINE_NS |
| 375 |
} // namespace mpt |
| 376 |
|
| 377 |
|
| 378 |
|
| 379 |
#endif // MPT_STRING_BUFFER_HPP |