mirror of
https://github.com/wpilibsuite/allwpilib
synced 2026-06-23 01:21:42 +00:00
Use std::string_view and fmtlib across all libraries (#3402)
- Twine, StringRef, Format, and NativeFormatting have been removed - Logging now uses fmtlib style formatting - Nearly all uses of wpi::outs/errs have been replaced with fmt::print() or std::puts()/std::fputs() (for unformatted strings). - A wpi/fmt/raw_ostream.h header has been added to enable fmt::print() with wpi::raw_ostream
This commit is contained in:
@@ -8,9 +8,12 @@
|
||||
#include <pty.h>
|
||||
#endif
|
||||
|
||||
#include <cstdio>
|
||||
|
||||
#include "fmt/format.h"
|
||||
#include "wpi/MathExtras.h"
|
||||
#include "wpi/SmallVector.h"
|
||||
#include "wpi/raw_ostream.h"
|
||||
#include "wpi/StringExtras.h"
|
||||
#include "wpi/raw_uv_ostream.h"
|
||||
#include "wpi/timestamp.h"
|
||||
#include "wpi/uv/Loop.h"
|
||||
@@ -30,9 +33,9 @@ static bool NewlineBuffer(std::string& rem, uv::Buffer& buf, size_t len,
|
||||
wpi::SmallVectorImpl<uv::Buffer>& bufs, bool tcp,
|
||||
uint16_t tcpSeq) {
|
||||
// scan for last newline
|
||||
wpi::StringRef str(buf.base, len);
|
||||
std::string_view str(buf.base, len);
|
||||
size_t idx = str.rfind('\n');
|
||||
if (idx == wpi::StringRef::npos) {
|
||||
if (idx == std::string_view::npos) {
|
||||
// no newline yet, just keep appending to remainder
|
||||
rem += str;
|
||||
return false;
|
||||
@@ -40,7 +43,7 @@ static bool NewlineBuffer(std::string& rem, uv::Buffer& buf, size_t len,
|
||||
|
||||
// build output
|
||||
wpi::raw_uv_ostream out(bufs, 4096);
|
||||
wpi::StringRef toCopy = str.slice(0, idx + 1);
|
||||
std::string_view toCopy = wpi::slice(str, 0, idx + 1);
|
||||
if (tcp) {
|
||||
// Header is 2 byte len, 1 byte type, 4 byte timestamp, 2 byte sequence num
|
||||
uint32_t ts = wpi::FloatToBits((wpi::Now() - startTime) * 1.0e-6);
|
||||
@@ -59,7 +62,7 @@ static bool NewlineBuffer(std::string& rem, uv::Buffer& buf, size_t len,
|
||||
out << rem << toCopy;
|
||||
|
||||
// reset remainder
|
||||
rem = str.slice(idx + 1, wpi::StringRef::npos);
|
||||
rem = wpi::slice(str, idx + 1, std::string_view::npos);
|
||||
return true;
|
||||
}
|
||||
|
||||
@@ -136,24 +139,26 @@ int main(int argc, char* argv[]) {
|
||||
bool err = false;
|
||||
|
||||
while (programArgc < argc && argv[programArgc][0] == '-') {
|
||||
if (wpi::StringRef(argv[programArgc]) == "-u") {
|
||||
if (std::string_view(argv[programArgc]) == "-u") {
|
||||
useUdp = true;
|
||||
} else if (wpi::StringRef(argv[programArgc]) == "-b") {
|
||||
} else if (std::string_view(argv[programArgc]) == "-b") {
|
||||
useUdp = true;
|
||||
broadcastUdp = true;
|
||||
} else {
|
||||
wpi::errs() << "unrecognized command line option " << argv[programArgc]
|
||||
<< '\n';
|
||||
fmt::print(stderr, "unrecognized command line option {}\n",
|
||||
argv[programArgc]);
|
||||
err = true;
|
||||
}
|
||||
++programArgc;
|
||||
}
|
||||
|
||||
if (err || (argc - programArgc) < 1) {
|
||||
wpi::errs()
|
||||
<< argv[0] << " [-ub] program [arguments ...]\n"
|
||||
<< " -u send udp to localhost port 6666 instead of using tcp\n"
|
||||
<< " -b broadcast udp to port 6666 instead of using tcp\n";
|
||||
std::fputs(argv[0], stderr);
|
||||
std::fputs(
|
||||
" [-ub] program [arguments ...]\n"
|
||||
" -u send udp to localhost port 6666 instead of using tcp\n"
|
||||
" -b broadcast udp to port 6666 instead of using tcp\n",
|
||||
stderr);
|
||||
return EXIT_FAILURE;
|
||||
}
|
||||
|
||||
@@ -161,7 +166,7 @@ int main(int argc, char* argv[]) {
|
||||
|
||||
auto loop = uv::Loop::Create();
|
||||
loop->error.connect(
|
||||
[](uv::Error err) { wpi::errs() << "uv ERROR: " << err.str() << '\n'; });
|
||||
[](uv::Error err) { fmt::print(stderr, "uv ERROR: {}\n", err.str()); });
|
||||
|
||||
// create pipes to communicate with child
|
||||
auto stdinPipe = uv::Pipe::Create(loop);
|
||||
@@ -249,7 +254,7 @@ int main(int argc, char* argv[]) {
|
||||
|
||||
auto proc = uv::Process::SpawnArray(loop, argv[programArgc], options);
|
||||
if (!proc) {
|
||||
wpi::errs() << "could not start subprocess\n";
|
||||
std::fputs("could not start subprocess\n", stderr);
|
||||
return EXIT_FAILURE;
|
||||
}
|
||||
proc->exited.connect([](int64_t status, int) { std::exit(status); });
|
||||
|
||||
Reference in New Issue
Block a user