From f700f5b355c92f01d90fa28bf5921f053d3ed949 Mon Sep 17 00:00:00 2001 From: Karl <5254025+karwa@users.noreply.github.com> Date: Wed, 5 Aug 2020 11:47:25 +0200 Subject: [PATCH] Rename startsWithSameUnicodeScalars -> startsWithExactly, switch to comparing UTF8 bytes. (#104) --- .../HTTPResponseCompressor.swift | 16 ++++++++-------- .../HTTPResponseCompressorTest.swift | 16 ++++++++-------- 2 files changed, 16 insertions(+), 16 deletions(-) diff --git a/Sources/NIOHTTPCompression/HTTPResponseCompressor.swift b/Sources/NIOHTTPCompression/HTTPResponseCompressor.swift index ef3fdc6..e22916b 100644 --- a/Sources/NIOHTTPCompression/HTTPResponseCompressor.swift +++ b/Sources/NIOHTTPCompression/HTTPResponseCompressor.swift @@ -17,15 +17,15 @@ import NIO import NIOHTTP1 extension StringProtocol { - /// Test if this `Collection` starts with the unicode scalars of `needle`. + /// Test if this string starts with the same unicode scalars as the given string, `prefix`. /// /// - note: This will be faster than `String.startsWith` as no unicode normalisations are performed. /// /// - parameters: - /// - needle: The `Collection` of `Unicode.Scalar`s to match at the beginning of `self` - /// - returns: If `self` started with the elements contained in `needle`. - func startsWithSameUnicodeScalars(string needle: S) -> Bool { - return self.unicodeScalars.starts(with: needle.unicodeScalars) + /// - prefix: The string to match at the beginning of `self` + /// - returns: Whether or not `self` starts with the same unicode scalars as `prefix`. + func startsWithExactly(_ prefix: S) -> Bool { + return self.utf8.starts(with: prefix.utf8) } } @@ -155,11 +155,11 @@ public final class HTTPResponseCompressor: ChannelDuplexHandler, RemovableChanne var anyQValue: Float = -1 for acceptHeader in acceptHeaders { - if acceptHeader.startsWithSameUnicodeScalars(string: "gzip") || acceptHeader.startsWithSameUnicodeScalars(string: "x-gzip") { + if acceptHeader.startsWithExactly("gzip") || acceptHeader.startsWithExactly("x-gzip") { gzipQValue = qValueFromHeader(acceptHeader) - } else if acceptHeader.startsWithSameUnicodeScalars(string: "deflate") { + } else if acceptHeader.startsWithExactly("deflate") { deflateQValue = qValueFromHeader(acceptHeader) - } else if acceptHeader.startsWithSameUnicodeScalars(string: "*") { + } else if acceptHeader.startsWithExactly("*") { anyQValue = qValueFromHeader(acceptHeader) } } diff --git a/Tests/NIOHTTPCompressionTests/HTTPResponseCompressorTest.swift b/Tests/NIOHTTPCompressionTests/HTTPResponseCompressorTest.swift index 4d3651a..09cb53f 100644 --- a/Tests/NIOHTTPCompressionTests/HTTPResponseCompressorTest.swift +++ b/Tests/NIOHTTPCompressionTests/HTTPResponseCompressorTest.swift @@ -578,19 +578,19 @@ class HTTPResponseCompressorTest: XCTestCase { } func testStartsWithSameUnicodeScalarsWorksOnEmptyStrings() throws { - XCTAssertTrue("".startsWithSameUnicodeScalars(string: "")) + XCTAssertTrue("".startsWithExactly("")) } func testStartsWithSameUnicodeScalarsWorksOnLongerNeedleFalse() throws { - XCTAssertFalse("_".startsWithSameUnicodeScalars(string: "__")) + XCTAssertFalse("_".startsWithExactly("__")) } func testStartsWithSameUnicodeScalarsWorksOnSameStrings() throws { - XCTAssertTrue("beer".startsWithSameUnicodeScalars(string: "beer")) + XCTAssertTrue("beer".startsWithExactly("beer")) } func testStartsWithSameUnicodeScalarsWorksOnPrefix() throws { - XCTAssertTrue("beer is good".startsWithSameUnicodeScalars(string: "beer")) + XCTAssertTrue("beer is good".startsWithExactly("beer")) } func testStartsWithSameUnicodeScalarsSaysNoForTheSameStringInDifferentNormalisations() throws { @@ -598,11 +598,11 @@ class HTTPResponseCompressorTest: XCTestCase { let nfdEncodedEAigu = "\u{65}\u{301}" XCTAssertEqual(nfcEncodedEAigu, nfdEncodedEAigu) - XCTAssertTrue(nfcEncodedEAigu.startsWithSameUnicodeScalars(string: nfcEncodedEAigu)) - XCTAssertTrue(nfdEncodedEAigu.startsWithSameUnicodeScalars(string: nfdEncodedEAigu)) + XCTAssertTrue(nfcEncodedEAigu.startsWithExactly(nfcEncodedEAigu)) + XCTAssertTrue(nfdEncodedEAigu.startsWithExactly(nfdEncodedEAigu)) // the both do _not_ start like the other - XCTAssertFalse(nfcEncodedEAigu.startsWithSameUnicodeScalars(string: nfdEncodedEAigu)) - XCTAssertFalse(nfdEncodedEAigu.startsWithSameUnicodeScalars(string: nfcEncodedEAigu)) + XCTAssertFalse(nfcEncodedEAigu.startsWithExactly(nfdEncodedEAigu)) + XCTAssertFalse(nfdEncodedEAigu.startsWithExactly(nfcEncodedEAigu)) } func testStartsWithSaysYesForTheSameStringInDifferentNormalisations() throws {