Replace std::snprintf() with wpi::format_to_n_c_str() (#5645)

fmtlib uses consteval format string processing, which makes it more
efficient than std::snprintf().

snprintf()s in libuv, mpack, processstarter, and wpigui were left alone.
processstarter uses stdlib only, and wpigui only depends on imgui.

fmt::format_to_n() is analogous to std::format_to_n()
(https://en.cppreference.com/w/cpp/utility/format/format_to_n)

wpi::format_to_n_c_str() is a wrapper which adds the trailing NUL.
This commit is contained in:
Tyler Veness
2023-09-17 20:00:16 -07:00
committed by GitHub
parent bb39900353
commit 17f1062885
25 changed files with 190 additions and 112 deletions

View File

@@ -8,7 +8,6 @@
#include <algorithm>
#include <atomic>
#include <cstdio>
#include <cstring>
#include <memory>
#include <string>
@@ -16,6 +15,7 @@
#include <vector>
#include <fmt/format.h>
#include <wpi/StringExtras.h>
#if defined(__GNUC__)
#pragma GCC diagnostic ignored "-Wformat-nonliteral"
@@ -321,8 +321,8 @@ PlotSeries::Action PlotSeries::EmitPlot(PlotView& view, double now, size_t i,
CheckSource();
char label[128];
std::snprintf(label, sizeof(label), "%s###name%d_%d", GetName(),
static_cast<int>(i), static_cast<int>(plotIndex));
wpi::format_to_n_c_str(label, sizeof(label), "{}###name{}_{}", GetName(),
static_cast<int>(i), static_cast<int>(plotIndex));
int size = m_size;
int offset = m_offset;
@@ -580,8 +580,9 @@ void Plot::EmitPlot(PlotView& view, double now, bool paused, size_t i) {
}
char label[128];
std::snprintf(label, sizeof(label), "%s###plot%d", m_name.c_str(),
static_cast<int>(i));
wpi::format_to_n_c_str(label, sizeof(label), "{}###plot{}", m_name,
static_cast<int>(i));
ImPlotFlags plotFlags = (m_legend ? 0 : ImPlotFlags_NoLegend) |
(m_crosshairs ? ImPlotFlags_Crosshairs : 0) |
(m_mousePosition ? 0 : ImPlotFlags_NoMouseText);
@@ -937,14 +938,15 @@ void PlotView::Settings() {
char name[64];
if (!plot->GetName().empty()) {
std::snprintf(name, sizeof(name), "%s", plot->GetName().c_str());
wpi::format_to_n_c_str(name, sizeof(name), "{}", plot->GetName().c_str());
} else {
std::snprintf(name, sizeof(name), "Plot %d", static_cast<int>(i));
wpi::format_to_n_c_str(name, sizeof(name), "Plot {}",
static_cast<int>(i));
}
char label[90];
std::snprintf(label, sizeof(label), "%s###header%d", name,
static_cast<int>(i));
wpi::format_to_n_c_str(label, sizeof(label), "{}###header{}", name,
static_cast<int>(i));
bool open = ImGui::CollapsingHeader(label);
@@ -1013,7 +1015,8 @@ void PlotProvider::DisplayMenu() {
char id[32];
size_t numWindows = m_windows.size();
for (size_t i = 0; i <= numWindows; ++i) {
std::snprintf(id, sizeof(id), "Plot <%d>", static_cast<int>(i));
wpi::format_to_n_c_str(id, sizeof(id), "Plot <{}>", static_cast<int>(i));
bool match = false;
for (size_t j = 0; j < numWindows; ++j) {
if (m_windows[j]->GetId() == id) {