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

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