4#ifndef TETL_CSTRING_ALGORITHM_HPP
5#define TETL_CSTRING_ALGORITHM_HPP
9template <
typename CharT>
10[[nodiscard]]
constexpr auto strcpy(CharT* dest, CharT
const* src) -> CharT*
13 while ((*dest++ = *src++) != CharT(0)) { }
17template <
typename CharT,
typename SizeT>
18[[nodiscard]]
constexpr auto strncpy(CharT* dest, CharT
const* src, SizeT count) -> CharT*
21 for (SizeT counter = 0; counter != count
and *src != CharT(0);) {
31template <
typename CharT,
typename SizeT>
32[[nodiscard]]
constexpr auto strlen(CharT
const* str) -> SizeT
34 CharT
const* s =
nullptr;
35 for (s = str; *s != CharT(0); ++s) { }
36 return static_cast<SizeT>(s - str);
39template <
typename CharT,
typename SizeT>
40[[nodiscard]]
constexpr auto strcat(CharT* dest, CharT
const* src) -> CharT*
42 auto* ptr = dest + strlen<CharT, SizeT>(dest);
43 while (*src != CharT(0)) {
50template <
typename CharT,
typename SizeT>
51[[nodiscard]]
constexpr auto strncat(CharT* dest, CharT
const* src, SizeT
const count) -> CharT*
53 auto* ptr = dest + strlen<CharT, SizeT>(dest);
54 SizeT localCounter = 0;
55 while (*src != CharT(0) && localCounter != count) {
64template <
typename CharT>
65[[nodiscard]]
constexpr auto strcmp(CharT
const* lhs, CharT
const* rhs) ->
int
67 for (; *lhs != CharT(0); ++lhs, ++rhs) {
72 return static_cast<
int>(*lhs) -
static_cast<
int>(*rhs);
75template <
typename CharT,
typename SizeT>
76[[nodiscard]]
constexpr auto strncmp(CharT
const* lhs, CharT
const* rhs, SizeT
const count) ->
int
81 auto localCount = count;
82 while (localCount-- > 0) {
83 u1 =
static_cast<CharT>(*lhs++);
84 u2 =
static_cast<CharT>(*rhs++);
86 return static_cast<
int>(u1 - u2);
96template <
typename CharT>
97[[nodiscard]]
constexpr auto strchr(CharT* str,
int ch) -> CharT*
99 while (*str != CharT(0)) {
100 if (*str ==
static_cast<CharT>(ch)) {
106 if (
static_cast<CharT>(ch) == CharT(0)) {
112template <
typename CharT,
typename SizeT>
113[[nodiscard]]
constexpr auto strrchr(CharT* str,
int ch) -> CharT*
115 if (str ==
nullptr) {
118 auto len = strlen<CharT, SizeT>(str);
119 if (
static_cast<CharT>(ch) == CharT(0)) {
124 if (str[len] ==
static_cast<CharT>(ch)) {
132template <
typename CharT,
typename SizeT,
bool InclusiveSearch>
133[[nodiscard]]
constexpr auto is_legal_char(CharT
const* options, SizeT len, CharT ch)
noexcept ->
bool
135 for (SizeT i = 0; i < len; ++i) {
136 if (options[i] == ch) {
137 return InclusiveSearch;
140 return !InclusiveSearch;
143template <
typename CharT,
typename SizeT,
bool InclusiveSearch>
144[[nodiscard]]
constexpr auto strspn(CharT
const* dest, CharT
const* src)
noexcept -> SizeT
146 auto result = SizeT{0};
147 auto const length = strlen<CharT, SizeT>(dest);
148 auto const srcLen = strlen<CharT, SizeT>(src);
149 for (SizeT i = 0; i < length; ++i) {
150 if (!is_legal_char<CharT, SizeT, InclusiveSearch>(src, srcLen, dest[i])) {
159template <
typename CharT,
typename SizeT>
160[[nodiscard]]
constexpr auto strpbrk_impl(CharT* s, CharT* del)
noexcept -> CharT*
162 auto const i = strspn<CharT, SizeT,
false>(s, del);
166 if (is_legal_char<CharT, SizeT,
true>(del, strlen<CharT, SizeT>(del), s[0])) {
172template <
typename CharT>
173[[nodiscard]]
constexpr auto strstr_impl(CharT* haystack, CharT* needle)
noexcept -> CharT*
175 while (*haystack != CharT(0)) {
176 if ((*haystack == *needle) && (strcmp(haystack, needle) == 0)) {
184template <
typename CharT,
typename SizeT>
185constexpr auto memcpy(
void* dest,
void const* src, SizeT n) ->
void*
187 auto* dp =
static_cast<CharT*>(dest);
188 auto const* sp =
static_cast<CharT
const*>(src);
189 while (n-- != CharT(0)) {
195template <
typename CharT,
typename ValT,
typename SizeT>
196constexpr auto memset(CharT*
const s, ValT
const c, SizeT n) -> CharT*
199 while (n-- != CharT(0)) {
200 *p++ =
static_cast<CharT>(c);
207template <
typename CharT,
typename SizeT>
208constexpr auto memmove(
void* dest,
void const* src, SizeT n) -> CharT*
210 auto const* ps =
static_cast<CharT
const*>(src);
211 auto* pd =
static_cast<CharT*>(dest);
214 for (pd += n, ps += n; n-- != CharT(0);) {
218 while (n-- != CharT(0)) {
223 return static_cast<CharT*>(dest);
226template <
typename CharT,
typename SizeT>
227constexpr auto memchr(CharT* ptr, CharT ch, SizeT n) -> CharT*
229 for (SizeT i{0}; i != n; ++i) {
Definition adjacent_find.hpp:9