Explorar el Código

the use of built-in function

Igor Voynov hace 9 años
padre
commit
82894dcfd5
Se han modificado 3 ficheros con 5 adiciones y 49 borrados
  1. 2 2
      Sources/HttpParser.swift
  2. 3 3
      Sources/HttpRequest.swift
  3. 0 44
      Sources/String+Misc.swift

+ 2 - 2
Sources/HttpParser.swift

@@ -38,8 +38,8 @@ public class HttpParser {
         }
         return query.split("&").reduce([(String, String)]()) { (c, s) -> [(String, String)] in
             let tokens = s.split(1, separator: "=")
-            if let name = tokens.first, let value = tokens.last {
-                return c + [(name.removePercentEncoding(), value.removePercentEncoding())]
+            if let name = tokens.first?.removingPercentEncoding, let value = tokens.last?.removingPercentEncoding {
+                return c + [(name, value)]
             }
             return c
         }

+ 3 - 3
Sources/HttpRequest.swift

@@ -34,9 +34,9 @@ public class HttpRequest {
         }
         return String.fromUInt8(body).split("&").map { param -> (String, String) in
             let tokens = param.split("=")
-            if let name = tokens.first, let value = tokens.last, tokens.count == 2 {
-                return (name.replace(old: "+", " ").removePercentEncoding(),
-                        value.replace(old: "+", " ").removePercentEncoding())
+            if let name = tokens.first?.removingPercentEncoding, let value = tokens.last?.removingPercentEncoding, tokens.count == 2 {
+                return (name.replace(old: "+", " "),
+                        value.replace(old: "+", " "))
             }
             return ("","")
         }

+ 0 - 44
Sources/String+Misc.swift

@@ -46,38 +46,6 @@ extension String {
         return array.reduce("", { $0.0 + String(UnicodeScalar($0.1)) })
     }
     
-    public func removePercentEncoding() -> String {
-        var scalars = self.unicodeScalars
-        var output = ""
-        var decodeBuffer = [UInt8]()
-        while let scalar = scalars.popFirst() {
-            if scalar == "%" {
-                let first = scalars.popFirst()
-                let secon = scalars.popFirst()
-                if let first = first?.asAlpha(), let secon = secon?.asAlpha() {
-                    decodeBuffer.append(first*16+secon)
-                } else {
-                    if !decodeBuffer.isEmpty {
-                        output.append(String.fromUInt8(decodeBuffer))
-                        decodeBuffer.removeAll()
-                    }
-                    if let first = first { output.append(Character(first)) }
-                    if let secon = secon { output.append(Character(secon)) }
-                }
-            } else {
-                if !decodeBuffer.isEmpty {
-                    output.append(String.fromUInt8(decodeBuffer))
-                    decodeBuffer.removeAll()
-                }
-                output.append(Character(scalar))
-            }
-        }
-        if !decodeBuffer.isEmpty {
-            output.append(String.fromUInt8(decodeBuffer))
-            decodeBuffer.removeAll()
-        }
-        return output
-    }
 }
 
 extension UnicodeScalar {
@@ -92,16 +60,4 @@ extension UnicodeScalar {
         return nil
     }
     
-    public func asAlpha() -> UInt8? {
-        if self.value >= 48 && self.value <= 57 {
-            return UInt8(self.value) - 48
-        }
-        if self.value >= 97 && self.value <= 102 {
-            return UInt8(self.value) - 87
-        }
-        if self.value >= 65 && self.value <= 70 {
-            return UInt8(self.value) - 55
-        }
-        return nil
-    }
 }