Skip to content

Commit 0ba5c21

Browse files
author
Raymond McCrae
committed
Add SwiftLint to the project
1 parent 3309a62 commit 0ba5c21

File tree

7 files changed

+173
-119
lines changed

7 files changed

+173
-119
lines changed

.swiftlint.yml

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
included:
2+
- Sources
3+
- Tests
4+
excluded:
5+
6+
opt_in_rules:
7+
- closure_spacing # closure should have single space inside each brace
8+
- empty_count # prefer isEmpty over comparing to 0
9+
- number_separator # underscore should be used as thousand separator in large decimal numbers
10+
disabled_rules:
11+
- todo # todo and fixme should be avoided. Use custom script for this
12+
- vertical_parameter_alignment # parameter alignment in functions. Xcode use another alignment logic
13+
line_length: 120
14+
function_body_length:
15+
- 20
16+
- 40
17+
nesting:
18+
type_level:
19+
warning: 3
20+
statement_level:
21+
warning: 10
22+
type_name:
23+
excluded: K
24+
variable_name:
25+
excluded:
26+
- id
27+
custom_rules:
28+
open_bracket_blank_line:
29+
included: ".*.swift"
30+
name: "blank line after open bracket"
31+
regex: "{\n[[[:blank:]]\n]*\n"
32+
message: "Blank line after open bracket should be removed"
33+
severity: warning
34+
closed_brackets_blank_line:
35+
included: ".*.swift"
36+
name: "blank lines between closed brackets"
37+
regex: "}\n[[[:blank:]]\n]*\n}"
38+
message: "Blank lines between closed brackets should be removed"
39+
severity: warning

HTMLSAXParser.xcodeproj/project.pbxproj

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -216,8 +216,8 @@
216216
isa = PBXGroup;
217217
children = (
218218
4D9286B81F3516C3001A16FC /* testdata */,
219-
4DDF8C2F1F211E2E008C2135 /* HTMLParserTests.swift */,
220219
4DDF8C311F211E2E008C2135 /* Info.plist */,
220+
4DDF8C2F1F211E2E008C2135 /* HTMLParserTests.swift */,
221221
4D161F401F228123002573EF /* HTMLEncodeEntitiesTests.swift */,
222222
);
223223
name = Tests;
@@ -316,6 +316,7 @@
316316
isa = PBXNativeTarget;
317317
buildConfigurationList = 4DDF8C351F211E2E008C2135 /* Build configuration list for PBXNativeTarget "HTMLSAXParser iOS" */;
318318
buildPhases = (
319+
4D63D6581F9CB6F6009EC3CB /* SwiftLint */,
319320
4DDF8C1C1F211E2D008C2135 /* Sources */,
320321
4DDF8C1D1F211E2D008C2135 /* Frameworks */,
321322
4DDF8C1E1F211E2D008C2135 /* Headers */,
@@ -442,6 +443,23 @@
442443
};
443444
/* End PBXResourcesBuildPhase section */
444445

