5#include "../SqlColumnTypeDefinitions.hpp"
7#include "StringInterface.hpp"
8#include "UnicodeConverter.hpp"
18enum class SqlFixedStringMode : uint8_t
21 FIXED_SIZE_RIGHT_TRIMMED,
32template <std::
size_t N,
typename T =
char, SqlFixedStringMode Mode = SqlFixedStringMode::FIXED_SIZE>
37 std::size_t _size = 0;
42 using const_iterator = T
const*;
43 using pointer_type = T*;
44 using const_pointer_type = T
const*;
46 static constexpr std::size_t Capacity = N;
47 static constexpr SqlFixedStringMode PostRetrieveOperation = Mode;
50 template <std::
size_t SourceSize>
51 constexpr LIGHTWEIGHT_FORCE_INLINE
SqlFixedString(T
const (&text)[SourceSize]):
52 _size { SourceSize - 1 }
54 static_assert(SourceSize <= N + 1,
"RHS string size must not exceed target string's capacity.");
55 std::copy_n(text, SourceSize, _data);
58 LIGHTWEIGHT_FORCE_INLINE
constexpr SqlFixedString() noexcept = default;
63 LIGHTWEIGHT_FORCE_INLINE constexpr ~
SqlFixedString() noexcept = default;
66 LIGHTWEIGHT_FORCE_INLINE constexpr
SqlFixedString(std::basic_string_view<T> s) noexcept:
67 _size { (std::min) (N, s.size()) }
69 std::copy_n(s.data(), _size, _data);
73 LIGHTWEIGHT_FORCE_INLINE
constexpr SqlFixedString(std::basic_string<T>
const& s)
noexcept:
74 _size { (std::min) (N, s.size()) }
76 std::copy_n(s.data(), _size, _data);
80 LIGHTWEIGHT_FORCE_INLINE
constexpr SqlFixedString(T
const* s, std::size_t len)
noexcept:
81 _size { (std::min) (N, len) }
83 std::copy_n(s, _size, _data);
87 LIGHTWEIGHT_FORCE_INLINE
constexpr SqlFixedString(T
const* s, T
const* e)
noexcept:
88 _size { (std::min) (N,
static_cast<std::size_t
>(e - s)) }
90 std::copy(s, e, _data);
93 LIGHTWEIGHT_FORCE_INLINE
void reserve(std::size_t
capacity)
96 throw std::length_error(std::format(
"SqlFixedString: capacity {} exceeds maximum capacity {}",
capacity, N));
100 [[nodiscard]] LIGHTWEIGHT_FORCE_INLINE
constexpr bool empty() const noexcept
106 [[nodiscard]] LIGHTWEIGHT_FORCE_INLINE
constexpr std::size_t
size() const noexcept
112 LIGHTWEIGHT_FORCE_INLINE
void setsize(std::size_t n)
noexcept
114 auto const newSize = (std::min) (n, N);
116 _data[newSize] =
'\0';
123 LIGHTWEIGHT_FORCE_INLINE
constexpr void resize(std::size_t n)
noexcept
125 auto const newSize = (std::min) (n, N);
127 _data[newSize] =
'\0';
131 [[nodiscard]] LIGHTWEIGHT_FORCE_INLINE
constexpr std::size_t
capacity() const noexcept
137 LIGHTWEIGHT_FORCE_INLINE
constexpr void clear() noexcept
159 LIGHTWEIGHT_FORCE_INLINE
constexpr void push_back(T c)
noexcept
169 LIGHTWEIGHT_FORCE_INLINE
constexpr void pop_back() noexcept
176 [[nodiscard]] LIGHTWEIGHT_FORCE_INLINE
constexpr std::basic_string_view<T>
substr(
177 std::size_t offset = 0, std::size_t count = (std::numeric_limits<std::size_t>::max)()) const noexcept
181 if (count == (std::numeric_limits<std::size_t>::max)())
182 return std::basic_string_view<T>(_data + offset, _size - offset);
183 if (offset + count > _size)
184 return std::basic_string_view<T>(_data + offset, _size - offset);
185 return std::basic_string_view<T>(_data + offset, count);
189 [[nodiscard]] LIGHTWEIGHT_FORCE_INLINE
constexpr std::basic_string_view<T>
str() const noexcept
191 return std::basic_string_view<T> { _data, _size };
195 [[nodiscard]] LIGHTWEIGHT_FORCE_INLINE
constexpr pointer_type data() noexcept {
return _data; }
196 [[nodiscard]] LIGHTWEIGHT_FORCE_INLINE
constexpr iterator begin() noexcept {
return _data; }
197 [[nodiscard]] LIGHTWEIGHT_FORCE_INLINE
constexpr iterator end() noexcept {
return _data +
size(); }
198 [[nodiscard]] LIGHTWEIGHT_FORCE_INLINE
constexpr T& operator[](std::size_t i)
noexcept {
return _data[i]; }
200 [[nodiscard]] LIGHTWEIGHT_FORCE_INLINE
constexpr const_pointer_type c_str() const noexcept {
return _data; }
201 [[nodiscard]] LIGHTWEIGHT_FORCE_INLINE
constexpr const_pointer_type data() const noexcept {
return _data; }
202 [[nodiscard]] LIGHTWEIGHT_FORCE_INLINE
constexpr const_iterator begin() const noexcept {
return _data; }
203 [[nodiscard]] LIGHTWEIGHT_FORCE_INLINE
constexpr const_iterator end() const noexcept {
return _data +
size(); }
204 [[nodiscard]] LIGHTWEIGHT_FORCE_INLINE
constexpr T
const& operator[](std::size_t i)
const noexcept {
return _data[i]; }
208 [[nodiscard]] LIGHTWEIGHT_FORCE_INLINE
constexpr std::basic_string<T>
ToString() const noexcept
210 return { _data, _size };
214 [[nodiscard]] LIGHTWEIGHT_FORCE_INLINE
constexpr explicit operator std::basic_string<T>() const noexcept
220 [[nodiscard]] LIGHTWEIGHT_FORCE_INLINE
constexpr std::basic_string_view<T>
ToStringView() const noexcept
222 return { _data, _size };
226 [[nodiscard]] LIGHTWEIGHT_FORCE_INLINE
constexpr explicit operator std::basic_string_view<T>() const noexcept
231 template <std::
size_t OtherSize, SqlFixedStringMode OtherMode>
232 LIGHTWEIGHT_FORCE_INLINE std::weak_ordering operator<=>(
233 SqlFixedString<OtherSize, T, OtherMode>
const& other)
const noexcept
235 if ((
void*)
this == (
void*) &other) [[unlikely]]
236 return std::weak_ordering::equivalent;
238 for (
auto const i: std::views::iota(0U, (std::min) (
size(), other.
size())))
239 if (auto const cmp = _data[i] <=> other._data[i]; cmp != std::weak_ordering::equivalent) [[unlikely]]
241 return size() <=> other.size();
244 template <std::
size_t OtherSize, SqlFixedStringMode OtherMode>
245 LIGHTWEIGHT_FORCE_INLINE
constexpr bool operator==(SqlFixedString<OtherSize, T, OtherMode>
const& other)
const noexcept
247 return (*this <=> other) == std::weak_ordering::equivalent;
250 template <std::
size_t OtherSize, SqlFixedStringMode OtherMode>
251 LIGHTWEIGHT_FORCE_INLINE
constexpr bool operator!=(SqlFixedString<OtherSize, T, OtherMode>
const& other)
const noexcept
253 return !(*
this == other);
256 LIGHTWEIGHT_FORCE_INLINE
constexpr bool operator==(std::basic_string_view<T> other)
const noexcept
258 return (
substr() <=> other) == std::weak_ordering::equivalent;
261 LIGHTWEIGHT_FORCE_INLINE
constexpr bool operator!=(std::basic_string_view<T> other)
const noexcept
263 return !(*
this == other);
267static_assert(SqlStringInterface<SqlFixedString<10>>);
269template <std::
size_t N,
typename CharT, SqlFixedStringMode Mode>
270struct detail::SqlViewHelper<SqlFixedString<N, CharT, Mode>>
272 static LIGHTWEIGHT_FORCE_INLINE std::basic_string_view<CharT> View(SqlFixedString<N, CharT, Mode>
const& str)
noexcept
274 return { str.data(), str.size() };
282 struct IsSqlFixedStringTypeImpl: std::false_type
286 template <std::
size_t N,
typename T, SqlFixedStringMode Mode>
287 struct IsSqlFixedStringTypeImpl<SqlFixedString<N, T, Mode>>: std::true_type
294constexpr bool IsSqlFixedString = detail::IsSqlFixedStringTypeImpl<T>::value;
299template <std::
size_t N>
305template <std::
size_t N>
311template <std::
size_t N>
317template <std::
size_t N>
323template <std::
size_t N>
329template <std::
size_t N>
332template <std::
size_t N,
typename T =
char>
335template <std::
size_t N,
typename T, SqlFixedStringMode Mode>
340 static constexpr auto ColumnType = []()
constexpr -> SqlColumnTypeDefinition {
341 if constexpr (std::same_as<CharType, char>)
343 if constexpr (Mode == SqlFixedStringMode::VARIABLE_SIZE)
344 return SqlColumnTypeDefinitions::Varchar { N };
346 return SqlColumnTypeDefinitions::Char { N };
350 if constexpr (Mode == SqlFixedStringMode::VARIABLE_SIZE)
351 return SqlColumnTypeDefinitions::NVarchar { N };
353 return SqlColumnTypeDefinitions::NChar { N };
357 static CharType
const* Data(ValueType
const* str)
noexcept
362 static CharType* Data(ValueType* str)
noexcept
367 static SQLULEN Size(ValueType
const* str)
noexcept
372 static void Clear(ValueType* str)
noexcept
377 static void Reserve(ValueType* str,
size_t capacity)
noexcept
379 str->reserve((std::min) (N, capacity));
380 str->resize((std::min) (N, capacity));
383 static void Resize(ValueType* str, SQLLEN indicator)
noexcept
385 str->resize(indicator);
388 static void PostProcessOutputColumn(ValueType* result, SQLLEN indicator)
399 auto const len = (std::min) (N,
static_cast<std::size_t
>(indicator) /
sizeof(CharType));
400 result->setsize(len);
402 if constexpr (Mode == SqlFixedStringMode::FIXED_SIZE_RIGHT_TRIMMED)
404 TrimRight(result, indicator);
409 StripTrailingNulls(result);
416 LIGHTWEIGHT_FORCE_INLINE
static void StripTrailingNulls(ValueType* str)
noexcept
418 size_t n = str->size();
419 while (n > 0 && (*str)[n - 1] ==
'\0')
424 LIGHTWEIGHT_FORCE_INLINE
static void TrimRight(ValueType* boundOutputString, SQLLEN indicator)
noexcept
427 size_t n = (std::min) (
static_cast<size_t>(indicator) /
sizeof(CharType), N - 1);
429 size_t n = std::min(
static_cast<size_t>(indicator), N - 1);
432 while (n > 0 && (std::isspace((*boundOutputString)[n - 1]) || (*boundOutputString)[n - 1] ==
'\0'))
434 boundOutputString->setsize(n);
440template <std::
size_t N,
typename T, Lightweight::SqlFixedStringMode P>
441struct std::formatter<Lightweight::SqlFixedString<N, T, P>>: std::formatter<std::string>
444 auto format(value_type
const& text, format_context& ctx)
const -> format_context::iterator
446 if constexpr (std::same_as<T, char16_t> || std::same_as<T, char32_t> || std::same_as<T, wchar_t>)
449 auto const stdstring = std::string(
reinterpret_cast<char const*
>(utf8.data()), utf8.size());
450 return std::formatter<std::string>::format(stdstring, ctx);
453 return std::formatter<std::string>::format(std::string(text.data(), text.size()), ctx);
LIGHTWEIGHT_FORCE_INLINE constexpr SqlFixedString(T const *s, std::size_t len) noexcept
Constructs a fixed-size string from a string pointer and length.
LIGHTWEIGHT_FORCE_INLINE constexpr std::size_t capacity() const noexcept
Returns the maximum capacity of the string.
LIGHTWEIGHT_FORCE_INLINE constexpr SqlFixedString(T const *s, T const *e) noexcept
Constructs a fixed-size string from a string pointer and end pointer.
LIGHTWEIGHT_FORCE_INLINE constexpr std::basic_string_view< T > str() const noexcept
Returns a string view of the string.
LIGHTWEIGHT_FORCE_INLINE constexpr void clear() noexcept
Clears the string.
LIGHTWEIGHT_FORCE_INLINE constexpr void resize(std::size_t n) noexcept
LIGHTWEIGHT_FORCE_INLINE constexpr void push_back(T c) noexcept
Appends a character to the string.
LIGHTWEIGHT_FORCE_INLINE constexpr bool empty() const noexcept
Tests if the string is empty.
constexpr LIGHTWEIGHT_FORCE_INLINE SqlFixedString(T const (&text)[SourceSize])
Constructs a fixed-size string from a string literal.
LIGHTWEIGHT_FORCE_INLINE constexpr std::basic_string_view< T > ToStringView() const noexcept
Returns a std::basic_string_view<T> from the string.
LIGHTWEIGHT_FORCE_INLINE constexpr std::basic_string_view< T > substr(std::size_t offset=0, std::size_t count=(std::numeric_limits< std::size_t >::max)()) const noexcept
Returns a sub string of the string.
LIGHTWEIGHT_FORCE_INLINE constexpr std::basic_string< T > ToString() const noexcept
Returns a std::basic_string<T> from the string.
LIGHTWEIGHT_FORCE_INLINE constexpr SqlFixedString(std::basic_string< T > const &s) noexcept
Constructs a fixed-size string from a string.
LIGHTWEIGHT_FORCE_INLINE constexpr std::size_t size() const noexcept
Returns the size of the string.
LIGHTWEIGHT_FORCE_INLINE constexpr void pop_back() noexcept
Removes the last character from the string.
LIGHTWEIGHT_API std::u8string ToUtf8(std::u32string_view u32InputString)