aboutsummaryrefslogtreecommitdiff
path: root/pw_string/public/pw_string/format.h
blob: 205210a8cb5b54da1f2f29e8e6e8a70a74af6fd4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
// 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.
#pragma once

// This provides the pw::string::Format functions, which are safer alternatives
// to std::snprintf and std::vsnprintf. The snprintf return value is awkward to
// interpret, and misinterpreting it can lead to serious bugs.
//
// These functions return a StatusWithSize. The Status is set to reflect any
// errors and the return value is always the number of characters written before
// the null terminator.

#include <cstdarg>

#include "pw_preprocessor/compiler.h"
#include "pw_span/span.h"
#include "pw_status/status_with_size.h"

namespace pw::string {

/// @brief Writes a printf-style formatted string to the provided buffer,
/// similarly to `std::snprintf()`.
///
/// The `std::snprintf()` return value is awkward to interpret, and
/// misinterpreting it can lead to serious bugs.
///
/// @returns The number of characters written, excluding the null
/// terminator. The buffer is always null-terminated unless it is empty.
/// The status is `OkStatus()` if the operation succeeded,
/// `Status::ResourceExhausted()` if the buffer was too small to fit the output,
/// or `Status::InvalidArgument()` if there was a formatting error.
PW_PRINTF_FORMAT(2, 3)
StatusWithSize Format(span<char> buffer, const char* format, ...);

/// @brief Writes a printf-style formatted string with va_list-packed arguments
/// to the provided buffer, similarly to `std::vsnprintf()`.
///
/// @returns See `pw::string::Format()`.
PW_PRINTF_FORMAT(2, 0)
StatusWithSize FormatVaList(span<char> buffer,
                            const char* format,
                            va_list args);

}  // namespace pw::string