446+
/* Begin PBXShellScriptBuildPhase section */
447+
4D63D6581F9CB6F6009EC3CB /* SwiftLint */ = {
448+
isa = PBXShellScriptBuildPhase;
449+
buildActionMask = 2147483647;
450+
files = (
451+
);
452+
inputPaths = (
453+
);
454+
name = SwiftLint;
455+
outputPaths = (
456+
);
457+
runOnlyForDeploymentPostprocessing = 0;
458+
shellPath = /bin/sh;
459+
shellScript = "if which swiftlint >/dev/null; then\n swiftlint\nelse\n\techo \"warning: SwiftLint not installed, download from https://github.com/realm/SwiftLint\"\nfi";
460+
};
461+
/* End PBXShellScriptBuildPhase section */
462+
445463
/* Begin PBXSourcesBuildPhase section */
446464
4D3D6DA41F3EDEDA00EE40CB /* Sources */ = {
447465
isa = PBXSourcesBuildPhase;

Sources/HTMLSAXParser/EscapeSpecialCharacters.swift

Lines changed: 34 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ public enum HTMLQuoteCharacter: Character {
2525
case none = "\0"
2626
case singleQuote = "'"
2727
case doubleQuote = "\""
28-
28+
2929
var characterCode: CInt {
3030
switch self {
3131
case .none:
@@ -39,7 +39,36 @@ public enum HTMLQuoteCharacter: Character {
3939
}
4040

4141
public extension Data {
42-
42+
43+
fileprivate func encodeHTMLEntitiesBytes(_ outputLength: inout Int, _ outputLengthBytes: UnsafeMutablePointer<CInt>, _ inputLengthBytes: UnsafeMutablePointer<CInt>, _ quoteCharacter: HTMLQuoteCharacter, _ inputLength: Int, _ loop: inout Bool, _ bufferGrowthFactor: Double) -> Data? {
44+
return self.withUnsafeBytes { (inputBytes: UnsafePointer<UInt8>) -> Data? in
45+
let outputBufferCapacity = outputLength
46+
let outputBuffer = UnsafeMutablePointer<UInt8>.allocate(capacity: outputBufferCapacity)
47+
defer {
48+
outputBuffer.deallocate(capacity: Int(outputBufferCapacity))
49+
}
50+
let result = htmlEncodeEntities(outputBuffer, outputLengthBytes, inputBytes, inputLengthBytes, quoteCharacter.characterCode)
51+
52+
if result == 0 { // zero represents success
53+
// Have we consumed the length of the input buffer
54+
let consumed = inputLengthBytes.pointee
55+
if consumed == inputLength {
56+
loop = false
57+
return Data(bytes: outputBuffer, count: Int(outputLengthBytes.pointee))
58+
} else {
59+
// if we have not consumed the full input buffer.
60+
// estimate a new output buffer length
61+
let ratio = Double(consumed) / Double(inputLength)
62+
outputLength = Int( (2.0 - ratio) * Double(outputLength) * bufferGrowthFactor )
63+
}
64+
} else {
65+
loop = false
66+
}
67+
68+
return nil
69+
}
70+
}
71+
4372
public func encodeHTMLEntities(quoteCharacter: HTMLQuoteCharacter = .doubleQuote) -> Data? {
4473
let bufferGrowthFactor = 1.4
4574
let inputLength = self.count
@@ -51,48 +80,21 @@ public extension Data {
5180
inputLengthBytes.deallocate(capacity: 1)
5281
outputLengthBytes.deallocate(capacity: 1)
5382
}
54-
83+
5584
var loop = true
5685

5786
repeat {
5887
inputLengthBytes.pointee = CInt(inputLength)
5988
outputLengthBytes.pointee = CInt(outputLength)
6089

61-
let outputData = self.withUnsafeBytes { (inputBytes: UnsafePointer<UInt8>) -> Data? in
62-
let outputBufferCapacity = outputLength
63-
let outputBuffer = UnsafeMutablePointer<UInt8>.allocate(capacity: outputBufferCapacity)
64-
defer {
65-
outputBuffer.deallocate(capacity: Int(outputBufferCapacity))
66-
}
67-
let result = htmlEncodeEntities(outputBuffer, outputLengthBytes, inputBytes, inputLengthBytes, quoteCharacter.characterCode)
68-
69-
if result == 0 { // zero represents success
70-
// Have we consumed the length of the input buffer
71-
let consumed = inputLengthBytes.pointee
72-
if consumed == inputLength {
73-
loop = false
74-
return Data(bytes: outputBuffer, count: Int(outputLengthBytes.pointee))
75-
}
76-
else {
77-
// if we have not consumed the full input buffer.
78-
// estimate a new output buffer length
79-
let ratio = Double(consumed) / Double(inputLength)
80-
outputLength = Int( (2.0 - ratio) * Double(outputLength) * bufferGrowthFactor )
81-
}
82-
}
83-
else {
84-
loop = false
85-
}
86-
87-
return nil
88-
}
90+
let outputData = encodeHTMLEntitiesBytes(&outputLength, outputLengthBytes, inputLengthBytes, quoteCharacter, inputLength, &loop, bufferGrowthFactor)
8991

9092
if let outputData = outputData {
9193
return outputData
9294
}
9395

9496
} while loop
95-
97+
9698
return nil
9799
}
98100
}

0 commit comments

Comments
 (0)