[wpiutil] Change StringExtras split() to template (#7636)

It now calls back a function for each part rather than creating a SmallVector.
This commit is contained in:
Peter Johnson
2025-01-05 20:53:43 -08:00
committed by GitHub
parent 0f6693594c
commit 03d9e96877
17 changed files with 161 additions and 171 deletions

View File

@@ -98,9 +98,7 @@ std::string_view EscapeHTML(std::string_view str, SmallVectorImpl<char>& buf) {
}
HttpQueryMap::HttpQueryMap(std::string_view query) {
SmallVector<std::string_view, 16> queryElems;
split(query, queryElems, '&', 100, false);
for (auto elem : queryElems) {
split(query, '&', 100, false, [&](auto elem) {
auto [nameEsc, valueEsc] = split(elem, '=');
SmallString<64> nameBuf;
bool err = false;
@@ -109,7 +107,7 @@ HttpQueryMap::HttpQueryMap(std::string_view query) {
if (!err) {
m_elems.try_emplace(name, valueEsc);
}
}
});
}
std::optional<std::string_view> HttpQueryMap::Get(
@@ -132,9 +130,7 @@ HttpPath::HttpPath(std::string_view path) {
m_pathEnds.emplace_back(0);
return;
}
wpi::SmallVector<std::string_view, 16> pathElems;
split(path, pathElems, '/', 100, false);
for (auto elem : pathElems) {
split(path, '/', 100, false, [&](auto elem) {
SmallString<64> buf;
bool err = false;
auto val = wpi::UnescapeURI(elem, buf, &err);
@@ -144,7 +140,7 @@ HttpPath::HttpPath(std::string_view path) {
}
m_pathBuf += val;
m_pathEnds.emplace_back(m_pathBuf.size());
}
});
}
bool HttpPath::startswith(size_t start,

View File

@@ -5,6 +5,7 @@
#include "wpinet/WebSocketServer.h"
#include <memory>
#include <string>
#include <utility>
#include <wpi/StringExtras.h>
@@ -31,14 +32,12 @@ WebSocketServerHelper::WebSocketServerHelper(HttpParser& req) {
m_version = value;
} else if (equals_lower(name, "sec-websocket-protocol")) {
// Protocols are comma delimited, repeated headers add to list
SmallVector<std::string_view, 2> protocols;
split(value, protocols, ",", -1, false);
for (auto protocol : protocols) {
split(value, ',', -1, false, [&](auto protocol) {
protocol = trim(protocol);
if (!protocol.empty()) {
m_protocols.emplace_back(protocol);
}
}
});
}
});
req.headersComplete.connect([&req, this](bool) {
@@ -53,7 +52,22 @@ std::pair<bool, std::string_view> WebSocketServerHelper::MatchProtocol(
if (protocols.empty() && m_protocols.empty()) {
return {true, {}};
}
for (auto protocol : protocols) {
for (auto&& protocol : protocols) {
for (auto&& clientProto : m_protocols) {
if (protocol == clientProto) {
return {true, protocol};
}
}
}
return {false, {}};
}
std::pair<bool, std::string_view> WebSocketServerHelper::MatchProtocol(
std::span<const std::string> protocols) {
if (protocols.empty() && m_protocols.empty()) {
return {true, {}};
}
for (auto&& protocol : protocols) {
for (auto&& clientProto : m_protocols) {
if (protocol == clientProto) {
return {true, protocol};
@@ -101,9 +115,7 @@ WebSocketServer::WebSocketServer(uv::Stream& stream,
}
// Negotiate sub-protocol
SmallVector<std::string_view, 2> protocols{m_protocols.begin(),
m_protocols.end()};
std::string_view protocol = m_helper.MatchProtocol(protocols).second;
std::string_view protocol = m_helper.MatchProtocol(m_protocols).second;
// Disconnect our header reader
m_dataConn.disconnect();