diff options
Diffstat (limited to 'pw_string/format.cc')
-rw-r--r-- | pw_string/format.cc | 34 |
1 files changed, 33 insertions, 1 deletions
diff --git a/pw_string/format.cc b/pw_string/format.cc index 57dea10f2..a5754d39f 100644 --- a/pw_string/format.cc +++ b/pw_string/format.cc @@ -1,4 +1,4 @@ -// Copyright 2019 The Pigweed Authors +// Copyright 2023 The Pigweed Authors // // Licensed under the Apache License, Version 2.0 (the "License"); you may not // use this file except in compliance with the License. You may obtain a copy of @@ -51,4 +51,36 @@ StatusWithSize FormatVaList(span<char> buffer, return StatusWithSize(result); } +Status Format(InlineString<>& string, const char* format, ...) { + va_list args; + va_start(args, format); + const Status status = FormatVaList(string, format, args); + va_end(args); + + return status; +} + +Status FormatVaList(InlineString<>& string, const char* format, va_list args) { + Status status; + string.resize_and_overwrite([&](char* buffer, size_t capacity) { + // The vsnprintf buffer size includes a byte for the null terminator. + StatusWithSize result = + FormatVaList(span(buffer + string.size(), capacity + 1 - string.size()), + format, + args); + status = result.status(); + return string.size() + result.size(); + }); + return status; +} + +Status FormatOverwrite(InlineString<>& string, const char* format, ...) { + va_list args; + va_start(args, format); + const Status status = FormatOverwriteVaList(string, format, args); + va_end(args); + + return status; +} + } // namespace pw::string |