third_party.pigweed.src/pw_string/format.cc
Wyatt Hepler 2596fe5418 pw_string: Rename va_list Format to FormatVaList
On Windows, sometimes the va_list overload would be used when formatting
with a single argument. To avoid potential conflicts like these, the
va_list versions of string::Format and StringBuilder::Format are renamed
to FormatVaList.

Change-Id: Id29fae21b13ed420b13841a479e3748a1d3b91ce
2020-01-24 22:53:22 +00:00

55 lines
1.7 KiB
C++

// Copyright 2019 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
// the License at
//
// https://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
// WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
// License for the specific language governing permissions and limitations under
// the License.
#include "pw_string/format.h"
#include <cstdio>
namespace pw::string {
StatusWithSize Format(const span<char>& buffer, const char* format, ...) {
va_list args;
va_start(args, format);
const StatusWithSize result = FormatVaList(buffer, format, args);
va_end(args);
return result;
}
StatusWithSize FormatVaList(const span<char>& buffer,
const char* format,
va_list args) {
if (buffer.empty()) {
return StatusWithSize(Status::RESOURCE_EXHAUSTED, 0);
}
const int result = std::vsnprintf(buffer.data(), buffer.size(), format, args);
// If an error occurred, the number of characters written is unknown.
// Discard any output by terminating the buffer.
if (result < 0) {
buffer[0] = '\0';
return StatusWithSize(Status::INVALID_ARGUMENT, 0);
}
// If result >= buffer.size(), the output was truncated and null-terminated.
if (static_cast<unsigned>(result) >= buffer.size()) {
return StatusWithSize(Status::RESOURCE_EXHAUSTED, buffer.size() - 1);
}
return StatusWithSize(result);
}
} // namespace pw::string