1 /*
2  * Copyright (c) Facebook, Inc. and its affiliates.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *     http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 #include <folly/lang/CString.h>
18 
19 #include <algorithm>
20 #include <cstring>
21 #include <type_traits>
22 
23 #include <folly/CppAttributes.h>
24 #include <folly/functional/Invoke.h>
25 
26 namespace {
27 
28 struct poison {};
29 
memrchr(poison)30 FOLLY_MAYBE_UNUSED FOLLY_ERASE void memrchr(poison) noexcept {}
31 
32 } // namespace
33 
34 namespace folly {
35 
36 namespace detail {
37 
memrchr_fallback(void * s,int c,std::size_t len)38 void* memrchr_fallback(void* s, int c, std::size_t len) noexcept {
39   return const_cast<void*>(
40       memrchr_fallback(const_cast<void const*>(s), c, len));
41 }
42 
memrchr_fallback(void const * s,int c,std::size_t len)43 void const* memrchr_fallback(void const* s, int c, std::size_t len) noexcept {
44   auto const ss = static_cast<unsigned char const*>(s);
45   for (auto it = ss + len - 1; it >= ss; --it) {
46     if (*it == static_cast<unsigned char>(c)) {
47       return it;
48     }
49   }
50   return nullptr;
51 }
52 
53 } // namespace detail
54 
55 namespace c_string_detail {
56 
57 struct invoke_fallback_memrchr_fn {
58   template <typename... A>
operator ()folly::c_string_detail::invoke_fallback_memrchr_fn59   FOLLY_MAYBE_UNUSED FOLLY_ERASE auto operator()(A... a) const noexcept
60       -> decltype(detail::memrchr_fallback(a...)) {
61     return detail::memrchr_fallback(a...);
62   }
63 };
64 struct invoke_primary_memrchr_fn {
65   template <typename... A>
operator ()folly::c_string_detail::invoke_primary_memrchr_fn66   FOLLY_MAYBE_UNUSED FOLLY_ERASE auto operator()(A... a) const noexcept
67       -> decltype(::memrchr(a...)) {
68     return ::memrchr(a...);
69   }
70 };
71 
72 } // namespace c_string_detail
73 
74 namespace {
75 
76 template <typename... A>
77 using invoke_memrchr_fn = conditional_t<
78     is_invocable_v<c_string_detail::invoke_primary_memrchr_fn, A...>,
79     c_string_detail::invoke_primary_memrchr_fn,
80     c_string_detail::invoke_fallback_memrchr_fn>;
81 template <typename... A>
82 constexpr invoke_memrchr_fn<A...> invoke_memrchr{};
83 
84 } // namespace
85 
memrchr(void * s,int c,std::size_t len)86 void* memrchr(void* s, int c, std::size_t len) noexcept {
87   return invoke_memrchr<void*, int, std::size_t>(s, c, len);
88 }
memrchr(void const * s,int c,std::size_t len)89 void const* memrchr(void const* s, int c, std::size_t len) noexcept {
90   return invoke_memrchr<void const*, int, std::size_t>(s, c, len);
91 }
92 
strlcpy(char * const dest,char const * const src,std::size_t const size)93 std::size_t strlcpy(
94     char* const dest, char const* const src, std::size_t const size) {
95   std::size_t const len = std::strlen(src);
96   if (size != 0) {
97     std::size_t const n = std::min(len, size - 1); // always null terminate!
98     std::memcpy(dest, src, n);
99     dest[n] = '\0';
100   }
101   return len;
102 }
103 
104 } // namespace folly
105