From 2319c7b39127c12956c11c026b30156308d64494 Mon Sep 17 00:00:00 2001 From: Peter Johnson Date: Sun, 29 Dec 2024 18:28:41 -0800 Subject: [PATCH 01/54] [cscore] HttpCameraImpl: Remove SmallVector usage --- cscore/src/main/native/cpp/HttpCameraImpl.cpp | 23 +++++++++---------- cscore/src/main/native/cpp/HttpCameraImpl.h | 4 ++-- 2 files changed, 13 insertions(+), 14 deletions(-) diff --git a/cscore/src/main/native/cpp/HttpCameraImpl.cpp b/cscore/src/main/native/cpp/HttpCameraImpl.cpp index dcfd49776c5..16ee1f4c6e6 100644 --- a/cscore/src/main/native/cpp/HttpCameraImpl.cpp +++ b/cscore/src/main/native/cpp/HttpCameraImpl.cpp @@ -123,8 +123,7 @@ void HttpCameraImpl::StreamThreadMain() { } // connect - wpi::SmallString<64> boundary; - wpi::HttpConnection* conn = DeviceStreamConnect(boundary); + auto [conn, boundary] = DeviceStreamConnect(); if (!m_active) { break; @@ -139,7 +138,7 @@ void HttpCameraImpl::StreamThreadMain() { SetConnected(true); // stream - DeviceStream(conn->is, boundary.str()); + DeviceStream(conn->is, boundary); { std::unique_lock lock(m_mutex); m_streamConn = nullptr; @@ -150,8 +149,8 @@ void HttpCameraImpl::StreamThreadMain() { SetConnected(false); } -wpi::HttpConnection* HttpCameraImpl::DeviceStreamConnect( - wpi::SmallVectorImpl& boundary) { +std::pair +HttpCameraImpl::DeviceStreamConnect() { // Build the request wpi::HttpRequest req; { @@ -159,7 +158,7 @@ wpi::HttpConnection* HttpCameraImpl::DeviceStreamConnect( if (m_locations.empty()) { SERROR("locations array is empty!?"); std::this_thread::sleep_for(std::chrono::seconds(1)); - return nullptr; + return {}; } if (m_nextLocation >= m_locations.size()) { m_nextLocation = 0; @@ -173,7 +172,7 @@ wpi::HttpConnection* HttpCameraImpl::DeviceStreamConnect( wpi::TCPConnector::connect(req.host.c_str(), req.port, m_logger, 1); if (!m_active || !stream) { - return nullptr; + return {}; } auto connPtr = std::make_unique(std::move(stream), 1); @@ -191,7 +190,7 @@ wpi::HttpConnection* HttpCameraImpl::DeviceStreamConnect( SWARNING("{}", warn); std::scoped_lock lock(m_mutex); m_streamConn = nullptr; - return nullptr; + return {}; } // Parse Content-Type header to get the boundary @@ -202,11 +201,11 @@ wpi::HttpConnection* HttpCameraImpl::DeviceStreamConnect( mediaType); std::scoped_lock lock(m_mutex); m_streamConn = nullptr; - return nullptr; + return {}; } // media parameters - boundary.clear(); + std::string boundary; while (!contentType.empty()) { std::string_view keyvalue; std::tie(keyvalue, contentType) = wpi::split(contentType, ';'); @@ -226,10 +225,10 @@ wpi::HttpConnection* HttpCameraImpl::DeviceStreamConnect( req.host.str()); std::scoped_lock lock(m_mutex); m_streamConn = nullptr; - return nullptr; + return {}; } - return conn; + return {conn, boundary}; } void HttpCameraImpl::DeviceStream(wpi::raw_istream& is, diff --git a/cscore/src/main/native/cpp/HttpCameraImpl.h b/cscore/src/main/native/cpp/HttpCameraImpl.h index 3211fd1083f..e780fb722a9 100644 --- a/cscore/src/main/native/cpp/HttpCameraImpl.h +++ b/cscore/src/main/native/cpp/HttpCameraImpl.h @@ -13,6 +13,7 @@ #include #include #include +#include #include #include @@ -99,8 +100,7 @@ class HttpCameraImpl : public SourceImpl { void StreamThreadMain(); // Functions used by StreamThreadMain() - wpi::HttpConnection* DeviceStreamConnect( - wpi::SmallVectorImpl& boundary); + std::pair DeviceStreamConnect(); void DeviceStream(wpi::raw_istream& is, std::string_view boundary); bool DeviceStreamFrame(wpi::raw_istream& is, std::string& imageBuf); From 777904af466b6d04e4e3f5e1e3761bc989dc91aa Mon Sep 17 00:00:00 2001 From: Peter Johnson Date: Sun, 29 Dec 2024 19:13:31 -0800 Subject: [PATCH 02/54] [cscore] Instance: Remove SmallVector --- cscore/src/main/native/cpp/Instance.h | 14 ++++++-------- .../main/native/cpp/UnlimitedHandleResource.h | 18 +++++++++++------- cscore/src/main/native/cpp/cscore_c.cpp | 9 +++------ cscore/src/main/native/cpp/cscore_cpp.cpp | 16 ++++++---------- cscore/src/main/native/cpp/cscore_oo.cpp | 9 +++------ .../main/native/cpp/jni/CameraServerJNI.cpp | 9 +++------ cscore/src/main/native/include/cscore_cpp.h | 10 +++------- 7 files changed, 35 insertions(+), 50 deletions(-) diff --git a/cscore/src/main/native/cpp/Instance.h b/cscore/src/main/native/cpp/Instance.h index c8ba81d8ad1..13a44937de3 100644 --- a/cscore/src/main/native/cpp/Instance.h +++ b/cscore/src/main/native/cpp/Instance.h @@ -86,18 +86,16 @@ class Instance { void DestroySource(CS_Source handle); void DestroySink(CS_Sink handle); - std::span EnumerateSourceHandles( - wpi::SmallVectorImpl& vec) { - return m_sources.GetAll(vec); + std::vector EnumerateSourceHandles() { + return m_sources.GetAll(); } - std::span EnumerateSinkHandles(wpi::SmallVectorImpl& vec) { - return m_sinks.GetAll(vec); + std::vector EnumerateSinkHandles() { + return m_sinks.GetAll(); } - std::span EnumerateSourceSinks(CS_Source source, - wpi::SmallVectorImpl& vec) { - vec.clear(); + std::vector EnumerateSourceSinks(CS_Source source) { + std::vector vec; m_sinks.ForEach([&](CS_Sink sinkHandle, const SinkData& data) { if (source == data.sourceHandle.load()) { vec.push_back(sinkHandle); diff --git a/cscore/src/main/native/cpp/UnlimitedHandleResource.h b/cscore/src/main/native/cpp/UnlimitedHandleResource.h index a489878e2c3..82f5e738508 100644 --- a/cscore/src/main/native/cpp/UnlimitedHandleResource.h +++ b/cscore/src/main/native/cpp/UnlimitedHandleResource.h @@ -6,11 +6,9 @@ #define CSCORE_UNLIMITEDHANDLERESOURCE_H_ #include -#include #include #include -#include #include namespace cs { @@ -50,7 +48,7 @@ class UnlimitedHandleResource { std::shared_ptr Free(THandle handle); template - std::span GetAll(wpi::SmallVectorImpl& vec); + std::vector GetAll(); std::vector> FreeAll(); @@ -147,10 +145,16 @@ UnlimitedHandleResource::Free( template template -inline std::span -UnlimitedHandleResource::GetAll( - wpi::SmallVectorImpl& vec) { - ForEach([&](THandle handle, const TStruct&) { vec.push_back(handle); }); +inline std::vector +UnlimitedHandleResource::GetAll() { + std::scoped_lock sync(m_handleMutex); + std::vector vec; + vec.reserve(m_structures.size()); + for (size_t i = 0; i < m_structures.size(); i++) { + if (m_structures[i] != nullptr) { + vec.push_back(MakeHandle(i)); + } + } return vec; } diff --git a/cscore/src/main/native/cpp/cscore_c.cpp b/cscore/src/main/native/cpp/cscore_c.cpp index b55d6979955..d82aa59562c 100644 --- a/cscore/src/main/native/cpp/cscore_c.cpp +++ b/cscore/src/main/native/cpp/cscore_c.cpp @@ -221,8 +221,7 @@ CS_VideoMode* CS_EnumerateSourceVideoModes(CS_Source source, int* count, CS_Sink* CS_EnumerateSourceSinks(CS_Source source, int* count, CS_Status* status) { - wpi::SmallVector buf; - auto handles = cs::EnumerateSourceSinks(source, buf, status); + auto handles = cs::EnumerateSourceSinks(source, status); CS_Sink* sinks = static_cast(wpi::safe_malloc(handles.size() * sizeof(CS_Sink))); *count = handles.size(); @@ -440,8 +439,7 @@ void CS_Shutdown(void) { } CS_Source* CS_EnumerateSources(int* count, CS_Status* status) { - wpi::SmallVector buf; - auto handles = cs::EnumerateSourceHandles(buf, status); + auto handles = cs::EnumerateSourceHandles(status); CS_Source* sources = static_cast( wpi::safe_malloc(handles.size() * sizeof(CS_Source))); *count = handles.size(); @@ -463,8 +461,7 @@ void CS_ReleaseEnumeratedSources(CS_Source* sources, int count) { } CS_Sink* CS_EnumerateSinks(int* count, CS_Status* status) { - wpi::SmallVector buf; - auto handles = cs::EnumerateSinkHandles(buf, status); + auto handles = cs::EnumerateSinkHandles(status); CS_Sink* sinks = static_cast(wpi::safe_malloc(handles.size() * sizeof(CS_Sink))); *count = handles.size(); diff --git a/cscore/src/main/native/cpp/cscore_cpp.cpp b/cscore/src/main/native/cpp/cscore_cpp.cpp index f0414f40100..3d10d85a7e4 100644 --- a/cscore/src/main/native/cpp/cscore_cpp.cpp +++ b/cscore/src/main/native/cpp/cscore_cpp.cpp @@ -401,16 +401,14 @@ std::vector EnumerateSourceVideoModes(CS_Source source, return data->source->EnumerateVideoModes(status); } -std::span EnumerateSourceSinks(CS_Source source, - wpi::SmallVectorImpl& vec, - CS_Status* status) { +std::vector EnumerateSourceSinks(CS_Source source, CS_Status* status) { auto& inst = Instance::GetInstance(); auto data = inst.GetSource(source); if (!data) { *status = CS_INVALID_HANDLE; return {}; } - return inst.EnumerateSourceSinks(source, vec); + return inst.EnumerateSourceSinks(source); } CS_Source CopySource(CS_Source source, CS_Status* status) { @@ -868,14 +866,12 @@ void Shutdown() { // Utility Functions // -std::span EnumerateSourceHandles( - wpi::SmallVectorImpl& vec, CS_Status* status) { - return Instance::GetInstance().EnumerateSourceHandles(vec); +std::vector EnumerateSourceHandles(CS_Status* status) { + return Instance::GetInstance().EnumerateSourceHandles(); } -std::span EnumerateSinkHandles(wpi::SmallVectorImpl& vec, - CS_Status* status) { - return Instance::GetInstance().EnumerateSinkHandles(vec); +std::vector EnumerateSinkHandles(CS_Status* status) { + return Instance::GetInstance().EnumerateSinkHandles(); } std::string GetHostname() { diff --git a/cscore/src/main/native/cpp/cscore_oo.cpp b/cscore/src/main/native/cpp/cscore_oo.cpp index 457fb1870c3..902494fe0de 100644 --- a/cscore/src/main/native/cpp/cscore_oo.cpp +++ b/cscore/src/main/native/cpp/cscore_oo.cpp @@ -36,9 +36,8 @@ std::vector VideoSource::EnumerateProperties() const { } std::vector VideoSource::EnumerateSinks() { - wpi::SmallVector handles_buf; CS_Status status = 0; - auto handles = EnumerateSourceSinks(m_handle, handles_buf, &status); + auto handles = EnumerateSourceSinks(m_handle, &status); std::vector sinks; sinks.reserve(handles.size()); @@ -49,9 +48,8 @@ std::vector VideoSource::EnumerateSinks() { } std::vector VideoSource::EnumerateSources() { - wpi::SmallVector handles_buf; CS_Status status = 0; - auto handles = ::cs::EnumerateSourceHandles(handles_buf, &status); + auto handles = ::cs::EnumerateSourceHandles(&status); std::vector sources; sources.reserve(handles.size()); @@ -75,9 +73,8 @@ std::vector VideoSink::EnumerateProperties() const { } std::vector VideoSink::EnumerateSinks() { - wpi::SmallVector handles_buf; CS_Status status = 0; - auto handles = ::cs::EnumerateSinkHandles(handles_buf, &status); + auto handles = ::cs::EnumerateSinkHandles(&status); std::vector sinks; sinks.reserve(handles.size()); diff --git a/cscore/src/main/native/cpp/jni/CameraServerJNI.cpp b/cscore/src/main/native/cpp/jni/CameraServerJNI.cpp index b000894310b..26d0672bbfc 100644 --- a/cscore/src/main/native/cpp/jni/CameraServerJNI.cpp +++ b/cscore/src/main/native/cpp/jni/CameraServerJNI.cpp @@ -888,8 +888,7 @@ Java_edu_wpi_first_cscore_CameraServerJNI_enumerateSourceSinks (JNIEnv* env, jclass, jint source) { CS_Status status = 0; - wpi::SmallVector buf; - auto arr = cs::EnumerateSourceSinks(source, buf, &status); + auto arr = cs::EnumerateSourceSinks(source, &status); if (!CheckStatus(env, status)) { return nullptr; } @@ -1945,8 +1944,7 @@ Java_edu_wpi_first_cscore_CameraServerJNI_enumerateSources (JNIEnv* env, jclass) { CS_Status status = 0; - wpi::SmallVector buf; - auto arr = cs::EnumerateSourceHandles(buf, &status); + auto arr = cs::EnumerateSourceHandles(&status); if (!CheckStatus(env, status)) { return nullptr; } @@ -1963,8 +1961,7 @@ Java_edu_wpi_first_cscore_CameraServerJNI_enumerateSinks (JNIEnv* env, jclass) { CS_Status status = 0; - wpi::SmallVector buf; - auto arr = cs::EnumerateSinkHandles(buf, &status); + auto arr = cs::EnumerateSinkHandles(&status); if (!CheckStatus(env, status)) { return nullptr; } diff --git a/cscore/src/main/native/include/cscore_cpp.h b/cscore/src/main/native/include/cscore_cpp.h index b57faee0ffe..ff2e576a35f 100644 --- a/cscore/src/main/native/include/cscore_cpp.h +++ b/cscore/src/main/native/include/cscore_cpp.h @@ -253,9 +253,7 @@ std::string GetSourceConfigJson(CS_Source source, CS_Status* status); wpi::json GetSourceConfigJsonObject(CS_Source source, CS_Status* status); std::vector EnumerateSourceVideoModes(CS_Source source, CS_Status* status); -std::span EnumerateSourceSinks(CS_Source source, - wpi::SmallVectorImpl& vec, - CS_Status* status); +std::vector EnumerateSourceSinks(CS_Source source, CS_Status* status); CS_Source CopySource(CS_Source source, CS_Status* status); void ReleaseSource(CS_Source source, CS_Status* status); /** @} */ @@ -436,10 +434,8 @@ void Shutdown(); */ std::vector EnumerateUsbCameras(CS_Status* status); -std::span EnumerateSourceHandles( - wpi::SmallVectorImpl& vec, CS_Status* status); -std::span EnumerateSinkHandles(wpi::SmallVectorImpl& vec, - CS_Status* status); +std::vector EnumerateSourceHandles(CS_Status* status); +std::vector EnumerateSinkHandles(CS_Status* status); std::string GetHostname(); From 339c0f2408be59b86d08777dc907f42bc092863b Mon Sep 17 00:00:00 2001 From: Peter Johnson Date: Sun, 29 Dec 2024 19:24:58 -0800 Subject: [PATCH 03/54] [cscore] Property: Remove SmallVector use --- .../src/main/native/cpp/MjpegServerImpl.cpp | 27 +++++--------- .../src/main/native/cpp/PropertyContainer.cpp | 28 +++++++-------- .../src/main/native/cpp/PropertyContainer.h | 13 ++----- cscore/src/main/native/cpp/cscore_c.cpp | 6 ++-- cscore/src/main/native/cpp/cscore_cpp.cpp | 36 +++---------------- .../main/native/cpp/jni/CameraServerJNI.cpp | 6 ++-- cscore/src/main/native/include/cscore_cpp.h | 6 ---- cscore/src/main/native/include/cscore_oo.h | 13 ------- 8 files changed, 32 insertions(+), 103 deletions(-) diff --git a/cscore/src/main/native/cpp/MjpegServerImpl.cpp b/cscore/src/main/native/cpp/MjpegServerImpl.cpp index 5ce5c39a528..31b96a36d6b 100644 --- a/cscore/src/main/native/cpp/MjpegServerImpl.cpp +++ b/cscore/src/main/native/cpp/MjpegServerImpl.cpp @@ -349,11 +349,9 @@ void MjpegServerImpl::ConnThread::SendHTML(wpi::raw_ostream& os, SendHTMLHeadTitle(os); os << startRootPage; - wpi::SmallVector properties_vec; CS_Status status = 0; - for (auto prop : source.EnumerateProperties(properties_vec, &status)) { - wpi::SmallString<128> name_buf; - auto name = source.GetPropertyName(prop, name_buf, &status); + for (auto prop : source.EnumerateProperties(&status)) { + auto name = source.GetPropertyName(prop, &status); if (wpi::starts_with(name, "raw_")) { continue; } @@ -411,18 +409,16 @@ void MjpegServerImpl::ConnThread::SendHTML(wpi::raw_ostream& os, } break; } - case CS_PROP_STRING: { - wpi::SmallString<128> strval_buf; + case CS_PROP_STRING: wpi::print(os, "\n", - name, source.GetStringProperty(prop, strval_buf, &status)); + name, source.GetStringProperty(prop, &status)); wpi::print(os, "\n", name); break; - } default: break; } @@ -489,18 +485,16 @@ void MjpegServerImpl::ConnThread::SendJSON(wpi::raw_ostream& os, } os << "{\n\"controls\": [\n"; - wpi::SmallVector properties_vec; bool first = true; CS_Status status = 0; - for (auto prop : source.EnumerateProperties(properties_vec, &status)) { + for (auto prop : source.EnumerateProperties(&status)) { if (first) { first = false; } else { os << ",\n"; } os << '{'; - wpi::SmallString<128> name_buf; - auto name = source.GetPropertyName(prop, name_buf, &status); + auto name = source.GetPropertyName(prop, &status); auto kind = source.GetPropertyKind(prop); wpi::print(os, "\n\"name\": \"{}\"", name); wpi::print(os, ",\n\"id\": \"{}\"", prop); @@ -518,11 +512,9 @@ void MjpegServerImpl::ConnThread::SendJSON(wpi::raw_ostream& os, case CS_PROP_ENUM: wpi::print(os, "{}", source.GetProperty(prop, &status)); break; - case CS_PROP_STRING: { - wpi::SmallString<128> strval_buf; - os << source.GetStringProperty(prop, strval_buf, &status); + case CS_PROP_STRING: + os << source.GetStringProperty(prop, &status); break; - } default: break; } @@ -773,8 +765,7 @@ void MjpegServerImpl::ConnThread::SendStream(wpi::raw_socket_ostream& os) { lastFrameTime = thisFrameTime; double timestamp = lastFrameTime / 1000000.0; header.clear(); - oss << "\r\n--" BOUNDARY "\r\n" - << "Content-Type: image/jpeg\r\n"; + oss << "\r\n--" BOUNDARY "\r\n" << "Content-Type: image/jpeg\r\n"; wpi::print(oss, "Content-Length: {}\r\n", size); wpi::print(oss, "X-Timestamp: {}\r\n", timestamp); oss << "\r\n"; diff --git a/cscore/src/main/native/cpp/PropertyContainer.cpp b/cscore/src/main/native/cpp/PropertyContainer.cpp index 5fdbd6899b9..96ca8e2930e 100644 --- a/cscore/src/main/native/cpp/PropertyContainer.cpp +++ b/cscore/src/main/native/cpp/PropertyContainer.cpp @@ -9,8 +9,6 @@ #include #include -#include -#include #include using namespace cs; @@ -31,12 +29,14 @@ int PropertyContainer::GetPropertyIndex(std::string_view name) const { return ndx; } -std::span PropertyContainer::EnumerateProperties( - wpi::SmallVectorImpl& vec, CS_Status* status) const { +std::vector PropertyContainer::EnumerateProperties( + CS_Status* status) const { if (!m_properties_cached && !CacheProperties(status)) { return {}; } std::scoped_lock lock(m_mutex); + std::vector vec; + vec.reserve(m_propertyData.size()); for (int i = 0; i < static_cast(m_propertyData.size()); ++i) { if (m_propertyData[i]) { vec.push_back(i + 1); @@ -58,8 +58,8 @@ CS_PropertyKind PropertyContainer::GetPropertyKind(int property) const { return prop->propKind; } -std::string_view PropertyContainer::GetPropertyName( - int property, wpi::SmallVectorImpl& buf, CS_Status* status) const { +std::string PropertyContainer::GetPropertyName(int property, + CS_Status* status) const { if (!m_properties_cached && !CacheProperties(status)) { return {}; } @@ -167,8 +167,8 @@ int PropertyContainer::GetPropertyDefault(int property, return prop->defaultValue; } -std::string_view PropertyContainer::GetStringProperty( - int property, wpi::SmallVectorImpl& buf, CS_Status* status) const { +std::string PropertyContainer::GetStringProperty(int property, + CS_Status* status) const { if (!m_properties_cached && !CacheProperties(status)) { return {}; } @@ -182,9 +182,7 @@ std::string_view PropertyContainer::GetStringProperty( *status = CS_WRONG_PROPERTY_TYPE; return {}; } - buf.clear(); - buf.append(prop->valueStr.begin(), prop->valueStr.end()); - return {buf.data(), buf.size()}; + return prop->valueStr; } void PropertyContainer::SetStringProperty(int property, std::string_view value, @@ -283,11 +281,9 @@ bool PropertyContainer::SetPropertiesJson(const wpi::json& config, wpi::json PropertyContainer::GetPropertiesJsonObject(CS_Status* status) { wpi::json j; - wpi::SmallVector propVec; - for (int p : EnumerateProperties(propVec, status)) { + for (int p : EnumerateProperties(status)) { wpi::json prop; - wpi::SmallString<128> strBuf; - prop.emplace("name", GetPropertyName(p, strBuf, status)); + prop.emplace("name", GetPropertyName(p, status)); switch (GetPropertyKind(p)) { case CS_PROP_BOOLEAN: prop.emplace("value", static_cast(GetProperty(p, status))); @@ -297,7 +293,7 @@ wpi::json PropertyContainer::GetPropertiesJsonObject(CS_Status* status) { prop.emplace("value", GetProperty(p, status)); break; case CS_PROP_STRING: - prop.emplace("value", GetStringProperty(p, strBuf, status)); + prop.emplace("value", GetStringProperty(p, status)); break; default: continue; diff --git a/cscore/src/main/native/cpp/PropertyContainer.h b/cscore/src/main/native/cpp/PropertyContainer.h index 99d030bfc68..e84288cc54f 100644 --- a/cscore/src/main/native/cpp/PropertyContainer.h +++ b/cscore/src/main/native/cpp/PropertyContainer.h @@ -22,8 +22,6 @@ namespace wpi { class Logger; -template -class SmallVectorImpl; } // namespace wpi namespace cs { @@ -33,21 +31,16 @@ class PropertyContainer { virtual ~PropertyContainer() = default; int GetPropertyIndex(std::string_view name) const; - std::span EnumerateProperties(wpi::SmallVectorImpl& vec, - CS_Status* status) const; + std::vector EnumerateProperties(CS_Status* status) const; CS_PropertyKind GetPropertyKind(int property) const; - std::string_view GetPropertyName(int property, - wpi::SmallVectorImpl& buf, - CS_Status* status) const; + std::string GetPropertyName(int property, CS_Status* status) const; int GetProperty(int property, CS_Status* status) const; virtual void SetProperty(int property, int value, CS_Status* status); int GetPropertyMin(int property, CS_Status* status) const; int GetPropertyMax(int property, CS_Status* status) const; int GetPropertyStep(int property, CS_Status* status) const; int GetPropertyDefault(int property, CS_Status* status) const; - std::string_view GetStringProperty(int property, - wpi::SmallVectorImpl& buf, - CS_Status* status) const; + std::string GetStringProperty(int property, CS_Status* status) const; virtual void SetStringProperty(int property, std::string_view value, CS_Status* status); std::vector GetEnumPropertyChoices(int property, diff --git a/cscore/src/main/native/cpp/cscore_c.cpp b/cscore/src/main/native/cpp/cscore_c.cpp index d82aa59562c..37629f77a7b 100644 --- a/cscore/src/main/native/cpp/cscore_c.cpp +++ b/cscore/src/main/native/cpp/cscore_c.cpp @@ -57,8 +57,7 @@ CS_PropertyKind CS_GetPropertyKind(CS_Property property, CS_Status* status) { void CS_GetPropertyName(CS_Property property, WPI_String* name, CS_Status* status) { - wpi::SmallString<128> buf; - cs::ConvertToC(name, cs::GetPropertyName(property, buf, status)); + cs::ConvertToC(name, cs::GetPropertyName(property, status)); } int CS_GetProperty(CS_Property property, CS_Status* status) { @@ -87,8 +86,7 @@ int CS_GetPropertyDefault(CS_Property property, CS_Status* status) { void CS_GetStringProperty(CS_Property property, WPI_String* value, CS_Status* status) { - wpi::SmallString<128> buf; - cs::ConvertToC(value, cs::GetStringProperty(property, buf, status)); + cs::ConvertToC(value, cs::GetStringProperty(property, status)); } void CS_SetStringProperty(CS_Property property, const struct WPI_String* value, diff --git a/cscore/src/main/native/cpp/cscore_cpp.cpp b/cscore/src/main/native/cpp/cscore_cpp.cpp index 3d10d85a7e4..51d7e0e1d5c 100644 --- a/cscore/src/main/native/cpp/cscore_cpp.cpp +++ b/cscore/src/main/native/cpp/cscore_cpp.cpp @@ -67,24 +67,12 @@ CS_PropertyKind GetPropertyKind(CS_Property property, CS_Status* status) { } std::string GetPropertyName(CS_Property property, CS_Status* status) { - wpi::SmallString<128> buf; - int propertyIndex; - auto container = GetPropertyContainer(property, &propertyIndex, status); - if (!container) { - return {}; - } - return std::string{container->GetPropertyName(propertyIndex, buf, status)}; -} - -std::string_view GetPropertyName(CS_Property property, - wpi::SmallVectorImpl& buf, - CS_Status* status) { int propertyIndex; auto container = GetPropertyContainer(property, &propertyIndex, status); if (!container) { return {}; } - return container->GetPropertyName(propertyIndex, buf, status); + return container->GetPropertyName(propertyIndex, status); } int GetProperty(CS_Property property, CS_Status* status) { @@ -142,24 +130,12 @@ int GetPropertyDefault(CS_Property property, CS_Status* status) { } std::string GetStringProperty(CS_Property property, CS_Status* status) { - wpi::SmallString<128> buf; - int propertyIndex; - auto container = GetPropertyContainer(property, &propertyIndex, status); - if (!container) { - return {}; - } - return std::string{container->GetStringProperty(propertyIndex, buf, status)}; -} - -std::string_view GetStringProperty(CS_Property property, - wpi::SmallVectorImpl& buf, - CS_Status* status) { int propertyIndex; auto container = GetPropertyContainer(property, &propertyIndex, status); if (!container) { return {}; } - return container->GetStringProperty(propertyIndex, buf, status); + return container->GetStringProperty(propertyIndex, status); } void SetStringProperty(CS_Property property, std::string_view value, @@ -297,9 +273,7 @@ std::span EnumerateSourceProperties( *status = CS_INVALID_HANDLE; return {}; } - wpi::SmallVector properties_buf; - for (auto property : - data->source->EnumerateProperties(properties_buf, status)) { + for (auto property : data->source->EnumerateProperties(status)) { vec.push_back(Handle{source, property, Handle::kProperty}); } return vec; @@ -591,9 +565,7 @@ std::span EnumerateSinkProperties( *status = CS_INVALID_HANDLE; return {}; } - wpi::SmallVector properties_buf; - for (auto property : - data->sink->EnumerateProperties(properties_buf, status)) { + for (auto property : data->sink->EnumerateProperties(status)) { vec.push_back(Handle{sink, property, Handle::kSinkProperty}); } return vec; diff --git a/cscore/src/main/native/cpp/jni/CameraServerJNI.cpp b/cscore/src/main/native/cpp/jni/CameraServerJNI.cpp index 26d0672bbfc..04701637399 100644 --- a/cscore/src/main/native/cpp/jni/CameraServerJNI.cpp +++ b/cscore/src/main/native/cpp/jni/CameraServerJNI.cpp @@ -311,8 +311,7 @@ Java_edu_wpi_first_cscore_CameraServerJNI_getPropertyName (JNIEnv* env, jclass, jint property) { CS_Status status = 0; - wpi::SmallString<128> buf; - auto str = cs::GetPropertyName(property, buf, &status); + auto str = cs::GetPropertyName(property, &status); if (!CheckStatus(env, status)) { return nullptr; } @@ -418,8 +417,7 @@ Java_edu_wpi_first_cscore_CameraServerJNI_getStringProperty (JNIEnv* env, jclass, jint property) { CS_Status status = 0; - wpi::SmallString<128> buf; - auto str = cs::GetStringProperty(property, buf, &status); + auto str = cs::GetStringProperty(property, &status); if (!CheckStatus(env, status)) { return nullptr; } diff --git a/cscore/src/main/native/include/cscore_cpp.h b/cscore/src/main/native/include/cscore_cpp.h index ff2e576a35f..9192db1508c 100644 --- a/cscore/src/main/native/include/cscore_cpp.h +++ b/cscore/src/main/native/include/cscore_cpp.h @@ -178,9 +178,6 @@ struct RawEvent { */ CS_PropertyKind GetPropertyKind(CS_Property property, CS_Status* status); std::string GetPropertyName(CS_Property property, CS_Status* status); -std::string_view GetPropertyName(CS_Property property, - wpi::SmallVectorImpl& buf, - CS_Status* status); int GetProperty(CS_Property property, CS_Status* status); void SetProperty(CS_Property property, int value, CS_Status* status); int GetPropertyMin(CS_Property property, CS_Status* status); @@ -188,9 +185,6 @@ int GetPropertyMax(CS_Property property, CS_Status* status); int GetPropertyStep(CS_Property property, CS_Status* status); int GetPropertyDefault(CS_Property property, CS_Status* status); std::string GetStringProperty(CS_Property property, CS_Status* status); -std::string_view GetStringProperty(CS_Property property, - wpi::SmallVectorImpl& buf, - CS_Status* status); void SetStringProperty(CS_Property property, std::string_view value, CS_Status* status); std::vector GetEnumPropertyChoices(CS_Property property, diff --git a/cscore/src/main/native/include/cscore_oo.h b/cscore/src/main/native/include/cscore_oo.h index 5b653042255..f031e196ca2 100644 --- a/cscore/src/main/native/include/cscore_oo.h +++ b/cscore/src/main/native/include/cscore_oo.h @@ -184,19 +184,6 @@ class VideoProperty { return GetStringProperty(m_handle, &m_status); } - /** - * Returns the string property value as a reference to the given buffer. - * - * This function is string-specific. - * - * @param buf The backing storage to which to write the property value. - * @return The string property value as a reference to the given buffer. - */ - std::string_view GetString(wpi::SmallVectorImpl& buf) const { - m_status = 0; - return GetStringProperty(m_handle, buf, &m_status); - } - /** * Sets the string property value. * From 1f2c01876041a6f26e2c3843017e4650fda87a47 Mon Sep 17 00:00:00 2001 From: Peter Johnson Date: Sun, 29 Dec 2024 19:31:00 -0800 Subject: [PATCH 04/54] [cscore] SourceImpl: Remove SmallVector use --- .../native/cpp/cameraserver/CameraServer.cpp | 8 +++--- cscore/src/main/native/cpp/SourceImpl.cpp | 6 ++--- cscore/src/main/native/cpp/SourceImpl.h | 2 +- cscore/src/main/native/cpp/cscore_c.cpp | 6 ++--- cscore/src/main/native/cpp/cscore_cpp.cpp | 25 +------------------ .../main/native/cpp/jni/CameraServerJNI.cpp | 6 ++--- cscore/src/main/native/include/cscore_cpp.h | 6 ----- .../src/main/native/linux/UsbCameraImpl.cpp | 3 +-- 8 files changed, 12 insertions(+), 50 deletions(-) diff --git a/cameraserver/src/main/native/cpp/cameraserver/CameraServer.cpp b/cameraserver/src/main/native/cpp/cameraserver/CameraServer.cpp index 69293bbaac9..e111dcdafe3 100644 --- a/cameraserver/src/main/native/cpp/cameraserver/CameraServer.cpp +++ b/cameraserver/src/main/native/cpp/cameraserver/CameraServer.cpp @@ -363,8 +363,7 @@ SourcePublisher::SourcePublisher(Instance& inst, CS_Status status = 0; wpi::SmallString<64> buf; sourcePublisher.Set(MakeSourceValue(source, buf)); - wpi::SmallString<64> descBuf; - descriptionPublisher.Set(cs::GetSourceDescription(source, descBuf, &status)); + descriptionPublisher.Set(cs::GetSourceDescription(source, &status)); connectedPublisher.Set(cs::IsSourceConnected(source, &status)); streamsPublisher.Set(inst.GetSourceStreamValues(source)); auto mode = cs::GetSourceVideoMode(source, &status); @@ -404,9 +403,8 @@ Instance::Instance() { case cs::VideoEvent::kSourceConnected: if (auto publisher = GetPublisher(event.sourceHandle)) { // update the description too (as it may have changed) - wpi::SmallString<64> descBuf; - publisher->descriptionPublisher.Set(cs::GetSourceDescription( - event.sourceHandle, descBuf, &status)); + publisher->descriptionPublisher.Set( + cs::GetSourceDescription(event.sourceHandle, &status)); publisher->connectedPublisher.Set(true); } break; diff --git a/cscore/src/main/native/cpp/SourceImpl.cpp b/cscore/src/main/native/cpp/SourceImpl.cpp index fc9eb24cb47..c1df595e402 100644 --- a/cscore/src/main/native/cpp/SourceImpl.cpp +++ b/cscore/src/main/native/cpp/SourceImpl.cpp @@ -50,11 +50,9 @@ void SourceImpl::SetDescription(std::string_view description) { m_description = description; } -std::string_view SourceImpl::GetDescription( - wpi::SmallVectorImpl& buf) const { +std::string SourceImpl::GetDescription() const { std::scoped_lock lock(m_mutex); - buf.append(m_description.begin(), m_description.end()); - return {buf.data(), buf.size()}; + return m_description; } void SourceImpl::SetConnected(bool connected) { diff --git a/cscore/src/main/native/cpp/SourceImpl.h b/cscore/src/main/native/cpp/SourceImpl.h index dbaa62762a7..6f92aea13fb 100644 --- a/cscore/src/main/native/cpp/SourceImpl.h +++ b/cscore/src/main/native/cpp/SourceImpl.h @@ -44,7 +44,7 @@ class SourceImpl : public PropertyContainer { std::string_view GetName() const { return m_name; } void SetDescription(std::string_view description); - std::string_view GetDescription(wpi::SmallVectorImpl& buf) const; + std::string GetDescription() const; void SetConnectionStrategy(CS_ConnectionStrategy strategy) { m_strategy = static_cast(strategy); diff --git a/cscore/src/main/native/cpp/cscore_c.cpp b/cscore/src/main/native/cpp/cscore_c.cpp index 37629f77a7b..3460c1b0a07 100644 --- a/cscore/src/main/native/cpp/cscore_c.cpp +++ b/cscore/src/main/native/cpp/cscore_c.cpp @@ -110,14 +110,12 @@ CS_SourceKind CS_GetSourceKind(CS_Source source, CS_Status* status) { } void CS_GetSourceName(CS_Source source, WPI_String* name, CS_Status* status) { - wpi::SmallString<128> buf; - cs::ConvertToC(name, cs::GetSourceName(source, buf, status)); + cs::ConvertToC(name, cs::GetSourceName(source, status)); } void CS_GetSourceDescription(CS_Source source, WPI_String* description, CS_Status* status) { - wpi::SmallString<128> buf; - cs::ConvertToC(description, cs::GetSourceDescription(source, buf, status)); + cs::ConvertToC(description, cs::GetSourceDescription(source, status)); } uint64_t CS_GetSourceLastFrameTime(CS_Source source, CS_Status* status) { diff --git a/cscore/src/main/native/cpp/cscore_cpp.cpp b/cscore/src/main/native/cpp/cscore_cpp.cpp index 51d7e0e1d5c..b1b12cd4d1b 100644 --- a/cscore/src/main/native/cpp/cscore_cpp.cpp +++ b/cscore/src/main/native/cpp/cscore_cpp.cpp @@ -180,36 +180,13 @@ std::string GetSourceName(CS_Source source, CS_Status* status) { return std::string{data->source->GetName()}; } -std::string_view GetSourceName(CS_Source source, - wpi::SmallVectorImpl& buf, - CS_Status* status) { - auto data = Instance::GetInstance().GetSource(source); - if (!data) { - *status = CS_INVALID_HANDLE; - return {}; - } - return data->source->GetName(); -} - std::string GetSourceDescription(CS_Source source, CS_Status* status) { auto data = Instance::GetInstance().GetSource(source); if (!data) { *status = CS_INVALID_HANDLE; return {}; } - wpi::SmallString<128> buf; - return std::string{data->source->GetDescription(buf)}; -} - -std::string_view GetSourceDescription(CS_Source source, - wpi::SmallVectorImpl& buf, - CS_Status* status) { - auto data = Instance::GetInstance().GetSource(source); - if (!data) { - *status = CS_INVALID_HANDLE; - return {}; - } - return data->source->GetDescription(buf); + return data->source->GetDescription(); } uint64_t GetSourceLastFrameTime(CS_Source source, CS_Status* status) { diff --git a/cscore/src/main/native/cpp/jni/CameraServerJNI.cpp b/cscore/src/main/native/cpp/jni/CameraServerJNI.cpp index 04701637399..216f2a53fd1 100644 --- a/cscore/src/main/native/cpp/jni/CameraServerJNI.cpp +++ b/cscore/src/main/native/cpp/jni/CameraServerJNI.cpp @@ -614,8 +614,7 @@ Java_edu_wpi_first_cscore_CameraServerJNI_getSourceName (JNIEnv* env, jclass, jint source) { CS_Status status = 0; - wpi::SmallString<128> buf; - auto str = cs::GetSourceName(source, buf, &status); + auto str = cs::GetSourceName(source, &status); if (!CheckStatus(env, status)) { return nullptr; } @@ -632,8 +631,7 @@ Java_edu_wpi_first_cscore_CameraServerJNI_getSourceDescription (JNIEnv* env, jclass, jint source) { CS_Status status = 0; - wpi::SmallString<128> buf; - auto str = cs::GetSourceDescription(source, buf, &status); + auto str = cs::GetSourceDescription(source, &status); if (!CheckStatus(env, status)) { return nullptr; } diff --git a/cscore/src/main/native/include/cscore_cpp.h b/cscore/src/main/native/include/cscore_cpp.h index 9192db1508c..ea824c12dbc 100644 --- a/cscore/src/main/native/include/cscore_cpp.h +++ b/cscore/src/main/native/include/cscore_cpp.h @@ -213,13 +213,7 @@ CS_Source CreateCvSource(std::string_view name, const VideoMode& mode, */ CS_SourceKind GetSourceKind(CS_Source source, CS_Status* status); std::string GetSourceName(CS_Source source, CS_Status* status); -std::string_view GetSourceName(CS_Source source, - wpi::SmallVectorImpl& buf, - CS_Status* status); std::string GetSourceDescription(CS_Source source, CS_Status* status); -std::string_view GetSourceDescription(CS_Source source, - wpi::SmallVectorImpl& buf, - CS_Status* status); uint64_t GetSourceLastFrameTime(CS_Source source, CS_Status* status); void SetSourceConnectionStrategy(CS_Source source, CS_ConnectionStrategy strategy, diff --git a/cscore/src/main/native/linux/UsbCameraImpl.cpp b/cscore/src/main/native/linux/UsbCameraImpl.cpp index 9c6548435b3..5c7804f2415 100644 --- a/cscore/src/main/native/linux/UsbCameraImpl.cpp +++ b/cscore/src/main/native/linux/UsbCameraImpl.cpp @@ -1453,8 +1453,7 @@ bool UsbCameraImpl::CacheProperties(CS_Status* status) const { } void UsbCameraImpl::SetQuirks() { - wpi::SmallString<128> descbuf; - std::string_view desc = GetDescription(descbuf); + auto desc = GetDescription(); m_lifecam_exposure = wpi::ends_with(desc, "LifeCam HD-3000") || wpi::ends_with(desc, "LifeCam Cinema (TM)"); m_ov9281_exposure = wpi::contains(desc, "OV9281"); From 435f86d0465a4d8fc5ebca9734ceed10c9b45cbb Mon Sep 17 00:00:00 2001 From: Peter Johnson Date: Sun, 29 Dec 2024 19:37:05 -0800 Subject: [PATCH 05/54] [cscore] Remove SmallVector use from enumerate handles --- cscore/src/main/native/cpp/cscore_c.cpp | 6 ++---- cscore/src/main/native/cpp/cscore_cpp.cpp | 19 ++++++++++++------- cscore/src/main/native/cpp/cscore_oo.cpp | 6 ++---- .../main/native/cpp/jni/CameraServerJNI.cpp | 6 ++---- cscore/src/main/native/include/cscore_cpp.h | 9 ++++----- 5 files changed, 22 insertions(+), 24 deletions(-) diff --git a/cscore/src/main/native/cpp/cscore_c.cpp b/cscore/src/main/native/cpp/cscore_c.cpp index 3460c1b0a07..e5bc68c94c7 100644 --- a/cscore/src/main/native/cpp/cscore_c.cpp +++ b/cscore/src/main/native/cpp/cscore_c.cpp @@ -144,8 +144,7 @@ CS_Property CS_GetSourceProperty(CS_Source source, CS_Property* CS_EnumerateSourceProperties(CS_Source source, int* count, CS_Status* status) { - wpi::SmallVector buf; - auto vec = cs::EnumerateSourceProperties(source, buf, status); + auto vec = cs::EnumerateSourceProperties(source, status); CS_Property* out = static_cast( wpi::safe_malloc(vec.size() * sizeof(CS_Property))); *count = vec.size(); @@ -290,8 +289,7 @@ CS_Property CS_GetSinkProperty(CS_Sink sink, const struct WPI_String* name, CS_Property* CS_EnumerateSinkProperties(CS_Sink sink, int* count, CS_Status* status) { - wpi::SmallVector buf; - auto vec = cs::EnumerateSinkProperties(sink, buf, status); + auto vec = cs::EnumerateSinkProperties(sink, status); CS_Property* out = static_cast( wpi::safe_malloc(vec.size() * sizeof(CS_Property))); *count = vec.size(); diff --git a/cscore/src/main/native/cpp/cscore_cpp.cpp b/cscore/src/main/native/cpp/cscore_cpp.cpp index b1b12cd4d1b..111287d8f86 100644 --- a/cscore/src/main/native/cpp/cscore_cpp.cpp +++ b/cscore/src/main/native/cpp/cscore_cpp.cpp @@ -242,15 +242,17 @@ CS_Property GetSourceProperty(CS_Source source, std::string_view name, return Handle{source, property, Handle::kProperty}; } -std::span EnumerateSourceProperties( - CS_Source source, wpi::SmallVectorImpl& vec, - CS_Status* status) { +std::vector EnumerateSourceProperties(CS_Source source, + CS_Status* status) { auto data = Instance::GetInstance().GetSource(source); if (!data) { *status = CS_INVALID_HANDLE; return {}; } - for (auto property : data->source->EnumerateProperties(status)) { + std::vector vec; + auto handles = data->source->EnumerateProperties(status); + vec.reserve(handles.size()); + for (auto property : handles) { vec.push_back(Handle{source, property, Handle::kProperty}); } return vec; @@ -535,14 +537,17 @@ CS_Property GetSinkProperty(CS_Sink sink, std::string_view name, return Handle{sink, property, Handle::kSinkProperty}; } -std::span EnumerateSinkProperties( - CS_Sink sink, wpi::SmallVectorImpl& vec, CS_Status* status) { +std::vector EnumerateSinkProperties( + CS_Sink sink, CS_Status* status) { auto data = Instance::GetInstance().GetSink(sink); if (!data) { *status = CS_INVALID_HANDLE; return {}; } - for (auto property : data->sink->EnumerateProperties(status)) { + std::vector vec; + auto handles = data->sink->EnumerateProperties(status); + vec.reserve(handles.size()); + for (auto property : handles) { vec.push_back(Handle{sink, property, Handle::kSinkProperty}); } return vec; diff --git a/cscore/src/main/native/cpp/cscore_oo.cpp b/cscore/src/main/native/cpp/cscore_oo.cpp index 902494fe0de..cf0c81ed8ec 100644 --- a/cscore/src/main/native/cpp/cscore_oo.cpp +++ b/cscore/src/main/native/cpp/cscore_oo.cpp @@ -23,9 +23,8 @@ wpi::json VideoSink::GetConfigJsonObject() const { } std::vector VideoSource::EnumerateProperties() const { - wpi::SmallVector handles_buf; CS_Status status = 0; - auto handles = EnumerateSourceProperties(m_handle, handles_buf, &status); + auto handles = EnumerateSourceProperties(m_handle, &status); std::vector properties; properties.reserve(handles.size()); @@ -60,9 +59,8 @@ std::vector VideoSource::EnumerateSources() { } std::vector VideoSink::EnumerateProperties() const { - wpi::SmallVector handles_buf; CS_Status status = 0; - auto handles = EnumerateSinkProperties(m_handle, handles_buf, &status); + auto handles = EnumerateSinkProperties(m_handle, &status); std::vector properties; properties.reserve(handles.size()); diff --git a/cscore/src/main/native/cpp/jni/CameraServerJNI.cpp b/cscore/src/main/native/cpp/jni/CameraServerJNI.cpp index 216f2a53fd1..a2e632c7b6c 100644 --- a/cscore/src/main/native/cpp/jni/CameraServerJNI.cpp +++ b/cscore/src/main/native/cpp/jni/CameraServerJNI.cpp @@ -728,8 +728,7 @@ Java_edu_wpi_first_cscore_CameraServerJNI_enumerateSourceProperties (JNIEnv* env, jclass, jint source) { CS_Status status = 0; - wpi::SmallVector buf; - auto arr = cs::EnumerateSourceProperties(source, buf, &status); + auto arr = cs::EnumerateSourceProperties(source, &status); if (!CheckStatus(env, status)) { return nullptr; } @@ -1442,8 +1441,7 @@ Java_edu_wpi_first_cscore_CameraServerJNI_enumerateSinkProperties (JNIEnv* env, jclass, jint source) { CS_Status status = 0; - wpi::SmallVector buf; - auto arr = cs::EnumerateSinkProperties(source, buf, &status); + auto arr = cs::EnumerateSinkProperties(source, &status); if (!CheckStatus(env, status)) { return nullptr; } diff --git a/cscore/src/main/native/include/cscore_cpp.h b/cscore/src/main/native/include/cscore_cpp.h index ea824c12dbc..bf89fe25a3e 100644 --- a/cscore/src/main/native/include/cscore_cpp.h +++ b/cscore/src/main/native/include/cscore_cpp.h @@ -222,9 +222,8 @@ bool IsSourceConnected(CS_Source source, CS_Status* status); bool IsSourceEnabled(CS_Source source, CS_Status* status); CS_Property GetSourceProperty(CS_Source source, std::string_view name, CS_Status* status); -std::span EnumerateSourceProperties( - CS_Source source, wpi::SmallVectorImpl& vec, - CS_Status* status); +std::vector EnumerateSourceProperties(CS_Source source, + CS_Status* status); VideoMode GetSourceVideoMode(CS_Source source, CS_Status* status); bool SetSourceVideoMode(CS_Source source, const VideoMode& mode, CS_Status* status); @@ -327,8 +326,8 @@ std::string_view GetSinkDescription(CS_Sink sink, CS_Status* status); CS_Property GetSinkProperty(CS_Sink sink, std::string_view name, CS_Status* status); -std::span EnumerateSinkProperties( - CS_Sink sink, wpi::SmallVectorImpl& vec, CS_Status* status); +std::vector EnumerateSinkProperties(CS_Sink sink, + CS_Status* status); void SetSinkSource(CS_Sink sink, CS_Source source, CS_Status* status); CS_Property GetSinkSourceProperty(CS_Sink sink, std::string_view name, CS_Status* status); From 254f25e1a80a47c6d965f7c718abeb45c705b7f6 Mon Sep 17 00:00:00 2001 From: Peter Johnson Date: Sun, 29 Dec 2024 19:42:37 -0800 Subject: [PATCH 06/54] [cscore] Remove SmallVector from SinkImpl --- cscore/src/main/native/cpp/SinkImpl.cpp | 31 ++----------------- cscore/src/main/native/cpp/SinkImpl.h | 3 +- cscore/src/main/native/cpp/cscore_c.cpp | 6 ++-- cscore/src/main/native/cpp/cscore_cpp.cpp | 25 +-------------- .../main/native/cpp/jni/CameraServerJNI.cpp | 10 ++---- cscore/src/main/native/include/cscore_cpp.h | 8 ----- 6 files changed, 10 insertions(+), 73 deletions(-) diff --git a/cscore/src/main/native/cpp/SinkImpl.cpp b/cscore/src/main/native/cpp/SinkImpl.cpp index 32d86adf163..1c8bd8830eb 100644 --- a/cscore/src/main/native/cpp/SinkImpl.cpp +++ b/cscore/src/main/native/cpp/SinkImpl.cpp @@ -37,11 +37,9 @@ void SinkImpl::SetDescription(std::string_view description) { m_description = description; } -std::string_view SinkImpl::GetDescription( - wpi::SmallVectorImpl& buf) const { +std::string SinkImpl::GetDescription() const { std::scoped_lock lock(m_mutex); - buf.append(m_description.begin(), m_description.end()); - return {buf.data(), buf.size()}; + return m_description; } void SinkImpl::Enable() { @@ -114,18 +112,6 @@ std::string SinkImpl::GetError() const { return std::string{m_source->GetCurFrame().GetError()}; } -std::string_view SinkImpl::GetError(wpi::SmallVectorImpl& buf) const { - std::scoped_lock lock(m_mutex); - if (!m_source) { - return "no source connected"; - } - // Make a copy as it's shared data - std::string_view error = m_source->GetCurFrame().GetError(); - buf.clear(); - buf.append(error.data(), error.data() + error.size()); - return {buf.data(), buf.size()}; -} - bool SinkImpl::SetConfigJson(std::string_view config, CS_Status* status) { wpi::json j; try { @@ -222,16 +208,6 @@ std::string GetSinkError(CS_Sink sink, CS_Status* status) { return data->sink->GetError(); } -std::string_view GetSinkError(CS_Sink sink, wpi::SmallVectorImpl& buf, - CS_Status* status) { - auto data = Instance::GetInstance().GetSink(sink); - if (!data || (data->kind & SinkMask) == 0) { - *status = CS_INVALID_HANDLE; - return {}; - } - return data->sink->GetError(buf); -} - void SetSinkEnabled(CS_Sink sink, bool enabled, CS_Status* status) { auto data = Instance::GetInstance().GetSink(sink); if (!data || (data->kind & SinkMask) == 0) { @@ -251,8 +227,7 @@ void CS_SetSinkDescription(CS_Sink sink, const struct WPI_String* description, void CS_GetSinkError(CS_Sink sink, struct WPI_String* error, CS_Status* status) { - wpi::SmallString<128> buf; - cs::ConvertToC(error, cs::GetSinkError(sink, buf, status)); + cs::ConvertToC(error, cs::GetSinkError(sink, status)); } void CS_SetSinkEnabled(CS_Sink sink, CS_Bool enabled, CS_Status* status) { diff --git a/cscore/src/main/native/cpp/SinkImpl.h b/cscore/src/main/native/cpp/SinkImpl.h index fae19671995..2d53197ab5c 100644 --- a/cscore/src/main/native/cpp/SinkImpl.h +++ b/cscore/src/main/native/cpp/SinkImpl.h @@ -32,7 +32,7 @@ class SinkImpl : public PropertyContainer { std::string_view GetName() const { return m_name; } void SetDescription(std::string_view description); - std::string_view GetDescription(wpi::SmallVectorImpl& buf) const; + std::string GetDescription() const; void Enable(); void Disable(); @@ -46,7 +46,6 @@ class SinkImpl : public PropertyContainer { } std::string GetError() const; - std::string_view GetError(wpi::SmallVectorImpl& buf) const; bool SetConfigJson(std::string_view config, CS_Status* status); virtual bool SetConfigJson(const wpi::json& config, CS_Status* status); diff --git a/cscore/src/main/native/cpp/cscore_c.cpp b/cscore/src/main/native/cpp/cscore_c.cpp index e5bc68c94c7..17e49a024bd 100644 --- a/cscore/src/main/native/cpp/cscore_c.cpp +++ b/cscore/src/main/native/cpp/cscore_c.cpp @@ -272,14 +272,12 @@ CS_SinkKind CS_GetSinkKind(CS_Sink sink, CS_Status* status) { } void CS_GetSinkName(CS_Sink sink, WPI_String* name, CS_Status* status) { - wpi::SmallString<128> buf; - cs::ConvertToC(name, cs::GetSinkName(sink, buf, status)); + cs::ConvertToC(name, cs::GetSinkName(sink, status)); } void CS_GetSinkDescription(CS_Sink sink, WPI_String* description, CS_Status* status) { - wpi::SmallString<128> buf; - cs::ConvertToC(description, cs::GetSinkDescription(sink, buf, status)); + cs::ConvertToC(description, cs::GetSinkDescription(sink, status)); } CS_Property CS_GetSinkProperty(CS_Sink sink, const struct WPI_String* name, diff --git a/cscore/src/main/native/cpp/cscore_cpp.cpp b/cscore/src/main/native/cpp/cscore_cpp.cpp index 111287d8f86..0cc0b22b3a3 100644 --- a/cscore/src/main/native/cpp/cscore_cpp.cpp +++ b/cscore/src/main/native/cpp/cscore_cpp.cpp @@ -8,7 +8,6 @@ #include #include -#include #include #include @@ -491,35 +490,13 @@ std::string GetSinkName(CS_Sink sink, CS_Status* status) { return std::string{data->sink->GetName()}; } -std::string_view GetSinkName(CS_Sink sink, wpi::SmallVectorImpl& buf, - CS_Status* status) { - auto data = Instance::GetInstance().GetSink(sink); - if (!data) { - *status = CS_INVALID_HANDLE; - return {}; - } - return data->sink->GetName(); -} - std::string GetSinkDescription(CS_Sink sink, CS_Status* status) { auto data = Instance::GetInstance().GetSink(sink); if (!data) { *status = CS_INVALID_HANDLE; return {}; } - wpi::SmallString<128> buf; - return std::string{data->sink->GetDescription(buf)}; -} - -std::string_view GetSinkDescription(CS_Sink sink, - wpi::SmallVectorImpl& buf, - CS_Status* status) { - auto data = Instance::GetInstance().GetSink(sink); - if (!data) { - *status = CS_INVALID_HANDLE; - return {}; - } - return data->sink->GetDescription(buf); + return data->sink->GetDescription(); } CS_Property GetSinkProperty(CS_Sink sink, std::string_view name, diff --git a/cscore/src/main/native/cpp/jni/CameraServerJNI.cpp b/cscore/src/main/native/cpp/jni/CameraServerJNI.cpp index a2e632c7b6c..8e94c92503d 100644 --- a/cscore/src/main/native/cpp/jni/CameraServerJNI.cpp +++ b/cscore/src/main/native/cpp/jni/CameraServerJNI.cpp @@ -10,7 +10,6 @@ #define WPI_RAWFRAME_JNI #include -#include #include #include "cscore_raw.h" @@ -1386,8 +1385,7 @@ Java_edu_wpi_first_cscore_CameraServerJNI_getSinkName (JNIEnv* env, jclass, jint sink) { CS_Status status = 0; - wpi::SmallString<128> buf; - auto str = cs::GetSinkName(sink, buf, &status); + auto str = cs::GetSinkName(sink, &status); if (!CheckStatus(env, status)) { return nullptr; } @@ -1404,8 +1402,7 @@ Java_edu_wpi_first_cscore_CameraServerJNI_getSinkDescription (JNIEnv* env, jclass, jint sink) { CS_Status status = 0; - wpi::SmallString<128> buf; - auto str = cs::GetSinkDescription(sink, buf, &status); + auto str = cs::GetSinkDescription(sink, &status); if (!CheckStatus(env, status)) { return nullptr; } @@ -1660,8 +1657,7 @@ Java_edu_wpi_first_cscore_CameraServerJNI_getSinkError (JNIEnv* env, jclass, jint sink) { CS_Status status = 0; - wpi::SmallString<128> buf; - auto str = cs::GetSinkError(sink, buf, &status); + auto str = cs::GetSinkError(sink, &status); if (!CheckStatus(env, status)) { return nullptr; } diff --git a/cscore/src/main/native/include/cscore_cpp.h b/cscore/src/main/native/include/cscore_cpp.h index bf89fe25a3e..e471f707de7 100644 --- a/cscore/src/main/native/include/cscore_cpp.h +++ b/cscore/src/main/native/include/cscore_cpp.h @@ -14,7 +14,6 @@ #include #include -#include #include #include "cscore_c.h" @@ -318,12 +317,7 @@ CS_Sink CreateCvSinkCallback(std::string_view name, */ CS_SinkKind GetSinkKind(CS_Sink sink, CS_Status* status); std::string GetSinkName(CS_Sink sink, CS_Status* status); -std::string_view GetSinkName(CS_Sink sink, wpi::SmallVectorImpl& buf, - CS_Status* status); std::string GetSinkDescription(CS_Sink sink, CS_Status* status); -std::string_view GetSinkDescription(CS_Sink sink, - wpi::SmallVectorImpl& buf, - CS_Status* status); CS_Property GetSinkProperty(CS_Sink sink, std::string_view name, CS_Status* status); std::vector EnumerateSinkProperties(CS_Sink sink, @@ -357,8 +351,6 @@ int GetMjpegServerPort(CS_Sink sink, CS_Status* status); void SetSinkDescription(CS_Sink sink, std::string_view description, CS_Status* status); std::string GetSinkError(CS_Sink sink, CS_Status* status); -std::string_view GetSinkError(CS_Sink sink, wpi::SmallVectorImpl& buf, - CS_Status* status); void SetSinkEnabled(CS_Sink sink, bool enabled, CS_Status* status); /** @} */ From 1a7f721d61895de14f3d447697a336e67ca679a5 Mon Sep 17 00:00:00 2001 From: Peter Johnson Date: Mon, 30 Dec 2024 08:23:07 -0800 Subject: [PATCH 07/54] Remove unused include --- cscore/src/main/native/cpp/MjpegServerImpl.h | 1 - 1 file changed, 1 deletion(-) diff --git a/cscore/src/main/native/cpp/MjpegServerImpl.h b/cscore/src/main/native/cpp/MjpegServerImpl.h index 01f41da3b72..27bc0a1e211 100644 --- a/cscore/src/main/native/cpp/MjpegServerImpl.h +++ b/cscore/src/main/native/cpp/MjpegServerImpl.h @@ -13,7 +13,6 @@ #include #include -#include #include #include #include From 9970cedc3bc15e3075f70c9522272d0b8631a6e6 Mon Sep 17 00:00:00 2001 From: Peter Johnson Date: Mon, 30 Dec 2024 08:28:06 -0800 Subject: [PATCH 08/54] UsbCameraProperty: Don't use SmallString --- .../main/native/linux/UsbCameraProperty.cpp | 26 ++++++++----------- 1 file changed, 11 insertions(+), 15 deletions(-) diff --git a/cscore/src/main/native/linux/UsbCameraProperty.cpp b/cscore/src/main/native/linux/UsbCameraProperty.cpp index d8847d17657..899ab7a6494 100644 --- a/cscore/src/main/native/linux/UsbCameraProperty.cpp +++ b/cscore/src/main/native/linux/UsbCameraProperty.cpp @@ -9,7 +9,6 @@ #include #include -#include #include #include "UsbUtil.h" @@ -98,7 +97,7 @@ static int GetStringCtrlIoctl(int fd, int id, int maximum, std::string* value) { static int SetStringCtrlIoctl(int fd, int id, int maximum, std::string_view value) { - wpi::SmallString<64> str{wpi::substr(value, 0, maximum)}; + std::string str{wpi::substr(value, 0, maximum)}; struct v4l2_ext_control ctrl; struct v4l2_ext_controls ctrls; @@ -115,21 +114,22 @@ static int SetStringCtrlIoctl(int fd, int id, int maximum, // Removes non-alphanumeric characters and replaces spaces with underscores. // e.g. "Zoom, Absolute" -> "zoom_absolute", "Pan (Absolute)" -> "pan_absolute" -static std::string_view NormalizeName(std::string_view name, - wpi::SmallVectorImpl& buf) { +static std::string NormalizeName(std::string_view name) { + std::string out; + out.reserve(name.size()); bool newWord = false; for (auto ch : name) { if (std::isalnum(ch)) { if (newWord) { - buf.push_back('_'); + out.push_back('_'); } newWord = false; - buf.push_back(std::tolower(ch)); - } else if (!buf.empty()) { + out.push_back(std::tolower(ch)); + } else if (!out.empty()) { newWord = true; } } - return {buf.data(), buf.size()}; + return out; } #ifdef VIDIOC_QUERY_EXT_CTRL @@ -170,8 +170,7 @@ UsbCameraProperty::UsbCameraProperty(const struct v4l2_query_ext_ctrl& ctrl) while (len < sizeof(ctrl.name) && ctrl.name[len] != '\0') { ++len; } - wpi::SmallString<64> name_buf; - name = NormalizeName({ctrl.name, len}, name_buf); + name = NormalizeName({ctrl.name, len}); } #endif @@ -209,9 +208,7 @@ UsbCameraProperty::UsbCameraProperty(const struct v4l2_queryctrl& ctrl) while (len < sizeof(ctrl.name) && ctrl.name[len] != '\0') { ++len; } - wpi::SmallString<64> name_buf; - name = - NormalizeName({reinterpret_cast(ctrl.name), len}, name_buf); + name = NormalizeName({reinterpret_cast(ctrl.name), len}); } std::unique_ptr UsbCameraProperty::DeviceQuery(int fd, @@ -309,8 +306,7 @@ bool UsbCameraProperty::DeviceGet(std::unique_lock& lock, int fd) { bool UsbCameraProperty::DeviceSet(std::unique_lock& lock, int fd) const { // Make a copy of the string as we're about to release the lock - wpi::SmallString<128> valueStrCopy{valueStr}; - return DeviceSet(lock, fd, value, valueStrCopy.str()); + return DeviceSet(lock, fd, value, std::string{valueStr}); } bool UsbCameraProperty::DeviceSet(std::unique_lock& lock, int fd, From 8066a8d1eb9a3c7dec5d72ae256c32c07c189001 Mon Sep 17 00:00:00 2001 From: Peter Johnson Date: Mon, 30 Dec 2024 08:30:12 -0800 Subject: [PATCH 09/54] Remove unused includes --- cscore/src/main/native/cpp/SinkImpl.cpp | 1 - cscore/src/main/native/cpp/cscore_c.cpp | 1 - 2 files changed, 2 deletions(-) diff --git a/cscore/src/main/native/cpp/SinkImpl.cpp b/cscore/src/main/native/cpp/SinkImpl.cpp index 1c8bd8830eb..9f1a975625d 100644 --- a/cscore/src/main/native/cpp/SinkImpl.cpp +++ b/cscore/src/main/native/cpp/SinkImpl.cpp @@ -6,7 +6,6 @@ #include -#include #include #include "Instance.h" diff --git a/cscore/src/main/native/cpp/cscore_c.cpp b/cscore/src/main/native/cpp/cscore_c.cpp index 17e49a024bd..6e4c1bca87b 100644 --- a/cscore/src/main/native/cpp/cscore_c.cpp +++ b/cscore/src/main/native/cpp/cscore_c.cpp @@ -11,7 +11,6 @@ #include #include -#include #include "c_util.h" #include "cscore_cpp.h" From 830398abf02a4a390b11be3cc27c95f7c81fd8cf Mon Sep 17 00:00:00 2001 From: Peter Johnson Date: Mon, 30 Dec 2024 08:34:44 -0800 Subject: [PATCH 10/54] Remove more SmallVector uses --- cscore/src/main/native/cpp/ConfigurableSourceImpl.cpp | 2 +- cscore/src/main/native/cpp/HttpCameraImpl.cpp | 4 ++-- cscore/src/main/native/cpp/jni/CameraServerJNI.cpp | 6 +++--- cscore/src/main/native/linux/UsbCameraImpl.h | 1 - 4 files changed, 6 insertions(+), 7 deletions(-) diff --git a/cscore/src/main/native/cpp/ConfigurableSourceImpl.cpp b/cscore/src/main/native/cpp/ConfigurableSourceImpl.cpp index 0244c28bfe3..7c615a9685c 100644 --- a/cscore/src/main/native/cpp/ConfigurableSourceImpl.cpp +++ b/cscore/src/main/native/cpp/ConfigurableSourceImpl.cpp @@ -238,7 +238,7 @@ CS_Property CS_CreateSourcePropertyCallback( void CS_SetSourceEnumPropertyChoices(CS_Source source, CS_Property property, const struct WPI_String* choices, int count, CS_Status* status) { - wpi::SmallVector vec; + std::vector vec; vec.reserve(count); for (int i = 0; i < count; ++i) { vec.emplace_back(wpi::to_string_view(&choices[i])); diff --git a/cscore/src/main/native/cpp/HttpCameraImpl.cpp b/cscore/src/main/native/cpp/HttpCameraImpl.cpp index 16ee1f4c6e6..d3fe48c1990 100644 --- a/cscore/src/main/native/cpp/HttpCameraImpl.cpp +++ b/cscore/src/main/native/cpp/HttpCameraImpl.cpp @@ -658,7 +658,7 @@ CS_Source CS_CreateHttpCamera(const struct WPI_String* name, CS_Source CS_CreateHttpCameraMulti(const struct WPI_String* name, const struct WPI_String* urls, int count, CS_HttpCameraKind kind, CS_Status* status) { - wpi::SmallVector vec; + std::vector vec; vec.reserve(count); for (int i = 0; i < count; ++i) { vec.emplace_back(wpi::to_string_view(&urls[i])); @@ -672,7 +672,7 @@ CS_HttpCameraKind CS_GetHttpCameraKind(CS_Source source, CS_Status* status) { void CS_SetHttpCameraUrls(CS_Source source, const struct WPI_String* urls, int count, CS_Status* status) { - wpi::SmallVector vec; + std::vector vec; vec.reserve(count); for (int i = 0; i < count; ++i) { vec.emplace_back(wpi::to_string_view(&urls[i])); diff --git a/cscore/src/main/native/cpp/jni/CameraServerJNI.cpp b/cscore/src/main/native/cpp/jni/CameraServerJNI.cpp index 8e94c92503d..61ed444bdb6 100644 --- a/cscore/src/main/native/cpp/jni/CameraServerJNI.cpp +++ b/cscore/src/main/native/cpp/jni/CameraServerJNI.cpp @@ -544,7 +544,7 @@ Java_edu_wpi_first_cscore_CameraServerJNI_createHttpCameraMulti return 0; } size_t len = env->GetArrayLength(urls); - wpi::SmallVector vec; + std::vector vec; vec.reserve(len); for (size_t i = 0; i < len; ++i) { JLocal elem{ @@ -1110,7 +1110,7 @@ Java_edu_wpi_first_cscore_CameraServerJNI_setHttpCameraUrls return; } size_t len = env->GetArrayLength(urls); - wpi::SmallVector vec; + std::vector vec; vec.reserve(len); for (size_t i = 0; i < len; ++i) { JLocal elem{ @@ -1300,7 +1300,7 @@ Java_edu_wpi_first_cscore_CameraServerJNI_setSourceEnumPropertyChoices return; } size_t len = env->GetArrayLength(choices); - wpi::SmallVector vec; + std::vector vec; vec.reserve(len); for (size_t i = 0; i < len; ++i) { JLocal elem{ diff --git a/cscore/src/main/native/linux/UsbCameraImpl.h b/cscore/src/main/native/linux/UsbCameraImpl.h index acf1bfcd453..d976f8611e0 100644 --- a/cscore/src/main/native/linux/UsbCameraImpl.h +++ b/cscore/src/main/native/linux/UsbCameraImpl.h @@ -15,7 +15,6 @@ #include #include -#include #include #include #include From b272d9e6094d990cabfc6775bd3a91d4eb17bb6d Mon Sep 17 00:00:00 2001 From: Peter Johnson Date: Mon, 30 Dec 2024 18:54:05 -0800 Subject: [PATCH 11/54] Linux UsbCameraImpl: Remove SmallString --- .../src/main/native/linux/UsbCameraImpl.cpp | 34 +++++++------------ 1 file changed, 12 insertions(+), 22 deletions(-) diff --git a/cscore/src/main/native/linux/UsbCameraImpl.cpp b/cscore/src/main/native/linux/UsbCameraImpl.cpp index 5c7804f2415..c7d75faab19 100644 --- a/cscore/src/main/native/linux/UsbCameraImpl.cpp +++ b/cscore/src/main/native/linux/UsbCameraImpl.cpp @@ -27,7 +27,6 @@ #include #include -#include #include #include #include @@ -395,7 +394,7 @@ void UsbCameraImpl::CameraThreadMain() { int notify_fd = inotify_init(); if (notify_fd >= 0) { // need to make a copy as dirname can modify it - wpi::SmallString<64> pathCopy{m_path}; + std::string pathCopy{m_path}; pathCopy.push_back('\0'); if (inotify_add_watch(notify_fd, dirname(pathCopy.data()), IN_CREATE | IN_DELETE) < 0) { @@ -409,9 +408,9 @@ void UsbCameraImpl::CameraThreadMain() { bool notified = (notify_fd < 0); // treat as always notified if cannot notify // Get the basename for later notify use - wpi::SmallString<64> pathCopy{m_path}; + std::string pathCopy{m_path}; pathCopy.push_back('\0'); - wpi::SmallString<64> base{basename(pathCopy.data())}; + std::string base{basename(pathCopy.data())}; // Used to restart streaming on reconnect bool wasStreaming = false; @@ -486,13 +485,13 @@ void UsbCameraImpl::CameraThreadMain() { // Read the event structure notify_is->read(&event, sizeof(event)); // Read the event name - wpi::SmallString<64> raw_name; + std::string raw_name; raw_name.resize(event.len); notify_is->read(raw_name.data(), event.len); // If the name is what we expect... std::string_view name{raw_name.c_str()}; SDEBUG4("got event on '{}' ({}) compare to '{}' ({}) mask {}", name, - name.size(), base.str(), base.size(), event.mask); + name.size(), base, base.size(), event.mask); if (name == base) { if ((event.mask & IN_DELETE) != 0) { wasStreaming = m_streaming; @@ -1667,18 +1666,15 @@ UsbCameraInfo GetUsbCameraInfo(CS_Source source, CS_Status* status) { // look through /dev/v4l/by-id and /dev/v4l/by-path for symlinks to the // keypath - wpi::SmallString<128> path; for (auto symlinkDir : symlinkDirs) { if (DIR* dp = ::opendir(symlinkDir)) { while (struct dirent* ep = ::readdir(dp)) { if (ep->d_type == DT_LNK) { - path = symlinkDir; - path += '/'; - path += ep->d_name; + auto path = fmt::format("{}/{}", symlinkDir, ep->d_name); char* target = ::realpath(path.c_str(), nullptr); if (target) { if (keypath == target) { - info.otherPaths.emplace_back(path.str()); + info.otherPaths.emplace_back(std::move(path)); } std::free(target); } @@ -1716,16 +1712,13 @@ std::vector EnumerateUsbCameras(CS_Status* status) { UsbCameraInfo info; info.dev = dev; + info.path = fmt::format("/dev/{}", fname); - wpi::SmallString<32> path{"/dev/"}; - path += fname; - info.path = path.str(); - - if (!IsVideoCaptureDevice(path.c_str())) { + if (!IsVideoCaptureDevice(info.path.c_str())) { continue; } - info.name = GetDescriptionImpl(path.c_str()); + info.name = GetDescriptionImpl(info.path.c_str()); if (info.name.empty()) { continue; } @@ -1746,14 +1739,11 @@ std::vector EnumerateUsbCameras(CS_Status* status) { // look through /dev/v4l/by-id and /dev/v4l/by-path for symlinks to // /dev/videoN - wpi::SmallString<128> path; for (auto symlinkDir : symlinkDirs) { if (DIR* dp = ::opendir(symlinkDir)) { while (struct dirent* ep = ::readdir(dp)) { if (ep->d_type == DT_LNK) { - path = symlinkDir; - path += '/'; - path += ep->d_name; + auto path = fmt::format("{}/{}", symlinkDir, ep->d_name); char* target = ::realpath(path.c_str(), nullptr); if (target) { std::string fname = fs::path{target}.filename(); @@ -1762,7 +1752,7 @@ std::vector EnumerateUsbCameras(CS_Status* status) { (dev = wpi::parse_integer(wpi::substr(fname, 5), 10)) && dev.value() < retval.size()) { - retval[dev.value()].otherPaths.emplace_back(path.str()); + retval[dev.value()].otherPaths.emplace_back(std::move(path)); } std::free(target); } From 87432388d34a97a0e998a7f88410ce748f3df56b Mon Sep 17 00:00:00 2001 From: Peter Johnson Date: Mon, 30 Dec 2024 18:57:11 -0800 Subject: [PATCH 12/54] UsbUtil: Remove some SmallStrign usage --- cscore/src/main/native/linux/UsbUtil.cpp | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/cscore/src/main/native/linux/UsbUtil.cpp b/cscore/src/main/native/linux/UsbUtil.cpp index 620871db0bd..3a7fbeef877 100644 --- a/cscore/src/main/native/linux/UsbUtil.cpp +++ b/cscore/src/main/native/linux/UsbUtil.cpp @@ -27,7 +27,7 @@ static std::string GetUsbNameFromFile(int vendor, int product) { return {}; } - wpi::SmallString<128> buf; + std::string buf; wpi::raw_fd_istream is{fd, true}; // build vendor and product 4-char hex strings @@ -59,13 +59,13 @@ static std::string GetUsbNameFromFile(int vendor, int product) { // next vendor, but didn't match product? if (line[0] != '\t') { buf += "Unknown"; - return std::string{buf}; + return buf; } // look for product if (wpi::starts_with(wpi::substr(line, 1), productStr)) { buf += wpi::trim(wpi::substr(line, 6)); - return std::string{buf}; + return buf; } } } From fa1b89ded3b113b1ab58cf44d60d2c00e5feb8f8 Mon Sep 17 00:00:00 2001 From: Peter Johnson Date: Mon, 30 Dec 2024 19:06:09 -0800 Subject: [PATCH 13/54] MjpegServerImpl: Remove some SmallString usage --- .../src/main/native/cpp/MjpegServerImpl.cpp | 20 ++++++++++--------- 1 file changed, 11 insertions(+), 9 deletions(-) diff --git a/cscore/src/main/native/cpp/MjpegServerImpl.cpp b/cscore/src/main/native/cpp/MjpegServerImpl.cpp index 31b96a36d6b..dce3b9137e2 100644 --- a/cscore/src/main/native/cpp/MjpegServerImpl.cpp +++ b/cscore/src/main/native/cpp/MjpegServerImpl.cpp @@ -199,8 +199,8 @@ bool MjpegServerImpl::ConnThread::ProcessCommand(wpi::raw_ostream& os, SourceImpl& source, std::string_view parameters, bool respond) { - wpi::SmallString<256> responseBuf; - wpi::raw_svector_ostream response{responseBuf}; + std::string responseBuf; + wpi::raw_string_ostream response{responseBuf}; // command format: param1=value1¶m2=value2... while (!parameters.empty()) { // split out next param and value @@ -393,19 +393,20 @@ void MjpegServerImpl::ConnThread::SendHTML(wpi::raw_ostream& os, continue; // skip empty choices } // replace any non-printable characters in name with spaces - wpi::SmallString<128> ch_name; + std::string ch_name; + ch_name.reserve(choice->size()); for (char ch : *choice) { ch_name.push_back(wpi::isPrint(ch) ? ch : ' '); } wpi::print(os, "\n", name, j, - ch_name.str()); + ch_name); } break; } @@ -534,11 +535,12 @@ void MjpegServerImpl::ConnThread::SendJSON(wpi::raw_ostream& os, os << ", "; } // replace any non-printable characters in name with spaces - wpi::SmallString<128> ch_name; + std::string ch_name; + ch_name.reserve(choice->size()); for (char ch : *choice) { ch_name.push_back(std::isprint(ch) ? ch : ' '); } - wpi::print(os, "\"{}\": \"{}\"", j, ch_name.str()); + wpi::print(os, "\"{}\": \"{}\"", j, ch_name); } os << "}\n"; } @@ -664,8 +666,8 @@ void MjpegServerImpl::ConnThread::SendStream(wpi::raw_socket_ostream& os) { os.SetUnbuffered(); - wpi::SmallString<256> header; - wpi::raw_svector_ostream oss{header}; + std::string header; + wpi::raw_string_ostream oss{header}; SendHeader(oss, 200, "OK", "multipart/x-mixed-replace;boundary=" BOUNDARY); os << oss.str(); From 325b7e79cabb096911eb79f7ffd3de7de8f5a3f1 Mon Sep 17 00:00:00 2001 From: Peter Johnson Date: Mon, 30 Dec 2024 19:42:02 -0800 Subject: [PATCH 14/54] HttpUtil: Change from SmallVector to std::string --- cscore/src/main/native/cpp/HttpCameraImpl.cpp | 23 ++-- .../src/main/native/cpp/MjpegServerImpl.cpp | 13 +- ntcore/src/main/native/cpp/NetworkClient.cpp | 3 +- ntcore/src/main/native/cpp/NetworkServer.cpp | 11 +- wpinet/src/main/native/cpp/HttpUtil.cpp | 122 ++++++++---------- wpinet/src/main/native/cpp/WebServer.cpp | 26 ++-- .../src/main/native/include/wpinet/HttpUtil.h | 46 +++---- 7 files changed, 104 insertions(+), 140 deletions(-) diff --git a/cscore/src/main/native/cpp/HttpCameraImpl.cpp b/cscore/src/main/native/cpp/HttpCameraImpl.cpp index d3fe48c1990..7f6a6336f6b 100644 --- a/cscore/src/main/native/cpp/HttpCameraImpl.cpp +++ b/cscore/src/main/native/cpp/HttpCameraImpl.cpp @@ -194,11 +194,10 @@ HttpCameraImpl::DeviceStreamConnect() { } // Parse Content-Type header to get the boundary - auto [mediaType, contentType] = wpi::split(conn->contentType.str(), ';'); + auto [mediaType, contentType] = wpi::split(conn->contentType, ';'); mediaType = wpi::trim(mediaType); if (mediaType != "multipart/x-mixed-replace") { - SWARNING("\"{}\": unrecognized Content-Type \"{}\"", req.host.str(), - mediaType); + SWARNING("\"{}\": unrecognized Content-Type \"{}\"", req.host, mediaType); std::scoped_lock lock(m_mutex); m_streamConn = nullptr; return {}; @@ -221,8 +220,7 @@ HttpCameraImpl::DeviceStreamConnect() { } if (boundary.empty()) { - SWARNING("\"{}\": empty multi-part boundary or no Content-Type", - req.host.str()); + SWARNING("\"{}\": empty multi-part boundary or no Content-Type", req.host); std::scoped_lock lock(m_mutex); m_streamConn = nullptr; return {}; @@ -276,26 +274,25 @@ void HttpCameraImpl::DeviceStream(wpi::raw_istream& is, bool HttpCameraImpl::DeviceStreamFrame(wpi::raw_istream& is, std::string& imageBuf) { // Read the headers - wpi::SmallString<64> contentTypeBuf; - wpi::SmallString<64> contentLengthBuf; - if (!ParseHttpHeaders(is, &contentTypeBuf, &contentLengthBuf)) { + std::string contentType; + std::string contentLength; + if (!ParseHttpHeaders(is, &contentType, &contentLength)) { SWARNING("disconnected during headers"); PutError("disconnected during headers", wpi::Now()); return false; } // Check the content type (if present) - if (!contentTypeBuf.str().empty() && - !wpi::starts_with(contentTypeBuf, "image/jpeg")) { - auto errMsg = fmt::format("received unknown Content-Type \"{}\"", - contentTypeBuf.str()); + if (!contentType.empty() && !wpi::starts_with(contentType, "image/jpeg")) { + auto errMsg = + fmt::format("received unknown Content-Type \"{}\"", contentType); SWARNING("{}", errMsg); PutError(errMsg, wpi::Now()); return false; } int width, height; - if (auto v = wpi::parse_integer(contentLengthBuf, 10)) { + if (auto v = wpi::parse_integer(contentLength, 10)) { // We know how big it is! Just get a frame of the right size and read // the data directly into it. unsigned int contentLength = v.value(); diff --git a/cscore/src/main/native/cpp/MjpegServerImpl.cpp b/cscore/src/main/native/cpp/MjpegServerImpl.cpp index dce3b9137e2..4f917c8e323 100644 --- a/cscore/src/main/native/cpp/MjpegServerImpl.cpp +++ b/cscore/src/main/native/cpp/MjpegServerImpl.cpp @@ -216,25 +216,24 @@ bool MjpegServerImpl::ConnThread::ProcessCommand(wpi::raw_ostream& os, SDEBUG4("HTTP parameter \"{}\" value \"{}\"", rawParam, rawValue); // unescape param - bool error = false; - wpi::SmallString<64> paramBuf; - std::string_view param = wpi::UnescapeURI(rawParam, paramBuf, &error); - if (error) { + auto exParam = wpi::UnescapeURI(rawParam); + if (!exParam) { auto estr = fmt::format("could not unescape parameter \"{}\"", rawParam); SendError(os, 500, estr); SDEBUG("{}", estr); return false; } + auto param = *std::move(exParam); // unescape value - wpi::SmallString<64> valueBuf; - std::string_view value = wpi::UnescapeURI(rawValue, valueBuf, &error); - if (error) { + auto exValue = wpi::UnescapeURI(rawValue); + if (!exValue) { auto estr = fmt::format("could not unescape value \"{}\"", rawValue); SendError(os, 500, estr); SDEBUG("{}", estr); return false; } + auto value = *std::move(exValue); // Handle resolution, compression, and FPS. These are handled locally // rather than passed to the source. diff --git a/ntcore/src/main/native/cpp/NetworkClient.cpp b/ntcore/src/main/native/cpp/NetworkClient.cpp index e01b53a9fcf..e77de7137fd 100644 --- a/ntcore/src/main/native/cpp/NetworkClient.cpp +++ b/ntcore/src/main/native/cpp/NetworkClient.cpp @@ -229,9 +229,8 @@ void NetworkClient::TcpConnected(uv::Tcp& tcp) { } wpi::WebSocket::ClientOptions options; options.handshakeTimeout = kWebsocketHandshakeTimeout; - wpi::SmallString<128> idBuf; auto ws = wpi::WebSocket::CreateClient( - tcp, fmt::format("/nt/{}", wpi::EscapeURI(m_id, idBuf)), "", + tcp, fmt::format("/nt/{}", wpi::EscapeURI(m_id)), "", {"v4.1.networktables.first.wpi.edu", "networktables.first.wpi.edu"}, options); ws->SetMaxMessageSize(kMaxMessageSize); diff --git a/ntcore/src/main/native/cpp/NetworkServer.cpp b/ntcore/src/main/native/cpp/NetworkServer.cpp index f3d0ef46819..35073f56d28 100644 --- a/ntcore/src/main/native/cpp/NetworkServer.cpp +++ b/ntcore/src/main/native/cpp/NetworkServer.cpp @@ -15,7 +15,6 @@ #include #include -#include #include #include #include @@ -169,13 +168,11 @@ void NetworkServer::ServerConnection4::ProcessWsUpgrade() { } DEBUG4("path: '{}'", path); - wpi::SmallString<128> nameBuf; - std::string_view name; - bool err = false; + wpi::expected name; if (auto uri = wpi::remove_prefix(path, "/nt/")) { - name = wpi::UnescapeURI(*uri, nameBuf, &err); + name = wpi::UnescapeURI(*uri); } - if (err || name.empty()) { + if (!name || name->empty()) { INFO("invalid path '{}' (from {}), must match /nt/[clientId], closing", path, m_connInfo); m_websocket->Fail( @@ -185,7 +182,7 @@ void NetworkServer::ServerConnection4::ProcessWsUpgrade() { m_websocket->SetMaxMessageSize(kMaxMessageSize); - m_websocket->open.connect([this, name = std::string{name}]( + m_websocket->open.connect([this, name = std::move(*name)]( std::string_view protocol) { m_info.protocol_version = protocol == "v4.1.networktables.first.wpi.edu" ? 0x0401 : 0x0400; diff --git a/wpinet/src/main/native/cpp/HttpUtil.cpp b/wpinet/src/main/native/cpp/HttpUtil.cpp index 5a63039fdf4..58552841420 100644 --- a/wpinet/src/main/native/cpp/HttpUtil.cpp +++ b/wpinet/src/main/native/cpp/HttpUtil.cpp @@ -10,118 +10,106 @@ #include #include +#include #include #include namespace wpi { -std::string_view UnescapeURI(std::string_view str, SmallVectorImpl& buf, - bool* error) { - buf.clear(); +wpi::expected UnescapeURI(std::string_view str) { + std::string out; + out.reserve(str.size()); for (auto i = str.begin(), end = str.end(); i != end; ++i) { // pass non-escaped characters to output if (*i != '%') { // decode + to space if (*i == '+') { - buf.push_back(' '); + out.push_back(' '); } else { - buf.push_back(*i); + out.push_back(*i); } continue; } // are there enough characters left? if (i + 2 >= end) { - *error = true; - return {}; + return wpi::unexpected{i - str.begin()}; } // replace %xx with the corresponding character unsigned val1 = hexDigitValue(*++i); if (val1 == -1U) { - *error = true; - return {}; + return wpi::unexpected{i - str.begin()}; } unsigned val2 = hexDigitValue(*++i); if (val2 == -1U) { - *error = true; - return {}; + return wpi::unexpected{i - str.begin()}; } - buf.push_back((val1 << 4) | val2); + out.push_back((val1 << 4) | val2); } - - *error = false; - return {buf.data(), buf.size()}; + return std::move(out); } -std::string_view EscapeURI(std::string_view str, SmallVectorImpl& buf, - bool spacePlus) { +std::string EscapeURI(std::string_view str, bool spacePlus) { static const char* const hexLut = "0123456789ABCDEF"; - buf.clear(); + std::string out; + out.reserve(str.size() + 1); for (auto i = str.begin(), end = str.end(); i != end; ++i) { // pass unreserved characters to output if (std::isalnum(*i) || *i == '-' || *i == '_' || *i == '.' || *i == '~') { - buf.push_back(*i); + out.push_back(*i); continue; } // encode space to + if (spacePlus && *i == ' ') { - buf.push_back('+'); + out.push_back('+'); continue; } // convert others to %xx - buf.push_back('%'); - buf.push_back(hexLut[((*i) >> 4) & 0x0f]); - buf.push_back(hexLut[(*i) & 0x0f]); + out.push_back('%'); + out.push_back(hexLut[((*i) >> 4) & 0x0f]); + out.push_back(hexLut[(*i) & 0x0f]); } - return {buf.data(), buf.size()}; + return out; } -std::string_view EscapeHTML(std::string_view str, SmallVectorImpl& buf) { - buf.clear(); +std::string EscapeHTML(std::string_view str) { + std::string out; + out.reserve(str.size() + 1); for (auto i = str.begin(), end = str.end(); i != end; ++i) { if (*i == '&') { - buf.append({'&', 'a', 'm', 'p', ';'}); + out.append({'&', 'a', 'm', 'p', ';'}); } else if (*i == '<') { - buf.append({'&', 'l', 't', ';'}); + out.append({'&', 'l', 't', ';'}); } else if (*i == '>') { - buf.append({'&', 'g', 't', ';'}); + out.append({'&', 'g', 't', ';'}); } else { - buf.push_back(*i); + out.push_back(*i); } } - return {buf.data(), buf.size()}; + return out; } HttpQueryMap::HttpQueryMap(std::string_view query) { split(query, '&', 100, false, [&](auto elem) { auto [nameEsc, valueEsc] = split(elem, '='); - SmallString<64> nameBuf; - bool err = false; - auto name = wpi::UnescapeURI(nameEsc, nameBuf, &err); // note: ignores duplicates - if (!err) { - m_elems.try_emplace(name, valueEsc); + if (auto name = wpi::UnescapeURI(nameEsc)) { + m_elems.try_emplace(*name, valueEsc); } }); } -std::optional HttpQueryMap::Get( - std::string_view name, wpi::SmallVectorImpl& buf) const { +std::optional HttpQueryMap::Get(std::string_view name) const { auto it = m_elems.find(name); if (it == m_elems.end()) { return {}; } - bool err = false; - auto val = wpi::UnescapeURI(it->second, buf, &err); - if (err) { - return {}; - } - return val; + return wpi::UnescapeURI(it->second).value_or(std::string{}); } HttpPath::HttpPath(std::string_view path) { @@ -131,14 +119,12 @@ HttpPath::HttpPath(std::string_view path) { return; } split(path, '/', 100, false, [&](auto elem) { - SmallString<64> buf; - bool err = false; - auto val = wpi::UnescapeURI(elem, buf, &err); - if (err) { + auto val = wpi::UnescapeURI(elem); + if (!val) { m_pathEnds.clear(); return; } - m_pathBuf += val; + m_pathBuf += *val; m_pathEnds.emplace_back(m_pathBuf.size()); }); } @@ -165,8 +151,8 @@ std::string_view HttpPath::operator[](size_t n) const { return slice(m_pathBuf, n == 0 ? 0 : m_pathEnds[n - 1], m_pathEnds[n]); } -bool ParseHttpHeaders(raw_istream& is, SmallVectorImpl* contentType, - SmallVectorImpl* contentLength) { +bool ParseHttpHeaders(raw_istream& is, std::string* contentType, + std::string* contentLength) { if (contentType) { contentType->clear(); } @@ -216,7 +202,7 @@ bool ParseHttpHeaders(raw_istream& is, SmallVectorImpl* contentType, bool FindMultipartBoundary(raw_istream& is, std::string_view boundary, std::string* saveBuf) { - SmallString<64> searchBuf; + std::string searchBuf; searchBuf.resize(boundary.size() + 2); size_t searchPos = 0; @@ -304,16 +290,17 @@ HttpLocation::HttpLocation(std::string_view url_, bool* error, if (!userpass.empty()) { auto [rawUser, rawPassword] = split(userpass, ':'); - SmallString<64> userBuf, passBuf; - user = UnescapeURI(rawUser, userBuf, error); - if (*error) { + auto user = UnescapeURI(rawUser); + if (!user) { *errorMsg = fmt::format("could not unescape user \"{}\"", rawUser); + *error = true; return; } - password = UnescapeURI(rawPassword, passBuf, error); - if (*error) { + auto password = UnescapeURI(rawPassword); + if (!password) { *errorMsg = fmt::format("could not unescape password \"{}\"", rawPassword); + *error = true; return; } } @@ -350,23 +337,22 @@ HttpLocation::HttpLocation(std::string_view url_, bool* error, std::tie(rawParam, rawValue) = split(rawParam, '='); // unescape param - *error = false; - SmallString<64> paramBuf; - std::string_view param = UnescapeURI(rawParam, paramBuf, error); - if (*error) { + auto param = UnescapeURI(rawParam); + if (!param) { *errorMsg = fmt::format("could not unescape parameter \"{}\"", rawParam); + *error = true; return; } // unescape value - SmallString<64> valueBuf; - std::string_view value = UnescapeURI(rawValue, valueBuf, error); - if (*error) { + auto value = UnescapeURI(rawValue); + if (!value) { *errorMsg = fmt::format("could not unescape value \"{}\"", rawValue); + *error = true; return; } - params.emplace_back(std::pair{param, value}); + params.emplace_back(std::pair{*param, *value}); } *error = false; @@ -374,11 +360,7 @@ HttpLocation::HttpLocation(std::string_view url_, bool* error, void HttpRequest::SetAuth(const HttpLocation& loc) { if (!loc.user.empty()) { - SmallString<64> userpass; - userpass += loc.user; - userpass += ':'; - userpass += loc.password; - Base64Encode(userpass.str(), &auth); + Base64Encode(fmt::format("{}:{}", loc.user, loc.password), &auth); } } diff --git a/wpinet/src/main/native/cpp/WebServer.cpp b/wpinet/src/main/native/cpp/WebServer.cpp index 9efd76a1900..e697131ea4d 100644 --- a/wpinet/src/main/native/cpp/WebServer.cpp +++ b/wpinet/src/main/native/cpp/WebServer.cpp @@ -235,19 +235,18 @@ void MyHttpConnection::ProcessRequest() { return; } - std::string_view path; + std::string_view origpath; if (url.HasPath()) { - path = url.GetPath(); + origpath = url.GetPath(); } // fmt::print(stderr, "path: \"{}\"\n", path); - wpi::SmallString<128> pathBuf; - bool error; - path = UnescapeURI(path, pathBuf, &error); - if (error) { + auto exPath = UnescapeURI(origpath); + if (!exPath) { SendError(400); return; } + auto path = *std::move(exPath); std::string_view query; if (url.HasQuery()) { @@ -269,8 +268,7 @@ void MyHttpConnection::ProcessRequest() { return; } // generate directory listing - wpi::SmallString<64> formatBuf; - if (qmap.Get("format", formatBuf).value_or("") == "json") { + if (qmap.Get("format").value_or("") == "json") { wpi::json dirs = wpi::json::array(); wpi::json files = wpi::json::array(); for (auto&& entry : fs::directory_iterator{fullpath}) { @@ -294,19 +292,17 @@ void MyHttpConnection::ProcessRequest() { for (auto&& entry : fs::directory_iterator{fullpath}) { bool subdir = entry.is_directory(ec); std::string name = entry.path().filename().string(); - wpi::SmallString<128> nameUriBuf, nameHtmlBuf; if (subdir) { dirs.emplace( name, fmt::format( "{}/", - EscapeURI(name, nameUriBuf), - EscapeHTML(name, nameHtmlBuf))); + EscapeURI(name), EscapeHTML(name))); } else { files.emplace( - name, fmt::format( - "{}{}", - EscapeURI(name, nameUriBuf), - EscapeHTML(name, nameHtmlBuf), entry.file_size(ec))); + name, + fmt::format( + "{}{}", + EscapeURI(name), EscapeHTML(name), entry.file_size(ec))); } } diff --git a/wpinet/src/main/native/include/wpinet/HttpUtil.h b/wpinet/src/main/native/include/wpinet/HttpUtil.h index dca5225809e..c6c31a83db5 100644 --- a/wpinet/src/main/native/include/wpinet/HttpUtil.h +++ b/wpinet/src/main/native/include/wpinet/HttpUtil.h @@ -14,9 +14,9 @@ #include #include -#include #include #include +#include #include #include "wpinet/NetworkStream.h" @@ -26,23 +26,20 @@ namespace wpi { // Unescape a %xx-encoded URI. -// @param buf Buffer for output -// @param error Set to true if an error occurred -// @return Escaped string -std::string_view UnescapeURI(std::string_view str, SmallVectorImpl& buf, - bool* error); +// @param str input string +// @return Escaped string, or index of error location +wpi::expected UnescapeURI(std::string_view str); // Escape a string with %xx-encoding. -// @param buf Buffer for output +// @param str input string // @param spacePlus If true, encodes spaces to '+' rather than "%20" // @return Escaped string -std::string_view EscapeURI(std::string_view str, SmallVectorImpl& buf, - bool spacePlus = true); +std::string EscapeURI(std::string_view str, bool spacePlus = true); // Escape a string for HTML output. -// @param buf Buffer for output +// @param str input string // @return Escaped string -std::string_view EscapeHTML(std::string_view str, SmallVectorImpl& buf); +std::string EscapeHTML(std::string_view str); // Parse a set of HTTP headers. Saves just the Content-Type and Content-Length // fields. @@ -50,8 +47,8 @@ std::string_view EscapeHTML(std::string_view str, SmallVectorImpl& buf); // @param contentType If not null, Content-Type contents are saved here. // @param contentLength If not null, Content-Length contents are saved here. // @return False if error occurred in input stream -bool ParseHttpHeaders(raw_istream& is, SmallVectorImpl* contentType, - SmallVectorImpl* contentLength); +bool ParseHttpHeaders(raw_istream& is, std::string* contentType, + std::string* contentLength); // Look for a MIME multi-part boundary. On return, the input stream will // be located at the character following the boundary (usually "\r\n"). @@ -89,12 +86,10 @@ class HttpQueryMap { * value are unescaped strings. * * @param name name (unescaped) - * @param buf result buffer for value * @return Optional unescaped value. Returns an empty optional if the * name is not present in the query map. */ - std::optional Get(std::string_view name, - SmallVectorImpl& buf) const; + std::optional Get(std::string_view name) const; private: StringMap m_elems; @@ -225,7 +220,7 @@ class HttpPath { HttpPathRef drop_front(size_t n) const; private: - SmallString<128> m_pathBuf; + std::string m_pathBuf; SmallVector m_pathEnds; }; @@ -346,10 +341,10 @@ class HttpRequest { SetAuth(loc); } - SmallString<128> host; + std::string host; int port; std::string auth; - SmallString<128> path; + std::string path; private: void SetAuth(const HttpLocation& loc); @@ -357,7 +352,7 @@ class HttpRequest { template void SetPath(std::string_view path_, const T& params) { // Build location including query string - raw_svector_ostream pathOs{path}; + raw_string_ostream pathOs{path}; pathOs << path_; bool first = true; for (const auto& param : params) { @@ -367,10 +362,9 @@ class HttpRequest { } else { pathOs << '&'; } - SmallString<64> escapeBuf; - pathOs << EscapeURI(GetFirst(param), escapeBuf, false); + pathOs << EscapeURI(GetFirst(param), false); if (!GetSecond(param).empty()) { - pathOs << '=' << EscapeURI(GetSecond(param), escapeBuf, false); + pathOs << '=' << EscapeURI(GetSecond(param), false); } } } @@ -401,8 +395,8 @@ class HttpConnection { wpi::raw_socket_ostream os; // Valid after Handshake() is successful - SmallString<64> contentType; - SmallString<64> contentLength; + std::string contentType; + std::string contentLength; explicit operator bool() const { return stream && !is.has_error(); } }; @@ -437,7 +431,7 @@ class HttpMultipartScanner { } private: - SmallString<64> m_boundaryWith, m_boundaryWithout; + std::string m_boundaryWith, m_boundaryWithout; // Internal state enum State { kBoundary, kPadding, kDone }; From 566c9539871bef7519a2fcf7cbe29174a14f56f8 Mon Sep 17 00:00:00 2001 From: Peter Johnson Date: Mon, 30 Dec 2024 19:47:43 -0800 Subject: [PATCH 15/54] raw_istream::getline(): Change from SmallVector to std::string --- .../src/main/native/cpp/MjpegServerImpl.cpp | 7 ++--- cscore/src/main/native/linux/UsbUtil.cpp | 4 +-- wpinet/src/main/native/cpp/HttpUtil.cpp | 7 ++--- wpiutil/src/main/native/cpp/raw_istream.cpp | 10 +++---- .../src/main/native/include/wpi/raw_istream.h | 26 +++---------------- 5 files changed, 14 insertions(+), 40 deletions(-) diff --git a/cscore/src/main/native/cpp/MjpegServerImpl.cpp b/cscore/src/main/native/cpp/MjpegServerImpl.cpp index 4f917c8e323..12608ae917e 100644 --- a/cscore/src/main/native/cpp/MjpegServerImpl.cpp +++ b/cscore/src/main/native/cpp/MjpegServerImpl.cpp @@ -9,7 +9,6 @@ #include #include -#include #include #include #include @@ -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; @@ -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()) { diff --git a/cscore/src/main/native/linux/UsbUtil.cpp b/cscore/src/main/native/linux/UsbUtil.cpp index 3a7fbeef877..0857fbf2ea9 100644 --- a/cscore/src/main/native/linux/UsbUtil.cpp +++ b/cscore/src/main/native/linux/UsbUtil.cpp @@ -11,7 +11,6 @@ #include #include -#include #include #include #include @@ -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; } diff --git a/wpinet/src/main/native/cpp/HttpUtil.cpp b/wpinet/src/main/native/cpp/HttpUtil.cpp index 58552841420..b065cae1780 100644 --- a/wpinet/src/main/native/cpp/HttpUtil.cpp +++ b/wpinet/src/main/native/cpp/HttpUtil.cpp @@ -10,7 +10,6 @@ #include #include -#include #include #include @@ -162,9 +161,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; } @@ -376,8 +374,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; diff --git a/wpiutil/src/main/native/cpp/raw_istream.cpp b/wpiutil/src/main/native/cpp/raw_istream.cpp index a179b982bab..c474100ebf1 100644 --- a/wpiutil/src/main/native/cpp/raw_istream.cpp +++ b/wpiutil/src/main/native/cpp/raw_istream.cpp @@ -33,23 +33,23 @@ using namespace wpi; -std::string_view raw_istream::getline(SmallVectorImpl& 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() {} diff --git a/wpiutil/src/main/native/include/wpi/raw_istream.h b/wpiutil/src/main/native/include/wpi/raw_istream.h index 2f278c86a03..b013e8d3171 100644 --- a/wpiutil/src/main/native/include/wpi/raw_istream.h +++ b/wpiutil/src/main/native/include/wpi/raw_istream.h @@ -9,14 +9,13 @@ #include #include +#include #include #include #include #include #include -#include "wpi/SmallVector.h" - namespace wpi { class raw_istream { @@ -53,22 +52,6 @@ class raw_istream { return m_read_count; } - raw_istream& readinto(SmallVectorImpl& 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& 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& buf, size_t len) { size_t old_size = buf.size(); buf.insert(buf.end(), len, 0); @@ -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& buf, int maxLen); + std::string getline(int maxLen); virtual void close() = 0; From 2cc59f625b9606b96d07a48c1550c1eb7eda0f74 Mon Sep 17 00:00:00 2001 From: Peter Johnson Date: Tue, 31 Dec 2024 00:59:12 -0800 Subject: [PATCH 16/54] [glass] Reduce SmallString usage Also update UnescapeCString to use std::string --- glass/src/lib/native/cpp/other/Field2D.cpp | 3 +- glass/src/lib/native/cpp/other/Plot.cpp | 2 -- .../native/include/glass/ContextInternal.h | 3 +- glass/src/libnt/native/cpp/NetworkTables.cpp | 5 +-- .../native/cpp/NetworkTablesProvider.cpp | 4 +-- .../networktables/NetworkTablesSettings.h | 5 --- .../thirdparty/llvm/cpp/llvm/StringExtras.cpp | 36 +++++++++---------- .../llvm/include/wpi/StringExtras.h | 10 +++--- .../test/native/cpp/UnescapeCStringTest.cpp | 28 +++++---------- 9 files changed, 37 insertions(+), 59 deletions(-) diff --git a/glass/src/lib/native/cpp/other/Field2D.cpp b/glass/src/lib/native/cpp/other/Field2D.cpp index 609a75b1823..ed75219f7e7 100644 --- a/glass/src/lib/native/cpp/other/Field2D.cpp +++ b/glass/src/lib/native/cpp/other/Field2D.cpp @@ -26,7 +26,6 @@ #include #include #include -#include #include #include #include @@ -964,7 +963,7 @@ void glass::DisplayField2DSettings(Field2DModel* model) { } PushID(name); - wpi::SmallString<64> nameBuf{name}; + std::string nameBuf{name}; if (ImGui::CollapsingHeader(nameBuf.c_str())) { auto& obj = field->m_objects.try_emplace(name, GetStorage()).first->second; diff --git a/glass/src/lib/native/cpp/other/Plot.cpp b/glass/src/lib/native/cpp/other/Plot.cpp index d17bd82f8d5..11b2078d261 100644 --- a/glass/src/lib/native/cpp/other/Plot.cpp +++ b/glass/src/lib/native/cpp/other/Plot.cpp @@ -28,8 +28,6 @@ #include #include #include -#include -#include #include #include "glass/Context.h" diff --git a/glass/src/lib/native/include/glass/ContextInternal.h b/glass/src/lib/native/include/glass/ContextInternal.h index 965d8ddf248..a09fb9a0f6d 100644 --- a/glass/src/lib/native/include/glass/ContextInternal.h +++ b/glass/src/lib/native/include/glass/ContextInternal.h @@ -10,7 +10,6 @@ #include #include -#include #include #include "glass/Context.h" @@ -32,7 +31,7 @@ class Context { std::string storageLoadDir = "."; std::string storageAutoSaveDir = "."; std::string storageName = "imgui"; - wpi::SmallVector storageStack; + std::vector storageStack; wpi::StringMap storageRoots; wpi::StringMap deviceHidden; wpi::StringMap sources; diff --git a/glass/src/libnt/native/cpp/NetworkTables.cpp b/glass/src/libnt/native/cpp/NetworkTables.cpp index e10ff93c09a..813c635781e 100644 --- a/glass/src/libnt/native/cpp/NetworkTables.cpp +++ b/glass/src/libnt/native/cpp/NetworkTables.cpp @@ -26,7 +26,6 @@ #include #include #include -#include #include #include #include @@ -1499,9 +1498,7 @@ static void EmitEntryValueEditable(NetworkTablesModel* model, entry.publisher = nt::Publish(entry.info.topic, NT_STRING, "string"); } - wpi::SmallString<128> buf; - nt::SetString(entry.publisher, - wpi::UnescapeCString(v + 1, buf).first); + nt::SetString(entry.publisher, wpi::UnescapeCString(v + 1).first); } } break; diff --git a/glass/src/libnt/native/cpp/NetworkTablesProvider.cpp b/glass/src/libnt/native/cpp/NetworkTablesProvider.cpp index 2386ee0e533..b7a3631a128 100644 --- a/glass/src/libnt/native/cpp/NetworkTablesProvider.cpp +++ b/glass/src/libnt/native/cpp/NetworkTablesProvider.cpp @@ -10,7 +10,7 @@ #include #include -#include +#include #include #include @@ -65,7 +65,7 @@ NetworkTablesProvider::NetworkTablesProvider(Storage& storage, void NetworkTablesProvider::DisplayMenu() { wpi::SmallVector path; - wpi::SmallString<64> name; + std::string name; for (auto&& entry : m_viewEntries) { path.clear(); wpi::split(entry->name, '/', -1, false, diff --git a/glass/src/libnt/native/include/glass/networktables/NetworkTablesSettings.h b/glass/src/libnt/native/include/glass/networktables/NetworkTablesSettings.h index 897eb9e2494..4bd5805d009 100644 --- a/glass/src/libnt/native/include/glass/networktables/NetworkTablesSettings.h +++ b/glass/src/libnt/native/include/glass/networktables/NetworkTablesSettings.h @@ -11,11 +11,6 @@ #include "glass/support/EnumSetting.h" -namespace wpi { -template -class SmallVectorImpl; -} // namespace wpi - namespace glass { class Storage; diff --git a/wpiutil/src/main/native/thirdparty/llvm/cpp/llvm/StringExtras.cpp b/wpiutil/src/main/native/thirdparty/llvm/cpp/llvm/StringExtras.cpp index 1503584754e..ce79659bfd9 100644 --- a/wpiutil/src/main/native/thirdparty/llvm/cpp/llvm/StringExtras.cpp +++ b/wpiutil/src/main/native/thirdparty/llvm/cpp/llvm/StringExtras.cpp @@ -299,43 +299,43 @@ std::optional wpi::parse_float( return val; } -std::pair wpi::UnescapeCString( - std::string_view str, wpi::SmallVectorImpl& buf) { - buf.clear(); - buf.reserve(str.size()); +std::pair wpi::UnescapeCString( + std::string_view str) { + std::string out; + out.reserve(str.size()); const char* s = str.data(); const char* end = str.data() + str.size(); for (; s != end && *s != '"'; ++s) { if (*s != '\\' || (s + 1) >= end) { - buf.push_back(*s); + out.push_back(*s); continue; } switch (*++s) { case 'a': - buf.push_back('\a'); + out.push_back('\a'); break; case 'b': - buf.push_back('\b'); + out.push_back('\b'); break; case 'f': - buf.push_back('\f'); + out.push_back('\f'); break; case 'n': - buf.push_back('\n'); + out.push_back('\n'); break; case 'r': - buf.push_back('\r'); + out.push_back('\r'); break; case 't': - buf.push_back('\t'); + out.push_back('\t'); break; case 'v': - buf.push_back('\v'); + out.push_back('\v'); break; case 'x': { // hex escape if ((s + 1) >= end || !isxdigit(*(s + 1))) { - buf.push_back('x'); // treat it like a unknown escape + out.push_back('x'); // treat it like a unknown escape break; } unsigned int ch = wpi::hexDigitValue(*++s); @@ -343,7 +343,7 @@ std::pair wpi::UnescapeCString( ch <<= 4; ch |= wpi::hexDigitValue(*++s); } - buf.push_back(static_cast(ch)); + out.push_back(static_cast(ch)); break; } case '0': @@ -366,17 +366,17 @@ std::pair wpi::UnescapeCString( ch <<= 3; ch |= *++s - '0'; } - buf.push_back(static_cast(ch)); + out.push_back(static_cast(ch)); break; } default: - buf.push_back(*s); + out.push_back(*s); break; } } if (s == end) { - return {{buf.data(), buf.size()}, {}}; + return {out, {}}; } else { - return {{buf.data(), buf.size()}, {s, static_cast(end - s)}}; + return {out, {s, static_cast(end - s)}}; } } diff --git a/wpiutil/src/main/native/thirdparty/llvm/include/wpi/StringExtras.h b/wpiutil/src/main/native/thirdparty/llvm/include/wpi/StringExtras.h index 9214f2b741a..b5ca6d0b542 100644 --- a/wpiutil/src/main/native/thirdparty/llvm/include/wpi/StringExtras.h +++ b/wpiutil/src/main/native/thirdparty/llvm/include/wpi/StringExtras.h @@ -363,7 +363,8 @@ inline bool contains_lower(std::string_view str, const char* other) noexcept { * starts with the prefix. If the string does not start with the prefix, return * an empty optional. */ -constexpr std::optional remove_prefix(std::string_view str, std::string_view prefix) noexcept { +constexpr std::optional remove_prefix( + std::string_view str, std::string_view prefix) noexcept { if (str.starts_with(prefix)) { str.remove_prefix(prefix.size()); return str; @@ -376,7 +377,8 @@ constexpr std::optional remove_prefix(std::string_view str, st * string ends with the suffix. If the string does not end with the suffix, * return an empty optional. */ -constexpr std::optional remove_suffix(std::string_view str, std::string_view suffix) noexcept { +constexpr std::optional remove_suffix( + std::string_view str, std::string_view suffix) noexcept { if (str.ends_with(suffix)) { str.remove_suffix(suffix.size()); return str; @@ -798,11 +800,9 @@ std::optional parse_float( * quote character is found. * * @param str input string - * @param buf buffer for unescaped characters * @return pair of the unescaped string and any remaining input */ -std::pair UnescapeCString( - std::string_view str, SmallVectorImpl& buf); +std::pair UnescapeCString(std::string_view str); /** * Like std::format_to_n() in that it writes at most n bytes to the output diff --git a/wpiutil/src/test/native/cpp/UnescapeCStringTest.cpp b/wpiutil/src/test/native/cpp/UnescapeCStringTest.cpp index 8b35b9746ba..c539b624a69 100644 --- a/wpiutil/src/test/native/cpp/UnescapeCStringTest.cpp +++ b/wpiutil/src/test/native/cpp/UnescapeCStringTest.cpp @@ -4,7 +4,6 @@ #include -#include "wpi/SmallString.h" #include "wpi/StringExtras.h" using namespace wpi; @@ -12,63 +11,54 @@ using namespace wpi; namespace { TEST(UnescapeCStringTest, Basic) { - SmallString<64> buf; - auto [out, rem] = UnescapeCString("abc\\\\\\a\\b\\f\\n\\r\\t\\v\\", buf); + auto [out, rem] = UnescapeCString("abc\\\\\\a\\b\\f\\n\\r\\t\\v\\"); EXPECT_EQ(out, "abc\\\a\b\f\n\r\t\v\\"); EXPECT_TRUE(rem.empty()); } TEST(UnescapeCStringTest, QuoteEnd) { - SmallString<64> buf; - auto [out, rem] = UnescapeCString("abc\\\"\"123", buf); + auto [out, rem] = UnescapeCString("abc\\\"\"123"); EXPECT_EQ(out, "abc\""); EXPECT_EQ(rem, "\"123"); } TEST(UnescapeCStringTest, Hex) { - SmallString<64> buf; - auto [out, rem] = UnescapeCString("\\xfe\\xFE\\x01", buf); + auto [out, rem] = UnescapeCString("\\xfe\\xFE\\x01"); EXPECT_EQ(out, "\xfe\xFE\x01"); EXPECT_TRUE(rem.empty()); } TEST(UnescapeCStringTest, HexPartial) { - SmallString<64> buf; - auto [out, rem] = UnescapeCString("\\xz\\x5z\\x2", buf); + auto [out, rem] = UnescapeCString("\\xz\\x5z\\x2"); EXPECT_EQ(out, "xz\x05z\x02"); EXPECT_TRUE(rem.empty()); } TEST(UnescapeCStringTest, HexPartial2) { - SmallString<64> buf; - auto [out, rem] = UnescapeCString("\\x", buf); + auto [out, rem] = UnescapeCString("\\x"); EXPECT_EQ(out, "x"); EXPECT_TRUE(rem.empty()); } TEST(UnescapeCStringTest, Octal) { - SmallString<64> buf; - auto [out, rem] = UnescapeCString("\\3\\11\\222\\4", buf); + auto [out, rem] = UnescapeCString("\\3\\11\\222\\4"); EXPECT_EQ(out, "\3\11\222\4"); EXPECT_TRUE(rem.empty()); } TEST(UnescapeCStringTest, EmptyString) { - SmallString<64> buf; - auto [out, rem] = UnescapeCString("", buf); + auto [out, rem] = UnescapeCString(""); EXPECT_EQ(out, ""); } TEST(UnescapeCStringTest, ShortString) { - SmallString<64> buf; - auto [out, rem] = UnescapeCString("a", buf); + auto [out, rem] = UnescapeCString("a"); EXPECT_EQ(out, "a"); } TEST(UnescapeCStringTest, NoEscapesString) { - SmallString<64> buf; std::string_view input = "abcdefghijklmnopqrstuvwxyz1234567890"; - auto [out, rem] = UnescapeCString(input, buf); + auto [out, rem] = UnescapeCString(input); EXPECT_EQ(out, input); } From 23b905daf8f0ff1165f9e7a694b486912d6c7408 Mon Sep 17 00:00:00 2001 From: Peter Johnson Date: Wed, 1 Jan 2025 14:30:17 -0800 Subject: [PATCH 17/54] Finish StringExtras split replacement --- wpinet/src/main/native/cpp/WebSocketServer.cpp | 15 +++++++++++++++ .../thirdparty/llvm/include/wpi/StringExtras.h | 3 --- 2 files changed, 15 insertions(+), 3 deletions(-) diff --git a/wpinet/src/main/native/cpp/WebSocketServer.cpp b/wpinet/src/main/native/cpp/WebSocketServer.cpp index 45413884fc6..24eb8bc0e7d 100644 --- a/wpinet/src/main/native/cpp/WebSocketServer.cpp +++ b/wpinet/src/main/native/cpp/WebSocketServer.cpp @@ -77,6 +77,21 @@ std::pair WebSocketServerHelper::MatchProtocol( return {false, {}}; } +std::pair WebSocketServerHelper::MatchProtocol( + std::span protocols) { + if (protocols.empty() && m_protocols.empty()) { + return {true, {}}; + } + for (auto protocol : protocols) { + for (auto&& clientProto : m_protocols) { + if (protocol == clientProto) { + return {true, protocol}; + } + } + } + return {false, {}}; +} + WebSocketServer::WebSocketServer(uv::Stream& stream, std::span protocols, ServerOptions options, const private_init&) diff --git a/wpiutil/src/main/native/thirdparty/llvm/include/wpi/StringExtras.h b/wpiutil/src/main/native/thirdparty/llvm/include/wpi/StringExtras.h index b5ca6d0b542..f6d90fdebab 100644 --- a/wpiutil/src/main/native/thirdparty/llvm/include/wpi/StringExtras.h +++ b/wpiutil/src/main/native/thirdparty/llvm/include/wpi/StringExtras.h @@ -30,9 +30,6 @@ namespace wpi { -template -class SmallVectorImpl; - /// hexdigit - Return the hexadecimal character for the /// given number \p X (which should be less than 16). constexpr char hexdigit(unsigned X, bool LowerCase = false) noexcept { From 6852ea45bfc9522ed6cb5d97b71f734f78e0b1b3 Mon Sep 17 00:00:00 2001 From: Peter Johnson Date: Wed, 1 Jan 2025 14:33:09 -0800 Subject: [PATCH 18/54] Fix up includes --- cscore/src/main/native/cpp/ConfigurableSourceImpl.cpp | 1 + cscore/src/main/native/cpp/Instance.h | 1 + cscore/src/main/native/cpp/jni/CameraServerJNI.cpp | 1 + glass/src/libnt/native/cpp/NetworkTablesProvider.cpp | 1 + wpiutil/src/main/native/cpp/raw_istream.cpp | 2 +- 5 files changed, 5 insertions(+), 1 deletion(-) diff --git a/cscore/src/main/native/cpp/ConfigurableSourceImpl.cpp b/cscore/src/main/native/cpp/ConfigurableSourceImpl.cpp index 7c615a9685c..10cbec80f02 100644 --- a/cscore/src/main/native/cpp/ConfigurableSourceImpl.cpp +++ b/cscore/src/main/native/cpp/ConfigurableSourceImpl.cpp @@ -6,6 +6,7 @@ #include #include +#include #include diff --git a/cscore/src/main/native/cpp/Instance.h b/cscore/src/main/native/cpp/Instance.h index 13a44937de3..a2337915d61 100644 --- a/cscore/src/main/native/cpp/Instance.h +++ b/cscore/src/main/native/cpp/Instance.h @@ -7,6 +7,7 @@ #include #include +#include #include #include diff --git a/cscore/src/main/native/cpp/jni/CameraServerJNI.cpp b/cscore/src/main/native/cpp/jni/CameraServerJNI.cpp index 61ed444bdb6..3a2351e6c13 100644 --- a/cscore/src/main/native/cpp/jni/CameraServerJNI.cpp +++ b/cscore/src/main/native/cpp/jni/CameraServerJNI.cpp @@ -5,6 +5,7 @@ #include #include #include +#include #include diff --git a/glass/src/libnt/native/cpp/NetworkTablesProvider.cpp b/glass/src/libnt/native/cpp/NetworkTablesProvider.cpp index b7a3631a128..dd9612fb936 100644 --- a/glass/src/libnt/native/cpp/NetworkTablesProvider.cpp +++ b/glass/src/libnt/native/cpp/NetworkTablesProvider.cpp @@ -6,6 +6,7 @@ #include #include +#include #include #include diff --git a/wpiutil/src/main/native/cpp/raw_istream.cpp b/wpiutil/src/main/native/cpp/raw_istream.cpp index c474100ebf1..eea0263afc6 100644 --- a/wpiutil/src/main/native/cpp/raw_istream.cpp +++ b/wpiutil/src/main/native/cpp/raw_istream.cpp @@ -14,9 +14,9 @@ #include #include +#include #include -#include "wpi/SmallVector.h" #include "wpi/fs.h" #if defined(_MSC_VER) From 96e6e0240de2d2096945d936f63341ac13b91f5d Mon Sep 17 00:00:00 2001 From: Peter Johnson Date: Wed, 1 Jan 2025 14:33:49 -0800 Subject: [PATCH 19/54] Remove redundant move --- wpinet/src/main/native/cpp/HttpUtil.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/wpinet/src/main/native/cpp/HttpUtil.cpp b/wpinet/src/main/native/cpp/HttpUtil.cpp index b065cae1780..8f2c04604ef 100644 --- a/wpinet/src/main/native/cpp/HttpUtil.cpp +++ b/wpinet/src/main/native/cpp/HttpUtil.cpp @@ -46,7 +46,7 @@ wpi::expected UnescapeURI(std::string_view str) { } out.push_back((val1 << 4) | val2); } - return std::move(out); + return out; } std::string EscapeURI(std::string_view str, bool spacePlus) { From be75784186e2c55bb9fd0553e24b81213291580c Mon Sep 17 00:00:00 2001 From: Peter Johnson Date: Wed, 1 Jan 2025 15:31:30 -0800 Subject: [PATCH 20/54] Don't copy string --- wpinet/src/main/native/cpp/WebSocketServer.cpp | 15 --------------- 1 file changed, 15 deletions(-) diff --git a/wpinet/src/main/native/cpp/WebSocketServer.cpp b/wpinet/src/main/native/cpp/WebSocketServer.cpp index 24eb8bc0e7d..45413884fc6 100644 --- a/wpinet/src/main/native/cpp/WebSocketServer.cpp +++ b/wpinet/src/main/native/cpp/WebSocketServer.cpp @@ -77,21 +77,6 @@ std::pair WebSocketServerHelper::MatchProtocol( return {false, {}}; } -std::pair WebSocketServerHelper::MatchProtocol( - std::span protocols) { - if (protocols.empty() && m_protocols.empty()) { - return {true, {}}; - } - for (auto protocol : protocols) { - for (auto&& clientProto : m_protocols) { - if (protocol == clientProto) { - return {true, protocol}; - } - } - } - return {false, {}}; -} - WebSocketServer::WebSocketServer(uv::Stream& stream, std::span protocols, ServerOptions options, const private_init&) From 9516ef785de28d4884c411b89673daa11105aa3e Mon Sep 17 00:00:00 2001 From: Peter Johnson Date: Wed, 1 Jan 2025 15:34:10 -0800 Subject: [PATCH 21/54] WebSocketServer: Use string/vector --- wpinet/src/main/native/cpp/WebSocketServer.cpp | 1 + .../src/main/native/include/wpinet/WebSocketServer.h | 11 +++++------ 2 files changed, 6 insertions(+), 6 deletions(-) diff --git a/wpinet/src/main/native/cpp/WebSocketServer.cpp b/wpinet/src/main/native/cpp/WebSocketServer.cpp index 45413884fc6..a97823b5bbb 100644 --- a/wpinet/src/main/native/cpp/WebSocketServer.cpp +++ b/wpinet/src/main/native/cpp/WebSocketServer.cpp @@ -8,6 +8,7 @@ #include #include +#include #include #include #include diff --git a/wpinet/src/main/native/include/wpinet/WebSocketServer.h b/wpinet/src/main/native/include/wpinet/WebSocketServer.h index 469a3ca3d04..9531a457037 100644 --- a/wpinet/src/main/native/include/wpinet/WebSocketServer.h +++ b/wpinet/src/main/native/include/wpinet/WebSocketServer.h @@ -12,10 +12,9 @@ #include #include #include +#include #include -#include -#include #include "wpinet/HttpParser.h" #include "wpinet/WebSocket.h" @@ -103,9 +102,9 @@ class WebSocketServerHelper { private: bool m_gotHost = false; bool m_websocket = false; - SmallVector m_protocols; - SmallString<64> m_key; - SmallString<16> m_version; + std::vector m_protocols; + std::string m_key; + std::string m_version; }; /** @@ -176,7 +175,7 @@ class WebSocketServer : public std::enable_shared_from_this { uv::Stream& m_stream; HttpParser m_req{HttpParser::kRequest}; WebSocketServerHelper m_helper; - SmallVector m_protocols; + std::vector m_protocols; ServerOptions m_options; bool m_aborted = false; sig::ScopedConnection m_dataConn; From c2bbb96f7caa9134ae31316b3074f5e1fa37404b Mon Sep 17 00:00:00 2001 From: Peter Johnson Date: Wed, 1 Jan 2025 15:35:54 -0800 Subject: [PATCH 22/54] Formatting --- cscore/src/main/native/cpp/MjpegServerImpl.cpp | 2 +- cscore/src/main/native/cpp/cscore_cpp.cpp | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/cscore/src/main/native/cpp/MjpegServerImpl.cpp b/cscore/src/main/native/cpp/MjpegServerImpl.cpp index 12608ae917e..5ee698bfb0c 100644 --- a/cscore/src/main/native/cpp/MjpegServerImpl.cpp +++ b/cscore/src/main/native/cpp/MjpegServerImpl.cpp @@ -765,7 +765,7 @@ void MjpegServerImpl::ConnThread::SendStream(wpi::raw_socket_ostream& os) { lastFrameTime = thisFrameTime; double timestamp = lastFrameTime / 1000000.0; header.clear(); - oss << "\r\n--" BOUNDARY "\r\n" << "Content-Type: image/jpeg\r\n"; + oss << "\r\n--" BOUNDARY "\r\nContent-Type: image/jpeg\r\n"; wpi::print(oss, "Content-Length: {}\r\n", size); wpi::print(oss, "X-Timestamp: {}\r\n", timestamp); oss << "\r\n"; diff --git a/cscore/src/main/native/cpp/cscore_cpp.cpp b/cscore/src/main/native/cpp/cscore_cpp.cpp index 0cc0b22b3a3..99e4b922105 100644 --- a/cscore/src/main/native/cpp/cscore_cpp.cpp +++ b/cscore/src/main/native/cpp/cscore_cpp.cpp @@ -514,8 +514,8 @@ CS_Property GetSinkProperty(CS_Sink sink, std::string_view name, return Handle{sink, property, Handle::kSinkProperty}; } -std::vector EnumerateSinkProperties( - CS_Sink sink, CS_Status* status) { +std::vector EnumerateSinkProperties(CS_Sink sink, + CS_Status* status) { auto data = Instance::GetInstance().GetSink(sink); if (!data) { *status = CS_INVALID_HANDLE; From 9d747f51b5acecf324f62f2b5fbee9bcbb59f0e2 Mon Sep 17 00:00:00 2001 From: Peter Johnson Date: Wed, 1 Jan 2025 15:40:37 -0800 Subject: [PATCH 23/54] Header cleanups --- ntcore/src/main/native/cpp/LoggerImpl.cpp | 1 - .../src/main/native/cpp/networktables/NetworkTableInstance.cpp | 1 - 2 files changed, 2 deletions(-) diff --git a/ntcore/src/main/native/cpp/LoggerImpl.cpp b/ntcore/src/main/native/cpp/LoggerImpl.cpp index 90a655a64ab..ebe969af7ac 100644 --- a/ntcore/src/main/native/cpp/LoggerImpl.cpp +++ b/ntcore/src/main/native/cpp/LoggerImpl.cpp @@ -7,7 +7,6 @@ #include #include -#include #include #include diff --git a/ntcore/src/main/native/cpp/networktables/NetworkTableInstance.cpp b/ntcore/src/main/native/cpp/networktables/NetworkTableInstance.cpp index 19c1ebfb3e0..2b51411c8fe 100644 --- a/ntcore/src/main/native/cpp/networktables/NetworkTableInstance.cpp +++ b/ntcore/src/main/native/cpp/networktables/NetworkTableInstance.cpp @@ -9,7 +9,6 @@ #include #include -#include #include #include "networktables/BooleanArrayTopic.h" From 02bdd2a0dc5acbb2f3c826f809099174834b1130 Mon Sep 17 00:00:00 2001 From: Peter Johnson Date: Wed, 1 Jan 2025 16:16:06 -0800 Subject: [PATCH 24/54] Change ConnectionList to vector --- ntcore/src/main/native/cpp/ConnectionList.cpp | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/ntcore/src/main/native/cpp/ConnectionList.cpp b/ntcore/src/main/native/cpp/ConnectionList.cpp index f4ae7475a14..b76af4e1366 100644 --- a/ntcore/src/main/native/cpp/ConnectionList.cpp +++ b/ntcore/src/main/native/cpp/ConnectionList.cpp @@ -7,7 +7,6 @@ #include #include -#include #include #include @@ -100,7 +99,7 @@ void ConnectionList::AddListener(NT_Listener listener, unsigned int eventMask) { if ((eventMask & (NT_EVENT_CONNECTED | NT_EVENT_IMMEDIATE)) == (NT_EVENT_CONNECTED | NT_EVENT_IMMEDIATE) && !m_connections.empty()) { - wpi::SmallVector infos; + std::vector infos; infos.reserve(m_connections.size()); for (auto&& conn : m_connections) { infos.emplace_back(&(*conn)); From 339f3e91eef37a9f3662c1f8baf43a54dc2c3aad Mon Sep 17 00:00:00 2001 From: Peter Johnson Date: Wed, 1 Jan 2025 16:42:59 -0800 Subject: [PATCH 25/54] Remove headers --- ntcore/src/main/native/cpp/NetworkClient.cpp | 1 - ntcore/src/test/native/cpp/net/WireDecoderTest.cpp | 4 +--- 2 files changed, 1 insertion(+), 4 deletions(-) diff --git a/ntcore/src/main/native/cpp/NetworkClient.cpp b/ntcore/src/main/native/cpp/NetworkClient.cpp index e77de7137fd..e352e087a2a 100644 --- a/ntcore/src/main/native/cpp/NetworkClient.cpp +++ b/ntcore/src/main/native/cpp/NetworkClient.cpp @@ -13,7 +13,6 @@ #include #include -#include #include #include #include diff --git a/ntcore/src/test/native/cpp/net/WireDecoderTest.cpp b/ntcore/src/test/native/cpp/net/WireDecoderTest.cpp index 6f8b6ca3400..222d422421a 100644 --- a/ntcore/src/test/native/cpp/net/WireDecoderTest.cpp +++ b/ntcore/src/test/native/cpp/net/WireDecoderTest.cpp @@ -2,11 +2,9 @@ // Open Source Software; you can modify and/or share it under the terms of // the WPILib BSD license file in the root directory of this project. -#include +#include #include -#include -#include #include "../MockLogger.h" #include "../PubSubOptionsMatcher.h" From f52d6d98699507f57c468a56cd92219feeeda0c4 Mon Sep 17 00:00:00 2001 From: Peter Johnson Date: Wed, 1 Jan 2025 16:44:54 -0800 Subject: [PATCH 26/54] [ntcore] Remove SmallVector version of NormalizeKey --- .../native/cpp/networktables/NetworkTable.cpp | 20 +++++++------------ .../include/networktables/NetworkTable.h | 4 ---- 2 files changed, 7 insertions(+), 17 deletions(-) diff --git a/ntcore/src/main/native/cpp/networktables/NetworkTable.cpp b/ntcore/src/main/native/cpp/networktables/NetworkTable.cpp index 1defd6200d0..17ae6923c6f 100644 --- a/ntcore/src/main/native/cpp/networktables/NetworkTable.cpp +++ b/ntcore/src/main/native/cpp/networktables/NetworkTable.cpp @@ -42,27 +42,21 @@ std::string_view NetworkTable::BasenameKey(std::string_view key) { std::string NetworkTable::NormalizeKey(std::string_view key, bool withLeadingSlash) { - wpi::SmallString<128> buf; - return std::string{NormalizeKey(key, buf, withLeadingSlash)}; -} - -std::string_view NetworkTable::NormalizeKey(std::string_view key, - wpi::SmallVectorImpl& buf, - bool withLeadingSlash) { - buf.clear(); + std::string out; + out.reserve(key.size() + 1); if (withLeadingSlash) { - buf.push_back(PATH_SEPARATOR_CHAR); + out.push_back(PATH_SEPARATOR_CHAR); } // for each path element, add it with a slash following wpi::split(key, PATH_SEPARATOR_CHAR, -1, false, [&](auto part) { - buf.append(part.begin(), part.end()); - buf.push_back(PATH_SEPARATOR_CHAR); + out.append(part.begin(), part.end()); + out.push_back(PATH_SEPARATOR_CHAR); }); // remove trailing slash if the input key didn't have one if (!key.empty() && key.back() != PATH_SEPARATOR_CHAR) { - buf.pop_back(); + out.pop_back(); } - return {buf.data(), buf.size()}; + return out; } std::vector NetworkTable::GetHierarchy(std::string_view key) { diff --git a/ntcore/src/main/native/include/networktables/NetworkTable.h b/ntcore/src/main/native/include/networktables/NetworkTable.h index b887337267b..14dac8377f5 100644 --- a/ntcore/src/main/native/include/networktables/NetworkTable.h +++ b/ntcore/src/main/native/include/networktables/NetworkTable.h @@ -94,10 +94,6 @@ class NetworkTable final { static std::string NormalizeKey(std::string_view key, bool withLeadingSlash = true); - static std::string_view NormalizeKey(std::string_view key, - wpi::SmallVectorImpl& buf, - bool withLeadingSlash = true); - /** * Gets a list of the names of all the super tables of a given key. For * example, the key "/foo/bar/baz" has a hierarchy of "/", "/foo", From 2455757228bc57ee22c28d63ff9d21c18576d235 Mon Sep 17 00:00:00 2001 From: Peter Johnson Date: Wed, 1 Jan 2025 16:47:31 -0800 Subject: [PATCH 27/54] [ntcore] NetworkTable: Remove SmallString from GetHeirarchy --- ntcore/src/main/native/cpp/networktables/NetworkTable.cpp | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/ntcore/src/main/native/cpp/networktables/NetworkTable.cpp b/ntcore/src/main/native/cpp/networktables/NetworkTable.cpp index 17ae6923c6f..62541b278a3 100644 --- a/ntcore/src/main/native/cpp/networktables/NetworkTable.cpp +++ b/ntcore/src/main/native/cpp/networktables/NetworkTable.cpp @@ -11,7 +11,6 @@ #include #include -#include #include #include @@ -63,18 +62,18 @@ std::vector NetworkTable::GetHierarchy(std::string_view key) { std::vector hierarchy; hierarchy.emplace_back(1, PATH_SEPARATOR_CHAR); // for each path element, add it to the end of what we built previously - wpi::SmallString<128> path; + std::string path; bool any = false; wpi::split(key, PATH_SEPARATOR_CHAR, -1, false, [&](auto part) { any = true; path += PATH_SEPARATOR_CHAR; path += part; - hierarchy.emplace_back(path.str()); + hierarchy.emplace_back(path); // copy, don't move; we need to keep building }); // handle trailing slash if (any && key.back() == PATH_SEPARATOR_CHAR) { path += PATH_SEPARATOR_CHAR; - hierarchy.emplace_back(path.str()); + hierarchy.emplace_back(std::move(path)); } return hierarchy; } From 8258e077982131afd320d2268b32a053f5d1d04a Mon Sep 17 00:00:00 2001 From: Peter Johnson Date: Wed, 1 Jan 2025 16:50:31 -0800 Subject: [PATCH 28/54] [ntcore] LocalStorage: Remove SmallString --- .../src/main/native/cpp/local/LocalStorageImpl.cpp | 12 +++--------- 1 file changed, 3 insertions(+), 9 deletions(-) diff --git a/ntcore/src/main/native/cpp/local/LocalStorageImpl.cpp b/ntcore/src/main/native/cpp/local/LocalStorageImpl.cpp index 60a03c1c77c..3d72c86002f 100644 --- a/ntcore/src/main/native/cpp/local/LocalStorageImpl.cpp +++ b/ntcore/src/main/native/cpp/local/LocalStorageImpl.cpp @@ -12,7 +12,6 @@ #include #include #include -#include #include #include "IListenerStorage.h" @@ -726,22 +725,17 @@ void StorageImpl::StopDataLog(NT_DataLogger logger) { // bool StorageImpl::HasSchema(std::string_view name) { - wpi::SmallString<128> fullName{"/.schema/"}; - fullName += name; - auto it = m_schemas.find(fullName); - return it != m_schemas.end(); + return m_schemas.contains(name); } void StorageImpl::AddSchema(std::string_view name, std::string_view type, std::span schema) { - wpi::SmallString<128> fullName{"/.schema/"}; - fullName += name; - auto& pubHandle = m_schemas[fullName]; + auto& pubHandle = m_schemas[name]; if (pubHandle != 0) { return; } - auto topic = GetOrCreateTopic(fullName); + auto topic = GetOrCreateTopic(fmt::format("/.schema/{}", name)); if (topic->localPublishers.size() >= kMaxPublishers) { ERR("reached maximum number of publishers to '{}', not publishing", From d27343aef4c73352e990a0edb3baf187d9831c68 Mon Sep 17 00:00:00 2001 From: Peter Johnson Date: Wed, 1 Jan 2025 17:06:44 -0800 Subject: [PATCH 29/54] [ntcore] Remove SmallVector and SmallString from most places --- .../src/main/native/cpp/ListenerStorage.cpp | 5 ++--- .../native/cpp/local/LocalStorageImpl.cpp | 4 +++- ntcore/src/main/native/cpp/ntcore_c.cpp | 19 +++++++++---------- .../main/native/cpp/server/ServerClient.cpp | 11 +++++------ .../src/main/native/cpp/server/ServerClient.h | 8 +++----- .../src/main/native/cpp/server/ServerImpl.cpp | 4 +--- .../main/native/cpp/server/ServerStorage.cpp | 3 ++- ntcore/src/main/native/include/ntcore_cpp.h | 5 ----- 8 files changed, 25 insertions(+), 34 deletions(-) diff --git a/ntcore/src/main/native/cpp/ListenerStorage.cpp b/ntcore/src/main/native/cpp/ListenerStorage.cpp index be11343de51..c5c54c76cc2 100644 --- a/ntcore/src/main/native/cpp/ListenerStorage.cpp +++ b/ntcore/src/main/native/cpp/ListenerStorage.cpp @@ -8,8 +8,6 @@ #include #include -#include - #include "ntcore_c.h" using namespace nt; @@ -300,7 +298,8 @@ ListenerStorage::DestroyListenerPoller(NT_ListenerPoller pollerHandle) { std::scoped_lock lock{m_mutex}; if (auto poller = m_pollers.Remove(pollerHandle)) { // ensure all listeners that use this poller are removed - wpi::SmallVector toRemove; + std::vector toRemove; + toRemove.reserve(m_listeners.size()); for (auto&& listener : m_listeners) { if (listener->poller == poller.get()) { toRemove.emplace_back(listener->handle); diff --git a/ntcore/src/main/native/cpp/local/LocalStorageImpl.cpp b/ntcore/src/main/native/cpp/local/LocalStorageImpl.cpp index 3d72c86002f..ffeeed3eeba 100644 --- a/ntcore/src/main/native/cpp/local/LocalStorageImpl.cpp +++ b/ntcore/src/main/native/cpp/local/LocalStorageImpl.cpp @@ -12,6 +12,7 @@ #include #include #include +#include #include #include "IListenerStorage.h" @@ -588,9 +589,10 @@ void StorageImpl::AddListenerImpl(NT_Listener listenerHandle, .get(); // if we're doing anything immediate, get the list of matching topics - wpi::SmallVector topics; + std::vector topics; if ((eventMask & NT_EVENT_IMMEDIATE) != 0 && (eventMask & (NT_EVENT_PUBLISH | NT_EVENT_VALUE_ALL)) != 0) { + topics.reserve(m_topics.size()); for (auto&& topic : m_topics) { if (topic->Exists() && subscriber->Matches(topic->name, topic->special)) { topics.emplace_back(topic.get()); diff --git a/ntcore/src/main/native/cpp/ntcore_c.cpp b/ntcore/src/main/native/cpp/ntcore_c.cpp index 610985abe8e..2bd55a69b0b 100644 --- a/ntcore/src/main/native/cpp/ntcore_c.cpp +++ b/ntcore/src/main/native/cpp/ntcore_c.cpp @@ -14,7 +14,6 @@ #include #include -#include #include #include @@ -226,7 +225,7 @@ NT_Topic* NT_GetTopics(NT_Inst inst, const struct WPI_String* prefix, NT_Topic* NT_GetTopicsStr(NT_Inst inst, const struct WPI_String* prefix, const struct WPI_String* types, size_t types_len, size_t* count) { - wpi::SmallVector typesCpp; + std::vector typesCpp; typesCpp.reserve(types_len); for (size_t i = 0; i < types_len; ++i) { typesCpp.emplace_back(wpi::to_string_view(&types[i])); @@ -246,7 +245,7 @@ struct NT_TopicInfo* NT_GetTopicInfosStr(NT_Inst inst, const struct WPI_String* prefix, const struct WPI_String* types, size_t types_len, size_t* count) { - wpi::SmallVector typesCpp; + std::vector typesCpp; typesCpp.reserve(types_len); for (size_t i = 0; i < types_len; ++i) { typesCpp.emplace_back(wpi::to_string_view(&types[i])); @@ -410,10 +409,10 @@ NT_Topic NT_GetTopicFromHandle(NT_Handle pubsubentry) { NT_MultiSubscriber NT_SubscribeMultiple( NT_Inst inst, const struct WPI_String* prefixes, size_t prefixes_len, const struct NT_PubSubOptions* options) { - wpi::SmallVector p; - p.resize_for_overwrite(prefixes_len); + std::vector p; + p.reserve(prefixes_len); for (size_t i = 0; i < prefixes_len; ++i) { - p[i] = wpi::to_string_view(&prefixes[i]); + p.emplace_back(wpi::to_string_view(&prefixes[i])); } return nt::SubscribeMultiple(inst, p, ConvertToCpp(options)); } @@ -463,10 +462,10 @@ NT_Listener NT_AddListenerMultiple(NT_Inst inst, const struct WPI_String* prefixes, size_t prefixes_len, unsigned int mask, void* data, NT_ListenerCallback callback) { - wpi::SmallVector p; + std::vector p; p.reserve(prefixes_len); for (size_t i = 0; i < prefixes_len; ++i) { - p.emplace_back(prefixes[i].str, prefixes[i].len); + p.emplace_back(wpi::to_string_view(&prefixes[i])); } return nt::AddListener(inst, p, mask, [=](auto& event) { NT_Event event_c; @@ -497,10 +496,10 @@ NT_Listener NT_AddPolledListenerMultiple(NT_ListenerPoller poller, const struct WPI_String* prefixes, size_t prefixes_len, unsigned int mask) { - wpi::SmallVector p; + std::vector p; p.reserve(prefixes_len); for (size_t i = 0; i < prefixes_len; ++i) { - p.emplace_back(prefixes[i].str, prefixes[i].len); + p.emplace_back(wpi::to_string_view(&prefixes[i])); } return nt::AddPolledListener(poller, p, mask); } diff --git a/ntcore/src/main/native/cpp/server/ServerClient.cpp b/ntcore/src/main/native/cpp/server/ServerClient.cpp index d192668fe9d..3e2a3ea33e7 100644 --- a/ntcore/src/main/native/cpp/server/ServerClient.cpp +++ b/ntcore/src/main/native/cpp/server/ServerClient.cpp @@ -45,15 +45,14 @@ void ServerClient::UpdateMetaClientSub() { } } -std::span ServerClient::GetSubscribers( - std::string_view name, bool special, - wpi::SmallVectorImpl& buf) { - buf.resize(0); +std::vector ServerClient::GetSubscribers( + std::string_view name, bool special) { + std::vector out; for (auto&& subPair : m_subscribers) { ServerSubscriber* subscriber = subPair.getSecond().get(); if (subscriber->Matches(name, special)) { - buf.emplace_back(subscriber); + out.emplace_back(subscriber); } } - return {buf.data(), buf.size()}; + return out; } diff --git a/ntcore/src/main/native/cpp/server/ServerClient.h b/ntcore/src/main/native/cpp/server/ServerClient.h index d56753b75b9..bb1594ae59d 100644 --- a/ntcore/src/main/native/cpp/server/ServerClient.h +++ b/ntcore/src/main/native/cpp/server/ServerClient.h @@ -12,6 +12,7 @@ #include #include #include +#include #include @@ -22,8 +23,6 @@ namespace wpi { class Logger; -template -class SmallVectorImpl; } // namespace wpi namespace nt::server { @@ -67,9 +66,8 @@ class ServerClient { void UpdateMetaClientPub(); void UpdateMetaClientSub(); - std::span GetSubscribers( - std::string_view name, bool special, - wpi::SmallVectorImpl& buf); + std::vector GetSubscribers(std::string_view name, + bool special); std::string_view GetName() const { return m_name; } int GetId() const { return m_id; } diff --git a/ntcore/src/main/native/cpp/server/ServerImpl.cpp b/ntcore/src/main/native/cpp/server/ServerImpl.cpp index 1856407dcd2..e0eb0b0701c 100644 --- a/ntcore/src/main/native/cpp/server/ServerImpl.cpp +++ b/ntcore/src/main/native/cpp/server/ServerImpl.cpp @@ -88,9 +88,7 @@ void ServerImpl::SendAnnounce(ServerTopic* topic, ServerClient* client) { } // look for subscriber matching prefixes - wpi::SmallVector subscribersBuf; - auto subscribers = - aClient->GetSubscribers(topic->name, topic->special, subscribersBuf); + auto subscribers = aClient->GetSubscribers(topic->name, topic->special); // don't announce to this client if no subscribers if (subscribers.empty()) { diff --git a/ntcore/src/main/native/cpp/server/ServerStorage.cpp b/ntcore/src/main/native/cpp/server/ServerStorage.cpp index 5ea352f6886..60c9de03095 100644 --- a/ntcore/src/main/native/cpp/server/ServerStorage.cpp +++ b/ntcore/src/main/native/cpp/server/ServerStorage.cpp @@ -146,7 +146,8 @@ void ServerStorage::SetValue(ServerClient* client, ServerTopic* topic, void ServerStorage::RemoveClient(ServerClient* client) { // remove all publishers and subscribers for this client - wpi::SmallVector toDelete; + std::vector toDelete; + toDelete.reserve(m_topics.size()); for (auto&& topic : m_topics) { bool pubChanged = false; bool subChanged = false; diff --git a/ntcore/src/main/native/include/ntcore_cpp.h b/ntcore/src/main/native/include/ntcore_cpp.h index f46ef2e335c..d4e51a91618 100644 --- a/ntcore/src/main/native/include/ntcore_cpp.h +++ b/ntcore/src/main/native/include/ntcore_cpp.h @@ -23,11 +23,6 @@ #include "ntcore_c.h" #include "ntcore_cpp_types.h" -namespace wpi { -template -class SmallVectorImpl; -} // namespace wpi - namespace wpi::log { class DataLog; } // namespace wpi::log From 1e8cdca267b1c70fbdfd301a4c088970fee096cb Mon Sep 17 00:00:00 2001 From: Peter Johnson Date: Wed, 1 Jan 2025 17:15:34 -0800 Subject: [PATCH 30/54] Add missing include --- ntcore/src/main/native/cpp/server/ServerClient.cpp | 1 + 1 file changed, 1 insertion(+) diff --git a/ntcore/src/main/native/cpp/server/ServerClient.cpp b/ntcore/src/main/native/cpp/server/ServerClient.cpp index 3e2a3ea33e7..4498ecdd485 100644 --- a/ntcore/src/main/native/cpp/server/ServerClient.cpp +++ b/ntcore/src/main/native/cpp/server/ServerClient.cpp @@ -5,6 +5,7 @@ #include "ServerClient.h" #include +#include #include From 3364126060473d67e86d34bf897d7ab1beba358a Mon Sep 17 00:00:00 2001 From: Peter Johnson Date: Wed, 1 Jan 2025 21:07:31 -0800 Subject: [PATCH 31/54] Fix windows --- cscore/src/main/native/windows/UsbCameraImpl.cpp | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/cscore/src/main/native/windows/UsbCameraImpl.cpp b/cscore/src/main/native/windows/UsbCameraImpl.cpp index 1f180a142ca..cc56b1e7f1b 100644 --- a/cscore/src/main/native/windows/UsbCameraImpl.cpp +++ b/cscore/src/main/native/windows/UsbCameraImpl.cpp @@ -1162,8 +1162,7 @@ UsbCameraInfo GetUsbCameraInfo(CS_Source source, CS_Status* status) { } info.path = static_cast(*data->source).GetPath(); - wpi::SmallVector buf; - info.name = static_cast(*data->source).GetDescription(buf); + info.name = static_cast(*data->source).GetDescription(); ParseVidAndPid(info.path, &info.productId, &info.vendorId); info.dev = -1; // We have lost dev information by this point in time. return info; From 3d377a31e28110adf7e5a65c5b120afd276bd76f Mon Sep 17 00:00:00 2001 From: Peter Johnson Date: Wed, 1 Jan 2025 21:57:03 -0800 Subject: [PATCH 32/54] [wpilibc] Reduce SmallVector usage --- .../native/cpp/shuffleboard/ShuffleboardInstance.cpp | 4 ++-- .../main/native/cpp/smartdashboard/FieldObject2d.cpp | 10 ++-------- .../include/frc/shuffleboard/RecordingController.h | 1 - .../native/include/frc/smartdashboard/FieldObject2d.h | 11 +---------- 4 files changed, 5 insertions(+), 21 deletions(-) diff --git a/wpilibc/src/main/native/cpp/shuffleboard/ShuffleboardInstance.cpp b/wpilibc/src/main/native/cpp/shuffleboard/ShuffleboardInstance.cpp index 0e216c34d34..c8ae649e4b6 100644 --- a/wpilibc/src/main/native/cpp/shuffleboard/ShuffleboardInstance.cpp +++ b/wpilibc/src/main/native/cpp/shuffleboard/ShuffleboardInstance.cpp @@ -10,7 +10,6 @@ #include #include #include -#include #include #include "frc/shuffleboard/Shuffleboard.h" @@ -53,7 +52,8 @@ frc::ShuffleboardTab& ShuffleboardInstance::GetTab(std::string_view title) { void ShuffleboardInstance::Update() { if (m_impl->tabsChanged) { - wpi::SmallVector tabTitles; + std::vector tabTitles; + tabTitles.reserve(m_impl->tabs.size()); for (auto& entry : m_impl->tabs) { tabTitles.emplace_back(entry.second.GetTitle()); } diff --git a/wpilibc/src/main/native/cpp/smartdashboard/FieldObject2d.cpp b/wpilibc/src/main/native/cpp/smartdashboard/FieldObject2d.cpp index 10a027024e3..cfd1370116e 100644 --- a/wpilibc/src/main/native/cpp/smartdashboard/FieldObject2d.cpp +++ b/wpilibc/src/main/native/cpp/smartdashboard/FieldObject2d.cpp @@ -7,6 +7,8 @@ #include #include +#include + #include "frc/trajectory/Trajectory.h" using namespace frc; @@ -69,14 +71,6 @@ std::vector FieldObject2d::GetPoses() const { return std::vector(m_poses.begin(), m_poses.end()); } -std::span FieldObject2d::GetPoses( - wpi::SmallVectorImpl& out) const { - std::scoped_lock lock(m_mutex); - UpdateFromEntry(); - out.assign(m_poses.begin(), m_poses.end()); - return out; -} - void FieldObject2d::UpdateEntry(bool setDefault) { if (!m_entry) { return; diff --git a/wpilibc/src/main/native/include/frc/shuffleboard/RecordingController.h b/wpilibc/src/main/native/include/frc/shuffleboard/RecordingController.h index 3ef51208767..cf18b72f748 100644 --- a/wpilibc/src/main/native/include/frc/shuffleboard/RecordingController.h +++ b/wpilibc/src/main/native/include/frc/shuffleboard/RecordingController.h @@ -12,7 +12,6 @@ #include #include #include -#include #include "frc/shuffleboard/ShuffleboardEventImportance.h" diff --git a/wpilibc/src/main/native/include/frc/smartdashboard/FieldObject2d.h b/wpilibc/src/main/native/include/frc/smartdashboard/FieldObject2d.h index 776e5216455..f3c0daa2dbf 100644 --- a/wpilibc/src/main/native/include/frc/smartdashboard/FieldObject2d.h +++ b/wpilibc/src/main/native/include/frc/smartdashboard/FieldObject2d.h @@ -12,7 +12,6 @@ #include #include -#include #include #include "frc/geometry/Pose2d.h" @@ -89,14 +88,6 @@ class FieldObject2d { */ std::vector GetPoses() const; - /** - * Get multiple poses. - * - * @param out output SmallVector to hold 2D poses - * @return span referring to output SmallVector - */ - std::span GetPoses(wpi::SmallVectorImpl& out) const; - private: void UpdateEntry(bool setDefault = false); void UpdateFromEntry() const; @@ -104,7 +95,7 @@ class FieldObject2d { mutable wpi::mutex m_mutex; std::string m_name; nt::DoubleArrayEntry m_entry; - mutable wpi::SmallVector m_poses; + mutable std::vector m_poses; }; } // namespace frc From 95e03f1b47e858e01fc26fc793d636ff5bb958cb Mon Sep 17 00:00:00 2001 From: Peter Johnson Date: Wed, 1 Jan 2025 22:02:59 -0800 Subject: [PATCH 33/54] [wpilibc] Reduce SmallString usage --- wpilibc/src/main/native/cpp/Tracer.cpp | 7 ++++--- wpilibc/src/test/native/cpp/ScopedTracerTest.cpp | 6 +++--- 2 files changed, 7 insertions(+), 6 deletions(-) diff --git a/wpilibc/src/main/native/cpp/Tracer.cpp b/wpilibc/src/main/native/cpp/Tracer.cpp index 018abde3eb1..7915780f75a 100644 --- a/wpilibc/src/main/native/cpp/Tracer.cpp +++ b/wpilibc/src/main/native/cpp/Tracer.cpp @@ -4,8 +4,9 @@ #include "frc/Tracer.h" +#include + #include -#include #include #include "frc/Errors.h" @@ -32,8 +33,8 @@ void Tracer::AddEpoch(std::string_view epochName) { } void Tracer::PrintEpochs() { - wpi::SmallString<128> buf; - wpi::raw_svector_ostream os(buf); + std::string buf; + wpi::raw_string_ostream os(buf); PrintEpochs(os); if (!buf.empty()) { FRC_ReportWarning("{}", buf.c_str()); diff --git a/wpilibc/src/test/native/cpp/ScopedTracerTest.cpp b/wpilibc/src/test/native/cpp/ScopedTracerTest.cpp index 4899a4a63d8..8420997a73a 100644 --- a/wpilibc/src/test/native/cpp/ScopedTracerTest.cpp +++ b/wpilibc/src/test/native/cpp/ScopedTracerTest.cpp @@ -2,10 +2,10 @@ // Open Source Software; you can modify and/or share it under the terms of // the WPILib BSD license file in the root directory of this project. +#include #include #include -#include #include #include @@ -13,8 +13,8 @@ #include "frc/simulation/SimHooks.h" TEST(ScopedTracerTest, Timing) { - wpi::SmallString<128> buf; - wpi::raw_svector_ostream os(buf); + std::string buf; + wpi::raw_string_ostream os(buf); frc::sim::PauseTiming(); { From ad7481b14a52334ee54537d7f76a92a9f9d68ede Mon Sep 17 00:00:00 2001 From: Peter Johnson Date: Wed, 1 Jan 2025 22:04:11 -0800 Subject: [PATCH 34/54] Header cleanup --- .../main/native/include/frc/shuffleboard/RecordingController.h | 1 - 1 file changed, 1 deletion(-) diff --git a/wpilibc/src/main/native/include/frc/shuffleboard/RecordingController.h b/wpilibc/src/main/native/include/frc/shuffleboard/RecordingController.h index cf18b72f748..02d30bdbc03 100644 --- a/wpilibc/src/main/native/include/frc/shuffleboard/RecordingController.h +++ b/wpilibc/src/main/native/include/frc/shuffleboard/RecordingController.h @@ -5,7 +5,6 @@ #pragma once #include -#include #include #include From 979a5877df464ad1de97c5e12dd40c4cd2ae3762 Mon Sep 17 00:00:00 2001 From: Peter Johnson Date: Wed, 1 Jan 2025 22:32:00 -0800 Subject: [PATCH 35/54] [wpinet] hostname: Remove SmallVector version --- wpinet/src/main/native/cpp/hostname.cpp | 24 ------------------- .../src/main/native/include/wpinet/hostname.h | 5 ---- wpinet/src/test/native/cpp/hostname.cpp | 10 -------- 3 files changed, 39 deletions(-) diff --git a/wpinet/src/main/native/cpp/hostname.cpp b/wpinet/src/main/native/cpp/hostname.cpp index ae25b29bc8f..bdb194e2393 100644 --- a/wpinet/src/main/native/cpp/hostname.cpp +++ b/wpinet/src/main/native/cpp/hostname.cpp @@ -6,9 +6,6 @@ #include #include -#include - -#include #include "uv.h" @@ -34,25 +31,4 @@ std::string GetHostname() { return rv; } -std::string_view GetHostname(SmallVectorImpl& name) { - // Use a tmp array to not require the SmallVector to be too large. - char tmpName[256]; - size_t size = sizeof(tmpName); - - name.clear(); - - int err = uv_os_gethostname(tmpName, &size); - if (err == 0) { - name.append(tmpName, tmpName + size); - } else if (err == UV_ENOBUFS) { - name.resize(size); - err = uv_os_gethostname(name.data(), &size); - if (err != 0) { - size = 0; - } - } - - return {name.data(), size}; -} - } // namespace wpi diff --git a/wpinet/src/main/native/include/wpinet/hostname.h b/wpinet/src/main/native/include/wpinet/hostname.h index e356fb40ac6..a4cfafdfbde 100644 --- a/wpinet/src/main/native/include/wpinet/hostname.h +++ b/wpinet/src/main/native/include/wpinet/hostname.h @@ -6,14 +6,9 @@ #define WPINET_HOSTNAME_H_ #include -#include namespace wpi { -template -class SmallVectorImpl; - std::string GetHostname(); -std::string_view GetHostname(SmallVectorImpl& name); } // namespace wpi #endif // WPINET_HOSTNAME_H_ diff --git a/wpinet/src/test/native/cpp/hostname.cpp b/wpinet/src/test/native/cpp/hostname.cpp index df7e2f0486c..c368c13ab1b 100644 --- a/wpinet/src/test/native/cpp/hostname.cpp +++ b/wpinet/src/test/native/cpp/hostname.cpp @@ -5,19 +5,9 @@ #include "wpinet/hostname.h" #include -#include -#include namespace wpi { TEST(HostNameTest, HostNameNotEmpty) { ASSERT_NE(GetHostname(), ""); } -TEST(HostNameTest, HostNameNotEmptySmallVector) { - SmallVector name; - ASSERT_NE(GetHostname(name), ""); -} -TEST(HostNameTest, HostNameEq) { - SmallVector nameBuf; - ASSERT_EQ(GetHostname(nameBuf), GetHostname()); -} } // namespace wpi From cba7bad8ba758b83e30e83fc0412bb84e9ad9c35 Mon Sep 17 00:00:00 2001 From: Peter Johnson Date: Wed, 1 Jan 2025 22:36:47 -0800 Subject: [PATCH 36/54] [wpinet] Process: Remove SmallVector --- wpinet/src/main/native/cpp/uv/Process.cpp | 11 +++++------ wpinet/src/main/native/include/wpinet/uv/Process.h | 6 ------ 2 files changed, 5 insertions(+), 12 deletions(-) diff --git a/wpinet/src/main/native/cpp/uv/Process.cpp b/wpinet/src/main/native/cpp/uv/Process.cpp index 7a653078e9d..89a04a09ce2 100644 --- a/wpinet/src/main/native/cpp/uv/Process.cpp +++ b/wpinet/src/main/native/cpp/uv/Process.cpp @@ -5,8 +5,7 @@ #include "wpinet/uv/Process.h" #include - -#include +#include #include "wpinet/uv/Loop.h" #include "wpinet/uv/Pipe.h" @@ -27,22 +26,22 @@ std::shared_ptr Process::SpawnArray(Loop& loop, std::string_view file, h.exited(status, signal); }; - SmallString<128> fileBuf{file}; + std::string fileBuf{file}; coptions.file = fileBuf.c_str(); coptions.cwd = nullptr; coptions.flags = 0; coptions.uid = 0; coptions.gid = 0; - SmallVector argsBuf; - SmallVector envBuf; + std::vector argsBuf; + std::vector envBuf; struct StdioContainer : public uv_stdio_container_t { StdioContainer() { flags = UV_IGNORE; data.fd = 0; } }; - SmallVector stdioBuf; + std::vector stdioBuf; for (auto&& o : options) { switch (o.m_type) { diff --git a/wpinet/src/main/native/include/wpinet/uv/Process.h b/wpinet/src/main/native/include/wpinet/uv/Process.h index 3d84bde0b62..0be2ebb6ff7 100644 --- a/wpinet/src/main/native/include/wpinet/uv/Process.h +++ b/wpinet/src/main/native/include/wpinet/uv/Process.h @@ -14,7 +14,6 @@ #include #include -#include #include "wpinet/uv/Handle.h" @@ -71,11 +70,6 @@ class Process final : public HandleImpl { m_data.str = m_strData.c_str(); } - /*implicit*/ Option(const SmallVectorImpl& arg) // NOLINT - : m_strData(arg.data(), arg.size()) { - m_data.str = m_strData.c_str(); - } - explicit Option(Type type) : m_type(type) {} Type m_type = kArg; From e4373b3d530b2ff507dc8273fb8e079fbfda95a6 Mon Sep 17 00:00:00 2001 From: Peter Johnson Date: Wed, 1 Jan 2025 22:38:14 -0800 Subject: [PATCH 37/54] [wpinet] MulticastServiceAnnouncer: Remove SmallVector --- wpinet/src/main/native/cpp/MulticastServiceAnnouncer.cpp | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/wpinet/src/main/native/cpp/MulticastServiceAnnouncer.cpp b/wpinet/src/main/native/cpp/MulticastServiceAnnouncer.cpp index 3885d93f283..c62df1e8e15 100644 --- a/wpinet/src/main/native/cpp/MulticastServiceAnnouncer.cpp +++ b/wpinet/src/main/native/cpp/MulticastServiceAnnouncer.cpp @@ -6,8 +6,7 @@ #include #include - -#include +#include #include "MulticastHandleManager.h" @@ -20,8 +19,8 @@ WPI_MulticastServiceAnnouncerHandle WPI_CreateMulticastServiceAnnouncer( auto& manager = wpi::GetMulticastManager(); std::scoped_lock lock{manager.mutex}; - wpi::SmallVector, 8> txts; - + std::vector> txts; + txts.reserve(txtCount); for (int32_t i = 0; i < txtCount; i++) { txts.emplace_back( std::pair{keys[i], values[i]}); From e4747ab23cb2d87690a8d66cd6c4d48c2d84bd32 Mon Sep 17 00:00:00 2001 From: Peter Johnson Date: Wed, 1 Jan 2025 22:42:51 -0800 Subject: [PATCH 38/54] [wpinet] JNI: Remove SmallVector use --- wpinet/src/main/native/cpp/jni/WPINetJNI.cpp | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/wpinet/src/main/native/cpp/jni/WPINetJNI.cpp b/wpinet/src/main/native/cpp/jni/WPINetJNI.cpp index 04efdab2044..fbb08477a63 100644 --- a/wpinet/src/main/native/cpp/jni/WPINetJNI.cpp +++ b/wpinet/src/main/native/cpp/jni/WPINetJNI.cpp @@ -122,7 +122,7 @@ Java_edu_wpi_first_net_WPINetJNI_createMulticastServiceAnnouncer JStringRef serviceNameRef{env, serviceName}; JStringRef serviceTypeRef{env, serviceType}; - wpi::SmallVector, 8> txtVec; + std::vector> txtVec; if (keys != nullptr && values != nullptr) { size_t keysLen = env->GetArrayLength(keys); @@ -338,9 +338,11 @@ Java_edu_wpi_first_net_WPINetJNI_getMulticastServiceResolverData JLocal serviceName{env, MakeJString(env, data.serviceName)}; JLocal hostName{env, MakeJString(env, data.hostName)}; - wpi::SmallVector keysRef; - wpi::SmallVector valuesRef; + std::vector keysRef; + std::vector valuesRef; + keysRef.reserve(data.txt.size()); + valuesRef.reserve(data.txt.size()); size_t index = 0; for (auto&& txt : data.txt) { keysRef.emplace_back(txt.first); From 113b09aed7529b33bfa1e86cbbf7b22540e9e865 Mon Sep 17 00:00:00 2001 From: Peter Johnson Date: Wed, 1 Jan 2025 23:31:55 -0800 Subject: [PATCH 39/54] More removals --- wpinet/src/main/native/cpp/HttpUtil.cpp | 2 +- wpinet/src/main/native/cpp/WebSocket.cpp | 31 ++++++++----------- .../wpinet/HttpWebSocketServerConnection.h | 9 ++---- .../native/macOS/MulticastServiceResolver.cpp | 3 +- .../src/netconsoleServer/native/cpp/main.cpp | 3 +- wpiutil/src/main/native/cpp/Base64.cpp | 24 +++++++------- wpiutil/src/main/native/cpp/sha1.cpp | 11 ++++++- wpiutil/src/main/native/include/wpi/Base64.h | 6 ++-- wpiutil/src/main/native/include/wpi/sha1.h | 1 + wpiutil/src/test/native/cpp/Base64Test.cpp | 17 +++------- 10 files changed, 52 insertions(+), 55 deletions(-) diff --git a/wpinet/src/main/native/cpp/HttpUtil.cpp b/wpinet/src/main/native/cpp/HttpUtil.cpp index 8f2c04604ef..c43347daaa0 100644 --- a/wpinet/src/main/native/cpp/HttpUtil.cpp +++ b/wpinet/src/main/native/cpp/HttpUtil.cpp @@ -358,7 +358,7 @@ HttpLocation::HttpLocation(std::string_view url_, bool* error, void HttpRequest::SetAuth(const HttpLocation& loc) { if (!loc.user.empty()) { - Base64Encode(fmt::format("{}:{}", loc.user, loc.password), &auth); + auth = Base64Encode(fmt::format("{}:{}", loc.user, loc.password)); } } diff --git a/wpinet/src/main/native/cpp/WebSocket.cpp b/wpinet/src/main/native/cpp/WebSocket.cpp index 142369c1da9..55840fb828e 100644 --- a/wpinet/src/main/native/cpp/WebSocket.cpp +++ b/wpinet/src/main/native/cpp/WebSocket.cpp @@ -11,9 +11,9 @@ #include #include #include +#include #include -#include #include #include #include @@ -123,8 +123,7 @@ class WebSocket::ClientHandshakeData { for (char& v : nonce) { v = static_cast(dist(gen)); } - raw_svector_ostream os(key); - Base64Encode(os, {nonce, 16}); + key = Base64Encode({nonce, 16}); } ~ClientHandshakeData() { if (auto t = timer.lock()) { @@ -133,8 +132,8 @@ class WebSocket::ClientHandshakeData { } } - SmallString<64> key; // the key sent to the server - SmallVector protocols; // valid protocols + std::string key; // the key sent to the server + std::vector protocols; // valid protocols HttpParser parser{HttpParser::kResponse}; // server response parser bool hasUpgrade = false; bool hasConnection = false; @@ -144,13 +143,11 @@ class WebSocket::ClientHandshakeData { std::weak_ptr timer; }; -static std::string_view AcceptHash(std::string_view key, - SmallVectorImpl& buf) { +static std::string AcceptHash(std::string_view key) { SHA1 hash; hash.Update(key); hash.Update("258EAFA5-E914-47DA-95CA-C5AB0DC85B11"); - SmallString<64> hashBuf; - return Base64Encode(hash.RawFinal(hashBuf), buf); + return Base64Encode(hash.RawFinal()); } WebSocket::WebSocket(uv::Stream& stream, bool server, const private_init&) @@ -286,8 +283,7 @@ void WebSocket::StartClient(std::string_view uri, std::string_view host, m_clientHandshake->hasConnection = true; } else if (equals_lower(name, "sec-websocket-accept")) { // Check against expected response - SmallString<64> acceptBuf; - if (!equals(value, AcceptHash(m_clientHandshake->key, acceptBuf))) { + if (!equals(value, AcceptHash(m_clientHandshake->key))) { return Terminate(1002, "invalid accept key"); } m_clientHandshake->hasAccept = true; @@ -365,8 +361,7 @@ void WebSocket::StartServer(std::string_view key, std::string_view version, os << "Connection: Upgrade\r\n"; // accept hash - SmallString<64> acceptBuf; - os << "Sec-WebSocket-Accept: " << AcceptHash(key, acceptBuf) << "\r\n"; + os << "Sec-WebSocket-Accept: " << AcceptHash(key) << "\r\n"; if (!protocol.empty()) { os << "Sec-WebSocket-Protocol: " << protocol << "\r\n"; @@ -711,7 +706,7 @@ void WebSocket::HandleIncoming(uv::Buffer& buf, size_t size) { static void VerboseDebug(const WebSocket::Frame& frame) { #ifdef WPINET_WEBSOCKET_VERBOSE_DEBUG if ((frame.opcode & 0x7f) == 0x01) { - SmallString<128> str; + std::string str; #ifdef WPINET_WEBSOCKET_VERBOSE_DEBUG_CONTENT for (auto&& d : frame.data) { str.append(std::string_view(d.base, d.len)); @@ -719,9 +714,9 @@ static void VerboseDebug(const WebSocket::Frame& frame) { #endif wpi::print("WS SendText({})\n", str.str()); } else if ((frame.opcode & 0x7f) == 0x02) { - SmallString<128> str; + std::string str; #ifdef WPINET_WEBSOCKET_VERBOSE_DEBUG_CONTENT - raw_svector_ostream stros{str}; + raw_string_ostream stros{str}; for (auto&& d : frame.data) { for (auto ch : d.data()) { stros << fmt::format("{:02x},", static_cast(ch) & 0xff); @@ -730,9 +725,9 @@ static void VerboseDebug(const WebSocket::Frame& frame) { #endif wpi::print("WS SendBinary({})\n", str.str()); } else { - SmallString<128> str; + std::string str; #ifdef WPINET_WEBSOCKET_VERBOSE_DEBUG_CONTENT - raw_svector_ostream stros{str}; + raw_string_ostream stros{str}; for (auto&& d : frame.data) { for (auto ch : d.data()) { stros << fmt::format("{:02x},", static_cast(ch) & 0xff); diff --git a/wpinet/src/main/native/include/wpinet/HttpWebSocketServerConnection.h b/wpinet/src/main/native/include/wpinet/HttpWebSocketServerConnection.h index 0b273ee7301..8b7d2933304 100644 --- a/wpinet/src/main/native/include/wpinet/HttpWebSocketServerConnection.h +++ b/wpinet/src/main/native/include/wpinet/HttpWebSocketServerConnection.h @@ -10,8 +10,7 @@ #include #include #include - -#include +#include #include "wpinet/HttpServerConnection.h" #include "wpinet/WebSocket.h" @@ -44,9 +43,7 @@ class HttpWebSocketServerConnection // Handle upgrade event m_helper.upgrade.connect([this] { // Negotiate sub-protocol - SmallVector 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; // Check that the upgrade is valid if (!IsValidWsUpgrade(protocol)) { @@ -119,7 +116,7 @@ class HttpWebSocketServerConnection private: WebSocketServerHelper m_helper; - SmallVector m_protocols; + std::vector m_protocols; }; } // namespace wpi diff --git a/wpinet/src/main/native/macOS/MulticastServiceResolver.cpp b/wpinet/src/main/native/macOS/MulticastServiceResolver.cpp index dc18dab7399..44e973584c0 100644 --- a/wpinet/src/main/native/macOS/MulticastServiceResolver.cpp +++ b/wpinet/src/main/native/macOS/MulticastServiceResolver.cpp @@ -197,7 +197,8 @@ void MulticastServiceResolver::Stop() { if (!pImpl->serviceRef) { return; } - wpi::SmallVector cleanupEvents; + std::vector cleanupEvents; + cleanupEvents.reserve(pImpl->ResolveStates.size() + 1); for (auto&& i : pImpl->ResolveStates) { cleanupEvents.push_back( pImpl->thread->RemoveServiceRefOutsideThread(i->ResolveRef)); diff --git a/wpinet/src/netconsoleServer/native/cpp/main.cpp b/wpinet/src/netconsoleServer/native/cpp/main.cpp index 8c6a1626855..1faa4c3aadf 100644 --- a/wpinet/src/netconsoleServer/native/cpp/main.cpp +++ b/wpinet/src/netconsoleServer/native/cpp/main.cpp @@ -12,6 +12,7 @@ #include #include #include +#include #include #include @@ -235,7 +236,7 @@ int main(int argc, char* argv[]) { } // build process options - wpi::SmallVector options; + std::vector options; // hook up pipes to child options.emplace_back( diff --git a/wpiutil/src/main/native/cpp/Base64.cpp b/wpiutil/src/main/native/cpp/Base64.cpp index b70298d12d1..d9e64301635 100644 --- a/wpiutil/src/main/native/cpp/Base64.cpp +++ b/wpiutil/src/main/native/cpp/Base64.cpp @@ -119,12 +119,12 @@ size_t Base64Decode(raw_ostream& os, std::string_view encoded) { return (end - bytes_begin) + ((4 - nprbytes) & 3); } -size_t Base64Decode(std::string_view encoded, std::string* plain) { - plain->resize(0); - raw_string_ostream os(*plain); - size_t rv = Base64Decode(os, encoded); +std::string Base64Decode(std::string_view encoded, size_t* num_read) { + std::string plain; + raw_string_ostream os(plain); + *num_read = Base64Decode(os, encoded); os.flush(); - return rv; + return plain; } std::string_view Base64Decode(std::string_view encoded, size_t* num_read, @@ -181,11 +181,12 @@ void Base64Encode(raw_ostream& os, std::string_view plain) { } } -void Base64Encode(std::string_view plain, std::string* encoded) { - encoded->resize(0); - raw_string_ostream os(*encoded); +std::string Base64Encode(std::string_view plain) { + std::string encoded; + raw_string_ostream os(encoded); Base64Encode(os, plain); os.flush(); + return encoded; } std::string_view Base64Encode(std::string_view plain, @@ -201,11 +202,12 @@ void Base64Encode(raw_ostream& os, std::span plain) { plain.size()}); } -void Base64Encode(std::span plain, std::string* encoded) { - encoded->resize(0); - raw_string_ostream os(*encoded); +std::string Base64Encode(std::span plain) { + std::string encoded; + raw_string_ostream os(encoded); Base64Encode(os, plain); os.flush(); + return encoded; } std::string_view Base64Encode(std::span plain, diff --git a/wpiutil/src/main/native/cpp/sha1.cpp b/wpiutil/src/main/native/cpp/sha1.cpp index 82041144922..330a2152ab8 100644 --- a/wpiutil/src/main/native/cpp/sha1.cpp +++ b/wpiutil/src/main/native/cpp/sha1.cpp @@ -292,7 +292,7 @@ std::string SHA1::Final() { finalize(digest, buffer, buf_size, transforms, os, true); - return os.str(); + return out; } std::string_view SHA1::Final(SmallVectorImpl& buf) { @@ -303,6 +303,15 @@ std::string_view SHA1::Final(SmallVectorImpl& buf) { return os.str(); } +std::string SHA1::RawFinal() { + std::string out; + raw_string_ostream os(out); + + finalize(digest, buffer, buf_size, transforms, os, false); + + return out; +} + std::string_view SHA1::RawFinal(SmallVectorImpl& buf) { raw_svector_ostream os(buf); diff --git a/wpiutil/src/main/native/include/wpi/Base64.h b/wpiutil/src/main/native/include/wpi/Base64.h index 858ff73fffd..57687907533 100644 --- a/wpiutil/src/main/native/include/wpi/Base64.h +++ b/wpiutil/src/main/native/include/wpi/Base64.h @@ -20,7 +20,7 @@ class raw_ostream; size_t Base64Decode(raw_ostream& os, std::string_view encoded); -size_t Base64Decode(std::string_view encoded, std::string* plain); +std::string Base64Decode(std::string_view encoded, size_t* num_read); std::string_view Base64Decode(std::string_view encoded, size_t* num_read, SmallVectorImpl& buf); @@ -32,14 +32,14 @@ std::span Base64Decode(std::string_view encoded, size_t* num_read, void Base64Encode(raw_ostream& os, std::string_view plain); -void Base64Encode(std::string_view plain, std::string* encoded); +std::string Base64Encode(std::string_view plain); std::string_view Base64Encode(std::string_view plain, SmallVectorImpl& buf); void Base64Encode(raw_ostream& os, std::span plain); -void Base64Encode(std::span plain, std::string* encoded); +std::string Base64Encode(std::span plain); std::string_view Base64Encode(std::span plain, SmallVectorImpl& buf); diff --git a/wpiutil/src/main/native/include/wpi/sha1.h b/wpiutil/src/main/native/include/wpi/sha1.h index 75464bfdf66..ca3567bbc85 100644 --- a/wpiutil/src/main/native/include/wpi/sha1.h +++ b/wpiutil/src/main/native/include/wpi/sha1.h @@ -37,6 +37,7 @@ class SHA1 { void Update(raw_istream& is); std::string Final(); std::string_view Final(SmallVectorImpl& buf); + std::string RawFinal(); std::string_view RawFinal(SmallVectorImpl& buf); static std::string FromFile(std::string_view filename); diff --git a/wpiutil/src/test/native/cpp/Base64Test.cpp b/wpiutil/src/test/native/cpp/Base64Test.cpp index b7ce945c58e..517d124d0e4 100644 --- a/wpiutil/src/test/native/cpp/Base64Test.cpp +++ b/wpiutil/src/test/native/cpp/Base64Test.cpp @@ -35,13 +35,7 @@ class Base64Test : public ::testing::TestWithParam { }; TEST_P(Base64Test, EncodeStdString) { - std::string s; - Base64Encode(GetPlain(), &s); - ASSERT_EQ(GetParam().encoded, s); - - // text already in s - Base64Encode(GetPlain(), &s); - ASSERT_EQ(GetParam().encoded, s); + ASSERT_EQ(GetParam().encoded, Base64Encode(GetPlain())); } TEST_P(Base64Test, EncodeSmallString) { @@ -52,13 +46,10 @@ TEST_P(Base64Test, EncodeSmallString) { } TEST_P(Base64Test, DecodeStdString) { - std::string s; std::string_view encoded = GetParam().encoded; - EXPECT_EQ(encoded.size(), Base64Decode(encoded, &s)); - ASSERT_EQ(GetPlain(), s); - - // text already in s - Base64Decode(encoded, &s); + size_t len; + std::string s = Base64Decode(encoded, &len); + EXPECT_EQ(encoded.size(), len); ASSERT_EQ(GetPlain(), s); } From 09abc3482dda04b7b4a3b31e3a8c9f4010aefbb2 Mon Sep 17 00:00:00 2001 From: Peter Johnson Date: Wed, 1 Jan 2025 23:33:30 -0800 Subject: [PATCH 40/54] Remove from sha1 --- .../test/native/cpp/WebSocketClientTest.cpp | 8 ++------ wpiutil/src/main/native/cpp/sha1.cpp | 18 ------------------ wpiutil/src/main/native/include/wpi/sha1.h | 4 ---- 3 files changed, 2 insertions(+), 28 deletions(-) diff --git a/wpinet/src/test/native/cpp/WebSocketClientTest.cpp b/wpinet/src/test/native/cpp/WebSocketClientTest.cpp index c5bff34ff24..6473f77d15a 100644 --- a/wpinet/src/test/native/cpp/WebSocketClientTest.cpp +++ b/wpinet/src/test/native/cpp/WebSocketClientTest.cpp @@ -10,7 +10,6 @@ #include #include -#include #include #include @@ -45,10 +44,7 @@ class WebSocketClientTest : public WebSocketTest { if (mockBadAccept) { hash.Update("1"); } - SmallString<64> hashBuf; - SmallString<64> acceptBuf; - os << "Sec-WebSocket-Accept: " - << Base64Encode(hash.RawFinal(hashBuf), acceptBuf) << "\r\n"; + os << "Sec-WebSocket-Accept: " << Base64Encode(hash.RawFinal()) << "\r\n"; if (!mockProtocol.empty()) { os << "Sec-WebSocket-Protocol: " << mockProtocol << "\r\n"; @@ -93,7 +89,7 @@ class WebSocketClientTest : public WebSocketTest { std::vector wireData; std::shared_ptr conn; HttpParser req{HttpParser::kRequest}; - SmallString<64> clientKey; + std::string clientKey; std::string mockProtocol; bool serverHeadersDone = false; std::function connected; diff --git a/wpiutil/src/main/native/cpp/sha1.cpp b/wpiutil/src/main/native/cpp/sha1.cpp index 330a2152ab8..8460b8f3d24 100644 --- a/wpiutil/src/main/native/cpp/sha1.cpp +++ b/wpiutil/src/main/native/cpp/sha1.cpp @@ -21,8 +21,6 @@ #include -#include "wpi/SmallVector.h" -#include "wpi/StringExtras.h" #include "wpi/raw_istream.h" #include "wpi/raw_ostream.h" @@ -295,14 +293,6 @@ std::string SHA1::Final() { return out; } -std::string_view SHA1::Final(SmallVectorImpl& buf) { - raw_svector_ostream os(buf); - - finalize(digest, buffer, buf_size, transforms, os, true); - - return os.str(); -} - std::string SHA1::RawFinal() { std::string out; raw_string_ostream os(out); @@ -312,14 +302,6 @@ std::string SHA1::RawFinal() { return out; } -std::string_view SHA1::RawFinal(SmallVectorImpl& buf) { - raw_svector_ostream os(buf); - - finalize(digest, buffer, buf_size, transforms, os, false); - - return os.str(); -} - std::string SHA1::FromFile(std::string_view filename) { std::error_code ec; raw_fd_istream stream(filename, ec); diff --git a/wpiutil/src/main/native/include/wpi/sha1.h b/wpiutil/src/main/native/include/wpi/sha1.h index ca3567bbc85..3d674883d2f 100644 --- a/wpiutil/src/main/native/include/wpi/sha1.h +++ b/wpiutil/src/main/native/include/wpi/sha1.h @@ -26,8 +26,6 @@ #include namespace wpi { -template -class SmallVectorImpl; class raw_istream; class SHA1 { @@ -36,9 +34,7 @@ class SHA1 { void Update(std::string_view s); void Update(raw_istream& is); std::string Final(); - std::string_view Final(SmallVectorImpl& buf); std::string RawFinal(); - std::string_view RawFinal(SmallVectorImpl& buf); static std::string FromFile(std::string_view filename); private: From 7860426e7cd95fb42496d583f5b471d13c69c4e5 Mon Sep 17 00:00:00 2001 From: Peter Johnson Date: Wed, 1 Jan 2025 23:35:38 -0800 Subject: [PATCH 41/54] Remove from base64 --- .../test/native/cpp/WebSocketClientTest.cpp | 1 + wpiutil/src/main/native/cpp/Base64.cpp | 33 ------------------- wpiutil/src/main/native/include/wpi/Base64.h | 14 -------- wpiutil/src/test/native/cpp/Base64Test.cpp | 21 ------------ 4 files changed, 1 insertion(+), 68 deletions(-) diff --git a/wpinet/src/test/native/cpp/WebSocketClientTest.cpp b/wpinet/src/test/native/cpp/WebSocketClientTest.cpp index 6473f77d15a..e7aaeb94f92 100644 --- a/wpinet/src/test/native/cpp/WebSocketClientTest.cpp +++ b/wpinet/src/test/native/cpp/WebSocketClientTest.cpp @@ -10,6 +10,7 @@ #include #include +#include #include #include diff --git a/wpiutil/src/main/native/cpp/Base64.cpp b/wpiutil/src/main/native/cpp/Base64.cpp index d9e64301635..60dd503b73c 100644 --- a/wpiutil/src/main/native/cpp/Base64.cpp +++ b/wpiutil/src/main/native/cpp/Base64.cpp @@ -60,7 +60,6 @@ #include #include -#include "wpi/SmallVector.h" #include "wpi/raw_ostream.h" namespace wpi { @@ -127,28 +126,12 @@ std::string Base64Decode(std::string_view encoded, size_t* num_read) { return plain; } -std::string_view Base64Decode(std::string_view encoded, size_t* num_read, - SmallVectorImpl& buf) { - buf.clear(); - raw_svector_ostream os(buf); - *num_read = Base64Decode(os, encoded); - return os.str(); -} - size_t Base64Decode(std::string_view encoded, std::vector* plain) { plain->resize(0); raw_uvector_ostream os(*plain); return Base64Decode(os, encoded); } -std::span Base64Decode(std::string_view encoded, size_t* num_read, - SmallVectorImpl& buf) { - buf.clear(); - raw_usvector_ostream os(buf); - *num_read = Base64Decode(os, encoded); - return os.array(); -} - static const char basis_64[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"; @@ -189,14 +172,6 @@ std::string Base64Encode(std::string_view plain) { return encoded; } -std::string_view Base64Encode(std::string_view plain, - SmallVectorImpl& buf) { - buf.clear(); - raw_svector_ostream os(buf); - Base64Encode(os, plain); - return os.str(); -} - void Base64Encode(raw_ostream& os, std::span plain) { Base64Encode(os, std::string_view{reinterpret_cast(plain.data()), plain.size()}); @@ -210,12 +185,4 @@ std::string Base64Encode(std::span plain) { return encoded; } -std::string_view Base64Encode(std::span plain, - SmallVectorImpl& buf) { - buf.clear(); - raw_svector_ostream os(buf); - Base64Encode(os, plain); - return os.str(); -} - } // namespace wpi diff --git a/wpiutil/src/main/native/include/wpi/Base64.h b/wpiutil/src/main/native/include/wpi/Base64.h index 57687907533..d0765ecd079 100644 --- a/wpiutil/src/main/native/include/wpi/Base64.h +++ b/wpiutil/src/main/native/include/wpi/Base64.h @@ -14,36 +14,22 @@ #include namespace wpi { -template -class SmallVectorImpl; class raw_ostream; size_t Base64Decode(raw_ostream& os, std::string_view encoded); std::string Base64Decode(std::string_view encoded, size_t* num_read); -std::string_view Base64Decode(std::string_view encoded, size_t* num_read, - SmallVectorImpl& buf); - size_t Base64Decode(std::string_view encoded, std::vector* plain); -std::span Base64Decode(std::string_view encoded, size_t* num_read, - SmallVectorImpl& buf); - void Base64Encode(raw_ostream& os, std::string_view plain); std::string Base64Encode(std::string_view plain); -std::string_view Base64Encode(std::string_view plain, - SmallVectorImpl& buf); - void Base64Encode(raw_ostream& os, std::span plain); std::string Base64Encode(std::span plain); -std::string_view Base64Encode(std::span plain, - SmallVectorImpl& buf); - } // namespace wpi #endif // WPIUTIL_WPI_BASE64_H_ diff --git a/wpiutil/src/test/native/cpp/Base64Test.cpp b/wpiutil/src/test/native/cpp/Base64Test.cpp index 517d124d0e4..c1c8579448e 100644 --- a/wpiutil/src/test/native/cpp/Base64Test.cpp +++ b/wpiutil/src/test/native/cpp/Base64Test.cpp @@ -7,7 +7,6 @@ #include #include "wpi/Base64.h" -#include "wpi/SmallString.h" namespace wpi { @@ -38,13 +37,6 @@ TEST_P(Base64Test, EncodeStdString) { ASSERT_EQ(GetParam().encoded, Base64Encode(GetPlain())); } -TEST_P(Base64Test, EncodeSmallString) { - SmallString<128> buf; - ASSERT_EQ(GetParam().encoded, Base64Encode(GetPlain(), buf)); - // reuse buf - ASSERT_EQ(GetParam().encoded, Base64Encode(GetPlain(), buf)); -} - TEST_P(Base64Test, DecodeStdString) { std::string_view encoded = GetParam().encoded; size_t len; @@ -53,19 +45,6 @@ TEST_P(Base64Test, DecodeStdString) { ASSERT_EQ(GetPlain(), s); } -TEST_P(Base64Test, DecodeSmallString) { - SmallString<128> buf; - std::string_view encoded = GetParam().encoded; - size_t len; - std::string_view plain = Base64Decode(encoded, &len, buf); - EXPECT_EQ(encoded.size(), len); - ASSERT_EQ(GetPlain(), plain); - - // reuse buf - plain = Base64Decode(encoded, &len, buf); - ASSERT_EQ(GetPlain(), plain); -} - static Base64TestParam sample[] = { {-1, "Send reinforcements", "U2VuZCByZWluZm9yY2VtZW50cw=="}, {-1, "Now is the time for all good coders\n to learn C++", From bb247ed76b80ccd622937dae9238c839efeba0d9 Mon Sep 17 00:00:00 2001 From: Peter Johnson Date: Wed, 1 Jan 2025 23:38:34 -0800 Subject: [PATCH 42/54] Simplify base64 vector decode --- ntcore/src/main/native/cpp/server/ServerStorage.cpp | 5 ++--- wpiutil/src/main/native/cpp/Base64.cpp | 10 ++++++---- wpiutil/src/main/native/include/wpi/Base64.h | 3 ++- 3 files changed, 10 insertions(+), 8 deletions(-) diff --git a/ntcore/src/main/native/cpp/server/ServerStorage.cpp b/ntcore/src/main/native/cpp/server/ServerStorage.cpp index 60c9de03095..3fada6f2a3c 100644 --- a/ntcore/src/main/native/cpp/server/ServerStorage.cpp +++ b/ntcore/src/main/native/cpp/server/ServerStorage.cpp @@ -582,9 +582,8 @@ std::string ServerStorage::LoadPersistent(std::string_view in) { } else { // raw if (auto v = valueIt->second.get_ptr()) { - std::vector data; - wpi::Base64Decode(*v, &data); - value = Value::MakeRaw(std::move(data), time); + size_t count; + value = Value::MakeRaw(wpi::Base64DecodeUnsigned(*v, &count), time); } else { error = "value type mismatch, expected string"; goto err; diff --git a/wpiutil/src/main/native/cpp/Base64.cpp b/wpiutil/src/main/native/cpp/Base64.cpp index 60dd503b73c..2aeff5d64a5 100644 --- a/wpiutil/src/main/native/cpp/Base64.cpp +++ b/wpiutil/src/main/native/cpp/Base64.cpp @@ -126,10 +126,12 @@ std::string Base64Decode(std::string_view encoded, size_t* num_read) { return plain; } -size_t Base64Decode(std::string_view encoded, std::vector* plain) { - plain->resize(0); - raw_uvector_ostream os(*plain); - return Base64Decode(os, encoded); +std::vector Base64DecodeUnsigned(std::string_view encoded, + size_t* num_read) { + std::vector plain; + raw_uvector_ostream os(plain); + *num_read = Base64Decode(os, encoded); + return plain; } static const char basis_64[] = diff --git a/wpiutil/src/main/native/include/wpi/Base64.h b/wpiutil/src/main/native/include/wpi/Base64.h index d0765ecd079..db8b39b7708 100644 --- a/wpiutil/src/main/native/include/wpi/Base64.h +++ b/wpiutil/src/main/native/include/wpi/Base64.h @@ -20,7 +20,8 @@ size_t Base64Decode(raw_ostream& os, std::string_view encoded); std::string Base64Decode(std::string_view encoded, size_t* num_read); -size_t Base64Decode(std::string_view encoded, std::vector* plain); +std::vector Base64DecodeUnsigned(std::string_view encoded, + size_t* num_read); void Base64Encode(raw_ostream& os, std::string_view plain); From 7512c9e6ad7c33df1ac9467b93ff32d5c92ac7bf Mon Sep 17 00:00:00 2001 From: Peter Johnson Date: Wed, 1 Jan 2025 23:41:31 -0800 Subject: [PATCH 43/54] windows service resolver --- .../native/windows/MulticastServiceResolver.cpp | 15 +++++++-------- 1 file changed, 7 insertions(+), 8 deletions(-) diff --git a/wpinet/src/main/native/windows/MulticastServiceResolver.cpp b/wpinet/src/main/native/windows/MulticastServiceResolver.cpp index c8a3d793e13..964c70ced6f 100644 --- a/wpinet/src/main/native/windows/MulticastServiceResolver.cpp +++ b/wpinet/src/main/native/windows/MulticastServiceResolver.cpp @@ -11,6 +11,7 @@ #include #include #include +#include #include #include @@ -49,10 +50,8 @@ MulticastServiceResolver::MulticastServiceResolver( if (wpi::ends_with_lower(serviceType, ".local")) { wpi::sys::windows::UTF8ToUTF16(serviceType, wideStorage); } else { - wpi::SmallString<128> storage; - storage.append(serviceType); - storage.append(".local"); - wpi::sys::windows::UTF8ToUTF16(storage.str(), wideStorage); + wpi::sys::windows::UTF8ToUTF16(fmt::format("{}.local", serviceType), + wideStorage); } pImpl->serviceType = std::wstring{wideStorage.data(), wideStorage.size()}; } @@ -71,10 +70,10 @@ static _Function_class_(DNS_QUERY_COMPLETION_ROUTINE) VOID WINAPI MulticastServiceResolver::Impl* impl = reinterpret_cast(pQueryContext); - wpi::SmallVector PtrRecords; - wpi::SmallVector SrvRecords; - wpi::SmallVector TxtRecords; - wpi::SmallVector ARecords; + std::vector PtrRecords; + std::vector SrvRecords; + std::vector TxtRecords; + std::vector ARecords; { DNS_RECORDW* current = pQueryResults->pQueryRecords; From b3386d2bb4c0079da97c596ca4b82ac46978d301 Mon Sep 17 00:00:00 2001 From: Peter Johnson Date: Wed, 1 Jan 2025 23:43:58 -0800 Subject: [PATCH 44/54] More cleanup --- wpinet/src/main/native/include/wpinet/HttpUtil.h | 2 +- wpinet/src/test/native/cpp/WebSocketTest.cpp | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/wpinet/src/main/native/include/wpinet/HttpUtil.h b/wpinet/src/main/native/include/wpinet/HttpUtil.h index c6c31a83db5..3dcb6b4b875 100644 --- a/wpinet/src/main/native/include/wpinet/HttpUtil.h +++ b/wpinet/src/main/native/include/wpinet/HttpUtil.h @@ -318,7 +318,7 @@ class HttpRequest { template HttpRequest(const HttpLocation& loc, const T& extraParams) : host{loc.host}, port{loc.port} { - SmallVector, 4> params; + std::vector> params; for (const auto& p : loc.params) { params.emplace_back(std::pair{GetFirst(p), GetSecond(p)}); } diff --git a/wpinet/src/test/native/cpp/WebSocketTest.cpp b/wpinet/src/test/native/cpp/WebSocketTest.cpp index fad6b3660b2..6d50f8f00b2 100644 --- a/wpinet/src/test/native/cpp/WebSocketTest.cpp +++ b/wpinet/src/test/native/cpp/WebSocketTest.cpp @@ -187,7 +187,7 @@ TEST_F(WebSocketTest, CreateClientExtraHeaders) { }); clientPipe->Connect(pipeName, [&]() { WebSocket::ClientOptions options; - SmallVector, 4> extraHeaders; + std::vector> extraHeaders; extraHeaders.emplace_back("Extra1", "Data1"); extraHeaders.emplace_back("Extra2", "Data2"); options.extraHeaders = extraHeaders; From 5db53c40ea8b6760bf506527ecd15c93e56d1abf Mon Sep 17 00:00:00 2001 From: Peter Johnson Date: Wed, 1 Jan 2025 23:48:10 -0800 Subject: [PATCH 45/54] Remove from SimpleBufferPool --- simulation/halsim_ds_socket/src/main/native/cpp/main.cpp | 4 ++-- .../src/main/native/include/HALSimWSClientConnection.h | 2 +- .../src/main/native/include/HALSimHttpConnection.h | 2 +- simulation/halsim_xrp/src/main/native/cpp/HALSimXRP.cpp | 4 ++-- simulation/halsim_xrp/src/main/native/include/HALSimXRP.h | 2 +- wpinet/src/main/native/include/wpinet/uv/Buffer.h | 7 ++----- wpinet/src/test/native/cpp/uv/UvBufferTest.cpp | 8 ++++---- 7 files changed, 13 insertions(+), 16 deletions(-) diff --git a/simulation/halsim_ds_socket/src/main/native/cpp/main.cpp b/simulation/halsim_ds_socket/src/main/native/cpp/main.cpp index 3686d3affdb..50be8bd5af0 100644 --- a/simulation/halsim_ds_socket/src/main/native/cpp/main.cpp +++ b/simulation/halsim_ds_socket/src/main/native/cpp/main.cpp @@ -47,8 +47,8 @@ struct DataStore { }; } // namespace -static SimpleBufferPool<4>& GetBufferPool() { - static SimpleBufferPool<4> bufferPool; +static SimpleBufferPool& GetBufferPool() { + static SimpleBufferPool bufferPool; return bufferPool; } diff --git a/simulation/halsim_ws_client/src/main/native/include/HALSimWSClientConnection.h b/simulation/halsim_ws_client/src/main/native/include/HALSimWSClientConnection.h index 005813f8d78..783f18d013a 100644 --- a/simulation/halsim_ws_client/src/main/native/include/HALSimWSClientConnection.h +++ b/simulation/halsim_ws_client/src/main/native/include/HALSimWSClientConnection.h @@ -41,7 +41,7 @@ class HALSimWSClientConnection bool m_ws_connected = false; wpi::WebSocket* m_websocket = nullptr; - wpi::uv::SimpleBufferPool<4> m_buffers; + wpi::uv::SimpleBufferPool m_buffers; std::mutex m_buffers_mutex; }; diff --git a/simulation/halsim_ws_server/src/main/native/include/HALSimHttpConnection.h b/simulation/halsim_ws_server/src/main/native/include/HALSimHttpConnection.h index 7073256de64..3889849c4cd 100644 --- a/simulation/halsim_ws_server/src/main/native/include/HALSimHttpConnection.h +++ b/simulation/halsim_ws_server/src/main/native/include/HALSimHttpConnection.h @@ -52,7 +52,7 @@ class HALSimHttpConnection bool m_isWsConnected = false; // these are only valid if the websocket is connected - wpi::uv::SimpleBufferPool<4> m_buffers; + wpi::uv::SimpleBufferPool m_buffers; std::mutex m_buffers_mutex; }; diff --git a/simulation/halsim_xrp/src/main/native/cpp/HALSimXRP.cpp b/simulation/halsim_xrp/src/main/native/cpp/HALSimXRP.cpp index 704e172c828..abebcfb75c2 100644 --- a/simulation/halsim_xrp/src/main/native/cpp/HALSimXRP.cpp +++ b/simulation/halsim_xrp/src/main/native/cpp/HALSimXRP.cpp @@ -137,8 +137,8 @@ void HALSimXRP::OnSimValueChanged(const wpi::json& simData) { } } -uv::SimpleBufferPool<4>& HALSimXRP::GetBufferPool() { - static uv::SimpleBufferPool<4> bufferPool(128); +uv::SimpleBufferPool& HALSimXRP::GetBufferPool() { + static uv::SimpleBufferPool bufferPool(128); return bufferPool; } diff --git a/simulation/halsim_xrp/src/main/native/include/HALSimXRP.h b/simulation/halsim_xrp/src/main/native/include/HALSimXRP.h index ee6d7303897..9dca798d35e 100644 --- a/simulation/halsim_xrp/src/main/native/include/HALSimXRP.h +++ b/simulation/halsim_xrp/src/main/native/include/HALSimXRP.h @@ -63,7 +63,7 @@ class HALSimXRP : public wpilibws::HALSimBaseWebSocketConnection, int m_port; void SendStateToXRP(); - wpi::uv::SimpleBufferPool<4>& GetBufferPool(); + wpi::uv::SimpleBufferPool& GetBufferPool(); std::mutex m_buffer_mutex; struct sockaddr_in m_dest; diff --git a/wpinet/src/main/native/include/wpinet/uv/Buffer.h b/wpinet/src/main/native/include/wpinet/uv/Buffer.h index 01dc881a653..53aea255790 100644 --- a/wpinet/src/main/native/include/wpinet/uv/Buffer.h +++ b/wpinet/src/main/native/include/wpinet/uv/Buffer.h @@ -8,12 +8,10 @@ #include #include -#include #include #include #include - -#include +#include namespace wpi::uv { @@ -108,7 +106,6 @@ class Buffer : public uv_buf_t { * to the heap. * @tparam DEPTH depth of pool */ -template class SimpleBufferPool { public: /** @@ -167,7 +164,7 @@ class SimpleBufferPool { size_t Remaining() const { return m_pool.size(); } private: - SmallVector m_pool; + std::vector m_pool; size_t m_size; // NOLINT }; diff --git a/wpinet/src/test/native/cpp/uv/UvBufferTest.cpp b/wpinet/src/test/native/cpp/uv/UvBufferTest.cpp index f349d513373..4e5690e4d8f 100644 --- a/wpinet/src/test/native/cpp/uv/UvBufferTest.cpp +++ b/wpinet/src/test/native/cpp/uv/UvBufferTest.cpp @@ -9,21 +9,21 @@ namespace wpi::uv { TEST(UvSimpleBufferPoolTest, ConstructDefault) { - SimpleBufferPool<> pool; + SimpleBufferPool pool; auto buf1 = pool.Allocate(); ASSERT_EQ(buf1.len, 4096u); // NOLINT pool.Release({&buf1, 1}); } TEST(UvSimpleBufferPoolTest, ConstructSize) { - SimpleBufferPool<4> pool{8192}; + SimpleBufferPool pool{8192}; auto buf1 = pool.Allocate(); ASSERT_EQ(buf1.len, 8192u); // NOLINT pool.Release({&buf1, 1}); } TEST(UvSimpleBufferPoolTest, ReleaseReuse) { - SimpleBufferPool<4> pool; + SimpleBufferPool pool; auto buf1 = pool.Allocate(); auto buf1copy = buf1; auto origSize = buf1.len; @@ -37,7 +37,7 @@ TEST(UvSimpleBufferPoolTest, ReleaseReuse) { } TEST(UvSimpleBufferPoolTest, ClearRemaining) { - SimpleBufferPool<4> pool; + SimpleBufferPool pool; auto buf1 = pool.Allocate(); pool.Release({&buf1, 1}); ASSERT_EQ(pool.Remaining(), 1u); From 25e08bfd500820bfbe9653789af9cafcc6068468 Mon Sep 17 00:00:00 2001 From: Peter Johnson Date: Thu, 2 Jan 2025 08:45:07 -0800 Subject: [PATCH 46/54] Missing includes --- .../src/main/native/cpp/shuffleboard/ShuffleboardInstance.cpp | 1 + wpinet/src/main/native/cpp/uv/Process.cpp | 1 + 2 files changed, 2 insertions(+) diff --git a/wpilibc/src/main/native/cpp/shuffleboard/ShuffleboardInstance.cpp b/wpilibc/src/main/native/cpp/shuffleboard/ShuffleboardInstance.cpp index c8ae649e4b6..db031b746c0 100644 --- a/wpilibc/src/main/native/cpp/shuffleboard/ShuffleboardInstance.cpp +++ b/wpilibc/src/main/native/cpp/shuffleboard/ShuffleboardInstance.cpp @@ -6,6 +6,7 @@ #include #include +#include #include #include diff --git a/wpinet/src/main/native/cpp/uv/Process.cpp b/wpinet/src/main/native/cpp/uv/Process.cpp index 89a04a09ce2..c746e3082a5 100644 --- a/wpinet/src/main/native/cpp/uv/Process.cpp +++ b/wpinet/src/main/native/cpp/uv/Process.cpp @@ -5,6 +5,7 @@ #include "wpinet/uv/Process.h" #include +#include #include #include "wpinet/uv/Loop.h" From 0f0d35d276dc7ab03c81ef3ced027b1b5396bdb5 Mon Sep 17 00:00:00 2001 From: Peter Johnson Date: Thu, 2 Jan 2025 08:46:48 -0800 Subject: [PATCH 47/54] Fix one more SimpleBufferPool use case --- .../src/test/native/include/WebServerClientTest.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/simulation/halsim_ws_server/src/test/native/include/WebServerClientTest.h b/simulation/halsim_ws_server/src/test/native/include/WebServerClientTest.h index 6f2995f0ea0..40fdc48f153 100644 --- a/simulation/halsim_ws_server/src/test/native/include/WebServerClientTest.h +++ b/simulation/halsim_ws_server/src/test/native/include/WebServerClientTest.h @@ -21,7 +21,7 @@ namespace wpilibws { class WebServerClientTest { public: - using BufferPool = wpi::uv::SimpleBufferPool<4>; + using BufferPool = wpi::uv::SimpleBufferPool; using LoopFunc = std::function; using UvExecFunc = wpi::uv::AsyncFunction; From 60a2521e66fc3b459164fbbce81f1b34363c1cf4 Mon Sep 17 00:00:00 2001 From: Peter Johnson Date: Thu, 2 Jan 2025 08:54:10 -0800 Subject: [PATCH 48/54] CameraServer: Remove SmallString --- .../native/cpp/cameraserver/CameraServer.cpp | 37 +++++-------------- 1 file changed, 9 insertions(+), 28 deletions(-) diff --git a/cameraserver/src/main/native/cpp/cameraserver/CameraServer.cpp b/cameraserver/src/main/native/cpp/cameraserver/CameraServer.cpp index e111dcdafe3..44d645b5846 100644 --- a/cameraserver/src/main/native/cpp/cameraserver/CameraServer.cpp +++ b/cameraserver/src/main/native/cpp/cameraserver/CameraServer.cpp @@ -18,13 +18,11 @@ #include #include #include -#include #include #include #include #include "cameraserver/CameraServerShared.h" -#include "ntcore_cpp.h" using namespace frc; @@ -93,26 +91,14 @@ static Instance& GetInstance() { return instance; } -static std::string_view MakeSourceValue(CS_Source source, - wpi::SmallVectorImpl& buf) { +static std::string MakeSourceValue(CS_Source source) { CS_Status status = 0; - buf.clear(); switch (cs::GetSourceKind(source, &status)) { - case CS_SOURCE_USB: { - std::string_view prefix{"usb:"}; - buf.append(prefix.begin(), prefix.end()); - auto path = cs::GetUsbCameraPath(source, &status); - buf.append(path.begin(), path.end()); - break; - } + case CS_SOURCE_USB: + return fmt::format("usb:{}", cs::GetUsbCameraPath(source, &status)); case CS_SOURCE_HTTP: { - std::string_view prefix{"ip:"}; - buf.append(prefix.begin(), prefix.end()); auto urls = cs::GetHttpCameraUrls(source, &status); - if (!urls.empty()) { - buf.append(urls[0].begin(), urls[0].end()); - } - break; + return fmt::format("ip:{}", urls.empty() ? "" : urls.front()); } case CS_SOURCE_CV: return "cv:"; @@ -121,8 +107,6 @@ static std::string_view MakeSourceValue(CS_Source source, default: return "unknown:"; } - - return {buf.begin(), buf.size()}; } static std::string MakeStreamValue(std::string_view address, int port) { @@ -361,8 +345,7 @@ SourcePublisher::SourcePublisher(Instance& inst, modeEntry{table->GetStringTopic("mode").GetEntry("")}, modesPublisher{table->GetStringArrayTopic("modes").Publish()} { CS_Status status = 0; - wpi::SmallString<64> buf; - sourcePublisher.Set(MakeSourceValue(source, buf)); + sourcePublisher.Set(MakeSourceValue(source)); descriptionPublisher.Set(cs::GetSourceDescription(source, &status)); connectedPublisher.Set(cs::IsSourceConnected(source, &status)); streamsPublisher.Set(inst.GetSourceStreamValues(source)); @@ -602,8 +585,7 @@ cs::CvSink CameraServer::GetVideo() { cs::CvSink CameraServer::GetVideo(const cs::VideoSource& camera) { auto& inst = ::GetInstance(); - wpi::SmallString<64> name{"opencv_"}; - name += camera.GetName(); + auto name = fmt::format("opencv_{}", camera.GetName()); { std::scoped_lock lock(inst.m_mutex); @@ -620,7 +602,7 @@ cs::CvSink CameraServer::GetVideo(const cs::VideoSource& camera) { } } - cs::CvSink newsink{name.str()}; + cs::CvSink newsink{name}; newsink.SetSource(camera); AddServer(newsink); return newsink; @@ -629,8 +611,7 @@ cs::CvSink CameraServer::GetVideo(const cs::VideoSource& camera) { cs::CvSink CameraServer::GetVideo(const cs::VideoSource& camera, cs::VideoMode::PixelFormat pixelFormat) { auto& inst = ::GetInstance(); - wpi::SmallString<64> name{"opencv_"}; - name += camera.GetName(); + auto name = fmt::format("opencv_{}", camera.GetName()); { std::scoped_lock lock(inst.m_mutex); @@ -647,7 +628,7 @@ cs::CvSink CameraServer::GetVideo(const cs::VideoSource& camera, } } - cs::CvSink newsink{name.str(), pixelFormat}; + cs::CvSink newsink{name, pixelFormat}; newsink.SetSource(camera); AddServer(newsink); return newsink; From 0371a30ea1bed466bb22ad65cdb8ce48e957d4ae Mon Sep 17 00:00:00 2001 From: Peter Johnson Date: Thu, 2 Jan 2025 11:48:26 -0800 Subject: [PATCH 49/54] Use std::string in more places --- hal/src/main/native/cpp/ErrorHandling.cpp | 5 +++-- wpinet/src/main/native/cpp/HttpServerConnection.cpp | 8 +++----- wpinet/src/main/native/cpp/WebServer.cpp | 5 ++--- 3 files changed, 8 insertions(+), 10 deletions(-) diff --git a/hal/src/main/native/cpp/ErrorHandling.cpp b/hal/src/main/native/cpp/ErrorHandling.cpp index ad1b40b5bd5..39777af7dba 100644 --- a/hal/src/main/native/cpp/ErrorHandling.cpp +++ b/hal/src/main/native/cpp/ErrorHandling.cpp @@ -2,8 +2,9 @@ // Open Source Software; you can modify and/or share it under the terms of // the WPILib BSD license file in the root directory of this project. +#include + #include -#include #include "hal/Errors.h" #include "hal/HALBase.h" @@ -11,7 +12,7 @@ namespace { struct LastErrorStorage { int32_t status; - wpi::SmallString<512> message; + std::string message; }; } // namespace diff --git a/wpinet/src/main/native/cpp/HttpServerConnection.cpp b/wpinet/src/main/native/cpp/HttpServerConnection.cpp index 8e8b5d0ffe0..77fb9026bc3 100644 --- a/wpinet/src/main/native/cpp/HttpServerConnection.cpp +++ b/wpinet/src/main/native/cpp/HttpServerConnection.cpp @@ -6,7 +6,7 @@ #include -#include +#include #include #include #include @@ -174,8 +174,6 @@ void HttpServerConnection::SendError(int code, std::string_view message) { baseMessage = "501: Not Implemented!"; break; } - SmallString<256> content{baseMessage}; - content += "\r\n"; - content += message; - SendResponse(code, codeText, "text/plain", content.str(), extra); + SendResponse(code, codeText, "text/plain", + fmt::format("{}\r\n{}", baseMessage, message), extra); } diff --git a/wpinet/src/main/native/cpp/WebServer.cpp b/wpinet/src/main/native/cpp/WebServer.cpp index e697131ea4d..4a73d09583e 100644 --- a/wpinet/src/main/native/cpp/WebServer.cpp +++ b/wpinet/src/main/native/cpp/WebServer.cpp @@ -15,7 +15,6 @@ #include #include #include -#include #include #include #include @@ -320,8 +319,8 @@ void MyHttpConnection::ProcessRequest() { SendResponse(200, "OK", "text/html", html); } } else { - wpi::SmallString<128> extraHeadersBuf; - wpi::raw_svector_ostream os{extraHeadersBuf}; + std::string extraHeaders; + wpi::raw_string_ostream os{extraHeaders}; os << "Content-Disposition: filename=\""; os.write_escaped(fullpath.filename().string()); os << "\"\r\n"; From 2602b62ccae35846549d31c9d18ea987bdde1f2f Mon Sep 17 00:00:00 2001 From: Peter Johnson Date: Thu, 2 Jan 2025 12:57:43 -0800 Subject: [PATCH 50/54] MulticastServiceResolver: Remove SmallString --- wpinet/src/main/native/linux/MulticastServiceResolver.cpp | 7 ++----- 1 file changed, 2 insertions(+), 5 deletions(-) diff --git a/wpinet/src/main/native/linux/MulticastServiceResolver.cpp b/wpinet/src/main/native/linux/MulticastServiceResolver.cpp index e0598a03681..058737e5701 100644 --- a/wpinet/src/main/native/linux/MulticastServiceResolver.cpp +++ b/wpinet/src/main/native/linux/MulticastServiceResolver.cpp @@ -8,7 +8,6 @@ #include #include -#include #include #include @@ -72,21 +71,19 @@ static void ResolveCallback(AvahiServiceResolver* r, AvahiIfIndex interface, wpi::substr(value, splitIndex + 1, value.size() - splitIndex - 1); data.txt.emplace_back(std::pair{key, value}); } - wpi::SmallString<256> outputHostName; char label[256]; do { impl->table.unescape_label(&host_name, label, sizeof(label)); if (label[0] == '\0') { break; } - outputHostName.append(label); - outputHostName.append("."); + data.hostName.append(label); + data.hostName.append("."); } while (true); data.ipv4Address = address->data.ipv4.address; data.port = port; data.serviceName = name; - data.hostName = std::string{outputHostName}; impl->onFound(std::move(data)); } From a227f620580071930b41684e8893de6c9bc6ccfa Mon Sep 17 00:00:00 2001 From: Peter Johnson Date: Thu, 2 Jan 2025 12:59:24 -0800 Subject: [PATCH 51/54] DynamicStruct: Don't use SmallString --- wpiutil/src/main/native/cpp/struct/DynamicStruct.cpp | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/wpiutil/src/main/native/cpp/struct/DynamicStruct.cpp b/wpiutil/src/main/native/cpp/struct/DynamicStruct.cpp index 120590d5f46..2a374c1d484 100644 --- a/wpiutil/src/main/native/cpp/struct/DynamicStruct.cpp +++ b/wpiutil/src/main/native/cpp/struct/DynamicStruct.cpp @@ -11,7 +11,6 @@ #include #include "wpi/Endian.h" -#include "wpi/SmallString.h" #include "wpi/SmallVector.h" #include "wpi/raw_ostream.h" #include "wpi/struct/SchemaParser.h" @@ -292,8 +291,8 @@ const StructDescriptor* StructDescriptorDatabase::Add(std::string_view name, // check for circular reference wpi::SmallVector stack; if (!theStruct.CheckCircular(stack)) { - wpi::SmallString<128> buf; - wpi::raw_svector_ostream os{buf}; + std::string buf; + wpi::raw_string_ostream os{buf}; for (auto&& elem : stack) { if (!buf.empty()) { os << " <- "; From 23814d1e88dc8eec6275bd1acd510111139777db Mon Sep 17 00:00:00 2001 From: Peter Johnson Date: Thu, 2 Jan 2025 15:48:48 -0800 Subject: [PATCH 52/54] [wpiutil] DataLog: use format instead of SmallString --- wpiutil/src/main/native/cpp/DataLog.cpp | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/wpiutil/src/main/native/cpp/DataLog.cpp b/wpiutil/src/main/native/cpp/DataLog.cpp index 16e66187f80..2f864259a64 100644 --- a/wpiutil/src/main/native/cpp/DataLog.cpp +++ b/wpiutil/src/main/native/cpp/DataLog.cpp @@ -13,9 +13,10 @@ #include #include +#include + #include "wpi/Endian.h" #include "wpi/Logger.h" -#include "wpi/SmallString.h" #include "wpi/print.h" #include "wpi/timestamp.h" @@ -145,9 +146,7 @@ void DataLog::AddSchema(std::string_view name, std::string_view type, return; // don't add duplicates } schemaInfo.data.assign(schema.begin(), schema.end()); - wpi::SmallString<128> fullName{"/.schema/"}; - fullName += name; - int entry = StartImpl(fullName, type, {}, timestamp); + int entry = StartImpl(fmt::format("/.schema/{}", name), type, {}, timestamp); // inline AppendRaw() without releasing lock if (entry <= 0) { From 243ddc15573d4d8a676c513074f66a8dc70a7096 Mon Sep 17 00:00:00 2001 From: Peter Johnson Date: Thu, 2 Jan 2025 16:50:34 -0800 Subject: [PATCH 53/54] [wpiutil] Convert a couple of use cases to std::string --- wpiutil/src/main/native/unix/Demangle.cpp | 10 ++++------ wpiutil/src/main/native/unix/StackTrace.cpp | 7 +++---- 2 files changed, 7 insertions(+), 10 deletions(-) diff --git a/wpiutil/src/main/native/unix/Demangle.cpp b/wpiutil/src/main/native/unix/Demangle.cpp index d376879b4c3..1138760bb98 100644 --- a/wpiutil/src/main/native/unix/Demangle.cpp +++ b/wpiutil/src/main/native/unix/Demangle.cpp @@ -9,24 +9,22 @@ #include #include -#include "wpi/SmallString.h" - namespace wpi { std::string Demangle(std::string_view mangledSymbol) { - SmallString<128> buf{mangledSymbol}; + std::string buf{mangledSymbol}; size_t length; int32_t status; char* symbol = abi::__cxa_demangle(buf.c_str(), nullptr, &length, &status); if (status == 0) { - std::string rv{symbol}; + buf = symbol; std::free(symbol); - return rv; + return buf; } // If everything else failed, just return the mangled symbol - return std::string{mangledSymbol}; + return buf; } } // namespace wpi diff --git a/wpiutil/src/main/native/unix/StackTrace.cpp b/wpiutil/src/main/native/unix/StackTrace.cpp index cc753c26b98..b4f8266d291 100644 --- a/wpiutil/src/main/native/unix/StackTrace.cpp +++ b/wpiutil/src/main/native/unix/StackTrace.cpp @@ -9,7 +9,6 @@ #include #include "wpi/Demangle.h" -#include "wpi/SmallString.h" #include "wpi/StringExtras.h" #include "wpi/raw_ostream.h" @@ -20,8 +19,8 @@ std::string GetStackTraceDefault(int offset) { void* stackTrace[128]; int stackSize = backtrace(stackTrace, 128); char** mangledSymbols = backtrace_symbols(stackTrace, stackSize); - wpi::SmallString<1024> buf; - wpi::raw_svector_ostream trace(buf); + std::string buf; + wpi::raw_string_ostream trace(buf); for (int i = offset; i < stackSize; i++) { // Only print recursive functions once in a row. @@ -38,7 +37,7 @@ std::string GetStackTraceDefault(int offset) { std::free(mangledSymbols); - return std::string{trace.str()}; + return buf; #else // backtrace_symbols not supported on android return ""; From 790b09964e1889c1bb8e6135a75cc11661568095 Mon Sep 17 00:00:00 2001 From: Peter Johnson Date: Sat, 18 Jan 2025 10:11:18 -0800 Subject: [PATCH 54/54] A few more fixups --- glass/src/lib/native/cpp/other/FMS.cpp | 4 +--- glass/src/libnt/native/cpp/NTFMS.cpp | 2 -- wpinet/src/dev/native/cpp/main.cpp | 4 +--- wpinet/src/main/native/cpp/EventLoopRunner.cpp | 4 ---- wpinet/src/test/native/cpp/WebSocketIntegrationTest.cpp | 2 -- wpinet/src/test/native/cpp/WebSocketServerTest.cpp | 1 - wpiutil/src/dev/native/cpp/main.cpp | 5 +---- 7 files changed, 3 insertions(+), 19 deletions(-) diff --git a/glass/src/lib/native/cpp/other/FMS.cpp b/glass/src/lib/native/cpp/other/FMS.cpp index ed56a669db5..21ec296353a 100644 --- a/glass/src/lib/native/cpp/other/FMS.cpp +++ b/glass/src/lib/native/cpp/other/FMS.cpp @@ -8,7 +8,6 @@ #include #include -#include #include "glass/DataSource.h" @@ -149,9 +148,8 @@ void glass::DisplayFMSReadOnly(FMSModel* model) { } } if (auto data = model->GetGameSpecificMessageData()) { - wpi::SmallString<64> gsmBuf; ImGui::Text("Game Specific: %s", - exists ? data->GetValue(gsmBuf).data() : "?"); + exists ? data->GetValue().c_str() : "?"); } if (!exists) { diff --git a/glass/src/libnt/native/cpp/NTFMS.cpp b/glass/src/libnt/native/cpp/NTFMS.cpp index d585da91d38..b445ed9ada3 100644 --- a/glass/src/libnt/native/cpp/NTFMS.cpp +++ b/glass/src/libnt/native/cpp/NTFMS.cpp @@ -9,8 +9,6 @@ #include #include -#include -#include using namespace glass; diff --git a/wpinet/src/dev/native/cpp/main.cpp b/wpinet/src/dev/native/cpp/main.cpp index 8b9f30a6ae7..0a1bc47a831 100644 --- a/wpinet/src/dev/native/cpp/main.cpp +++ b/wpinet/src/dev/native/cpp/main.cpp @@ -2,10 +2,8 @@ // Open Source Software; you can modify and/or share it under the terms of // the WPILib BSD license file in the root directory of this project. -#include #include int main() { - wpi::SmallString<128> v1("Hello"); - wpi::print("{}\n", v1.str()); + wpi::print("{}\n", "Hello"); } diff --git a/wpinet/src/main/native/cpp/EventLoopRunner.cpp b/wpinet/src/main/native/cpp/EventLoopRunner.cpp index 8ee976c0d66..85a90209bce 100644 --- a/wpinet/src/main/native/cpp/EventLoopRunner.cpp +++ b/wpinet/src/main/native/cpp/EventLoopRunner.cpp @@ -7,10 +7,6 @@ #include #include -#include -#include -#include - #include "wpinet/uv/AsyncFunction.h" #include "wpinet/uv/Loop.h" diff --git a/wpinet/src/test/native/cpp/WebSocketIntegrationTest.cpp b/wpinet/src/test/native/cpp/WebSocketIntegrationTest.cpp index 1105a23076e..db924f82f81 100644 --- a/wpinet/src/test/native/cpp/WebSocketIntegrationTest.cpp +++ b/wpinet/src/test/native/cpp/WebSocketIntegrationTest.cpp @@ -6,8 +6,6 @@ #include -#include - #include "WebSocketTest.h" namespace wpi { diff --git a/wpinet/src/test/native/cpp/WebSocketServerTest.cpp b/wpinet/src/test/native/cpp/WebSocketServerTest.cpp index 51b6f3d0808..51f35b7fae0 100644 --- a/wpinet/src/test/native/cpp/WebSocketServerTest.cpp +++ b/wpinet/src/test/native/cpp/WebSocketServerTest.cpp @@ -9,7 +9,6 @@ #include #include -#include #include #include "WebSocketTest.h" diff --git a/wpiutil/src/dev/native/cpp/main.cpp b/wpiutil/src/dev/native/cpp/main.cpp index 4c053de3872..0a1bc47a831 100644 --- a/wpiutil/src/dev/native/cpp/main.cpp +++ b/wpiutil/src/dev/native/cpp/main.cpp @@ -4,9 +4,6 @@ #include -#include "wpi/SmallString.h" - int main() { - wpi::SmallString<128> v1("Hello"); - wpi::print("{}\n", v1.str()); + wpi::print("{}\n", "Hello"); }