mirror of
https://github.com/wpilibsuite/allwpilib
synced 2026-06-19 00:41:43 +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:
@@ -2,9 +2,12 @@
|
||||
// Open Source Software; you can modify and/or share it under the terms of
|
||||
// the WPILib BSD license file in the root directory of this project.
|
||||
|
||||
#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"
|
||||
@@ -21,9 +24,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;
|
||||
@@ -31,7 +34,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);
|
||||
@@ -50,7 +53,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;
|
||||
}
|
||||
|
||||
@@ -128,37 +131,42 @@ int main(int argc, char* argv[]) {
|
||||
int port = -1;
|
||||
|
||||
while (arg < argc && argv[arg][0] == '-') {
|
||||
if (wpi::StringRef(argv[arg]) == "-u") {
|
||||
if (std::string_view(argv[arg]) == "-u") {
|
||||
useUdp = true;
|
||||
} else if (wpi::StringRef(argv[arg]) == "-b") {
|
||||
} else if (std::string_view(argv[arg]) == "-b") {
|
||||
useUdp = true;
|
||||
broadcastUdp = true;
|
||||
} else if (wpi::StringRef(argv[arg]) == "-p") {
|
||||
} else if (std::string_view(argv[arg]) == "-p") {
|
||||
++arg;
|
||||
std::optional<int> portValue;
|
||||
if (arg >= argc || argv[arg][0] == '-' ||
|
||||
wpi::StringRef(argv[arg]).getAsInteger(10, port)) {
|
||||
wpi::errs() << "-p must be followed by port number\n";
|
||||
!(portValue = wpi::parse_integer<int>(argv[arg], 10))) {
|
||||
std::fputs("-p must be followed by port number\n", stderr);
|
||||
err = true;
|
||||
} else if (portValue) {
|
||||
port = portValue.value();
|
||||
}
|
||||
} else {
|
||||
wpi::errs() << "unrecognized command line option " << argv[arg] << '\n';
|
||||
fmt::print(stderr, "unrecognized command line option {}\n", argv[arg]);
|
||||
err = true;
|
||||
}
|
||||
++arg;
|
||||
}
|
||||
|
||||
if (err) {
|
||||
wpi::errs()
|
||||
<< argv[0] << " [-ub] [-p PORT]\n"
|
||||
<< " -u send udp to localhost port 6666 instead of using tcp\n"
|
||||
<< " -b broadcast udp to port 6666 instead of using tcp\n"
|
||||
<< " -p PORT use port PORT instead of 6666 (udp) or 1740 (tcp)\n";
|
||||
std::fputs(argv[0], stderr);
|
||||
std::fputs(
|
||||
" [-ub] [-p PORT]\n"
|
||||
" -u send udp to localhost port 6666 instead of using tcp\n"
|
||||
" -b broadcast udp to port 6666 instead of using tcp\n"
|
||||
" -p PORT use port PORT instead of 6666 (udp) or 1740 (tcp)\n",
|
||||
stderr);
|
||||
return EXIT_FAILURE;
|
||||
}
|
||||
|
||||
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 ttys
|
||||
auto stdinTty = uv::Tty::Create(loop, 0, true);
|
||||
|
||||
Reference in New Issue
Block a user