Length and capacity accessors for Genode::String
This commit is contained in:
parent
64a9a53c98
commit
8ca0f04ba0
|
@ -451,29 +451,33 @@ namespace Genode {
|
||||||
/**
|
/**
|
||||||
* Buffer that contains a null-terminated string
|
* Buffer that contains a null-terminated string
|
||||||
*
|
*
|
||||||
* \param SIZE buffer size
|
* \param CAPACITY buffer size including the terminating zero
|
||||||
*/
|
*/
|
||||||
template <size_t SIZE>
|
template <size_t CAPACITY>
|
||||||
class String
|
class String
|
||||||
{
|
{
|
||||||
private:
|
private:
|
||||||
|
|
||||||
char _buf[SIZE];
|
char _buf[CAPACITY];
|
||||||
size_t _length;
|
size_t _length;
|
||||||
|
|
||||||
public:
|
public:
|
||||||
|
|
||||||
constexpr static size_t size() { return SIZE; }
|
constexpr static size_t size() { return CAPACITY; }
|
||||||
|
|
||||||
String() : _length(0) { }
|
String() : _length(0) { }
|
||||||
|
|
||||||
String(char const *str) : _length(min(strlen(str) + 1, SIZE))
|
String(char const *str) : _length(min(strlen(str) + 1, CAPACITY))
|
||||||
{
|
{
|
||||||
strncpy(_buf, str, _length);
|
strncpy(_buf, str, _length);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
size_t length() const { return _length; }
|
||||||
|
|
||||||
|
static constexpr size_t capacity() { return CAPACITY; }
|
||||||
|
|
||||||
bool valid() const {
|
bool valid() const {
|
||||||
return (_length <= SIZE) && (_buf[_length - 1] == '\0'); }
|
return (_length <= CAPACITY) && (_buf[_length - 1] == '\0'); }
|
||||||
|
|
||||||
char const *string() const { return valid() ? _buf : ""; }
|
char const *string() const { return valid() ? _buf : ""; }
|
||||||
};
|
};
|
||||||
|
|
Loading…
Reference in New Issue