Skip to content

Commit b240381

Browse files
committed
Mark more functions as public
1 parent 2412d3a commit b240381

File tree

6 files changed

+162
-104
lines changed

6 files changed

+162
-104
lines changed

BencodeKit.xcodeproj/project.pbxproj

Lines changed: 40 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@
1414
9EA794D01E3B1E0F000D6C50 /* Decode.swift in Sources */ = {isa = PBXBuildFile; fileRef = 9EA794CB1E3B0F82000D6C50 /* Decode.swift */; };
1515
9EA794D11E3B1E8F000D6C50 /* Dictionary.swift in Sources */ = {isa = PBXBuildFile; fileRef = 9EB798361E3AEE1F005A7C9E /* Dictionary.swift */; };
1616
9EA794EB1E41D11A000D6C50 /* CryptoSwift.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 9EA794E71E41D104000D6C50 /* CryptoSwift.framework */; };
17-
9EA794ED1E41DC86000D6C50 /* OrderedDictionary.swift in Sources */ = {isa = PBXBuildFile; fileRef = 9EA794EC1E41DC86000D6C50 /* OrderedDictionary.swift */; };
1817
9EB798241E3AE7EA005A7C9E /* BencodeKit.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 9EB7981A1E3AE7EA005A7C9E /* BencodeKit.framework */; };
1918
9EB798291E3AE7EA005A7C9E /* Tests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 9EB798281E3AE7EA005A7C9E /* Tests.swift */; };
2019
9EB7982B1E3AE7EA005A7C9E /* BencodeKit.h in Headers */ = {isa = PBXBuildFile; fileRef = 9EB7981D1E3AE7EA005A7C9E /* BencodeKit.h */; settings = {ATTRIBUTES = (Public, ); }; };
@@ -48,10 +47,9 @@
4847
/* End PBXContainerItemProxy section */
4948

