Skip to content

Commit 82894dc

Browse files
committed
the use of built-in function
1 parent 9aa9f56 commit 82894dc

File tree

3 files changed

+5
-49
lines changed

3 files changed

+5
-49
lines changed

Sources/HttpParser.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,8 +38,8 @@ public class HttpParser {
3838
}
3939
return query.split("&").reduce([(String, String)]()) { (c, s) -> [(String, String)] in
4040
let tokens = s.split(1, separator: "=")
41-
if let name = tokens.first, let value = tokens.last {
42-
return c + [(name.removePercentEncoding(), value.removePercentEncoding())]
41+
if let name = tokens.first?.removingPercentEncoding, let value = tokens.last?.removingPercentEncoding {
42+
return c + [(name, value)]
4343
}
4444
return c
4545
}

Sources/HttpRequest.swift

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -34,9 +34,9 @@ public class HttpRequest {
3434
}
3535
return String.fromUInt8(body).split("&").map { param -> (String, String) in
3636
let tokens = param.split("=")
37-
if let name = tokens.first, let value = tokens.last, tokens.count == 2 {
38-
return (name.replace(old: "+", " ").removePercentEncoding(),
39-
value.replace(old: "+", " ").removePercentEncoding())
37+
if let name = tokens.first?.removingPercentEncoding, let value = tokens.last?.removingPercentEncoding, tokens.count == 2 {
38+
return (name.replace(old: "+", " "),
39+
value.replace(old: "+", " "))
4040
}
4141
return ("","")
4242
}

Sources/String+Misc.swift

Lines changed: 0 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -46,38 +46,6 @@ extension String {
4646
return array.reduce("", { $0.0 + String(UnicodeScalar($0.1)) })
4747
}
4848

49-
public func removePercentEncoding() -> String {
50-
var scalars = self.unicodeScalars
51-
var output = ""
52-
var decodeBuffer = [UInt8]()
53-
while let scalar = scalars.popFirst() {
54-
if scalar == "%" {
55-
let first = scalars.popFirst()
56-
let secon = scalars.popFirst()
57-
if let first = first?.asAlpha(), let secon = secon?.asAlpha() {
58-
decodeBuffer.append(first*16+secon)
59-
} else {
60-
if !decodeBuffer.isEmpty {
61-
output.append(String.fromUInt8(decodeBuffer))
62-
decodeBuffer.removeAll()
63-
}
64-
if let first = first { output.append(Character(first)) }
65-
if let secon = secon { output.append(Character(secon)) }
66-
}
67-
} else {
68-
if !decodeBuffer.isEmpty {
69-
output.append(String.fromUInt8(decodeBuffer))
70-
decodeBuffer.removeAll()
71-
}
72-
output.append(Character(scalar))
73-
}
74-
}
75-
if !decodeBuffer.isEmpty {
76-
output.append(String.fromUInt8(decodeBuffer))
77-
decodeBuffer.removeAll()
78-
}
79-
return output
80-
}
8149
}
8250

8351
extension UnicodeScalar {
@@ -92,16 +60,4 @@ extension UnicodeScalar {
9260
return nil
9361
}
9462

95-
public func asAlpha() -> UInt8? {
96-
if self.value >= 48 && self.value <= 57 {
97-
return UInt8(self.value) - 48
98-
}
99-
if self.value >= 97 && self.value <= 102 {
100-
return UInt8(self.value) - 87
101-
}
102-
if self.value >= 65 && self.value <= 70 {
103-
return UInt8(self.value) - 55
104-
}
105-
return nil
106-
}
10763
}

0 commit comments

Comments
 (0)