From 1444cbb3df4767c75b2b8422acc97c988dc149f1 Mon Sep 17 00:00:00 2001 From: apio Date: Tue, 2 May 2023 10:49:12 +0200 Subject: [PATCH] libluna: Allow constructing a StringView from a string that might not be null-terminated --- libluna/include/luna/StringView.h | 2 ++ libluna/src/StringView.cpp | 7 ++++++- 2 files changed, 8 insertions(+), 1 deletion(-) diff --git a/libluna/include/luna/StringView.h b/libluna/include/luna/StringView.h index 1e57e434..a1000b55 100644 --- a/libluna/include/luna/StringView.h +++ b/libluna/include/luna/StringView.h @@ -43,6 +43,8 @@ class StringView Result> split(StringView delim) const; Result> split_once(char delim) const; + static StringView from_fixed_size_cstring(const char* string, usize max); + Result to_uint() const; Iterator begin() const diff --git a/libluna/src/StringView.cpp b/libluna/src/StringView.cpp index 234f116a..f3654df8 100644 --- a/libluna/src/StringView.cpp +++ b/libluna/src/StringView.cpp @@ -120,5 +120,10 @@ Result StringView::to_uint() const Result StringView::to_string() { - return String::from_cstring(m_string); + return String::from_string_view(*this); +} + +StringView StringView::from_fixed_size_cstring(const char* string, usize max) +{ + return { string, strnlen(string, max) }; }