Go to the documentation of this file.
9 #ifndef EAGINE_STRING_SPAN_HPP
10 #define EAGINE_STRING_SPAN_HPP
16 #include <string_view>
24 template <
typename C,
typename P = C*,
typename S = span_
size_t>
30 using string_type = std::basic_string<std::remove_const_t<C>>;
44 typename = std::enable_if_t<
45 !std::is_array_v<R> && std::is_convertible_v<R, P> &&
46 std::is_same_v<std::remove_const_t<std::remove_pointer_t<R>>,
char>>>
48 : base(
addr, -limit_cast<S>(std::strlen(
addr))) {}
51 template <std::
size_t N>
54 static_cast<P
>(array),
55 array[N - 1] == C(0) ? 1 - limit_cast<S>(N) : limit_cast<S>(N)) {}
58 template <std::
size_t N>
60 :
base(
static_cast<P
>(array), limit_cast<S>(n)) {}
64 :
base(
static_cast<P
>(str.c_str()), -limit_cast<S>(str.size())) {}
72 typename = std::enable_if_t<
73 memory::has_span_data_member_v<Str, C> &&
74 memory::has_span_size_member_v<Str>>>
76 :
base(
static_cast<P
>(str.data()), limit_cast<S>(str.size())) {}
84 typename = std::enable_if_t<
85 memory::has_span_data_member_v<Str, C> &&
86 memory::has_span_size_member_v<Str>>>
88 :
base(
static_cast<P
>(str.data()), limit_cast<S>(str.size())) {}
120 template <
typename C,
typename P,
typename S>
122 -> std::basic_string_view<std::remove_const_t<C>> {
128 template <
typename C,
typename P,
typename S>
130 -> std::basic_string<std::remove_const_t<C>> {
136 template <
typename C,
typename T,
typename A,
typename P,
typename S>
138 std::basic_string<C, T, A>& str,
140 str.assign(spn.data(),
std_size(spn.size()));
146 template <
typename C,
typename T,
typename A,
typename P,
typename S>
148 std::basic_string<C, T, A>& str,
150 str.append(spn.data(),
std_size(spn.size()));
156 template <
typename T,
typename P,
typename S>
157 struct cmp_decay_to<basic_string_span<T, P, S>>
158 : type_identity<memory::basic_span<T, P, S>> {};
164 template <
typename Str,
typename Spn>
166 using is_transparent = std::true_type;
168 constexpr
auto operator()(
const Str& l,
const Str& r)
const noexcept
173 constexpr
auto operator()(
const Str& l, Spn r)
const noexcept ->
bool {
174 return std::strncmp(l.data(), r.data(),
std_size(r.size())) < 0;
177 constexpr
auto operator()(Spn l,
const Str& r)
const noexcept ->
bool {
178 return std::strncmp(l.data(), r.data(),
std_size(l.size())) < 0;
197 template <
typename C,
typename P,
typename S>
200 using string_type = std::basic_string<std::remove_const_t<C>>;
201 using pointer_type = P;
208 constexpr
auto c_str() const noexcept -> pointer_type {
209 return _span.empty() ? _str.c_str() : _span.data();
214 constexpr
operator pointer_type() const noexcept {
225 template <
typename C,
typename P,
typename S>
232 template <
typename C,
typename T,
typename A,
typename Transform>
233 static inline auto make_span_putter(
235 std::basic_string<C, T, A>& str,
236 Transform transform) {
237 return [&i, &str, transform](
auto value)
mutable ->
bool {
239 if(
auto transformed = transform(value)) {
240 str[i++] = std::move(
extract(transformed));
250 #endif // EAGINE_STRING_SPAN_HPP
constexpr basic_string_span(C(&array)[N], span_size_t n) noexcept
Construction from C-array and explicit length value.
Definition: string_span.hpp:59
constexpr auto empty() const noexcept -> bool
Indicates that the span is empty.
Definition: span.hpp:235
constexpr basic_string_span(const string_type &str) noexcept
Construction from related standard string type.
Definition: string_span.hpp:63
std::ptrdiff_t span_size_t
Signed span size type used by eagine.
Definition: types.hpp:36
static constexpr auto c_str(memory::basic_span< C, P, S > s) -> std::enable_if_t< std::is_convertible_v< memory::basic_span< C, P, S >, basic_string_span< C, P, S >>, basic_c_str< C, P, S >>
Functions that construct a basic_c_str from a basic_string_span.
Definition: string_span.hpp:226
Common code is placed in this namespace.
Definition: eagine.hpp:21
static constexpr auto extract(api_result_value< Result, api_result_validity::never > &) noexcept -> Result &
Overload of extract for api_result_value.
Definition: c_api_wrap.hpp:270
Comparator template for standard string - string span comparisons.
Definition: string_span.hpp:165
constexpr auto addr() const noexcept -> address_type
Returns the memory address of the start of the span.
Definition: span.hpp:279
constexpr auto c_str() const noexcept -> pointer_type
Return a zero terminated C-string as pointer_type.
Definition: string_span.hpp:208
static constexpr auto assign_to(std::basic_string< C, T, A > &str, memory::basic_span< const C, P, S > spn) -> auto &
Assigns the contents of a span of characters to a standard string.
Definition: string_span.hpp:137
constexpr auto data() const noexcept -> pointer
Returns a pointer to the start of the span.
Definition: span.hpp:254
std::basic_string< std::remove_const_t< const char > > string_type
Related standard string type.
Definition: string_span.hpp:30
std::size_t std_size_t
Size type used by std.
Definition: types.hpp:32
Specialization of memory::basic_span for character string containers.
Definition: string_span.hpp:25
constexpr basic_string_span(C(&array)[N]) noexcept
Construction from C string literal.
Definition: string_span.hpp:52
static constexpr auto std_view(memory::basic_span< C, P, S > spn) noexcept -> std::basic_string_view< std::remove_const_t< C >>
Converts a basic_span of characters to string_span.
Definition: string_span.hpp:121
static constexpr auto std_size(T v) noexcept
Converts argument to std size type.
Definition: types.hpp:52
Non-owning view of a contiguous range of memory with ValueType elements.
Definition: flatten_fwd.hpp:16
constexpr auto is_zero_terminated() const noexcept -> bool
Indicates that the span is terminated with value T(0) if applicable.
Definition: span.hpp:240
constexpr basic_string_span(const Str &str) noexcept
Construction from compatible container const reference.
Definition: string_span.hpp:87
static constexpr auto to_string(memory::basic_span< C, P, S > spn) -> std::basic_string< std::remove_const_t< C >>
Converts a basic_span of characters to standard string.
Definition: string_span.hpp:129
constexpr auto to_string() const -> string_type
Conversion to the corresponding standard string.
Definition: string_span.hpp:105
constexpr auto size() const noexcept -> size_type
Returns the number of elements in the span.
Definition: span.hpp:246
Helper template for getting zero-terminated strings from string spans.
Definition: string_span.hpp:198
constexpr basic_string_span(Str &str) noexcept
Construction from compatible container reference.
Definition: string_span.hpp:75
constexpr auto std_view() const noexcept -> std_view_type
Named conversion to the corresponding standard string view.
Definition: string_span.hpp:95
std::basic_string_view< std::remove_const_t< const char > > std_view_type
Related standard string view type.
Definition: string_span.hpp:33
constexpr basic_string_span() noexcept=default
Default constructor.
static constexpr auto append_to(std::basic_string< C, T, A > &str, memory::basic_span< const C, P, S > spn) -> auto &
Appends the contents of a span of characters to a standard string.
Definition: string_span.hpp:147