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/_cstddef/size_t.hpp>
8#include <etl/_strings/cstr.hpp>
9
10namespace etl {
11
12/// \ingroup cstring
13/// @{
14
15/// \brief Converts ch to unsigned char and locates the first occurrence of that
16/// value in the initial count characters (each interpreted as unsigned char) of
17/// the object pointed to by ptr.
18///
19/// \details This function behaves as if it reads the characters sequentially
20/// and stops as soon as a matching character is found: if the array pointed to
21/// by ptr is smaller than count, but the match is found within the array, the
22/// behavior is well-defined.
23///
24/// https://en.cppreference.com/w/cpp/string/byte/memchr
25///
26/// \returns Pointer to the location of the character, or a null pointer if no
27/// such character is found.
28/// \ingroup cstring
29[[nodiscard]] inline auto memchr(void* ptr, int ch, etl::size_t n) -> void*
30{
31#if defined(__clang__)
32 return __builtin_memchr(ptr, ch, n);
33#else
34 auto* p = static_cast<unsigned char*>(ptr);
35 return etl::detail::memchr(p, static_cast<unsigned char>(ch), n);
36#endif
37}
38
39[[nodiscard]] inline auto memchr(void const* ptr, int ch, etl::size_t n) -> void const*
40{
41#if defined(__clang__)
42 return __builtin_memchr(ptr, ch, n);
43#else
44 auto const* const p = static_cast<unsigned char const*>(ptr);
45 auto const c = static_cast<unsigned char>(ch);
46 return etl::detail::memchr<unsigned char const, etl::size_t>(p, c, n);
47#endif
48}
49
50/// @}
51
52} // namespace etl
53
54#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:39
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:29
Definition adjacent_find.hpp:9