1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4; fill-column: 100 -*- */
3 * This file is part of the LibreOffice project.
5 * This Source Code Form is subject to the terms of the Mozilla Public
6 * License, v. 2.0. If a copy of the MPL was not distributed with this
7 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
12 #include <sal/config.h>
19 #include <o3tl/safeint.hxx>
23 // A drop-in replacement for safe uses of std::sprintf where it is statically known that the
24 // provided buffer is large enough. Compared to a plain use of std::sprintf, using o3tl::sprintf
25 // for one makes it explicit that the call is considered safe and for another avoids deprecation
26 // warnings on platforms like the macOS 13 SDK that mark std::sprintf as deprecated. Many simple
27 // uses of std::sprintf across the code base can be replaced with alternative code using e.g.
28 // OString::number. This is for the remaining formatting-rich cases for which there is no easy
29 // replacement yet in our C++17 baseline. Ultimately, it should be removed again once alternatives
30 // for those remaining cases, like C++20 std::format, are available.
31 template <std::size_t N
, typename
... T
>
32 int sprintf(char (&s
)[N
], char const* format
, T
&&... arguments
)
34 auto const n
= std::snprintf(s
, N
, format
, std::forward
<T
>(arguments
)...);
35 assert(n
< 0 || o3tl::make_unsigned(n
) < N
);
41 /* vim:set shiftwidth=4 softtabstop=4 expandtab cinoptions=b1,g0,N-s cinkeys+=0=break: */