From 8809d02db350960fc7824f5ae5bc161960910fd9 Mon Sep 17 00:00:00 2001 From: Yuri Kunde Schlesner Date: Wed, 6 May 2015 21:32:52 -0300 Subject: Common: Add StringFromFixedZeroTerminatedBuffer --- src/common/string_util.cpp | 8 ++++++++ src/common/string_util.h | 6 ++++++ 2 files changed, 14 insertions(+) (limited to 'src/common') diff --git a/src/common/string_util.cpp b/src/common/string_util.cpp index 3a6e51da..7dc0ba7b 100644 --- a/src/common/string_util.cpp +++ b/src/common/string_util.cpp @@ -477,4 +477,12 @@ std::string SHIFTJISToUTF8(const std::string& input) #endif +std::string StringFromFixedZeroTerminatedBuffer(const char * buffer, size_t max_len) { + size_t len = 0; + while (len < max_len && buffer[len] != '\0') + ++len; + + return std::string(buffer, len); +} + } diff --git a/src/common/string_util.h b/src/common/string_util.h index 356da5b6..fdc41049 100644 --- a/src/common/string_util.h +++ b/src/common/string_util.h @@ -128,4 +128,10 @@ bool ComparePartialString(InIt begin, InIt end, const char* other) { return (begin == end) == (*other == '\0'); } +/** + * Creates a std::string from a fixed-size NUL-terminated char buffer. If the buffer isn't + * NUL-terminated then the string ends at max_len characters. + */ +std::string StringFromFixedZeroTerminatedBuffer(const char* buffer, size_t max_len); + } -- cgit v1.2.3