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

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