3#ifndef TETL_CSTRING_ALGORITHM_HPP
4#define TETL_CSTRING_ALGORITHM_HPP
8template <
typename CharT>
9[[nodiscard]]
constexpr auto strcpy(CharT* dest, CharT
const* src) -> CharT*
12 while ((*dest++ = *src++) != CharT(0)) { }
16template <
typename CharT,
typename SizeT>
17[[nodiscard]]
constexpr auto strncpy(CharT* dest, CharT
const* src, SizeT
count) -> CharT*
20 for (SizeT counter = 0; counter !=
count and *src != CharT(0);) {
30template <
typename CharT,
typename SizeT>
31[[nodiscard]]
constexpr auto strlen(CharT
const* str) -> SizeT
33 CharT
const* s =
nullptr;
34 for (s = str; *s != CharT(0); ++s) { }
35 return static_cast<SizeT
>(s - str);
38template <
typename CharT,
typename SizeT>
39[[nodiscard]]
constexpr auto strcat(CharT* dest, CharT
const* src) -> CharT*
41 auto* ptr = dest + strlen<CharT, SizeT>(dest);
42 while (*src != CharT(0)) {
49template <
typename CharT,
typename SizeT>
50[[nodiscard]]
constexpr auto strncat(CharT* dest, CharT
const* src, SizeT
const count) -> CharT*
52 auto* ptr = dest + strlen<CharT, SizeT>(dest);
53 SizeT localCounter = 0;
54 while (*src != CharT(0) && localCounter !=
count) {
63template <
typename CharT>
64[[nodiscard]]
constexpr auto strcmp(CharT
const* lhs, CharT
const* rhs) ->
int
66 for (; *lhs != CharT(0); ++lhs, ++rhs) {
71 return static_cast<int>(*lhs) -
static_cast<int>(*rhs);
74template <
typename CharT,
typename SizeT>
75[[nodiscard]]
constexpr auto strncmp(CharT
const* lhs, CharT
const* rhs, SizeT
const count) ->
int
80 auto localCount =
count;
81 while (localCount-- > 0) {
82 u1 =
static_cast<CharT
>(*lhs++);
83 u2 =
static_cast<CharT
>(*rhs++);
85 return static_cast<int>(u1 - u2);
95template <
typename CharT>
96[[nodiscard]]
constexpr auto strchr(CharT* str,
int ch) -> CharT*
98 while (*str != CharT(0)) {
99 if (*str ==
static_cast<CharT
>(ch)) {
105 if (
static_cast<CharT
>(ch) == CharT(0)) {
111template <
typename CharT,
typename SizeT>
112[[nodiscard]]
constexpr auto strrchr(CharT* str,
int ch) -> CharT*
114 if (str ==
nullptr) {
117 auto len = strlen<CharT, SizeT>(str);
118 if (
static_cast<CharT
>(ch) == CharT(0)) {
123 if (str[len] ==
static_cast<CharT
>(ch)) {
131template <
typename CharT,
typename SizeT,
bool InclusiveSearch>
132[[nodiscard]]
constexpr auto is_legal_char(CharT
const* options, SizeT len, CharT ch)
noexcept ->
bool
134 for (SizeT i = 0; i < len; ++i) {
135 if (options[i] == ch) {
136 return InclusiveSearch;
139 return !InclusiveSearch;
142template <
typename CharT,
typename SizeT,
bool InclusiveSearch>
143[[nodiscard]]
constexpr auto strspn(CharT
const* dest, CharT
const* src)
noexcept -> SizeT
145 auto result = SizeT{0};
146 auto const length = strlen<CharT, SizeT>(dest);
147 auto const srcLen = strlen<CharT, SizeT>(src);
148 for (SizeT i = 0; i < length; ++i) {
149 if (!is_legal_char<CharT, SizeT, InclusiveSearch>(src, srcLen, dest[i])) {
158template <
typename CharT,
typename SizeT>
159[[nodiscard]]
constexpr auto strpbrk_impl(CharT* s, CharT* del)
noexcept -> CharT*
161 auto const i = strspn<CharT, SizeT, false>(s, del);
165 if (is_legal_char<CharT, SizeT, true>(del, strlen<CharT, SizeT>(del), s[0])) {
171template <
typename CharT>
172[[nodiscard]]
constexpr auto strstr_impl(CharT* haystack, CharT* needle)
noexcept -> CharT*
174 while (*haystack != CharT(0)) {
175 if ((*haystack == *needle) && (strcmp(haystack, needle) == 0)) {
183template <
typename CharT,
typename SizeT>
184constexpr auto memcpy(
void* dest,
void const* src, SizeT n) ->
void*
186 auto* dp =
static_cast<CharT*
>(dest);
187 auto const* sp =
static_cast<CharT const*
>(src);
188 while (n-- != CharT(0)) {
194template <
typename CharT,
typename ValT,
typename SizeT>
195constexpr auto memset(CharT*
const s, ValT
const c, SizeT n) -> CharT*
198 while (n-- != CharT(0)) {
199 *p++ =
static_cast<CharT
>(c);
206template <
typename CharT,
typename SizeT>
207constexpr auto memmove(
void* dest,
void const* src, SizeT n) -> CharT*
209 auto const* ps =
static_cast<CharT const*
>(src);
210 auto* pd =
static_cast<CharT*
>(dest);
213 for (pd += n, ps += n; n-- != CharT(0);) {
217 while (n-- != CharT(0)) {
222 return static_cast<CharT*
>(dest);
225template <
typename CharT,
typename SizeT>
226constexpr auto memchr(CharT* ptr, CharT ch, SizeT n) -> CharT*
228 for (SizeT i{0}; i != n; ++i) {
constexpr auto count(InputIt first, InputIt last, T const &value) -> typename iterator_traits< InputIt >::difference_type
Returns the number of elements in the range [first, last) satisfying specific criteria....
Definition count.hpp:21