3838#include < cstring> // needed for memcmp
3939#include < string>
4040#include < stdexcept>
41- #include < climits>
4241#include < limits>
4342#include < cerrno>
4443
@@ -258,30 +257,33 @@ struct Descriptor {
258257 // /
259258 // / Note: no assignment operator operator=(const char*) as this potentially runs
260259 // / into trouble with this general pointer type.
261- void runtimeInit (const char * string, short length = - 1 )
260+ void runtimeInit (std::string_view string)
262261 {
263- char * target = str;
264- char * targetEnd = target;
265- if (length >= 0 && length < (int )N) {
266- targetEnd += length;
267- } else {
268- targetEnd += N;
269- }
270- const char * source = string;
271- for (; source != nullptr && target < targetEnd && *source != 0 ; ++target, ++source) {
272- *target = *source;
262+ // empty strings and string longet than the descriptor size are not allowed
263+ if (!string.empty () && (string.size () > (int )N)) {
264+ throw std::invalid_argument (" argument must not be longer than the descriptor size" );
273265 }
274- targetEnd = str + N;
275- for (; target < targetEnd; ++target) {
276- *target = 0 ;
277- }
278- // require the string to be not longer than the descriptor size
279- if (source != nullptr && (*source == 0 || (length >= 0 && length <= (int )N))) {
280- } else {
281- throw std::invalid_argument (" argument must not be longer than descriptor size" );
266+ // copy the content directly
267+ std::memcpy (str, string.data (), string.size ());
268+ // nullify the remainder
269+ auto * ptr = str + string.size ();
270+ for (; ptr < str + N; ++ptr) {
271+ *ptr = 0 ;
282272 }
283273 }
284274
275+ // / compatibility version for null-terminated strings
276+ void runtimeInit (const char * string)
277+ {
278+ runtimeInit (std::string_view{string, std::strlen (string)});
279+ }
280+
281+ // / compatibility version for explicit length
282+ void runtimeInit (const char * buffer, size_t length)
283+ {
284+ runtimeInit (std::string_view{buffer, length});
285+ }
286+
285287 bool operator ==(const Descriptor& other) const { return std::memcmp (this ->str , other.str , N) == 0 ; }
286288 bool operator <(const Descriptor& other) const { return std::memcmp (this ->str , other.str , N) < 0 ; }
287289 bool operator !=(const Descriptor& other) const { return not this ->operator ==(other); }
@@ -298,7 +300,8 @@ struct Descriptor {
298300
299301 // / get the descriptor as std::string
300302 template <typename T>
301- std::enable_if_t <std::is_same<T, std::string>::value == true , T> as () const
303+ requires (std::same_as<T, std::string>)
304+ T as () const
302305 {
303306 // backward search to find first non-zero char
304307 // FIXME: can optimize this by using the int value to start at e.g. size/2
0 commit comments