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

@@ -6,7 +6,6 @@
#include <algorithm>
#include <cinttypes>
#include <cstdio>
#include <filesystem>
#include <fmt/format.h>
@@ -533,7 +532,8 @@ void glass::PushID(const char* str_id_begin, const char* str_id_end) {
void glass::PushID(int int_id) {
char buf[16];
std::snprintf(buf, sizeof(buf), "%d", int_id);
wpi::format_to_n_c_str(buf, sizeof(buf), "{}", int_id);
PushStorageStack(buf);
ImGui::PushID(int_id);
}

View File

@@ -4,9 +4,8 @@
#include "glass/MainMenuBar.h"
#include <cstdio>
#include <imgui.h>
#include <wpi/StringExtras.h>
#include <wpigui.h>
#include "glass/Context.h"
@@ -52,11 +51,11 @@ void MainMenuBar::Display() {
#if 0
char str[64];
std::snprintf(str, sizeof(str), "%.3f ms/frame (%.1f FPS)",
1000.0f / ImGui::GetIO().Framerate,
ImGui::GetIO().Framerate);
ImGui::SameLine(ImGui::GetWindowWidth() - ImGui::CalcTextSize(str).x -
10);
wpi::format_to_n_c_str(str, sizeof(str), "{:.3f} ms/frame ({:.1f} FPS)",
1000.0f / ImGui::GetIO().Framerate,
ImGui::GetIO().Framerate);
ImGui::SameLine(ImGui::GetWindowWidth() - ImGui::CalcTextSize(str).x - 10);
ImGui::Text("%s", str);
#endif
ImGui::EndMainMenuBar();

View File

@@ -56,9 +56,12 @@ void Window::Display() {
}
char label[128];
std::snprintf(label, sizeof(label), "%s###%s",
m_name.empty() ? m_defaultName.c_str() : m_name.c_str(),
m_id.c_str());
if (m_name.empty()) {
wpi::format_to_n_c_str(label, sizeof(label), "{}###{}", m_defaultName,
m_id);
} else {
wpi::format_to_n_c_str(label, sizeof(label), "{}###{}", m_name, m_id);
}
if (Begin(label, &m_visible, m_flags)) {
if (m_renamePopupEnabled || m_view->HasSettings()) {

View File

@@ -4,6 +4,8 @@
#include "glass/hardware/AnalogGyro.h"
#include <wpi/StringExtras.h>
#include "glass/DataSource.h"
#include "glass/other/DeviceTree.h"
@@ -11,7 +13,8 @@ using namespace glass;
void glass::DisplayAnalogGyroDevice(AnalogGyroModel* model, int index) {
char name[32];
std::snprintf(name, sizeof(name), "AnalogGyro[%d]", index);
wpi::format_to_n_c_str(name, sizeof(name), "AnalogGyro[{}]", index);
if (BeginDevice(name)) {
// angle
if (auto angleData = model->GetAngleData()) {

View File

@@ -5,6 +5,7 @@
#include "glass/hardware/AnalogInput.h"
#include <imgui.h>
#include <wpi/StringExtras.h>
#include "glass/Context.h"
#include "glass/DataSource.h"
@@ -22,9 +23,9 @@ void glass::DisplayAnalogInput(AnalogInputModel* model, int index) {
std::string& name = GetStorage().GetString("name");
char label[128];
if (!name.empty()) {
std::snprintf(label, sizeof(label), "%s [%d]###name", name.c_str(), index);
wpi::format_to_n_c_str(label, sizeof(label), "{} [{}]###name", name, index);
} else {
std::snprintf(label, sizeof(label), "In[%d]###name", index);
wpi::format_to_n_c_str(label, sizeof(label), "In[{}]###name", index);
}
if (model->IsGyro()) {

View File

@@ -4,6 +4,8 @@
#include "glass/hardware/AnalogOutput.h"
#include <wpi/StringExtras.h>
#include "glass/Context.h"
#include "glass/DataSource.h"
#include "glass/Storage.h"
@@ -30,9 +32,9 @@ void glass::DisplayAnalogOutputsDevice(AnalogOutputsModel* model) {
std::string& name = GetStorage().GetString("name");
char label[128];
if (!name.empty()) {
std::snprintf(label, sizeof(label), "%s [%d]###name", name.c_str(), i);
wpi::format_to_n_c_str(label, sizeof(label), "{} [{}]###name", name, i);
} else {
std::snprintf(label, sizeof(label), "Out[%d]###name", i);
wpi::format_to_n_c_str(label, sizeof(label), "Out[{}]###name", i);
}
double value = analogOutData->GetValue();

View File

@@ -6,6 +6,7 @@
#include <fmt/format.h>
#include <imgui.h>
#include <wpi/StringExtras.h>
#include "glass/Context.h"
#include "glass/DataSource.h"
@@ -70,10 +71,11 @@ void glass::DisplayEncoder(EncoderModel* model) {
std::string& name = GetStorage().GetString("name");
char label[128];
if (!name.empty()) {
std::snprintf(label, sizeof(label), "%s [%d,%d]###header", name.c_str(),
chA, chB);
wpi::format_to_n_c_str(label, sizeof(label), "{} [{},{}]###header", name,
chA, chB);
} else {
std::snprintf(label, sizeof(label), "Encoder[%d,%d]###header", chA, chB);
wpi::format_to_n_c_str(label, sizeof(label), "Encoder[{},{}]###header", chA,
chB);
}
// header

View File

@@ -5,12 +5,13 @@
#include "glass/hardware/Gyro.h"
#include <cmath>
#include <numbers>
#define IMGUI_DEFINE_MATH_OPERATORS
#include <imgui.h>
#include <imgui_internal.h>
#include <numbers>
#include <wpi/StringExtras.h>
#include "glass/Context.h"
#include "glass/DataSource.h"
@@ -65,7 +66,8 @@ void glass::DisplayGyro(GyroModel* m) {
color, 1.2f);
if (major) {
char txt[16];
std::snprintf(txt, sizeof(txt), "%d°", i);
wpi::format_to_n_c_str(txt, sizeof(txt), "{}°", i);
draw->AddText(
center + (direction * radius * 1.25) - ImGui::CalcTextSize(txt) * 0.5,
primaryColor, txt, nullptr);

View File

@@ -9,6 +9,7 @@
#include <imgui.h>
#include <wpi/SmallVector.h>
#include <wpi/StringExtras.h>
#include "glass/Context.h"
#include "glass/DataSource.h"
@@ -46,10 +47,10 @@ bool glass::DisplayPCMSolenoids(PCMModel* model, int index,
std::string& name = GetStorage().GetString("name");
char label[128];
if (!name.empty()) {
std::snprintf(label, sizeof(label), "%s [%d]###header", name.c_str(),
index);
wpi::format_to_n_c_str(label, sizeof(label), "{} [{}]###header", name,
index);
} else {
std::snprintf(label, sizeof(label), "PCM[%d]###header", index);
wpi::format_to_n_c_str(label, sizeof(label), "PCM[{}]###header", index);
}
// header
@@ -111,7 +112,8 @@ void glass::DisplayCompressorDevice(PCMModel* model, int index,
void glass::DisplayCompressorDevice(CompressorModel* model, int index,
bool outputsEnabled) {
char name[32];
std::snprintf(name, sizeof(name), "Compressor[%d]", index);
wpi::format_to_n_c_str(name, sizeof(name), "Compressor[{}]", index);
if (BeginDevice(name)) {
// output enabled
if (auto runningData = model->GetRunningData()) {

View File

@@ -5,6 +5,7 @@
#include "glass/hardware/PWM.h"
#include <imgui.h>
#include <wpi/StringExtras.h>
#include "glass/Context.h"
#include "glass/DataSource.h"
@@ -22,9 +23,9 @@ void glass::DisplayPWM(PWMModel* model, int index, bool outputsEnabled) {
std::string& name = GetStorage().GetString("name");
char label[128];
if (!name.empty()) {
std::snprintf(label, sizeof(label), "%s [%d]###name", name.c_str(), index);
wpi::format_to_n_c_str(label, sizeof(label), "{} [{}]###name", name, index);
} else {
std::snprintf(label, sizeof(label), "PWM[%d]###name", index);
wpi::format_to_n_c_str(label, sizeof(label), "PWM[{}]###name", index);
}
int led = model->GetAddressableLED();

View File

@@ -5,9 +5,9 @@
#include "glass/hardware/PowerDistribution.h"
#include <algorithm>
#include <cstdio>
#include <imgui.h>
#include <wpi/StringExtras.h>
#include "glass/Context.h"
#include "glass/DataSource.h"
@@ -36,7 +36,8 @@ static float DisplayChannel(PowerDistributionModel& pdp, int channel) {
void glass::DisplayPowerDistribution(PowerDistributionModel* model, int index) {
char name[128];
std::snprintf(name, sizeof(name), "PowerDistribution[%d]", index);
wpi::format_to_n_c_str(name, sizeof(name), "PowerDistribution[{}]", index);
if (CollapsingHeader(name)) {
// temperature
if (auto tempData = model->GetTemperatureData()) {

View File

@@ -7,6 +7,7 @@
#include <cinttypes>
#include <imgui.h>
#include <wpi/StringExtras.h>
#include "glass/Context.h"
#include "glass/ContextInternal.h"
@@ -53,8 +54,11 @@ bool glass::BeginDevice(const char* id, ImGuiTreeNodeFlags flags) {
// build label
std::string& name = GetStorage().GetString("name");
char label[128];
std::snprintf(label, sizeof(label), "%s###header",
name.empty() ? id : name.c_str());
if (name.empty()) {
wpi::format_to_n_c_str(label, sizeof(label), "{}###header", id);
} else {
wpi::format_to_n_c_str(label, sizeof(label), "{}###header", name);
}
bool open = CollapsingHeader(label, flags);
PopupEditName("header", &name);

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) {

View File

@@ -4,9 +4,6 @@
#include "glass/support/NameSetting.h"
#include <cstdio>
#include <cstring>
#include <imgui_internal.h>
#include <imgui_stdlib.h>
#include <wpi/StringExtras.h>
@@ -16,75 +13,80 @@ using namespace glass;
void NameSetting::GetName(char* buf, size_t size,
const char* defaultName) const {
if (!m_name.empty()) {
std::snprintf(buf, size, "%s", m_name.c_str());
wpi::format_to_n_c_str(buf, size, "{}", m_name);
} else {
std::snprintf(buf, size, "%s", defaultName);
wpi::format_to_n_c_str(buf, size, "{}", defaultName);
}
}
void NameSetting::GetName(char* buf, size_t size, const char* defaultName,
int index) const {
if (!m_name.empty()) {
std::snprintf(buf, size, "%s [%d]", m_name.c_str(), index);
wpi::format_to_n_c_str(buf, size, "{} [{}]", m_name, index);
} else {
std::snprintf(buf, size, "%s[%d]", defaultName, index);
wpi::format_to_n_c_str(buf, size, "{}[{}]", defaultName, index);
}
}
void NameSetting::GetName(char* buf, size_t size, const char* defaultName,
int index, int index2) const {
if (!m_name.empty()) {
std::snprintf(buf, size, "%s [%d,%d]", m_name.c_str(), index, index2);
wpi::format_to_n_c_str(buf, size, "{} [{},{}]", m_name, index, index2);
} else {
std::snprintf(buf, size, "%s[%d,%d]", defaultName, index, index2);
wpi::format_to_n_c_str(buf, size, "{}[{},{}]", defaultName, index, index2);
}
}
void NameSetting::GetLabel(char* buf, size_t size,
const char* defaultName) const {
if (!m_name.empty()) {
std::snprintf(buf, size, "%s###Name%s", m_name.c_str(), defaultName);
wpi::format_to_n_c_str(buf, size, "{}###Name{}", m_name, defaultName);
} else {
std::snprintf(buf, size, "%s###Name%s", defaultName, defaultName);
wpi::format_to_n_c_str(buf, size, "{}###Name{}", defaultName, defaultName);
}
}
void NameSetting::GetLabel(char* buf, size_t size, const char* defaultName,
int index) const {
if (!m_name.empty()) {
std::snprintf(buf, size, "%s [%d]###Name%d", m_name.c_str(), index, index);
wpi::format_to_n_c_str(buf, size, "{} [{}]###Name{}", m_name, index, index);
} else {
std::snprintf(buf, size, "%s[%d]###Name%d", defaultName, index, index);
wpi::format_to_n_c_str(buf, size, "{}[{}]###Name{}", defaultName, index,
index);
}
}
void NameSetting::GetLabel(char* buf, size_t size, const char* defaultName,
int index, int index2) const {
if (!m_name.empty()) {
std::snprintf(buf, size, "%s [%d,%d]###Name%d", m_name.c_str(), index,
index2, index);
wpi::format_to_n_c_str(buf, size, "{} [{},{}]###Name{}", m_name, index,
index2, index);
} else {
std::snprintf(buf, size, "%s[%d,%d]###Name%d", defaultName, index, index2,
index);
wpi::format_to_n_c_str(buf, size, "{}[{},{}]###Name{}", defaultName, index,
index2, index);
}
}
void NameSetting::PushEditNameId(int index) {
char id[64];
std::snprintf(id, sizeof(id), "Name%d", index);
wpi::format_to_n_c_str(id, sizeof(id), "Name{}", index);
ImGui::PushID(id);
}
void NameSetting::PushEditNameId(const char* name) {
char id[128];
std::snprintf(id, sizeof(id), "Name%s", name);
wpi::format_to_n_c_str(id, sizeof(id), "Name{}", name);
ImGui::PushID(id);
}
bool NameSetting::PopupEditName(int index) {
bool rv = false;
char id[64];
std::snprintf(id, sizeof(id), "Name%d", index);
wpi::format_to_n_c_str(id, sizeof(id), "Name{}", index);
if (ImGui::BeginPopupContextItem(id)) {
ImGui::Text("Edit name:");
if (InputTextName("##edit")) {
@@ -101,8 +103,10 @@ bool NameSetting::PopupEditName(int index) {
bool NameSetting::PopupEditName(const char* name) {
bool rv = false;
char id[128];
std::snprintf(id, sizeof(id), "Name%s", name);
wpi::format_to_n_c_str(id, sizeof(id), "Name{}", name);
if (ImGui::BeginPopupContextItem(id)) {
ImGui::Text("Edit name:");
if (InputTextName("##edit")) {