[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:
Peter Johnson
2021-11-27 21:31:40 -08:00
committed by GitHub
parent 6f51cb3b98
commit ae208d2b17
18 changed files with 118 additions and 77 deletions

View File

@@ -208,7 +208,7 @@ wpi::HttpConnection* HttpCameraImpl::DeviceStreamConnect(
if (wpi::trim(key) == "boundary") {
value = wpi::trim(wpi::trim(value), '"'); // value may be quoted
if (wpi::starts_with(value, "--")) {
value = value.substr(2);
value = wpi::substr(value, 2);
}
boundary.append(value.begin(), value.end());
}

View File

@@ -4,6 +4,7 @@
#include "JpegUtil.h"
#include <wpi/StringExtras.h>
#include <wpi/raw_istream.h>
namespace cs {
@@ -64,7 +65,7 @@ bool GetJpegSize(std::string_view data, int* width, int* height) {
return false;
}
data = data.substr(2); // Get to the first block
data = wpi::substr(data, 2); // Get to the first block
for (;;) {
if (data.size() < 4) {
return false; // EOF
@@ -89,7 +90,7 @@ bool GetJpegSize(std::string_view data, int* width, int* height) {
return true;
}
// Go to the next block
data = data.substr(bytes[2] * 256 + bytes[3] + 2);
data = wpi::substr(data, bytes[2] * 256 + bytes[3] + 2);
}
}
@@ -102,7 +103,7 @@ bool JpegNeedsDHT(const char* data, size_t* size, size_t* locSOF) {
*locSOF = *size;
// Search until SOS for DHT tag
sdata = sdata.substr(2); // Get to the first block
sdata = wpi::substr(sdata, 2); // Get to the first block
for (;;) {
if (sdata.size() < 4) {
return false; // EOF
@@ -121,7 +122,7 @@ bool JpegNeedsDHT(const char* data, size_t* size, size_t* locSOF) {
*locSOF = sdata.data() - data; // SOF
}
// Go to the next block
sdata = sdata.substr(bytes[2] * 256 + bytes[3] + 2);
sdata = wpi::substr(sdata, bytes[2] * 256 + bytes[3] + 2);
}
// Only add DHT if we also found SOF (insertion point)

View File

@@ -797,14 +797,14 @@ void MjpegServerImpl::ConnThread::ProcessRequest() {
// compatibility, others are for Axis camera compatibility.
if ((pos = req.find("POST /stream")) != std::string_view::npos) {
kind = kStream;
parameters = req.substr(req.find('?', pos + 12)).substr(1);
parameters = wpi::substr(wpi::substr(req, req.find('?', pos + 12)), 1);
} else if ((pos = req.find("GET /?action=stream")) !=
std::string_view::npos) {
kind = kStream;
parameters = req.substr(req.find('&', pos + 19)).substr(1);
parameters = wpi::substr(wpi::substr(req, req.find('&', pos + 19)), 1);
} else if ((pos = req.find("GET /stream.mjpg")) != std::string_view::npos) {
kind = kStream;
parameters = req.substr(req.find('?', pos + 16)).substr(1);
parameters = wpi::substr(wpi::substr(req, req.find('?', pos + 16)), 1);
} else if (req.find("GET /settings") != std::string_view::npos &&
req.find(".json") != std::string_view::npos) {
kind = kGetSettings;
@@ -820,7 +820,7 @@ void MjpegServerImpl::ConnThread::ProcessRequest() {
} else if ((pos = req.find("GET /?action=command")) !=
std::string_view::npos) {
kind = kCommand;
parameters = req.substr(req.find('&', pos + 20)).substr(1);
parameters = wpi::substr(wpi::substr(req, req.find('&', pos + 20)), 1);
} else if (req.find("GET / ") != std::string_view::npos || req == "GET /\n") {
kind = kRootPage;
} else {
@@ -833,7 +833,7 @@ void MjpegServerImpl::ConnThread::ProcessRequest() {
pos = parameters.find_first_not_of(
"abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ_"
"-=&1234567890%./");
parameters = parameters.substr(0, pos);
parameters = wpi::substr(parameters, 0, pos);
SDEBUG("command parameters: \"{}\"", parameters);
// Read the rest of the HTTP request.