blob: b069fe86a73159b0402fec68107296237d6efd02 [file] [log] [blame]
Wyatt Heplere2dc6d12019-11-15 09:05:07 -08001// Copyright 2019 The Pigweed Authors
2//
3// Licensed under the Apache License, Version 2.0 (the "License"); you may not
Wyatt Hepler1a960942019-11-26 14:13:38 -08004// use this file except in compliance with the License. You may obtain a copy of
5// the License at
Wyatt Heplere2dc6d12019-11-15 09:05:07 -08006//
7// https://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
11// WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
Wyatt Hepler1a960942019-11-26 14:13:38 -080012// License for the specific language governing permissions and limitations under
13// the License.
Wyatt Heplere2dc6d12019-11-15 09:05:07 -080014
15#include "pw_string/format.h"
16
17#include <cstdio>
18
19namespace pw::string {
20
21StatusWithSize Format(const span<char>& buffer, const char* format, ...) {
22 va_list args;
23 va_start(args, format);
Wyatt Hepler2596fe52020-01-23 17:40:10 -080024 const StatusWithSize result = FormatVaList(buffer, format, args);
Wyatt Heplere2dc6d12019-11-15 09:05:07 -080025 va_end(args);
26
27 return result;
28}
29
Wyatt Hepler2596fe52020-01-23 17:40:10 -080030StatusWithSize FormatVaList(const span<char>& buffer,
31 const char* format,
32 va_list args) {
Wyatt Heplere2dc6d12019-11-15 09:05:07 -080033 if (buffer.empty()) {
Wyatt Heplerf7078802020-02-25 13:50:05 -080034 return StatusWithSize::RESOURCE_EXHAUSTED;
Wyatt Heplere2dc6d12019-11-15 09:05:07 -080035 }
36
37 const int result = std::vsnprintf(buffer.data(), buffer.size(), format, args);
38
39 // If an error occurred, the number of characters written is unknown.
40 // Discard any output by terminating the buffer.
41 if (result < 0) {
42 buffer[0] = '\0';
Wyatt Heplerf7078802020-02-25 13:50:05 -080043 return StatusWithSize::INVALID_ARGUMENT;
Wyatt Heplere2dc6d12019-11-15 09:05:07 -080044 }
45
46 // If result >= buffer.size(), the output was truncated and null-terminated.
47 if (static_cast<unsigned>(result) >= buffer.size()) {
48 return StatusWithSize(Status::RESOURCE_EXHAUSTED, buffer.size() - 1);
49 }
50
51 return StatusWithSize(result);
52}
53
54} // namespace pw::string