12#include "libxr_def.hpp"
17template <Print::Text Source,
typename... Args>
27template <
typename... Values>
36template <
typename T,
bool IsEnum = std::is_enum_v<T>>
45 using Type = std::underlying_type_t<T>;
54static constexpr bool runtime_string_always_false =
false;
56inline constexpr size_t runtime_string_unbounded_capacity =
57 std::numeric_limits<size_t>::max();
58inline constexpr size_t runtime_string_max_field_width =
59 std::numeric_limits<uint8_t>::max();
74[[nodiscard]]
constexpr size_t RuntimeStringFieldCapacity(Print::FormatPackKind pack)
76 constexpr size_t width = runtime_string_max_field_width;
77 constexpr size_t precision = runtime_string_max_field_width;
81 case Print::FormatPackKind::I32:
82 return 1U + precision;
83 case Print::FormatPackKind::I64:
84 return 1U + precision;
85 case Print::FormatPackKind::U32:
86 return 2U + precision;
87 case Print::FormatPackKind::U64:
88 return 2U + precision;
89 case Print::FormatPackKind::Pointer:
91 (precision > 2U *
sizeof(uintptr_t) ? precision : 2U *
sizeof(uintptr_t));
92 case Print::FormatPackKind::Character:
94 case Print::FormatPackKind::StringView:
95 return runtime_string_unbounded_capacity;
96 case Print::FormatPackKind::F32:
97 return 1U +
static_cast<size_t>(std::numeric_limits<float>::max_exponent10) + 2U +
99 case Print::FormatPackKind::F64:
100 return 1U +
static_cast<size_t>(std::numeric_limits<double>::max_exponent10) + 2U +
102 case Print::FormatPackKind::LongDouble:
103 return 1U +
static_cast<size_t>(std::numeric_limits<long double>::max_exponent10) +
107 return runtime_string_unbounded_capacity;
110[[nodiscard]]
constexpr bool RuntimeStringAddCapacity(
size_t& total,
size_t value)
112 if (value == runtime_string_unbounded_capacity ||
113 runtime_string_unbounded_capacity - total < value)
121template <
typename Built>
122[[nodiscard]]
consteval size_t RuntimeStringMaxFormattedSize(
size_t source_size)
124 size_t total = source_size;
125 for (
const auto& argument : Built::ArgumentList())
127 if (!RuntimeStringAddCapacity(total, RuntimeStringFieldCapacity(argument.pack)))
129 return runtime_string_unbounded_capacity;
150 using Decayed = std::remove_cvref_t<T>;
151 using Normalized =
typename RuntimeStringNormalized<Decayed>::Type;
153 static constexpr bool is_text =
154 std::is_array_v<Decayed> || std::is_same_v<Decayed, char*> ||
155 std::is_same_v<Decayed, const char*> || std::is_same_v<Decayed, std::string> ||
156 std::is_same_v<Decayed, std::string_view>;
158 static constexpr bool is_pointer =
159 std::is_pointer_v<Decayed> && !std::is_function_v<std::remove_pointer_t<Decayed>>;
161 static constexpr bool has_static_capacity =
163 (std::is_same_v<Decayed, std::nullptr_t> || is_pointer ||
164 std::is_integral_v<Normalized> || std::is_floating_point_v<Normalized>);
173template <
typename... Args>
176 template <
typename... CallArgs>
177 static constexpr bool matches = std::is_same_v<
188 std::string_view view = {};
203inline constexpr bool runtime_string_is_view_v =
212 char* data =
nullptr;
216 [[nodiscard]]
ErrorCode Write(std::string_view chunk)
218 if (capacity - size < chunk.size())
224 std::memcpy(data + size, chunk.data(), chunk.size());
225 size += chunk.size();
249template <
Print::Text Source =
"",
typename... Args>
253 static_assert((std::is_same_v<
254 Args,
typename Detail::RuntimeStringArgumentTraits<Args>::Decayed> &&
256 "LibXR::RuntimeStringView argument types must be unqualified "
259 "LibXR::RuntimeStringView formatted arguments cannot contain "
260 "runtime strings; use RuntimeStringView<> for text concatenation");
270 : data_(other.data_),
272 capacity_(other.capacity_),
273 status_(other.status_)
275 other.data_ =
nullptr;
285 requires(Source.
Size() == 0 &&
sizeof...(Args) == 0)
287 static_cast<void>(AssignCopy(text));
293 requires(Source.
Size() == 0 &&
sizeof...(Args) == 0)
295 static_cast<void>(AssignCopy(std::string_view(text, BoundedTextLength(text, N))));
301 requires(Source.
Size() == 0 &&
sizeof...(Args) == 0)
303 static_cast<void>(AssignCopy(std::string_view(text, BoundedTextLength(text, N))));
308 template <
typename CharPtr>
309 requires(Source.
Size() == 0 &&
sizeof...(Args) == 0 &&
310 (std::is_same_v<CharPtr, char*> || std::is_same_v<CharPtr, const char*>))
313 static_cast<void>(AssignCopy(text));
319 requires(Source.
Size() == 0 &&
sizeof...(Args) == 0)
321 static_cast<void>(AssignCopy(
static_cast<const char*
>(text)));
325 template <
typename First,
typename Second,
typename... Rest>
327 requires(Source.
Size() == 0 &&
sizeof...(Args) == 0)
329 static_cast<void>(AssignConcat(std::forward<First>(first),
330 std::forward<Second>(second),
331 std::forward<Rest>(rest)...));
338 template <
typename... CallArgs>
340 requires((Source.
Size() != 0 ||
sizeof...(Args) != 0) &&
344 "LibXR::RuntimeStringView format argument types do not match "
347 static constexpr size_t max_size =
348 Detail::RuntimeStringMaxFormattedSize<Built>(Source.
Size());
349 static_assert(max_size != Detail::runtime_string_unbounded_capacity,
350 "LibXR::RuntimeStringView cannot retain unbounded formatted "
353 return AssignFormatted(
354 max_size, [&](
auto& sink)
355 {
return Print::FormatTo<Source>(sink, std::forward<CallArgs>(args)...); });
362 template <
typename... CallArgs>
364 requires((Source.
Size() != 0 ||
sizeof...(Args) != 0) &&
368 "LibXR::RuntimeStringView printf argument types do not match "
371 static constexpr size_t max_size =
372 Detail::RuntimeStringMaxFormattedSize<Built>(Source.Size());
373 static_assert(max_size != Detail::runtime_string_unbounded_capacity,
374 "LibXR::RuntimeStringView cannot retain unbounded formatted "
377 return AssignFormatted(
378 max_size, [&](
auto& sink)
379 {
return Print::PrintfTo<Source>(sink, std::forward<CallArgs>(args)...); });
383 [[nodiscard]]
constexpr std::string_view
View()
const
385 return data_ ==
nullptr ? std::string_view{} : std::string_view(data_, size_);
389 [[nodiscard]]
const char*
CStr()
const {
return data_ ==
nullptr ?
"" : data_; }
391 [[nodiscard]]
constexpr size_t Size()
const {
return size_; }
393 [[nodiscard]]
constexpr bool Empty()
const {
return size_ == 0; }
397 [[nodiscard]]
constexpr operator std::string_view()
const {
return View(); }
399 [[nodiscard]]
operator const char*()
const {
return CStr(); }
409 if (data_ !=
nullptr)
433 [[nodiscard]]
ErrorCode EnsureCapacity(
size_t payload_size)
435 if (payload_size == std::numeric_limits<size_t>::max())
439 if (payload_size <= capacity_ && data_ !=
nullptr)
443 if (data_ !=
nullptr)
447 if (payload_size == 0)
452 data_ =
new char[payload_size + 1U];
453 capacity_ = payload_size;
461 [[nodiscard]]
ErrorCode AssignCopy(std::string_view text)
463 ErrorCode status = EnsureCapacity(text.size());
466 return SetFailure(status);
470 std::memcpy(data_, text.data(), text.size());
472 if (data_ !=
nullptr)
474 data_[text.size()] =
'\0';
486 [[nodiscard]]
ErrorCode AssignCopy(
const char* text)
489 : AssignCopy(std::
string_view(text, std::strlen(text)));
501 template <
typename... Parts>
502 [[nodiscard]]
ErrorCode AssignConcat(Parts&&... parts)
504 size_t total_size = 0;
507 auto count = [&](
auto&& part)
514 Detail::RuntimeStringTextPart text =
515 NormalizeConcatPart(std::forward<
decltype(part)>(part));
518 status = text.status;
520 else if (std::numeric_limits<size_t>::max() - total_size < text.view.size())
526 total_size += text.view.size();
529 (count(std::forward<Parts>(parts)), ...);
533 return SetFailure(status);
535 status = EnsureCapacity(total_size);
538 return SetFailure(status);
542 auto copy = [&](
auto&& part)
544 Detail::RuntimeStringTextPart text =
545 NormalizeConcatPart(std::forward<
decltype(part)>(part));
546 if (!text.view.empty())
548 std::memcpy(data_ + offset, text.view.data(), text.view.size());
549 offset += text.view.size();
552 (copy(std::forward<Parts>(parts)), ...);
554 if (data_ !=
nullptr)
556 data_[total_size] =
'\0';
573 template <
typename WriteFn>
574 [[nodiscard]]
ErrorCode AssignFormatted(
size_t max_size, WriteFn&& write)
576 if (data_ ==
nullptr)
578 ErrorCode status = EnsureCapacity(max_size);
581 return SetFailure(status);
585 Detail::RuntimeStringBufferSink sink{.data = data_, .capacity = capacity_};
589 return SetFailure(status);
591 if (data_ !=
nullptr)
593 data_[sink.size] =
'\0';
605 [[nodiscard]]
static constexpr size_t BoundedTextLength(
const char* text,
606 size_t bound)
noexcept
609 while (size < bound && text[size] !=
'\0')
623 template <
typename T>
624 [[nodiscard]]
static constexpr Detail::RuntimeStringTextPart NormalizeConcatPart(
627 using Bare = std::remove_cvref_t<T>;
628 if constexpr (Detail::runtime_string_is_view_v<T>)
630 const auto& retained = text;
632 ? Detail::RuntimeStringTextPart{.view = retained.View()}
633 : Detail::RuntimeStringTextPart{.status = retained.Status()};
635 else if constexpr (std::is_array_v<Bare> &&
636 std::is_same_v<std::remove_cv_t<std::remove_extent_t<Bare>>,
char>)
639 std::string_view(text, BoundedTextLength(text, std::extent_v<Bare>))};
641 else if constexpr (std::is_same_v<Bare, std::string_view>)
643 return {.view = text};
645 else if constexpr (std::is_same_v<Bare, std::string>)
647 return {.view = std::string_view(text.data(), text.size())};
649 else if constexpr (std::is_same_v<Bare, std::nullptr_t>)
653 else if constexpr (std::is_same_v<Bare, char*> || std::is_same_v<Bare, const char*>)
655 return text ==
nullptr
657 : Detail::RuntimeStringTextPart{
658 .view = std::string_view(text, std::strlen(text))};
662 static_assert(Detail::runtime_string_always_false<T>,
663 "RuntimeStringView constructor only accepts text-like parts. "
664 "Use Reformat() or Reprintf() for numeric formatting.");
671 char* data_ =
nullptr;
677 size_t capacity_ = 0;
691template <
typename First,
typename Second,
typename... Rest>
static consteval bool Matches()
判断 Args... 是否与启用的转换项精确匹配 / Return whether Args... exactly match the enabled conversions
运行期构造、长期保留的 NUL 结尾字符串视图。 Runtime-built retained NUL-terminated string view.
constexpr RuntimeStringView()=default
构造一个空的有效视图 / Constructs an empty valid view.
constexpr ErrorCode Status() const
返回最近一次操作状态 / Returns the latest operation status.
RuntimeStringView(First &&first, Second &&second, Rest &&... rest)
拼接多个文本片段到保留存储 / Concatenates text parts into retained storage.
RuntimeStringView(const char(&text)[N])
按文本语义拷贝一个有界只读字符数组 / Copies one bounded const char array as text.
ErrorCode Reformat(CallArgs &&... args)
使用绑定的 brace-style 格式重写当前内容。 Rewrite current content with the bound brace-style format.
RuntimeStringView(std::nullptr_t text)
constexpr size_t Size() const
返回不含结尾 NUL 的文本长度 / Returns the text size excluding the trailing NUL.
const char * CStr() const
返回 NUL 结尾 C 字符串 / Returns a NUL-terminated C string.
RuntimeStringView(CharPtr text)
RuntimeStringView(RuntimeStringView &&other) noexcept
移动构造并接管保留存储句柄 / Move-constructs by taking the retained storage handle.
ErrorCode Reprintf(CallArgs &&... args)
使用绑定的 printf-style 格式重写当前内容。 Rewrite current content with the bound printf-style format.
constexpr operator std::string_view() const
转换为只读字符串视图 / Converts to a read-only string view.
RuntimeStringView(std::string_view text)
拷贝文本到保留存储 / Copies text into retained storage.
constexpr std::string_view View() const
返回当前只读视图 / Returns the current read-only view.
RuntimeStringView(char(&text)[N])
按文本语义拷贝一个有界可变字符数组 / Copies one bounded mutable char array as text.
constexpr bool Empty() const
返回当前可见文本是否为空 / Returns whether the current visible text is empty.
LibXR 命名空间 / LibXR namespace.
ErrorCode
定义错误码枚举 / Defines an enumeration for error codes
@ OUT_OF_RANGE
超出范围 / Out of range
@ PTR_NULL
空指针 / Null pointer
@ OK
操作成功 / Operation successful
@ ARG_ERR
参数错误 / Argument error
RuntimeStringView(std::string_view) -> RuntimeStringView<>
格式化保留字符串参数的编译期类型特征。 Compile-time traits for formatted retained-string arguments.
检查刷新调用是否精确使用 RuntimeStringView<Source, Args...> 绑定的参数类型。 Checks that a rewrite call uses exactly the ...
格式化第二遍使用的保留存储写入端。 Sink used by the second formatting pass to fill retained storage.
为容量探测把枚举参数归一化到底层整数类型。 Normalizes enum arguments to their underlying type for capacity probes.
拷贝/拼接路径使用的归一化文本片段。 Normalized text part used by the copy/concatenation path.
仅用于精确比较参数类型列表的标签类型。 Type-list tag used only for exact argument-list comparison.
作为 printf 格式源的结构化字符串字面量包装 / Structural literal wrapper used as the NTTP source for printf formats
constexpr size_t Size() const
返回不含结尾零字节的格式串长度 / Return the format length without the terminating zero byte