diff --git a/Sources/NIOHPACK/HPACKHeader.swift b/Sources/NIOHPACK/HPACKHeader.swift index be08cf9..866025c 100644 --- a/Sources/NIOHPACK/HPACKHeader.swift +++ b/Sources/NIOHPACK/HPACKHeader.swift @@ -2,7 +2,7 @@ // // This source file is part of the SwiftNIO open source project // -// Copyright (c) 2017-2021 Apple Inc. and the SwiftNIO project authors +// Copyright (c) 2017-2023 Apple Inc. and the SwiftNIO project authors // Licensed under Apache License v2.0 // // See LICENSE.txt for license information @@ -425,6 +425,15 @@ extension HPACKHeaders { public mutating func reserveCapacity(_ minimumCapacity: Int) { self.headers.reserveCapacity(minimumCapacity) } + + /// Removes all headers whilst optionally keeping the capacity. + /// + /// - Parameter keepingCapacity: Whether to keep the underlying memory or not. Use this flag to + /// reduce allocations when reusing the header collection. + @inlinable + public mutating func removeAll(keepingCapacity: Bool = false) { + self.headers.removeAll(keepingCapacity: keepingCapacity) + } } extension HPACKHeaders: RandomAccessCollection { diff --git a/Tests/NIOHPACKTests/HPACKHeadersTests.swift b/Tests/NIOHPACKTests/HPACKHeadersTests.swift index deea261..e415e6d 100644 --- a/Tests/NIOHPACKTests/HPACKHeadersTests.swift +++ b/Tests/NIOHPACKTests/HPACKHeadersTests.swift @@ -2,7 +2,7 @@ // // This source file is part of the SwiftNIO open source project // -// Copyright (c) 2022 Apple Inc. and the SwiftNIO project authors +// Copyright (c) 2022-2023 Apple Inc. and the SwiftNIO project authors // Licensed under Apache License v2.0 // // See LICENSE.txt for license information @@ -180,4 +180,30 @@ final class HPACKHeadersTests: XCTestCase { } } } + + func testRemoveAll() { + let original: HPACKHeaders = [ + "foo": "bar", + "bar": "foo", + "foo": "baz", + "foo": "bar,baz", + "foo": " bar ,baz ,,", + "bar": "foo" + ] + + XCTAssertEqual(original.capacity, 6) + var keepCapacity = original + XCTAssertEqual(keepCapacity.count, 6) + XCTAssertEqual(keepCapacity.capacity, 6) + keepCapacity.removeAll(keepingCapacity: true) + XCTAssertEqual(keepCapacity.count, 0) + XCTAssertEqual(keepCapacity.capacity, 6) + + var lowerCapacity = original + XCTAssertEqual(lowerCapacity.count, 6) + XCTAssertEqual(lowerCapacity.capacity, 6) + lowerCapacity.removeAll(keepingCapacity: false) + XCTAssertEqual(lowerCapacity.count, 0) + XCTAssertEqual(lowerCapacity.capacity, 0) + } } diff --git a/scripts/soundness.sh b/scripts/soundness.sh index 3e680a9..b0c1837 100755 --- a/scripts/soundness.sh +++ b/scripts/soundness.sh @@ -18,7 +18,7 @@ here="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )" function replace_acceptable_years() { # this needs to replace all acceptable forms with 'YEARS' - sed -e 's/20[12][78901]-20[12][890123]/YEARS/' -e 's/20[12][890123]/YEARS/' + sed -e 's/20[12][789012]-20[12][890123]/YEARS/' -e 's/20[12][890123]/YEARS/' } printf "=> Checking for unacceptable language... "