My Project
cstring.hpp
Go to the documentation of this file.
1 
28 #ifndef BSL_CSTRING_HPP
29 #define BSL_CSTRING_HPP
30 
31 #include "cstdint.hpp"
32 #include "char_type.hpp"
33 #include "cstr_type.hpp"
34 #include "discard.hpp"
36 #include "is_void.hpp"
37 #include "min_of.hpp"
38 
39 // Notes: --
40 // - In general, you should not use these functions as they are not
41 // Core Guideline or AUTOSAR compliant. We leverage them internally
42 // within the BSL as this is the only way to ensure some of the BSL's
43 // APIs have the proper optimizations.
44 // - We only implement the functions that we need here. Once again, do
45 // not rely on this header.
46 // - The APIs of these functions have been modified to remove the need
47 // for conversions as well as ensure sizes are provided (where possible).
48 // This means that some of the APIs are our own to ensure there are not
49 // issues with name collisions.
50 // - Instead of calling the builtin functions manually, we rely on the
51 // build system to provide the implementation of each of these functions.
52 // This allows you to replace these with your compiler's version, or
53 // with a static answer to analysis if needed (for example, PRQA does
54 // not support these builtins)
55 // - Each of these functions has safety added to them to ensure crashing is
56 // not possible.
57 // - All function arguments are sent to bsl::discard to ensure you can replace
58 // the builtins with static values without getting unused argument errors.
59 //
60 
61 namespace bsl
62 {
78  [[maybe_unused]] inline void *
79  builtin_memset(void *const dst, bsl::int8 const ch, bsl::uintmax const count) noexcept
80  {
81  bsl::discard(dst);
82  bsl::discard(ch);
83  bsl::discard(count);
84 
85  if ((nullptr == dst) || (count == 0U)) {
86  return nullptr;
87  }
88 
89  return BSL_BUILTIN_MEMSET;
90  }
91 
107  [[nodiscard]] inline bsl::int32
108  builtin_memcmp(void const *const lhs, void const *const rhs, bsl::uintmax const count) noexcept
109  {
110  bsl::discard(lhs);
111  bsl::discard(rhs);
112  bsl::discard(count);
113 
114  if ((nullptr == lhs) || (nullptr == rhs) || (0U == count)) {
115  return 0;
116  }
117 
118  return BSL_BUILTIN_MEMCMP;
119  }
120 
131  [[nodiscard]] inline constexpr bsl::int32
132  builtin_strncmp(cstr_type const lhs, cstr_type const rhs, bsl::uintmax const count) noexcept
133  {
134  bsl::discard(lhs);
135  bsl::discard(rhs);
136  bsl::discard(count);
137 
138  if ((nullptr == lhs) || (nullptr == rhs) || (0U == count)) {
139  return 0;
140  }
141 
142  return BSL_BUILTIN_STRNCMP;
143  }
144 
153  [[nodiscard]] inline constexpr bsl::uintmax
154  builtin_strlen(cstr_type const str) noexcept
155  {
156  bsl::discard(str);
157 
158  if (nullptr == str) {
159  return 0U;
160  }
161 
162  return BSL_BUILTIN_STRLEN;
163  }
164 
175  [[nodiscard]] inline constexpr cstr_type
176  builtin_strnchr(cstr_type const str, char_type const ch, bsl::uintmax const count) noexcept
177  {
178  bsl::discard(str);
179  bsl::discard(ch);
180  bsl::discard(count);
181 
182  if ((nullptr == str) || (0U == count)) {
183  return nullptr;
184  }
185 
186  return BSL_BUILTIN_CHAR_MEMCHR;
187  }
188 }
189 
190 #endif
void * builtin_memset(void *const dst, bsl::int8 const ch, bsl::uintmax const count) noexcept
Same as std::memset with parameter checks. If dst is a nullptr, or count is 0, this function returns ...
Definition: cstring.hpp:79
constexpr void discard(ARGS &&... args) noexcept
This function discards a parameter that it is given. This is the same as executing a static cast....
Definition: discard.hpp:58
constexpr bsl::int32 builtin_strncmp(cstr_type const lhs, cstr_type const rhs, bsl::uintmax const count) noexcept
Same as std::strncmp with parameter checks. If lhs, rhs are a nullptr, or count is 0,...
Definition: cstring.hpp:132
char const * cstr_type
C-style string type.
Definition: cstr_type.hpp:39
::int8_t int8
defines an 8bit signed integer
Definition: cstdint.hpp:36
::int32_t int32
defines an 32bit signed integer
Definition: cstdint.hpp:40
char char_type
Standard Char Type.
Definition: char_type.hpp:41
constexpr cstr_type builtin_strnchr(cstr_type const str, char_type const ch, bsl::uintmax const count) noexcept
Same as std::strnchr with parameter checks. If str is a nullptr, or count is 0, this function returns...
Definition: cstring.hpp:176
constexpr bsl::uintmax builtin_strlen(cstr_type const str) noexcept
Same as std::strlen with parameter checks. If str is a nullptr, this returns 0.
Definition: cstring.hpp:154
::uintmax_t uintmax
defines a unsigned integer with the maximum possible size
Definition: cstdint.hpp:97
bsl::int32 builtin_memcmp(void const *const lhs, void const *const rhs, bsl::uintmax const count) noexcept
Same as std::memcmp with parameter checks. If lhs, rhs are a nullptr, or count is 0,...
Definition: cstring.hpp:108