Jelajahi Sumber

Added tests for String+Misc::removePercentEncoding().

Damian Kołakowski 10 tahun lalu
induk
melakukan
707c3b33ea

+ 11 - 11
Sources/String+Misc.swift

@@ -52,32 +52,32 @@ extension String {
     public func removePercentEncoding() -> String {
         var scalars = self.unicodeScalars
         var output = ""
-        var bytesBuffer = [UInt8]()
+        var decodeBuffer = [UInt8]()
         while let scalar = scalars.popFirst() {
             if scalar == "%" {
                 let first = scalars.popFirst()
                 let secon = scalars.popFirst()
                 if let first = first?.asAlpha(), secon = secon?.asAlpha() {
-                    bytesBuffer.append(first*16+secon)
+                    decodeBuffer.append(first*16+secon)
                 } else {
-                    if !bytesBuffer.isEmpty {
-                        output.appendContentsOf(String.fromUInt8(bytesBuffer))
-                        bytesBuffer.removeAll()
+                    if !decodeBuffer.isEmpty {
+                        output.appendContentsOf(String.fromUInt8(decodeBuffer))
+                        decodeBuffer.removeAll()
                     }
                     if let first = first { output.append(Character(first)) }
                     if let secon = secon { output.append(Character(secon)) }
                 }
             } else {
-                if !bytesBuffer.isEmpty {
-                    output.appendContentsOf(String.fromUInt8(bytesBuffer))
-                    bytesBuffer.removeAll()
+                if !decodeBuffer.isEmpty {
+                    output.appendContentsOf(String.fromUInt8(decodeBuffer))
+                    decodeBuffer.removeAll()
                 }
                 output.append(Character(scalar))
             }
         }
-        if !bytesBuffer.isEmpty {
-            output.appendContentsOf(String.fromUInt8(bytesBuffer))
-            bytesBuffer.removeAll()
+        if !decodeBuffer.isEmpty {
+            output.appendContentsOf(String.fromUInt8(decodeBuffer))
+            decodeBuffer.removeAll()
         }
         return output
     }

+ 20 - 0
SwifterTestsCommon/SwifterTestsStringExtensions.swift

@@ -89,4 +89,24 @@ class SwifterTestsStringExtensions: XCTestCase {
         XCTAssertEqual("t&e&s&t12%3%".replace("&", "+").replace("%", "+"), "t+e+s+t12+3+")
         XCTAssertEqual("test 1234 #$%^&*( test   ".replace(" ", "_"), "test_1234_#$%^&*(_test___")
     }
+    
+    func testMiscRemovePercentEncoding() {
+        XCTAssertEqual("".removePercentEncoding(), "")
+        XCTAssertEqual("%20".removePercentEncoding(), " ")
+        XCTAssertEqual("%22".removePercentEncoding(), "\"")
+        XCTAssertEqual("%25".removePercentEncoding(), "%")
+        XCTAssertEqual("%2d".removePercentEncoding(), "-")
+        XCTAssertEqual("%2e".removePercentEncoding(), ".")
+        XCTAssertEqual("%3C".removePercentEncoding(), "<")
+        XCTAssertEqual("%3E".removePercentEncoding(), ">")
+        XCTAssertEqual("%5C".removePercentEncoding(), "\\")
+        XCTAssertEqual("%5E".removePercentEncoding(), "^")
+        XCTAssertEqual("%5F".removePercentEncoding(), "_")
+        XCTAssertEqual("%60".removePercentEncoding(), "`")
+        XCTAssertEqual("%7B".removePercentEncoding(), "{")
+        XCTAssertEqual("%7C".removePercentEncoding(), "|")
+        XCTAssertEqual("%7D".removePercentEncoding(), "}")
+        XCTAssertEqual("%7E".removePercentEncoding(), "~")
+        XCTAssertEqual("%7e".removePercentEncoding(), "~")
+    }
 }