mirror of
https://github.com/genodelabs/genode.git
synced 2025-06-16 14:18:27 +00:00
Add 'String<SIZE>' buffer type to 'util/string.h'
The new 'String' buffer type is meant to replace the manually created character buffers that are scattered throughout Genode. It plainly holds a null-terminated string to be stored as a member variable (e.g., a session label) or passed as RPC argument. It is not intended to become a string API.
This commit is contained in:
@ -447,6 +447,36 @@ namespace Genode {
|
||||
|
||||
return i;
|
||||
}
|
||||
|
||||
/**
|
||||
* Buffer that contains a null-terminated string
|
||||
*
|
||||
* \param SIZE buffer size
|
||||
*/
|
||||
template <size_t SIZE>
|
||||
class String
|
||||
{
|
||||
private:
|
||||
|
||||
char _buf[SIZE];
|
||||
size_t _length;
|
||||
|
||||
public:
|
||||
|
||||
constexpr static size_t size() { return SIZE; }
|
||||
|
||||
String() : _length(0) { }
|
||||
|
||||
String(char const *str) : _length(min(strlen(str) + 1, SIZE))
|
||||
{
|
||||
strncpy(_buf, str, _length);
|
||||
}
|
||||
|
||||
bool valid() const {
|
||||
return (_length <= SIZE) && (_buf[_length - 1] == '\0'); }
|
||||
|
||||
char const *string() const { return valid() ? _buf : ""; }
|
||||
};
|
||||
}
|
||||
|
||||
#endif /* _INCLUDE__UTIL__STRING_H_ */
|
||||
|
Reference in New Issue
Block a user