5049
/* Begin PBXFileReference section */
51-
9EA794C91E3B0E44000D6C50 /* Extensions.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; name = Extensions.swift; path = Sources/Types/Extensions.swift; sourceTree = "<group>"; };
50+
9EA794C91E3B0E44000D6C50 /* Extensions.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = Extensions.swift; sourceTree = "<group>"; };
5251
9EA794CB1E3B0F82000D6C50 /* Decode.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = Decode.swift; sourceTree = "<group>"; };
5352
9EA794E11E41D104000D6C50 /* CryptoSwift.xcodeproj */ = {isa = PBXFileReference; lastKnownFileType = "wrapper.pb-project"; name = CryptoSwift.xcodeproj; path = Carthage/Checkouts/CryptoSwift/CryptoSwift.xcodeproj; sourceTree = "<group>"; };
54-
9EA794EC1E41DC86000D6C50 /* OrderedDictionary.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = OrderedDictionary.swift; sourceTree = "<group>"; };
5553
9EB7981A1E3AE7EA005A7C9E /* BencodeKit.framework */ = {isa = PBXFileReference; explicitFileType = wrapper.framework; includeInIndex = 0; path = BencodeKit.framework; sourceTree = BUILT_PRODUCTS_DIR; };
5654
9EB7981D1E3AE7EA005A7C9E /* BencodeKit.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = BencodeKit.h; sourceTree = "<group>"; };
5755
9EB7981E1E3AE7EA005A7C9E /* Info.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist.xml; path = Info.plist; sourceTree = "<group>"; };
@@ -107,8 +105,6 @@
107105
isa = PBXGroup;
108106
children = (
109107
9EB798321E3AE884005A7C9E /* Sources */,
110-
9EA794C91E3B0E44000D6C50 /* Extensions.swift */,
111-
9EA794EC1E41DC86000D6C50 /* OrderedDictionary.swift */,
112108
9EB798271E3AE7EA005A7C9E /* Tests */,
113109
9EA794EA1E41D109000D6C50 /* Frameworks */,
114110
9EB7981B1E3AE7EA005A7C9E /* Products */,
@@ -149,6 +145,7 @@
149145
9EB798351E3AEE1F005A7C9E /* Types */ = {
150146
isa = PBXGroup;
151147
children = (
148+
9EA794C91E3B0E44000D6C50 /* Extensions.swift */,
152149
9EB798361E3AEE1F005A7C9E /* Dictionary.swift */,
153150
9EB798371E3AEE1F005A7C9E /* Integer.swift */,
154151
9EB798381E3AEE1F005A7C9E /* List.swift */,
@@ -297,7 +294,6 @@
297294
9EB7983B1E3AEE1F005A7C9E /* Integer.swift in Sources */,
298295
9EA794CE1E3B1691000D6C50 /* String.swift in Sources */,
299296
9EA794D11E3B1E8F000D6C50 /* Dictionary.swift in Sources */,
300-
9EA794ED1E41DC86000D6C50 /* OrderedDictionary.swift in Sources */,
301297
9EA794CF1E3B1D42000D6C50 /* List.swift in Sources */,
302298
9EA794CA1E3B0E44000D6C50 /* Extensions.swift in Sources */,
303299
);
@@ -325,12 +321,48 @@
325321
9EB798131E3AE7B7005A7C9E /* Debug */ = {
326322
isa = XCBuildConfiguration;
327323
buildSettings = {
324+
CLANG_WARN_BOOL_CONVERSION = YES;
325+
CLANG_WARN_CONSTANT_CONVERSION = YES;
326+
CLANG_WARN_EMPTY_BODY = YES;
327+
CLANG_WARN_ENUM_CONVERSION = YES;
328+
CLANG_WARN_INFINITE_RECURSION = YES;
329+
CLANG_WARN_INT_CONVERSION = YES;
330+
CLANG_WARN_SUSPICIOUS_MOVE = YES;
331+
CLANG_WARN_UNREACHABLE_CODE = YES;
332+
CLANG_WARN__DUPLICATE_METHOD_MATCH = YES;
333+
ENABLE_STRICT_OBJC_MSGSEND = YES;
334+
ENABLE_TESTABILITY = YES;
335+
GCC_NO_COMMON_BLOCKS = YES;
336+
GCC_WARN_64_TO_32_BIT_CONVERSION = YES;
337+
GCC_WARN_ABOUT_RETURN_TYPE = YES;
338+
GCC_WARN_UNDECLARED_SELECTOR = YES;
339+
GCC_WARN_UNINITIALIZED_AUTOS = YES;
340+
GCC_WARN_UNUSED_FUNCTION = YES;
341+
GCC_WARN_UNUSED_VARIABLE = YES;
342+
ONLY_ACTIVE_ARCH = YES;
328343
};
329344
name = Debug;
330345
};
331346
9EB798141E3AE7B7005A7C9E /* Release */ = {
332347
isa = XCBuildConfiguration;
333348
buildSettings = {
349+
CLANG_WARN_BOOL_CONVERSION = YES;
350+
CLANG_WARN_CONSTANT_CONVERSION = YES;
351+
CLANG_WARN_EMPTY_BODY = YES;
352+
CLANG_WARN_ENUM_CONVERSION = YES;
353+
CLANG_WARN_INFINITE_RECURSION = YES;
354+
CLANG_WARN_INT_CONVERSION = YES;
355+
CLANG_WARN_SUSPICIOUS_MOVE = YES;
356+
CLANG_WARN_UNREACHABLE_CODE = YES;
357+
CLANG_WARN__DUPLICATE_METHOD_MATCH = YES;
358+
ENABLE_STRICT_OBJC_MSGSEND = YES;
359+
GCC_NO_COMMON_BLOCKS = YES;
360+
GCC_WARN_64_TO_32_BIT_CONVERSION = YES;
361+
GCC_WARN_ABOUT_RETURN_TYPE = YES;
362+
GCC_WARN_UNDECLARED_SELECTOR = YES;
363+
GCC_WARN_UNINITIALIZED_AUTOS = YES;
364+
GCC_WARN_UNUSED_FUNCTION = YES;
365+
GCC_WARN_UNUSED_VARIABLE = YES;
334366
};
335367
name = Release;
336368
};
@@ -384,6 +416,7 @@
384416
INSTALL_PATH = "$(LOCAL_LIBRARY_DIR)/Frameworks";
385417
IPHONEOS_DEPLOYMENT_TARGET = 10.2;
386418
LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks @loader_path/Frameworks";
419+
"LD_RUNPATH_SEARCH_PATHS[sdk=macosx*]" = "$(inherited) @executable_path/../Frameworks @loader_path/../Frameworks";
387420
MTL_ENABLE_DEBUG_INFO = YES;
388421
ONLY_ACTIVE_ARCH = YES;
389422
PRODUCT_BUNDLE_IDENTIFIER = monadicconsulting.github.io.BencodeKit;
@@ -444,6 +477,7 @@
444477
INSTALL_PATH = "$(LOCAL_LIBRARY_DIR)/Frameworks";
445478
IPHONEOS_DEPLOYMENT_TARGET = 10.2;
446479
LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks @loader_path/Frameworks";
480+
"LD_RUNPATH_SEARCH_PATHS[sdk=macosx*]" = "$(inherited) @executable_path/../Frameworks @loader_path/../Frameworks";
447481
MTL_ENABLE_DEBUG_INFO = NO;
448482
PRODUCT_BUNDLE_IDENTIFIER = monadicconsulting.github.io.BencodeKit;
449483
PRODUCT_NAME = "$(PROJECT_NAME)";

OrderedDictionary.swift

Lines changed: 0 additions & 68 deletions
This file was deleted.

Sources/Bencode.swift

Lines changed: 21 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ public indirect enum Bencode: Equatable {
2929
case integer(Int)
3030
case bytes(Data)
3131
case list([Bencode])
32-
case dictionary(BencodeDictionary)
32+
case dictionary(OrderedDictionary<String, Bencode>)
3333

3434
init(_ integer: Int) {
3535
self = .integer(integer)
@@ -49,46 +49,42 @@ public indirect enum Bencode: Equatable {
4949
}
5050

5151
init(_ dictionary: [(String, Bencode)]) {
52-
self.init(BencodeDictionary(dictionary))
52+
self.init(OrderedDictionary<String, Bencode>(dictionary))
5353
}
5454

55-
init(_ dictionary: BencodeDictionary) {
55+
init(_ dictionary: OrderedDictionary<String, Bencode>) {
5656
self = .dictionary(dictionary)
5757
}
5858

59-
func encodedString() -> String {
59+
public func encodedString() -> String {
6060
switch self {
6161
case .integer(let integer):
6262
return "i\(String(integer))e"
6363
case .bytes(let bytes):
64-
let string = bytes.reduce("") { string, byte in
65-
string.appendingFormat("%c", byte)
66-
}
64+
let string = bytes.asciiString
6765
return "\(string.characters.count):\(string)"
6866
case .list(let list):
69-
return "l\(list.map({ $0.encodedString() }).joined())e"
67+
return "l\(list.map { $0.encodedString() }.joined())e"
7068
case .dictionary(let dictionary):
71-
return "d\(dictionary.map({ "\($0.characters.count):\($0)\($1.encodedString())" }).joined())e"
69+
return "d\(dictionary.map { "\($0.characters.count):\($0)\($1.encodedString())" }.joined())e"
7270
}
7371
}
7472

75-
func toString() -> String {
73+
public func toString() -> String {
7674
switch self {
7775
case .integer(let integer):
7876
return String(integer)
7977
case .bytes(let bytes):
80-
return bytes.reduce("") { string, byte in
81-
string.appendingFormat("%c", byte)
82-
}
78+
return bytes.asciiString
8379
case .list(let list):
84-
return "[" + list.map({ $0.toString() }).joined(separator: ", ") + "]"
80+
return "[" + list.map { $0.toString() }.joined(separator: ", ") + "]"
8581
case .dictionary(let dictionary):
86-
return "[" + dictionary.map({ "\"\($0)\": \($1.toString())" }).joined(separator: ", ") + "]"
82+
return "[" + dictionary.map { "\"\($0)\": \($1.toString())" }.joined(separator: ", ") + "]"
8783
}
8884

8985
}
9086

91-
subscript(index: String) -> Bencode? {
87+
public subscript(index: String) -> Bencode? {
9288
get {
9389
guard case .dictionary(let dictionary) = self else {
9490
return nil
@@ -121,18 +117,16 @@ public extension Bencode {
121117
case .bytes(let bytes):
122118
return "\(bytes.count):".asciiData + bytes
123119
case .list(let list):
124-
return "l".asciiData + list.map({ $0.encoded() }).joined() + "e".asciiData
120+
return "l".asciiData + list.map { $0.encoded() }.joined() + "e".asciiData
125121
case .dictionary(let dictionary):
126-
return "d".asciiData + dictionary.map({ "\($0.characters.count):\($0)".asciiData + $1.encoded() }).joined() + "e".asciiData
122+
return "d".asciiData + dictionary.map { "\($0.characters.count):\($0)".asciiData + $1.encoded() }.joined() + "e".asciiData
127123
}
128124
}
129125
}
130126

131127
public extension Bencode {
132-
func sha1Hash() -> String {
133-
return SHA1().calculate(for: encoded().bytes).reduce("") { hex, byte in
134-
hex.appendingFormat("%02x", byte)
135-
}
128+
public func sha1Hash() -> String {
129+
return SHA1().calculate(for: encoded().bytes).reduce("") { $0.appendingFormat("%02x", $1) }
136130
}
137131
}
138132

@@ -143,11 +137,11 @@ public extension Bencode {
143137
return a == b
144138
case (.bytes(let a), .bytes(let b)):
145139
return a == b
146-
case (.list(let a), .list(let b)):
147-
return a.count == b.count && zip(a, b).reduce(true, { $0 && ($1.0 == $1.1) })
148-
case (.dictionary(let a), .dictionary(let b)):
149-
return a.count == b.count && zip(a, b).reduce(true, { $0 && ($1.0 == $1.1) })
150-
case _:
140+
case (.list(let a), .list(let b)) where a.count == b.count:
141+
return zip(a, b).all(where: ==)
142+
case (.dictionary(let a), .dictionary(let b)) where a.count == b.count:
143+
return zip(a, b).all(where: ==)
144+
default:
151145
return false
152146
}
153147
}

Sources/Types/Dictionary.swift

Lines changed: 57 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,62 @@
66
// Copyright © 2017 Monadic Consulting. All rights reserved.
77
//
88

9+
public struct OrderedDictionary<Key: Hashable, Value>: Collection {
10+
public typealias Index = Int
11+
12+
public func index(after i: Index) -> Index {
13+
return i + 1
14+
}
15+
16+
public var startIndex: Index {
17+
return 0
18+
}
19+
20+
public var endIndex: Index {
21+
return keys.count
22+
}
23+
24+
public var keys: Array<Key> = []
25+
public var values: Array<Value> {
26+
return keys.flatMap { dictionary[$0] }
27+
}
28+
private var dictionary: Dictionary<Key,Value> = [:]
29+
30+
public init(_ elements: [(Key, Value)] = []) {
31+
self.keys = elements.map { $0.0 }
32+
self.dictionary = elements.reduce(into: [:]) { dictionary, pair in
33+
dictionary[pair.0] = pair.1
34+
}
35+
}
36+
37+
public init(_ elements: (Key, Value)...) {
38+
self.init(elements)
39+
}
40+
41+
public subscript(key: Key) -> Value? {
42+
get {
43+
return dictionary[key]
44+
}
45+
set(newValue) {
46+
if let value = newValue {
47+
if dictionary.updateValue(value, forKey: key) == nil {
48+
keys.append(key)
49+
}
50+
} else {
51+
dictionary.removeValue(forKey: key)
52+
keys = keys.filter { $0 != key }
53+
}
54+
}
55+
}
56+
57+
public subscript(index: Index) -> (Key, Value) {
58+
get {
59+
let key = keys[index]
60+
return (key, dictionary[key]!)
61+
}
62+
}
63+
}
64+
965
internal func bdecodeDictionary(_ data: Data, _ index: Data.Index) throws -> (Bencode, Data.Index) {
1066
guard data[index] == "d" else {
1167
throw BencodingError.invalidDictionary(index)
@@ -35,5 +91,5 @@ internal func bdecodeDictionary(_ data: Data, _ index: Data.Index) throws -> (Be
3591
}
3692
}
3793

38-
return (.dictionary(BencodeDictionary(values)), data.index(after: currentIndex))
94+
return (.dictionary(OrderedDictionary(values)), data.index(after: currentIndex))
3995
}

Sources/Types/Extensions.swift

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,3 +25,32 @@ internal extension String {
2525
return data(using: .ascii)!
2626
}
2727
}
28+
29+
internal extension Data {
30+
var asciiString: String {
31+
return reduce("") { string, byte in
32+
string.appendingFormat("%c", byte)
33+
}
34+
}
35+
}
36+
37+
internal extension Sequence {
38+
func reduce<A>(into initial: A, _ combine: (inout A, Iterator.Element) -> ()) -> A {
39+
var result = initial
40+
for element in self {
41+
combine(&result, element)
42+
}
43+
return result
44+
}
45+
46+
func all(where predicate: (Self.Iterator.Element) throws -> Bool) rethrows -> Bool {
47+
var returnValue = true
48+
for value in self {
49+
if try !predicate(value) {
50+
returnValue = false
51+
break
52+
}
53+
}
54+
return returnValue
55+
}
56+
}

0 commit comments

Comments
 (0)