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,6 +8,7 @@
#include <fmt/format.h>
#include <imgui.h>
#include <ntcore_cpp.h>
#include <wpi/StringExtras.h>
#include <wpigui.h>
#include "glass/Context.h"
@@ -317,11 +318,13 @@ int main(int argc, char** argv) {
char nameBuf[32];
const char* name = glfwGetKeyName(*gEnterKey, 0);
if (!name) {
std::snprintf(nameBuf, sizeof(nameBuf), "%d", *gEnterKey);
wpi::format_to_n_c_str(nameBuf, sizeof(nameBuf), "{}", *gEnterKey);
name = nameBuf;
}
std::snprintf(editLabel, sizeof(editLabel), "%s###edit",
gKeyEdit ? "(press key)" : name);
wpi::format_to_n_c_str(editLabel, sizeof(editLabel), "{}###edit",
gKeyEdit ? "(press key)" : name);
if (ImGui::SmallButton(editLabel)) {
gKeyEdit = true;
}

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

View File

@@ -6,7 +6,6 @@
#include <cinttypes>
#include <concepts>
#include <cstdio>
#include <cstring>
#include <initializer_list>
#include <memory>
@@ -1177,12 +1176,17 @@ static void EmitValueTree(
ImGui::TableNextRow();
ImGui::TableNextColumn();
EmitValueName(child.source.get(), child.name.c_str(), child.path.c_str());
ImGui::TableNextColumn();
if (!child.valueChildren.empty()) {
char label[64];
std::snprintf(label, sizeof(label),
child.valueChildrenMap ? "{...}##v_%s" : "[...]##v_%s",
child.name.c_str());
if (child.valueChildrenMap) {
wpi::format_to_n_c_str(label, sizeof(label), "{{...}}##v_{}",
child.name);
} else {
wpi::format_to_n_c_str(label, sizeof(label), "[...]##v_{}", child.name);
}
if (TreeNodeEx(label, ImGuiTreeNodeFlags_SpanFullWidth)) {
EmitValueTree(child.valueChildren, flags);
TreePop();
@@ -1208,10 +1212,16 @@ static void EmitEntry(NetworkTablesModel* model,
ImGui::TableNextColumn();
if (!entry.valueChildren.empty()) {
auto pos = ImGui::GetCursorPos();
char label[64];
std::snprintf(label, sizeof(label),
entry.valueChildrenMap ? "{...}##v_%s" : "[...]##v_%s",
entry.info.name.c_str());
if (entry.valueChildrenMap) {
wpi::format_to_n_c_str(label, sizeof(label), "{{...}}##v_{}",
entry.info.name.c_str());
} else {
wpi::format_to_n_c_str(label, sizeof(label), "[...]##v_{}",
entry.info.name.c_str());
}
valueChildrenOpen =
TreeNodeEx(label, ImGuiTreeNodeFlags_SpanFullWidth |
ImGuiTreeNodeFlags_AllowItemOverlap);

View File

@@ -4,8 +4,7 @@
#include <jni.h>
#include <cstring>
#include <wpi/StringExtras.h>
#include <wpi/jni_util.h>
#include "CallbackStore.h"
@@ -539,11 +538,12 @@ Java_edu_wpi_first_hal_simulation_DriverStationDataJNI_setMatchInfo
JStringRef gameSpecificMessageRef{env, gameSpecificMessage};
HAL_MatchInfo halMatchInfo;
std::snprintf(halMatchInfo.eventName, sizeof(halMatchInfo.eventName), "%s",
eventNameRef.c_str());
std::snprintf(reinterpret_cast<char*>(halMatchInfo.gameSpecificMessage),
sizeof(halMatchInfo.gameSpecificMessage), "%s",
gameSpecificMessageRef.c_str());
wpi::format_to_n_c_str(halMatchInfo.eventName, sizeof(halMatchInfo.eventName),
"{}", eventNameRef.str());
wpi::format_to_n_c_str(
reinterpret_cast<char*>(halMatchInfo.gameSpecificMessage),
sizeof(halMatchInfo.gameSpecificMessage), "{}",
gameSpecificMessageRef.str());
halMatchInfo.gameSpecificMessageSize = gameSpecificMessageRef.size();
halMatchInfo.matchType = (HAL_MatchType)matchType;
halMatchInfo.matchNumber = matchNumber;

View File

@@ -11,6 +11,7 @@
#include <string>
#include <wpi/SmallVector.h>
#include <wpi/StringExtras.h>
#include <wpi/condition_variable.h>
#include <wpi/mutex.h>
@@ -316,8 +317,9 @@ int32_t HALSIM_GetNotifierInfo(struct HALSIM_NotifierInfo* arr, int32_t size) {
if (num < size) {
arr[num].handle = handle;
if (notifier->name.empty()) {
std::snprintf(arr[num].name, sizeof(arr[num].name), "Notifier%d",
static_cast<int>(getHandleIndex(handle)));
wpi::format_to_n_c_str(arr[num].name, sizeof(arr[num].name),
"Notifier{}",
static_cast<int>(getHandleIndex(handle)));
} else {
std::strncpy(arr[num].name, notifier->name.c_str(),
sizeof(arr[num].name) - 1);

View File

@@ -5,6 +5,7 @@
#include <cstring>
#include <gtest/gtest.h>
#include <wpi/StringExtras.h>
#include "hal/HAL.h"
#include "hal/simulation/DriverStationData.h"
@@ -117,13 +118,12 @@ TEST(DriverStationTest, Joystick) {
}
TEST(DriverStationTest, EventInfo) {
std::string eventName = "UnitTest";
std::string gameData = "Insert game specific info here :D";
constexpr std::string_view eventName = "UnitTest";
constexpr std::string_view gameData = "Insert game specific info here :D";
HAL_MatchInfo info;
std::snprintf(info.eventName, sizeof(info.eventName), "%s",
eventName.c_str());
std::snprintf(reinterpret_cast<char*>(info.gameSpecificMessage),
sizeof(info.gameSpecificMessage), "%s", gameData.c_str());
wpi::format_to_n_c_str(info.eventName, sizeof(info.eventName), eventName);
wpi::format_to_n_c_str(reinterpret_cast<char*>(info.gameSpecificMessage),
sizeof(info.gameSpecificMessage), gameData);
info.gameSpecificMessageSize = gameData.size();
info.matchNumber = 5;
info.matchType = HAL_MatchType::HAL_kMatchType_qualification;
@@ -136,7 +136,7 @@ TEST(DriverStationTest, EventInfo) {
std::string gsm{reinterpret_cast<char*>(dataBack.gameSpecificMessage),
dataBack.gameSpecificMessageSize};
EXPECT_STREQ(eventName.c_str(), dataBack.eventName);
EXPECT_EQ(eventName, dataBack.eventName);
EXPECT_EQ(gameData, gsm);
EXPECT_EQ(5, dataBack.matchNumber);
EXPECT_EQ(HAL_MatchType::HAL_kMatchType_qualification, dataBack.matchType);

View File

@@ -555,8 +555,8 @@ KeyboardJoystick::KeyboardJoystick(glass::Storage& storage, int index)
m_axisStorage{storage.GetChildArray("axisConfig")},
m_buttonKey{storage.GetIntArray("buttonKeys")},
m_povStorage{storage.GetChildArray("povConfig")} {
std::snprintf(m_name, sizeof(m_name), "Keyboard %d", index);
std::snprintf(m_guid, sizeof(m_guid), "Keyboard%d", index);
wpi::format_to_n_c_str(m_name, sizeof(m_name), "Keyboard {}", index);
wpi::format_to_n_c_str(m_guid, sizeof(m_guid), "Keyboard{}", index);
// init axes
for (auto&& axisConfig : m_axisStorage) {
@@ -587,9 +587,15 @@ void KeyboardJoystick::EditKey(const char* label, int* key) {
ImGui::PushID(label);
ImGui::Text("%s", label);
ImGui::SameLine();
char editLabel[32];
std::snprintf(editLabel, sizeof(editLabel), "%s###edit",
s_keyEdit == key ? "(press key)" : GetKeyName(*key));
if (s_keyEdit == key) {
wpi::format_to_n_c_str(editLabel, sizeof(editLabel), "(press key)###edit");
} else {
wpi::format_to_n_c_str(editLabel, sizeof(editLabel), "{}###edit",
GetKeyName(*key));
}
if (ImGui::SmallButton(editLabel)) {
s_keyEdit = key;
}
@@ -633,7 +639,8 @@ void KeyboardJoystick::SettingsDisplay() {
m_axisConfig.emplace_back(*m_axisStorage.back());
}
for (int i = 0; i < m_axisCount; ++i) {
std::snprintf(label, sizeof(label), "Axis %d", i);
wpi::format_to_n_c_str(label, sizeof(label), "Axis {}", i);
if (ImGui::TreeNodeEx(label, ImGuiTreeNodeFlags_DefaultOpen)) {
EditKey("Increase", &m_axisConfig[i].incKey);
EditKey("Decrease", &m_axisConfig[i].decKey);
@@ -666,7 +673,8 @@ void KeyboardJoystick::SettingsDisplay() {
m_buttonKey.emplace_back(-1);
}
for (int i = 0; i < m_buttonCount; ++i) {
std::snprintf(label, sizeof(label), "Button %d", i + 1);
wpi::format_to_n_c_str(label, sizeof(label), "Button {}", i + 1);
EditKey(label, &m_buttonKey[i]);
}
ImGui::PopID();
@@ -688,7 +696,8 @@ void KeyboardJoystick::SettingsDisplay() {
m_povConfig.emplace_back(*m_povStorage.back());
}
for (int i = 0; i < m_povCount; ++i) {
std::snprintf(label, sizeof(label), "POV %d", i);
wpi::format_to_n_c_str(label, sizeof(label), "POV {}", i);
if (ImGui::TreeNodeEx(label, ImGuiTreeNodeFlags_DefaultOpen)) {
EditKey(" 0 deg", &m_povConfig[i].key0);
EditKey(" 45 deg", &m_povConfig[i].key45);
@@ -1174,7 +1183,7 @@ bool FMSSimModel::IsReadOnly() {
static void DisplaySystemJoystick(SystemJoystick& joy, int i) {
char label[64];
std::snprintf(label, sizeof(label), "%d: %s", i, joy.GetName());
wpi::format_to_n_c_str(label, sizeof(label), "{}: {}", i, joy.GetName());
// highlight if any buttons pressed
bool anyButtonPressed = joy.IsAnyButtonPressed();
@@ -1208,7 +1217,8 @@ static void DisplaySystemJoysticks() {
DisplaySystemJoystick(*joy, i + GLFW_JOYSTICK_LAST + 1);
if (ImGui::BeginPopupContextItem()) {
char buf[64];
std::snprintf(buf, sizeof(buf), "%s Settings", joy->GetName());
wpi::format_to_n_c_str(buf, sizeof(buf), "{} Settings", joy->GetName());
if (ImGui::MenuItem(buf)) {
if (auto win = DriverStationGui::dsManager->GetWindow(buf)) {
win->SetVisible(true);
@@ -1293,7 +1303,8 @@ static void DisplayJoysticks() {
for (int j = 0; j < joy.data.axes.count; ++j) {
if (source && source->axes[j]) {
char label[64];
std::snprintf(label, sizeof(label), "Axis[%d]", j);
wpi::format_to_n_c_str(label, sizeof(label), "Axis[{}]", j);
ImGui::Selectable(label);
source->axes[j]->EmitDrag();
ImGui::SameLine();
@@ -1306,7 +1317,8 @@ static void DisplayJoysticks() {
for (int j = 0; j < joy.data.povs.count; ++j) {
if (source && source->povs[j]) {
char label[64];
std::snprintf(label, sizeof(label), "POVs[%d]", j);
wpi::format_to_n_c_str(label, sizeof(label), "POVs[{}]", j);
ImGui::Selectable(label);
source->povs[j]->EmitDrag();
ImGui::SameLine();
@@ -1406,7 +1418,9 @@ void DriverStationGui::GlobalInit() {
int i = 0;
for (auto&& joy : gKeyboardJoysticks) {
char label[64];
std::snprintf(label, sizeof(label), "%s Settings", joy->GetName());
wpi::format_to_n_c_str(label, sizeof(label), "{} Settings",
joy->GetName());
if (auto win = dsManager->AddWindow(
label, [j = joy.get()] { j->SettingsDisplay(); },
glass::Window::kHide)) {

View File

@@ -4,7 +4,6 @@
#include "frc/smartdashboard/Mechanism2d.h"
#include <cstdio>
#include <string_view>
#include <networktables/NTSendableBuilder.h>

View File

@@ -4,7 +4,7 @@
#include "frc/smartdashboard/MechanismLigament2d.h"
#include <cstdio>
#include <wpi/StringExtras.h>
using namespace frc;
@@ -36,8 +36,10 @@ void MechanismLigament2d::UpdateEntries(
void MechanismLigament2d::SetColor(const Color8Bit& color) {
std::scoped_lock lock(m_mutex);
std::snprintf(m_color, sizeof(m_color), "#%02X%02X%02X", color.red,
color.green, color.blue);
wpi::format_to_n_c_str(m_color, sizeof(m_color), "#{:02X}{:02X}{:02X}",
color.red, color.green, color.blue);
if (m_colorEntry) {
m_colorEntry.Set(m_color);
}

View File

@@ -36,6 +36,8 @@
#include <cerrno>
#include <wpi/StringExtras.h>
using namespace wpi;
TCPStream::TCPStream(int sd, sockaddr_in* address)
@@ -85,12 +87,9 @@ size_t TCPStream::send(const char* buffer, size_t len, Error* err) {
}
if (!result) {
char Buffer[128];
#ifdef _MSC_VER
sprintf_s(Buffer, "Send() failed: WSA error=%d\n", WSAGetLastError());
#else
std::snprintf(Buffer, sizeof(Buffer), "Send() failed: WSA error=%d\n",
WSAGetLastError());
#endif
wpi::format_to_n_c_str(Buffer, sizeof(Buffer),
"Send() failed: WSA error={}\n", WSAGetLastError());
OutputDebugStringA(Buffer);
*err = kConnectionReset;
return 0;

View File

@@ -91,7 +91,7 @@ ext {
include '**/*.cpp'
}
exportedHeaders {
srcDirs 'src/main/native/include', 'src/main/native/cpp', 'src/main/native/thirdparty/llvm/include', 'src/main/native/thirdparty/sigslot/include', 'src/main/native/thirdparty/mpack/include'
srcDirs 'src/main/native/include', 'src/main/native/cpp', 'src/main/native/thirdparty/llvm/include', 'src/main/native/thirdparty/fmtlib/include', 'src/main/native/thirdparty/sigslot/include', 'src/main/native/thirdparty/mpack/include'
include '**/*.h'
}
}

View File

@@ -17,6 +17,7 @@
#pragma once
#include <iterator>
#include <limits>
#include <optional>
#include <string>
@@ -24,6 +25,8 @@
#include <type_traits>
#include <utility>
#include <fmt/format.h>
namespace wpi {
template <typename T>
@@ -721,4 +724,23 @@ std::optional<long double> parse_float<long double>(
std::pair<std::string_view, std::string_view> UnescapeCString(
std::string_view str, SmallVectorImpl<char>& buf);
/**
* Like std::format_to_n() in that it writes at most n bytes to the output
* buffer, but also includes a terminating null byte in n.
*
* This is essentially a more performant replacement for std::snprintf().
*
* @param out The output buffer.
* @param n The size of the output buffer.
* @param fmt The format string.
* @param args The format string arguments.
*/
template <class OutputIt, class... Args>
inline void format_to_n_c_str(OutputIt out, std::iter_difference_t<OutputIt> n,
fmt::format_string<Args...> fmt, Args&&... args) {
const auto result =
fmt::format_to_n(out, n - 1, fmt, std::forward<Args>(args)...);
*result.out = '\0';
}
} // namespace wpi