mirror of
https://github.com/wpilibsuite/allwpilib
synced 2026-06-22 01:11:42 +00:00
[wpiutil] StringExtras: Add substr() (#3742)
Unlike std::string and std::string_view, this substr() allows a start greater than the length of the string, in which case an empty string is returned. This matches llvm::StringRef behavior.
This commit is contained in:
@@ -232,7 +232,7 @@ bool FindMultipartBoundary(raw_istream& is, std::string_view boundary,
|
||||
|
||||
// Did we find the boundary?
|
||||
if (searchBuf[0] == '-' && searchBuf[1] == '-' &&
|
||||
searchBuf.substr(2) == boundary) {
|
||||
wpi::substr(searchBuf, 2) == boundary) {
|
||||
return true;
|
||||
}
|
||||
|
||||
|
||||
@@ -4,6 +4,7 @@
|
||||
|
||||
#include "wpi/MimeTypes.h"
|
||||
|
||||
#include "wpi/StringExtras.h"
|
||||
#include "wpi/StringMap.h"
|
||||
|
||||
namespace wpi {
|
||||
@@ -53,11 +54,11 @@ std::string_view MimeTypeFromPath(std::string_view path) {
|
||||
|
||||
auto pos = path.find_last_of("/");
|
||||
if (pos != std::string_view::npos) {
|
||||
path = path.substr(pos + 1);
|
||||
path = wpi::substr(path, pos + 1);
|
||||
}
|
||||
auto dot_pos = path.find_last_of(".");
|
||||
if (dot_pos > 0 && dot_pos != std::string_view::npos) {
|
||||
auto type = mimeTypes.find(path.substr(dot_pos + 1));
|
||||
auto type = mimeTypes.find(wpi::substr(path, dot_pos + 1));
|
||||
if (type != mimeTypes.end()) {
|
||||
return type->getValue();
|
||||
}
|
||||
|
||||
@@ -65,7 +65,7 @@ std::string_view::size_type wpi::find_lower(
|
||||
std::string_view::size_type wpi::find_lower(
|
||||
std::string_view str, std::string_view other,
|
||||
std::string_view::size_type from) noexcept {
|
||||
auto s = str.substr(from);
|
||||
auto s = substr(str, from);
|
||||
while (s.size() >= other.size()) {
|
||||
if (starts_with_lower(s, other)) {
|
||||
return from;
|
||||
@@ -98,7 +98,7 @@ std::string_view::size_type wpi::rfind_lower(std::string_view str,
|
||||
}
|
||||
for (size_t i = str.size() - n + 1, e = 0; i != e;) {
|
||||
--i;
|
||||
if (equals_lower(str.substr(i, n), other)) {
|
||||
if (equals_lower(substr(str, i, n), other)) {
|
||||
return i;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user