MIP_SDK  v3.0.0-736-g212583cf
MicroStrain Communications Library for embedded systems
strings.hpp
Go to the documentation of this file.
1 #pragma once
2 
3 #include <microstrain/strings.h>
5 
6 #if MICROSTRAIN_HAS_STD_STRING
7 #if __cpp_lib_string_view >= 201606L
8 #include <string_view>
9 #endif
10 #include <string>
11 #endif
12 
13 #include <cstring>
14 
15 
16 namespace microstrain {
17 namespace strings {
18 
25 
48 inline bool concat(CharArrayView buffer, size_t* index, const char* str, size_t len)
49 {
50  return C::microstrain_string_concat(buffer.data(), buffer.size(), index, str, len);
51 }
52 
74 inline bool concat(CharArrayView buffer, size_t* index, ConstCharArrayView str)
75 {
76  return concat(buffer, index, str.data(), str.size());
77 }
78 
79 #if MICROSTRAIN_HAS_STD_STRING
80 #if __cpp_lib_string_view >= 201606L
81 
105 inline bool concat(CharArrayView buffer, size_t* index, std::string_view str)
106 {
107  return concat(buffer, index, str.data(), str.size());
108 }
109 #endif // __cpp_lib_string_view
110 
133 inline bool concat(CharArrayView buffer, size_t* index, const std::string& str)
134 {
135  return concat(buffer, index, str.data(), str.size());
136 }
137 #endif // MICROSTRAIN_HAS_STD_STRING
138 
139 
173 inline bool concat_cstr(CharArrayView buffer, size_t* index, const char* str, size_t maxLen=size_t(-1))
174 {
175  const size_t len = std::min(maxLen, std::strlen(str));
176  return concat(buffer, index, ConstCharArrayView{str, len});
177 }
178 
208 template<size_t N>
209 bool concat_l(CharArrayView buffer, size_t* index, const char(&str)[N])
210 {
211  return concat(buffer, index, str, N-1);
212 }
213 
214 // TODO: Use better check for this or move to extras
215 #ifdef MICROSTRAIN_LOGGING_ENABLED
216 inline bool format_v(CharArrayView buffer, size_t* index, const char* fmt, va_list args)
240 {
241  return C::microstrain_string_format_v(buffer.data(), buffer.size(), index, fmt, args);
242 }
243 
265 inline bool format(CharArrayView buffer, size_t* index, const char* fmt, ...)
266 {
267  va_list args;
268  va_start(args, fmt);
269  bool ok = format_v(buffer, index, fmt, args);
270  va_end(args);
271  return ok;
272 }
273 #endif // MICROSTRAIN_LOGGING_ENABLED
274 
311 inline bool bytesToHexStr(CharArrayView buffer, size_t* index, ConstU8ArrayView data, unsigned int byte_grouping)
312 {
313  return C::microstrain_string_bytes_to_hex_str(buffer.data(), buffer.size(), index, data.data(), data.size(), byte_grouping);
314 }
315 
316 
319 
320 } // namespace strings
321 } // namespace microstrain
microstrain::C::microstrain_string_format_v
bool microstrain_string_format_v(char *buffer, size_t buffer_size, size_t *index, const char *fmt, va_list args)
Wrapper for std::vsnprintf with a better interface.
Definition: strings.c:124
microstrain::CharArrayView
ArrayView< char > CharArrayView
Definition: array_view.hpp:153
microstrain::strings::concat_cstr
bool concat_cstr(CharArrayView buffer, size_t *index, const char *str, size_t maxLen=size_t(-1))
Concatenate a NULL-terminated C string into a buffer.
Definition: strings.hpp:173
array_view.hpp
microstrain::strings::concat
bool concat(CharArrayView buffer, size_t *index, const char *str, size_t len)
Concatenate a string into a buffer.
Definition: strings.hpp:48
microstrain::C::microstrain_string_bytes_to_hex_str
bool microstrain_string_bytes_to_hex_str(char *buffer, size_t buffer_size, size_t *index, const uint8_t *data, size_t data_size, unsigned int byte_grouping)
Formats a byte array to a text buffer in hexadecimal.
Definition: strings.c:239
strings.h
microstrain::C::microstrain_string_concat
bool microstrain_string_concat(char *buffer, size_t buffer_size, size_t *index, const char *str, size_t str_len)
Concatenate a string into a buffer.
Definition: strings.c:33
microstrain::strings::concat_l
bool concat_l(CharArrayView buffer, size_t *index, const char(&str)[N])
Concatenate a string literal into a buffer.
Definition: strings.hpp:209
microstrain::ArrayView::data
constexpr pointer data() const noexcept
Definition: array_view.hpp:69
microstrain::ArrayView
Represents a view over a contiguous array of objects, similar to std::span, and is implemented as a p...
Definition: array_view.hpp:44
microstrain::strings::bytesToHexStr
bool bytesToHexStr(CharArrayView buffer, size_t *index, ConstU8ArrayView data, unsigned int byte_grouping)
Formats a byte array to a text buffer in hexadecimal.
Definition: strings.hpp:311
microstrain::ArrayView::size
constexpr size_t size() const noexcept
Definition: array_view.hpp:71
microstrain
Definition: embedded_time.h:8