diff --git a/glass/src/app/native/cpp/main.cpp b/glass/src/app/native/cpp/main.cpp index 66e4841702..02593f730e 100644 --- a/glass/src/app/native/cpp/main.cpp +++ b/glass/src/app/native/cpp/main.cpp @@ -8,6 +8,7 @@ #include #include #include +#include #include #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; } diff --git a/glass/src/lib/native/cpp/Context.cpp b/glass/src/lib/native/cpp/Context.cpp index a55cf8273d..036c38d172 100644 --- a/glass/src/lib/native/cpp/Context.cpp +++ b/glass/src/lib/native/cpp/Context.cpp @@ -6,7 +6,6 @@ #include #include -#include #include #include @@ -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); } diff --git a/glass/src/lib/native/cpp/MainMenuBar.cpp b/glass/src/lib/native/cpp/MainMenuBar.cpp index 879f6648f4..b426df40fc 100644 --- a/glass/src/lib/native/cpp/MainMenuBar.cpp +++ b/glass/src/lib/native/cpp/MainMenuBar.cpp @@ -4,9 +4,8 @@ #include "glass/MainMenuBar.h" -#include - #include +#include #include #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(); diff --git a/glass/src/lib/native/cpp/Window.cpp b/glass/src/lib/native/cpp/Window.cpp index f43c0eecb1..32c7a2104d 100644 --- a/glass/src/lib/native/cpp/Window.cpp +++ b/glass/src/lib/native/cpp/Window.cpp @@ -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()) { diff --git a/glass/src/lib/native/cpp/hardware/AnalogGyro.cpp b/glass/src/lib/native/cpp/hardware/AnalogGyro.cpp index be06a71d00..259539fc2b 100644 --- a/glass/src/lib/native/cpp/hardware/AnalogGyro.cpp +++ b/glass/src/lib/native/cpp/hardware/AnalogGyro.cpp @@ -4,6 +4,8 @@ #include "glass/hardware/AnalogGyro.h" +#include + #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()) { diff --git a/glass/src/lib/native/cpp/hardware/AnalogInput.cpp b/glass/src/lib/native/cpp/hardware/AnalogInput.cpp index af22511650..a2051cf6b4 100644 --- a/glass/src/lib/native/cpp/hardware/AnalogInput.cpp +++ b/glass/src/lib/native/cpp/hardware/AnalogInput.cpp @@ -5,6 +5,7 @@ #include "glass/hardware/AnalogInput.h" #include +#include #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()) { diff --git a/glass/src/lib/native/cpp/hardware/AnalogOutput.cpp b/glass/src/lib/native/cpp/hardware/AnalogOutput.cpp index 174e0136de..2436dd28f4 100644 --- a/glass/src/lib/native/cpp/hardware/AnalogOutput.cpp +++ b/glass/src/lib/native/cpp/hardware/AnalogOutput.cpp @@ -4,6 +4,8 @@ #include "glass/hardware/AnalogOutput.h" +#include + #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(); diff --git a/glass/src/lib/native/cpp/hardware/Encoder.cpp b/glass/src/lib/native/cpp/hardware/Encoder.cpp index 70326365dd..b359274e5a 100644 --- a/glass/src/lib/native/cpp/hardware/Encoder.cpp +++ b/glass/src/lib/native/cpp/hardware/Encoder.cpp @@ -6,6 +6,7 @@ #include #include +#include #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 diff --git a/glass/src/lib/native/cpp/hardware/Gyro.cpp b/glass/src/lib/native/cpp/hardware/Gyro.cpp index 607b251974..8ba5d5e1dd 100644 --- a/glass/src/lib/native/cpp/hardware/Gyro.cpp +++ b/glass/src/lib/native/cpp/hardware/Gyro.cpp @@ -5,12 +5,13 @@ #include "glass/hardware/Gyro.h" #include +#include #define IMGUI_DEFINE_MATH_OPERATORS #include #include -#include +#include #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); diff --git a/glass/src/lib/native/cpp/hardware/PCM.cpp b/glass/src/lib/native/cpp/hardware/PCM.cpp index d260bdaa89..6238fd9ae4 100644 --- a/glass/src/lib/native/cpp/hardware/PCM.cpp +++ b/glass/src/lib/native/cpp/hardware/PCM.cpp @@ -9,6 +9,7 @@ #include #include +#include #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()) { diff --git a/glass/src/lib/native/cpp/hardware/PWM.cpp b/glass/src/lib/native/cpp/hardware/PWM.cpp index 0200ac6922..f719a2b15a 100644 --- a/glass/src/lib/native/cpp/hardware/PWM.cpp +++ b/glass/src/lib/native/cpp/hardware/PWM.cpp @@ -5,6 +5,7 @@ #include "glass/hardware/PWM.h" #include +#include #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(); diff --git a/glass/src/lib/native/cpp/hardware/PowerDistribution.cpp b/glass/src/lib/native/cpp/hardware/PowerDistribution.cpp index f1de46186a..90aea0ecf1 100644 --- a/glass/src/lib/native/cpp/hardware/PowerDistribution.cpp +++ b/glass/src/lib/native/cpp/hardware/PowerDistribution.cpp @@ -5,9 +5,9 @@ #include "glass/hardware/PowerDistribution.h" #include -#include #include +#include #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()) { diff --git a/glass/src/lib/native/cpp/other/DeviceTree.cpp b/glass/src/lib/native/cpp/other/DeviceTree.cpp index cfce8c4425..b242c07b46 100644 --- a/glass/src/lib/native/cpp/other/DeviceTree.cpp +++ b/glass/src/lib/native/cpp/other/DeviceTree.cpp @@ -7,6 +7,7 @@ #include #include +#include #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); diff --git a/glass/src/lib/native/cpp/other/Plot.cpp b/glass/src/lib/native/cpp/other/Plot.cpp index f02a1f577c..0b61709854 100644 --- a/glass/src/lib/native/cpp/other/Plot.cpp +++ b/glass/src/lib/native/cpp/other/Plot.cpp @@ -8,7 +8,6 @@ #include #include -#include #include #include #include @@ -16,6 +15,7 @@ #include #include +#include #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(i), static_cast(plotIndex)); + wpi::format_to_n_c_str(label, sizeof(label), "{}###name{}_{}", GetName(), + static_cast(i), static_cast(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(i)); + wpi::format_to_n_c_str(label, sizeof(label), "{}###plot{}", m_name, + static_cast(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(i)); + wpi::format_to_n_c_str(name, sizeof(name), "Plot {}", + static_cast(i)); } char label[90]; - std::snprintf(label, sizeof(label), "%s###header%d", name, - static_cast(i)); + wpi::format_to_n_c_str(label, sizeof(label), "{}###header{}", name, + static_cast(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(i)); + wpi::format_to_n_c_str(id, sizeof(id), "Plot <{}>", static_cast(i)); + bool match = false; for (size_t j = 0; j < numWindows; ++j) { if (m_windows[j]->GetId() == id) { diff --git a/glass/src/lib/native/cpp/support/NameSetting.cpp b/glass/src/lib/native/cpp/support/NameSetting.cpp index 1dc1d2059f..cfc7ab3045 100644 --- a/glass/src/lib/native/cpp/support/NameSetting.cpp +++ b/glass/src/lib/native/cpp/support/NameSetting.cpp @@ -4,9 +4,6 @@ #include "glass/support/NameSetting.h" -#include -#include - #include #include #include @@ -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")) { diff --git a/glass/src/libnt/native/cpp/NetworkTables.cpp b/glass/src/libnt/native/cpp/NetworkTables.cpp index a433b9f419..7318e22b7d 100644 --- a/glass/src/libnt/native/cpp/NetworkTables.cpp +++ b/glass/src/libnt/native/cpp/NetworkTables.cpp @@ -6,7 +6,6 @@ #include #include -#include #include #include #include @@ -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); diff --git a/hal/src/main/native/cpp/jni/simulation/DriverStationDataJNI.cpp b/hal/src/main/native/cpp/jni/simulation/DriverStationDataJNI.cpp index 2aaa7f41a0..8b1ba4f68f 100644 --- a/hal/src/main/native/cpp/jni/simulation/DriverStationDataJNI.cpp +++ b/hal/src/main/native/cpp/jni/simulation/DriverStationDataJNI.cpp @@ -4,8 +4,7 @@ #include -#include - +#include #include #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(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(halMatchInfo.gameSpecificMessage), + sizeof(halMatchInfo.gameSpecificMessage), "{}", + gameSpecificMessageRef.str()); halMatchInfo.gameSpecificMessageSize = gameSpecificMessageRef.size(); halMatchInfo.matchType = (HAL_MatchType)matchType; halMatchInfo.matchNumber = matchNumber; diff --git a/hal/src/main/native/sim/Notifier.cpp b/hal/src/main/native/sim/Notifier.cpp index c686eeaa5d..3d2c802cd1 100644 --- a/hal/src/main/native/sim/Notifier.cpp +++ b/hal/src/main/native/sim/Notifier.cpp @@ -11,6 +11,7 @@ #include #include +#include #include #include @@ -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(getHandleIndex(handle))); + wpi::format_to_n_c_str(arr[num].name, sizeof(arr[num].name), + "Notifier{}", + static_cast(getHandleIndex(handle))); } else { std::strncpy(arr[num].name, notifier->name.c_str(), sizeof(arr[num].name) - 1); diff --git a/hal/src/test/native/cpp/mockdata/DriverStationDataTest.cpp b/hal/src/test/native/cpp/mockdata/DriverStationDataTest.cpp index 642136c709..ffab223b18 100644 --- a/hal/src/test/native/cpp/mockdata/DriverStationDataTest.cpp +++ b/hal/src/test/native/cpp/mockdata/DriverStationDataTest.cpp @@ -5,6 +5,7 @@ #include #include +#include #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(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(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(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); diff --git a/simulation/halsim_gui/src/main/native/cpp/DriverStationGui.cpp b/simulation/halsim_gui/src/main/native/cpp/DriverStationGui.cpp index f75ecac299..4edaa5c770 100644 --- a/simulation/halsim_gui/src/main/native/cpp/DriverStationGui.cpp +++ b/simulation/halsim_gui/src/main/native/cpp/DriverStationGui.cpp @@ -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)) { diff --git a/wpilibc/src/main/native/cpp/smartdashboard/Mechanism2d.cpp b/wpilibc/src/main/native/cpp/smartdashboard/Mechanism2d.cpp index 355cbc6ef0..1bee916c91 100644 --- a/wpilibc/src/main/native/cpp/smartdashboard/Mechanism2d.cpp +++ b/wpilibc/src/main/native/cpp/smartdashboard/Mechanism2d.cpp @@ -4,7 +4,6 @@ #include "frc/smartdashboard/Mechanism2d.h" -#include #include #include diff --git a/wpilibc/src/main/native/cpp/smartdashboard/MechanismLigament2d.cpp b/wpilibc/src/main/native/cpp/smartdashboard/MechanismLigament2d.cpp index a43aa16f5d..75ec44efe0 100644 --- a/wpilibc/src/main/native/cpp/smartdashboard/MechanismLigament2d.cpp +++ b/wpilibc/src/main/native/cpp/smartdashboard/MechanismLigament2d.cpp @@ -4,7 +4,7 @@ #include "frc/smartdashboard/MechanismLigament2d.h" -#include +#include 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); } diff --git a/wpinet/src/main/native/thirdparty/tcpsockets/cpp/TCPStream.cpp b/wpinet/src/main/native/thirdparty/tcpsockets/cpp/TCPStream.cpp index 920f7b1591..ccd7591940 100644 --- a/wpinet/src/main/native/thirdparty/tcpsockets/cpp/TCPStream.cpp +++ b/wpinet/src/main/native/thirdparty/tcpsockets/cpp/TCPStream.cpp @@ -36,6 +36,8 @@ #include +#include + 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; diff --git a/wpiutil/build.gradle b/wpiutil/build.gradle index 4e8e3e87d6..024f5abe7c 100644 --- a/wpiutil/build.gradle +++ b/wpiutil/build.gradle @@ -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' } } diff --git a/wpiutil/src/main/native/thirdparty/llvm/include/wpi/StringExtras.h b/wpiutil/src/main/native/thirdparty/llvm/include/wpi/StringExtras.h index 75c637b848..e07576ef9f 100644 --- a/wpiutil/src/main/native/thirdparty/llvm/include/wpi/StringExtras.h +++ b/wpiutil/src/main/native/thirdparty/llvm/include/wpi/StringExtras.h @@ -17,6 +17,7 @@ #pragma once +#include #include #include #include @@ -24,6 +25,8 @@ #include #include +#include + namespace wpi { template @@ -721,4 +724,23 @@ std::optional parse_float( std::pair UnescapeCString( std::string_view str, SmallVectorImpl& 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 +inline void format_to_n_c_str(OutputIt out, std::iter_difference_t n, + fmt::format_string fmt, Args&&... args) { + const auto result = + fmt::format_to_n(out, n - 1, fmt, std::forward(args)...); + *result.out = '\0'; +} + } // namespace wpi