mirror of
https://github.com/wpilibsuite/allwpilib
synced 2026-06-20 00:51: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:
@@ -23,7 +23,6 @@
|
||||
|
||||
#include "wpi/SmallString.h"
|
||||
#include "wpi/SmallVector.h"
|
||||
#include "wpi/StringRef.h"
|
||||
|
||||
// strncasecmp() is not available on non-POSIX systems, so define an
|
||||
// alternative function here.
|
||||
@@ -178,34 +177,143 @@ void wpi::split(std::string_view str, SmallVectorImpl<std::string_view>& arr,
|
||||
}
|
||||
}
|
||||
|
||||
bool wpi::detail::GetAsUnsignedInteger(
|
||||
std::string_view str, unsigned radix,
|
||||
unsigned long long& result) noexcept { // NOLINT(runtime/int)
|
||||
return wpi::getAsUnsignedInteger(str, radix, result);
|
||||
}
|
||||
static unsigned GetAutoSenseRadix(std::string_view& str) noexcept {
|
||||
if (str.empty()) {
|
||||
return 10;
|
||||
}
|
||||
|
||||
bool wpi::detail::GetAsSignedInteger(
|
||||
std::string_view str, unsigned radix,
|
||||
long long& result) noexcept { // NOLINT(runtime/int)
|
||||
return wpi::getAsSignedInteger(str, radix, result);
|
||||
if (wpi::starts_with(str, "0x") || wpi::starts_with(str, "0X")) {
|
||||
str.remove_prefix(2);
|
||||
return 16;
|
||||
}
|
||||
|
||||
if (wpi::starts_with(str, "0b") || wpi::starts_with(str, "0B")) {
|
||||
str.remove_prefix(2);
|
||||
return 2;
|
||||
}
|
||||
|
||||
if (wpi::starts_with(str, "0o")) {
|
||||
str.remove_prefix(2);
|
||||
return 8;
|
||||
}
|
||||
|
||||
if (str[0] == '0' && str.size() > 1 && wpi::isDigit(str[1])) {
|
||||
str.remove_prefix(1);
|
||||
return 8;
|
||||
}
|
||||
|
||||
return 10;
|
||||
}
|
||||
|
||||
bool wpi::detail::ConsumeUnsignedInteger(
|
||||
std::string_view& str, unsigned radix,
|
||||
unsigned long long& result) noexcept { // NOLINT(runtime/int)
|
||||
wpi::StringRef sref = str;
|
||||
bool rv = wpi::consumeUnsignedInteger(sref, radix, result);
|
||||
str = sref;
|
||||
return rv;
|
||||
// Autosense radix if not specified.
|
||||
if (radix == 0) {
|
||||
radix = GetAutoSenseRadix(str);
|
||||
}
|
||||
|
||||
// Empty strings (after the radix autosense) are invalid.
|
||||
if (str.empty()) {
|
||||
return true;
|
||||
}
|
||||
|
||||
// Parse all the bytes of the string given this radix. Watch for overflow.
|
||||
std::string_view str2 = str;
|
||||
result = 0;
|
||||
while (!str2.empty()) {
|
||||
unsigned charVal;
|
||||
if (str2[0] >= '0' && str2[0] <= '9') {
|
||||
charVal = str2[0] - '0';
|
||||
} else if (str2[0] >= 'a' && str2[0] <= 'z') {
|
||||
charVal = str2[0] - 'a' + 10;
|
||||
} else if (str2[0] >= 'A' && str2[0] <= 'Z') {
|
||||
charVal = str2[0] - 'A' + 10;
|
||||
} else {
|
||||
break;
|
||||
}
|
||||
|
||||
// If the parsed value is larger than the integer radix, we cannot
|
||||
// consume any more characters.
|
||||
if (charVal >= radix) {
|
||||
break;
|
||||
}
|
||||
|
||||
// Add in this character.
|
||||
unsigned long long prevResult = result; // NOLINT(runtime/int)
|
||||
result = result * radix + charVal;
|
||||
|
||||
// Check for overflow by shifting back and seeing if bits were lost.
|
||||
if (result / radix < prevResult) {
|
||||
return true;
|
||||
}
|
||||
|
||||
str2.remove_prefix(1);
|
||||
}
|
||||
|
||||
// We consider the operation a failure if no characters were consumed
|
||||
// successfully.
|
||||
if (str.size() == str2.size()) {
|
||||
return true;
|
||||
}
|
||||
|
||||
str = str2;
|
||||
return false;
|
||||
}
|
||||
|
||||
bool wpi::detail::ConsumeSignedInteger(
|
||||
std::string_view& str, unsigned radix,
|
||||
long long& result) noexcept { // NOLINT(runtime/int)
|
||||
wpi::StringRef sref = str;
|
||||
bool rv = wpi::consumeSignedInteger(sref, radix, result);
|
||||
str = sref;
|
||||
return rv;
|
||||
unsigned long long ullVal; // NOLINT(runtime/int)
|
||||
|
||||
// Handle positive strings first.
|
||||
if (str.empty() || str.front() != '-') {
|
||||
if (wpi::detail::ConsumeUnsignedInteger(str, radix, ullVal) ||
|
||||
// Check for value so large it overflows a signed value.
|
||||
static_cast<long long>(ullVal) < 0) { // NOLINT(runtime/int)
|
||||
return true;
|
||||
}
|
||||
result = ullVal;
|
||||
return false;
|
||||
}
|
||||
|
||||
// Get the positive part of the value.
|
||||
std::string_view str2 = wpi::drop_front(str, 1);
|
||||
if (wpi::detail::ConsumeUnsignedInteger(str2, radix, ullVal) ||
|
||||
// Reject values so large they'd overflow as negative signed, but allow
|
||||
// "-0". This negates the unsigned so that the negative isn't undefined
|
||||
// on signed overflow.
|
||||
static_cast<long long>(-ullVal) > 0) { // NOLINT(runtime/int)
|
||||
return true;
|
||||
}
|
||||
|
||||
str = str2;
|
||||
result = -ullVal;
|
||||
return false;
|
||||
}
|
||||
|
||||
bool wpi::detail::GetAsUnsignedInteger(
|
||||
std::string_view str, unsigned radix,
|
||||
unsigned long long& result) noexcept { // NOLINT(runtime/int)
|
||||
if (wpi::detail::ConsumeUnsignedInteger(str, radix, result)) {
|
||||
return true;
|
||||
}
|
||||
|
||||
// For getAsUnsignedInteger, we require the whole string to be consumed or
|
||||
// else we consider it a failure.
|
||||
return !str.empty();
|
||||
}
|
||||
|
||||
bool wpi::detail::GetAsSignedInteger(
|
||||
std::string_view str, unsigned radix,
|
||||
long long& result) noexcept { // NOLINT(runtime/int)
|
||||
if (wpi::detail::ConsumeSignedInteger(str, radix, result)) {
|
||||
return true;
|
||||
}
|
||||
|
||||
// For getAsSignedInteger, we require the whole string to be consumed or else
|
||||
// we consider it a failure.
|
||||
return !str.empty();
|
||||
}
|
||||
|
||||
template <>
|
||||
|
||||
Reference in New Issue
Block a user