tetl 0.1.0
Embedded Template Library
Loading...
Searching...
No Matches
memchr.hpp
Go to the documentation of this file.
1// SPDX-License-Identifier: BSL-1.0
2// SPDX-FileCopyrightText: Copyright (C) 2021 Tobias Hienzsch
3
4#ifndef TETL_CSTRING_MEMCHR_HPP
5#define TETL_CSTRING_MEMCHR_HPP
6
7#include <etl/_config/all.hpp>
8
9#include <etl/_cstddef/size_t.hpp>
10#include <etl/_strings/cstr.hpp>
11
12namespace etl {
13
14/// \ingroup cstring
15/// @{
16
17/// \brief Converts ch to unsigned char and locates the first occurrence of that
18/// value in the initial count characters (each interpreted as unsigned char) of
19/// the object pointed to by ptr.
20///
21/// \details This function behaves as if it reads the characters sequentially
22/// and stops as soon as a matching character is found: if the array pointed to
23/// by ptr is smaller than count, but the match is found within the array, the
24/// behavior is well-defined.
25///
26/// https://en.cppreference.com/w/cpp/string/byte/memchr
27///
28/// \returns Pointer to the location of the character, or a null pointer if no
29/// such character is found.
30/// \ingroup cstring
31[[nodiscard]] inline auto memchr(void* ptr, int ch, etl::size_t n) -> void*
32{
33#if __has_builtin(__builtin_memchr)
34 return __builtin_memchr(ptr, ch, n);
35#else
36 auto* p = static_cast<unsigned char*>(ptr);
37 return etl::detail::memchr(p, static_cast<unsigned char>(ch), n);
38#endif
39}
40
41[[nodiscard]] inline auto memchr(void const* ptr, int ch, etl::size_t n) -> void const*
42{
43#if __has_builtin(__builtin_memchr)
44 return __builtin_memchr(ptr, ch, n);
45#else
46 auto const* const p = static_cast<unsigned char const*>(ptr);
47 auto const c = static_cast<unsigned char>(ch);
48 return etl::detail::memchr<unsigned char const, etl::size_t>(p, c, n);
49#endif
50}
51
52/// @}
53
54} // namespace etl
55
56#endif // TETL_CSTRING_MEMCHR_HPP
auto memchr(void const *ptr, int ch, etl::size_t n) -> void const *
Converts ch to unsigned char and locates the first occurrence of that value in the initial count char...
Definition memchr.hpp:41
auto memchr(void *ptr, int ch, etl::size_t n) -> void *
Converts ch to unsigned char and locates the first occurrence of that value in the initial count char...
Definition memchr.hpp:31
Definition adjacent_find.hpp:9