5#include "../SqlColumnTypeDefinitions.hpp"
7#include "UnicodeConverter.hpp"
14enum class SqlFixedStringMode : uint8_t
17 FIXED_SIZE_RIGHT_TRIMMED,
28template <std::
size_t N,
typename T =
char, SqlFixedStringMode Mode = SqlFixedStringMode::FIXED_SIZE>
33 std::size_t _size = 0;
38 using const_iterator = T
const*;
39 using pointer_type = T*;
40 using const_pointer_type = T
const*;
42 static constexpr std::size_t Capacity = N;
43 static constexpr SqlFixedStringMode PostRetrieveOperation = Mode;
46 template <std::
size_t SourceSize>
47 constexpr LIGHTWEIGHT_FORCE_INLINE
SqlFixedString(T
const (&text)[SourceSize]):
48 _size { SourceSize - 1 }
50 static_assert(SourceSize <= N + 1,
"RHS string size must not exceed target string's capacity.");
51 std::copy_n(text, SourceSize, _data);
54 LIGHTWEIGHT_FORCE_INLINE
constexpr SqlFixedString() noexcept = default;
59 LIGHTWEIGHT_FORCE_INLINE constexpr ~
SqlFixedString() noexcept = default;
62 LIGHTWEIGHT_FORCE_INLINE constexpr
SqlFixedString(std::basic_string_view<T> s) noexcept:
63 _size { (std::min) (N, s.size()) }
65 std::copy_n(s.data(), _size, _data);
69 LIGHTWEIGHT_FORCE_INLINE
constexpr SqlFixedString(std::basic_string<T>
const& s)
noexcept:
70 _size { (std::min) (N, s.size()) }
72 std::copy_n(s.data(), _size, _data);
76 LIGHTWEIGHT_FORCE_INLINE
constexpr SqlFixedString(T
const* s, std::size_t len)
noexcept:
77 _size { (std::min) (N, len) }
79 std::copy_n(s, _size, _data);
83 LIGHTWEIGHT_FORCE_INLINE
constexpr SqlFixedString(T
const* s, T
const* e)
noexcept:
84 _size { (std::min) (N,
static_cast<std::size_t
>(e - s)) }
86 std::copy(s, e, _data);
89 LIGHTWEIGHT_FORCE_INLINE
void reserve(std::size_t
capacity)
92 throw std::length_error(std::format(
"SqlFixedString: capacity {} exceeds maximum capacity {}",
capacity, N));
96 [[nodiscard]] LIGHTWEIGHT_FORCE_INLINE
constexpr bool empty() const noexcept
102 [[nodiscard]] LIGHTWEIGHT_FORCE_INLINE
constexpr std::size_t
size() const noexcept
107 LIGHTWEIGHT_FORCE_INLINE
void setsize(std::size_t n)
noexcept
109 auto const newSize = (std::min) (n, N);
111 _data[newSize] =
'\0';
118 LIGHTWEIGHT_FORCE_INLINE
constexpr void resize(std::size_t n)
noexcept
120 auto const newSize = (std::min) (n, N);
122 _data[newSize] =
'\0';
126 [[nodiscard]] LIGHTWEIGHT_FORCE_INLINE
constexpr std::size_t
capacity() const noexcept
132 LIGHTWEIGHT_FORCE_INLINE
constexpr void clear() noexcept
138 template <std::
size_t SourceSize>
139 LIGHTWEIGHT_FORCE_INLINE
constexpr void assign(T
const (&source)[SourceSize])
noexcept
141 static_assert(SourceSize <= N + 1,
"Source string must not overflow the target string's capacity.");
142 _size = SourceSize - 1;
143 std::copy_n(source, SourceSize, _data);
147 LIGHTWEIGHT_FORCE_INLINE
constexpr void assign(std::basic_string_view<T> s)
noexcept
149 _size = (std::min) (N, s.size());
150 std::copy_n(s.data(), _size, _data);
154 LIGHTWEIGHT_FORCE_INLINE
constexpr void push_back(T c)
noexcept
164 LIGHTWEIGHT_FORCE_INLINE
constexpr void pop_back() noexcept
171 [[nodiscard]] LIGHTWEIGHT_FORCE_INLINE
constexpr std::basic_string_view<T>
substr(
172 std::size_t offset = 0, std::size_t count = (std::numeric_limits<std::size_t>::max)()) const noexcept
176 if (count == (std::numeric_limits<std::size_t>::max)())
177 return std::basic_string_view<T>(_data + offset, _size - offset);
178 if (offset + count > _size)
179 return std::basic_string_view<T>(_data + offset, _size - offset);
180 return std::basic_string_view<T>(_data + offset, count);
184 [[nodiscard]] LIGHTWEIGHT_FORCE_INLINE
constexpr std::basic_string_view<T>
str() const noexcept
186 return std::basic_string_view<T> { _data, _size };
190 [[nodiscard]] LIGHTWEIGHT_FORCE_INLINE
constexpr pointer_type c_str() noexcept { _data[_size] =
'\0';
return _data; }
191 [[nodiscard]] LIGHTWEIGHT_FORCE_INLINE
constexpr pointer_type data() noexcept {
return _data; }
192 [[nodiscard]] LIGHTWEIGHT_FORCE_INLINE
constexpr iterator begin() noexcept {
return _data; }
193 [[nodiscard]] LIGHTWEIGHT_FORCE_INLINE
constexpr iterator end() noexcept {
return _data +
size(); }
194 [[nodiscard]] LIGHTWEIGHT_FORCE_INLINE
constexpr T& at(std::size_t i)
noexcept {
return _data[i]; }
195 [[nodiscard]] LIGHTWEIGHT_FORCE_INLINE
constexpr T& operator[](std::size_t i)
noexcept {
return _data[i]; }
197 [[nodiscard]] LIGHTWEIGHT_FORCE_INLINE
constexpr const_pointer_type c_str() const noexcept {
return _data; }
198 [[nodiscard]] LIGHTWEIGHT_FORCE_INLINE
constexpr const_pointer_type data() const noexcept {
return _data; }
199 [[nodiscard]] LIGHTWEIGHT_FORCE_INLINE
constexpr const_iterator begin() const noexcept {
return _data; }
200 [[nodiscard]] LIGHTWEIGHT_FORCE_INLINE
constexpr const_iterator end() const noexcept {
return _data +
size(); }
201 [[nodiscard]] LIGHTWEIGHT_FORCE_INLINE
constexpr T
const& at(std::size_t i)
const noexcept {
return _data[i]; }
202 [[nodiscard]] LIGHTWEIGHT_FORCE_INLINE
constexpr T
const& operator[](std::size_t i)
const noexcept {
return _data[i]; }
206 [[nodiscard]] LIGHTWEIGHT_FORCE_INLINE
constexpr std::basic_string<T>
ToString() const noexcept
208 return { _data, _size };
212 [[nodiscard]] LIGHTWEIGHT_FORCE_INLINE
constexpr explicit operator std::basic_string<T>() const noexcept
218 [[nodiscard]] LIGHTWEIGHT_FORCE_INLINE
constexpr std::basic_string_view<T>
ToStringView() const noexcept
220 return { _data, _size };
224 [[nodiscard]] LIGHTWEIGHT_FORCE_INLINE
constexpr explicit operator std::basic_string_view<T>() const noexcept
229 template <std::
size_t OtherSize, SqlFixedStringMode OtherMode>
230 LIGHTWEIGHT_FORCE_INLINE std::weak_ordering operator<=>(
233 if ((
void*)
this == (
void*) &other) [[unlikely]]
234 return std::weak_ordering::equivalent;
236 for (
auto const i: std::views::iota(0U, (std::min) (
size(), other.
size())))
237 if (auto const cmp = _data[i] <=> other._data[i]; cmp != std::weak_ordering::equivalent) [[unlikely]]
239 return size() <=> other.size();
242 template <std::
size_t OtherSize, SqlFixedStringMode OtherMode>
245 return (*this <=> other) == std::weak_ordering::equivalent;
248 template <std::
size_t OtherSize, SqlFixedStringMode OtherMode>
251 return !(*
this == other);
254 LIGHTWEIGHT_FORCE_INLINE
constexpr bool operator==(std::basic_string_view<T> other)
const noexcept
256 return (
substr() <=> other) == std::weak_ordering::equivalent;
259 LIGHTWEIGHT_FORCE_INLINE
constexpr bool operator!=(std::basic_string_view<T> other)
const noexcept
261 return !(*
this == other);
265template <std::
size_t N,
typename CharT, SqlFixedStringMode Mode>
270 return { str.data(), str.size() };
278struct IsSqlFixedStringTypeImpl: std::false_type
282template <std::
size_t N,
typename T, SqlFixedStringMode Mode>
283struct IsSqlFixedStringTypeImpl<
SqlFixedString<N, T, Mode>>: std::true_type
290constexpr bool IsSqlFixedString = detail::IsSqlFixedStringTypeImpl<T>::value;
295template <std::
size_t N>
301template <std::
size_t N>
307template <std::
size_t N>
313template <std::
size_t N>
319template <std::
size_t N,
typename T =
char>
322template <std::
size_t N,
typename T =
char>
325template <std::
size_t N,
typename T, SqlFixedStringMode Mode>
330 static constexpr auto ColumnType = []()
constexpr -> SqlColumnTypeDefinition {
331 if constexpr (std::same_as<CharType, char>)
333 if constexpr (Mode == SqlFixedStringMode::VARIABLE_SIZE)
334 return SqlColumnTypeDefinitions::Varchar { N };
336 return SqlColumnTypeDefinitions::Char { N };
340 if constexpr (Mode == SqlFixedStringMode::VARIABLE_SIZE)
341 return SqlColumnTypeDefinitions::NVarchar { N };
343 return SqlColumnTypeDefinitions::NChar { N };
347 static CharType
const* Data(ValueType
const* str)
noexcept
352 static CharType* Data(ValueType* str)
noexcept
357 static SQLULEN Size(ValueType
const* str)
noexcept
362 static void Clear(ValueType* str)
noexcept
367 static void Reserve(ValueType* str,
size_t capacity)
noexcept
369 str->reserve((std::min) (N, capacity));
370 str->resize((std::min) (N, capacity));
373 static void Resize(ValueType* str, SQLLEN indicator)
noexcept
375 str->resize(indicator);
378 static void PostProcessOutputColumn(ValueType* result, SQLLEN indicator)
389 auto const len = (std::min) (N,
static_cast<std::size_t
>(indicator) /
sizeof(CharType));
390 result->setsize(len);
392 if constexpr (Mode == SqlFixedStringMode::FIXED_SIZE_RIGHT_TRIMMED)
394 TrimRight(result, indicator);
401 LIGHTWEIGHT_FORCE_INLINE
static void TrimRight(ValueType* boundOutputString, SQLLEN indicator)
noexcept
403 size_t n = (std::min) ((
size_t) indicator, N - 1);
404 while (n > 0 && std::isspace((*boundOutputString)[n - 1]))
406 boundOutputString->setsize(n);
410template <std::
size_t N,
typename T, SqlFixedStringMode P>
411struct std::formatter<
SqlFixedString<N, T, P>>: std::formatter<std::string>
414 auto format(value_type
const& text, format_context& ctx)
const -> format_context::iterator
416 if constexpr (std::same_as<T, wchar_t>)
417 return std::formatter<std::string>::format((
char const*)
ToUtf8(text.ToStringView()).c_str(), ctx);
419 return std::formatter<std::string>::format(text.c_str(), ctx);
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 void assign(T const (&source)[SourceSize]) noexcept
Assigns a string literal to the string.
LIGHTWEIGHT_FORCE_INLINE constexpr void resize(std::size_t n) noexcept
LIGHTWEIGHT_FORCE_INLINE constexpr void assign(std::basic_string_view< T > s) noexcept
Assigns a string view to the string.
LIGHTWEIGHT_FORCE_INLINE constexpr bool empty() const noexcept
Tests if the string is empty.
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 std::size_t size() const noexcept
Returns the size of the string.
LIGHTWEIGHT_FORCE_INLINE constexpr void push_back(T c) noexcept
Appends a character to the string.
constexpr LIGHTWEIGHT_FORCE_INLINE SqlFixedString(T const (&text)[SourceSize])
Constructs a fixed-size string from a string literal.
LIGHTWEIGHT_FORCE_INLINE constexpr void pop_back() noexcept
Removes the last character from 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::size_t capacity() const noexcept
Returns the maximum capacity of 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::basic_string< T > ToString() const noexcept
Returns a std::basic_string<T> from the string.
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 SqlFixedString(T const *s, std::size_t len) noexcept
Constructs a fixed-size string from a string pointer and length.
LIGHTWEIGHT_API std::u8string ToUtf8(std::u32string_view u32InputString)