Skip to content

Commit

Permalink
raw_istream::getline(): Change from SmallVector to std::string
Browse files Browse the repository at this point in the history
  • Loading branch information
PeterJohnson committed Dec 31, 2024
1 parent b6646fe commit b920ba2
Show file tree
Hide file tree
Showing 5 changed files with 14 additions and 40 deletions.
7 changes: 2 additions & 5 deletions cscore/src/main/native/cpp/MjpegServerImpl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
#include <string>
#include <utility>

#include <wpi/SmallString.h>
#include <wpi/StringExtras.h>
#include <wpi/fmt/raw_ostream.h>
#include <wpi/print.h>
Expand Down Expand Up @@ -789,8 +788,7 @@ void MjpegServerImpl::ConnThread::ProcessRequest() {
wpi::raw_socket_ostream os{*m_stream, true};

// Read the request string from the stream
wpi::SmallString<128> reqBuf;
std::string_view req = is.getline(reqBuf, 4096);
auto req = is.getline(4096);
if (is.has_error()) {
SDEBUG("error getting request string");
return;
Expand Down Expand Up @@ -847,9 +845,8 @@ void MjpegServerImpl::ConnThread::ProcessRequest() {

// Read the rest of the HTTP request.
// The end of the request is marked by a single, empty line
wpi::SmallString<128> lineBuf;
for (;;) {
if (wpi::starts_with(is.getline(lineBuf, 4096), "\n")) {
if (wpi::starts_with(is.getline(4096), "\n")) {
break;
}
if (is.has_error()) {
Expand Down
4 changes: 1 addition & 3 deletions cscore/src/main/native/linux/UsbUtil.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
#include <string>

#include <fmt/format.h>
#include <wpi/SmallString.h>
#include <wpi/StringExtras.h>
#include <wpi/fs.h>
#include <wpi/raw_istream.h>
Expand All @@ -35,10 +34,9 @@ static std::string GetUsbNameFromFile(int vendor, int product) {
auto productStr = fmt::format("{:04x}", product);

// scan file
wpi::SmallString<128> lineBuf;
bool foundVendor = false;
for (;;) {
auto line = is.getline(lineBuf, 4096);
auto line = is.getline(4096);
if (is.has_error()) {
break;
}
Expand Down
7 changes: 2 additions & 5 deletions wpinet/src/main/native/cpp/HttpUtil.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@

#include <fmt/format.h>
#include <wpi/Base64.h>
#include <wpi/SmallString.h>
#include <wpi/StringExtras.h>
#include <wpi/raw_ostream.h>

Expand Down Expand Up @@ -166,9 +165,8 @@ bool ParseHttpHeaders(raw_istream& is, std::string* contentType,

bool inContentType = false;
bool inContentLength = false;
SmallString<64> lineBuf;
for (;;) {
std::string_view line = rtrim(is.getline(lineBuf, 1024));
auto line = rtrim(is.getline(1024));
if (is.has_error()) {
return false;
}
Expand Down Expand Up @@ -380,8 +378,7 @@ bool HttpConnection::Handshake(const HttpRequest& request,
os.flush();

// read first line of response
SmallString<64> lineBuf;
std::string_view line = rtrim(is.getline(lineBuf, 1024));
auto line = rtrim(is.getline(1024));
if (is.has_error()) {
*warnMsg = "disconnected before response";
return false;
Expand Down
10 changes: 5 additions & 5 deletions wpiutil/src/main/native/cpp/raw_istream.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,23 +33,23 @@

using namespace wpi;

std::string_view raw_istream::getline(SmallVectorImpl<char>& buf, int maxLen) {
buf.clear();
std::string raw_istream::getline(int maxLen) {
std::string out;
for (int i = 0; i < maxLen; ++i) {
char c;
read(c);
if (has_error()) {
return {buf.data(), buf.size()};
return out;
}
if (c == '\r') {
continue;
}
buf.push_back(c);
out.push_back(c);
if (c == '\n') {
break;
}
}
return {buf.data(), buf.size()};
return out;
}

void raw_mem_istream::close() {}
Expand Down
26 changes: 4 additions & 22 deletions wpiutil/src/main/native/include/wpi/raw_istream.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,13 @@

#include <algorithm>
#include <cstddef>
#include <cstring>
#include <span>
#include <string>
#include <string_view>
#include <system_error>
#include <vector>

#include "wpi/SmallVector.h"

namespace wpi {

class raw_istream {
Expand Down Expand Up @@ -53,22 +52,6 @@ class raw_istream {
return m_read_count;
}

raw_istream& readinto(SmallVectorImpl<char>& buf, size_t len) {
size_t old_size = buf.size();
buf.append(len, 0);
read_impl(&buf[old_size], len);
buf.resize(old_size + m_read_count);
return *this;
}

raw_istream& readinto(SmallVectorImpl<uint8_t>& buf, size_t len) {
size_t old_size = buf.size();
buf.append(len, 0);
read_impl(&buf[old_size], len);
buf.resize(old_size + m_read_count);
return *this;
}

raw_istream& readinto(std::vector<char>& buf, size_t len) {
size_t old_size = buf.size();
buf.insert(buf.end(), len, 0);
Expand All @@ -94,12 +77,11 @@ class raw_istream {
}

// Read a line from an input stream (up to a maximum length).
// The returned buffer will contain the trailing \n (unless the maximum length
// was reached). \r's are stripped from the buffer.
// @param buf Buffer for output
// The returned string will contain the trailing \n (unless the maximum length
// was reached). \r's are stripped from the string.
// @param maxLen Maximum length
// @return Line
std::string_view getline(SmallVectorImpl<char>& buf, int maxLen);
std::string getline(int maxLen);

virtual void close() = 0;

Expand Down

0 comments on commit b920ba2

Please sign in to comment.