From 20faa8b84e49208a08c704f5cfe1c94690a97ac2 Mon Sep 17 00:00:00 2001 From: Norman Feske Date: Fri, 28 Oct 2016 12:12:08 +0200 Subject: [PATCH] base: 'String' constructor overload for literals This overload covers the common case for initializing a string from a literal without employing the 'Output' mechanism. This way, such strings can by constructed without calling virtual functions, which in turn makes the 'String' usable for the 'init_rtld' phase of the dynamic linker. --- repos/base/include/util/string.h | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/repos/base/include/util/string.h b/repos/base/include/util/string.h index ec8c04f3a2..90f8587827 100644 --- a/repos/base/include/util/string.h +++ b/repos/base/include/util/string.h @@ -545,6 +545,8 @@ class Genode::Cstring { } void print(Output &out) const { out.out_string(_str, _len); } + + size_t length() const { return _len; } }; @@ -629,6 +631,17 @@ class Genode::String _len = output.num_chars() + 1; } + /** + * Constructor + * + * Overload for the common case of constructing a 'String' from a + * string literal. + */ + String(char const *cstr) : _len(min(Genode::strlen(cstr) + 1, CAPACITY)) + { + Genode::strncpy(_buf, cstr, _len); + } + /** * Copy constructor */