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

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