From 09fe3cbcd567e676037594420807dc3bfa38ff7f Mon Sep 17 00:00:00 2001 From: Honza Dvorsky Date: Fri, 6 May 2016 00:06:09 +0200 Subject: [PATCH] changed type to byte from cchar --- Sources/Redbird/ClientSocket.swift | 16 ++++++++-------- Sources/Redbird/Errors.swift | 4 ++-- Sources/Redbird/Extensions.swift | 12 ++++++------ Sources/Redbird/Formatters.swift | 2 +- Sources/Redbird/Parsers.swift | 20 ++++++++++---------- Sources/Redbird/Redbird.swift | 4 +++- Tests/Redbird/ParsingTests.swift | 12 ++++++------ Tests/Redbird/RedbirdTests.swift | 6 +++--- Tests/Redbird/StringTests.swift | 6 +++--- Tests/Redbird/XCTestManifests.swift | 2 +- 10 files changed, 43 insertions(+), 41 deletions(-) diff --git a/Sources/Redbird/ClientSocket.swift b/Sources/Redbird/ClientSocket.swift index 484cbc1..0c248da 100644 --- a/Sources/Redbird/ClientSocket.swift +++ b/Sources/Redbird/ClientSocket.swift @@ -13,13 +13,13 @@ typealias SocketError = SocksCore.Error protocol Socket: class, SocketReader { func write(string: String) throws - func read(bytes: Int) throws -> [CChar] + func read(bytes: Int) throws -> [Byte] func newWithConfig(config: RedbirdConfig) throws -> Socket func close() } extension Socket { - func read() throws -> [CChar] { + func read() throws -> [Byte] { return try self.read(bytes: BufferCapacity) } } @@ -47,13 +47,13 @@ class ClientSocket: Socket { try self.client.write(data: string) } - func read(bytes: Int = BufferCapacity) throws -> [CChar] { - return try self.client.read(maxBytes: bytes).map { CChar($0) } + func read(bytes: Int = BufferCapacity) throws -> [Byte] { + return try self.client.read(maxBytes: bytes).map { Byte($0) } } } protocol SocketReader: class { - func read(bytes: Int) throws -> [CChar] + func read(bytes: Int) throws -> [Byte] } let BufferCapacity = 512 @@ -62,10 +62,10 @@ extension SocketReader { /// Reads until 1) we run out of characters or 2) we detect the delimiter /// whichever happens first. - func readUntilDelimiter(alreadyRead: [CChar], delimiter: String) throws -> ([CChar], [CChar]?) { + func readUntilDelimiter(alreadyRead: [Byte], delimiter: String) throws -> ([Byte], [Byte]?) { var totalBuffer = alreadyRead - let delimiterChars = delimiter.ccharArrayView() + let delimiterChars = delimiter.byteArrayView() var lastReadCount = BufferCapacity while true { @@ -93,7 +93,7 @@ extension SocketReader { extension ClientSocket: SocketReader {} -extension Collection where Iterator.Element == CChar { +extension Collection where Iterator.Element == Byte { func stringView() throws -> String { return try self.toString() diff --git a/Sources/Redbird/Errors.swift b/Sources/Redbird/Errors.swift index cb4d9b9..31bc3f5 100644 --- a/Sources/Redbird/Errors.swift +++ b/Sources/Redbird/Errors.swift @@ -13,9 +13,9 @@ public enum RedbirdError: ErrorProtocol { case IntegerInvalidInput(String) case FormatterNotForThisType(RespObject, RespType?) case ReceivedStringNotTerminatedByRespTerminator(String) - case StringNotConvertibleToCChar(String) + case StringNotConvertibleToByte(String) case NoDataFromSocket - case NotEnoughCharactersToReadFromSocket(Int, [CChar]) + case NotEnoughCharactersToReadFromSocket(Int, [Byte]) case BulkStringProvidedUnparseableByteCount(String) case ArrayProvidedUnparseableCount(String) case NoFormatterFoundForObject(RespObject) diff --git a/Sources/Redbird/Extensions.swift b/Sources/Redbird/Extensions.swift index 1cba000..61b5f03 100644 --- a/Sources/Redbird/Extensions.swift +++ b/Sources/Redbird/Extensions.swift @@ -70,19 +70,19 @@ extension String { return self.characters.contains(character) } - func ccharArrayView() -> [CChar] { + func byteArrayView() -> [Byte] { return self.withCString { ptr in let count = Int(strlen(ptr)) var idx = 0 - var out = Array(repeating: 0, count: count) - while idx < count { out[idx] = ptr[idx]; idx += 1 } + var out = Array(repeating: 0, count: count) + while idx < count { out[idx] = Byte(ptr[idx]); idx += 1 } return out } } func splitAround(delimiter: String) throws -> (String, String?) { - let split = self.ccharArrayView().splitAround(delimiter: delimiter.ccharArrayView()) + let split = self.byteArrayView().splitAround(delimiter: delimiter.byteArrayView()) let first = try split.0.toString() if let second = split.1 { return (first, try second.stringView()) @@ -91,14 +91,14 @@ extension String { } } -extension Collection where Iterator.Element == CChar { +extension Collection where Iterator.Element == Byte { /// Splits string around a delimiter, returns the first subarray /// as the first return value (including the delimiter) and the rest /// as the second, if found (empty array if found at the end). /// Otherwise first array contains the original /// collection and the second is nil. - func splitAround(delimiter: [CChar]) -> ([CChar], [CChar]?) { + func splitAround(delimiter: [Byte]) -> ([Byte], [Byte]?) { let orig = Array(self) let end = orig.endIndex diff --git a/Sources/Redbird/Formatters.swift b/Sources/Redbird/Formatters.swift index e714383..1dc4eb8 100644 --- a/Sources/Redbird/Formatters.swift +++ b/Sources/Redbird/Formatters.swift @@ -89,7 +89,7 @@ struct BulkStringFormatter: Formatter { let content = (object as! RespBulkString).content //first count the number of bytes of the string - let byteCount = content.ccharArrayView().count + let byteCount = content.byteArrayView().count //format the outgoing string let prefix = String(byteCount) diff --git a/Sources/Redbird/Parsers.swift b/Sources/Redbird/Parsers.swift index 9d1fb1d..85f889a 100644 --- a/Sources/Redbird/Parsers.swift +++ b/Sources/Redbird/Parsers.swift @@ -10,12 +10,12 @@ protocol Parser { /// takes already read chars and the reader, returns the parsed response /// object and the read, but unused trailing characters - func parse(_ alreadyRead: [CChar], reader: SocketReader) throws -> (RespObject, [CChar]) + func parse(_ alreadyRead: [Byte], reader: SocketReader) throws -> (RespObject, [Byte]) } extension Parser { - func ensureReadElements(min: Int, alreadyRead: [CChar], reader: SocketReader) throws -> [CChar] { + func ensureReadElements(min: Int, alreadyRead: [Byte], reader: SocketReader) throws -> [Byte] { precondition(min > 0) @@ -39,7 +39,7 @@ extension Parser { /// to the specific parser. struct InitialParser: Parser { - func parse(_ alreadyRead: [CChar], reader: SocketReader) throws -> (RespObject, [CChar]) { + func parse(_ alreadyRead: [Byte], reader: SocketReader) throws -> (RespObject, [Byte]) { let read = try self.ensureReadElements(min: 1, alreadyRead: alreadyRead, reader: reader) @@ -64,7 +64,7 @@ struct InitialParser: Parser { /// Parses the Error type struct ErrorParser: Parser { - func parse(_ alreadyRead: [CChar], reader: SocketReader) throws -> (RespObject, [CChar]) { + func parse(_ alreadyRead: [Byte], reader: SocketReader) throws -> (RespObject, [Byte]) { let (head, tail) = try reader.readUntilDelimiter(alreadyRead: alreadyRead, delimiter: RespTerminator) let readString = try head.stringView() @@ -77,7 +77,7 @@ struct ErrorParser: Parser { /// Parses the SimpleString type struct SimpleStringParser: Parser { - func parse(_ alreadyRead: [CChar], reader: SocketReader) throws -> (RespObject, [CChar]) { + func parse(_ alreadyRead: [Byte], reader: SocketReader) throws -> (RespObject, [Byte]) { let (head, tail) = try reader.readUntilDelimiter(alreadyRead: alreadyRead, delimiter: RespTerminator) let readString = try head.stringView() @@ -90,7 +90,7 @@ struct SimpleStringParser: Parser { /// Parses the Integer type struct IntegerParser: Parser { - func parse(_ alreadyRead: [CChar], reader: SocketReader) throws -> (RespObject, [CChar]) { + func parse(_ alreadyRead: [Byte], reader: SocketReader) throws -> (RespObject, [Byte]) { let (head, tail) = try reader.readUntilDelimiter(alreadyRead: alreadyRead, delimiter: RespTerminator) let readString = try head.stringView() @@ -103,7 +103,7 @@ struct IntegerParser: Parser { /// Parses the BulkString type struct BulkStringParser: Parser { - func parse(_ alreadyRead: [CChar], reader: SocketReader) throws -> (RespObject, [CChar]) { + func parse(_ alreadyRead: [Byte], reader: SocketReader) throws -> (RespObject, [Byte]) { //first parse the number of string bytes let (head, maybeTail) = try reader.readUntilDelimiter(alreadyRead: alreadyRead, delimiter: RespTerminator) @@ -132,8 +132,8 @@ struct BulkStringParser: Parser { //in such case, split the tail into prefixTail, which we'll use //and suffixTail that we'll pass along - let neededChars: [CChar] - let suffixTail: [CChar] + let neededChars: [Byte] + let suffixTail: [Byte] if bytesToRead > 0 { //we need to read further chars @@ -155,7 +155,7 @@ struct BulkStringParser: Parser { // Parses the Array type struct ArrayParser: Parser { - func parse(_ alreadyRead: [CChar], reader: SocketReader) throws -> (RespObject, [CChar]) { + func parse(_ alreadyRead: [Byte], reader: SocketReader) throws -> (RespObject, [Byte]) { //first parse the number of string bytes let (head, maybeTail) = try reader.readUntilDelimiter(alreadyRead: alreadyRead, delimiter: RespTerminator) diff --git a/Sources/Redbird/Redbird.swift b/Sources/Redbird/Redbird.swift index 3319bae..208318e 100644 --- a/Sources/Redbird/Redbird.swift +++ b/Sources/Redbird/Redbird.swift @@ -18,6 +18,8 @@ public struct RedbirdConfig { } } +public typealias Byte = UInt8 + ///Redis client object public class Redbird { @@ -153,7 +155,7 @@ public class Pipeline: Redbird { //delegate reading to parsers let reader: SocketReader = self.socket - var leftovers = [CChar]() + var leftovers = [Byte]() var responses = [RespObject]() for _ in self.commands { //try to parse the string into a Resp object, fail if no parser accepts it diff --git a/Tests/Redbird/ParsingTests.swift b/Tests/Redbird/ParsingTests.swift index 2165712..1b28b03 100644 --- a/Tests/Redbird/ParsingTests.swift +++ b/Tests/Redbird/ParsingTests.swift @@ -11,13 +11,13 @@ import XCTest class TestReader: SocketReader { - var content: [CChar] + var content: [Byte] init(content: String) { - self.content = content.ccharArrayView() + self.content = content.byteArrayView() } - func read(bytes: Int) throws -> [CChar] { + func read(bytes: Int) throws -> [Byte] { precondition(bytes > 0) let toReadCount = min(bytes, self.content.count) @@ -44,7 +44,7 @@ class ParsingTests: XCTestCase { func testParsingError_FirstReadChar() { let reader = TestReader(content: "WAAAT unknown command 'BLAH'\r\n") - let (obj, leftovers) = try! InitialParser().parse("-".ccharArrayView(), reader: reader) + let (obj, leftovers) = try! InitialParser().parse("-".byteArrayView(), reader: reader) XCTAssertEqual(obj.respType, RespType.Error) XCTAssertEqual(leftovers, []) let err = obj as! RespError @@ -70,7 +70,7 @@ class ParsingTests: XCTestCase { let reader = TestReader(content: "+OK\r\nleftover") let (obj, leftovers) = try! InitialParser().parse([], reader: reader) XCTAssertEqual(obj.respType, RespType.SimpleString) - XCTAssertEqual(leftovers, "leftover".ccharArrayView()) + XCTAssertEqual(leftovers, "leftover".byteArrayView()) let simpleString = obj as! RespSimpleString XCTAssertEqual(simpleString.content, "OK") } @@ -101,7 +101,7 @@ class ParsingTests: XCTestCase { let reader = TestReader(content: "$6\r\nfoobar\r\nleftover") let (obj, leftovers) = try! InitialParser().parse([], reader: reader) XCTAssertEqual(obj.respType, RespType.BulkString) - XCTAssertEqual(leftovers, "leftover".ccharArrayView()) + XCTAssertEqual(leftovers, "leftover".byteArrayView()) let bulkString = obj as! RespBulkString XCTAssertEqual(bulkString.content, "foobar") } diff --git a/Tests/Redbird/RedbirdTests.swift b/Tests/Redbird/RedbirdTests.swift index cfd5cae..7950b93 100644 --- a/Tests/Redbird/RedbirdTests.swift +++ b/Tests/Redbird/RedbirdTests.swift @@ -193,7 +193,7 @@ class GoodSocket: Socket { func close() {} - func read(bytes: Int) throws -> [CChar] { + func read(bytes: Int) throws -> [Byte] { return try self.testReader.read(bytes: bytes) } @@ -210,7 +210,7 @@ class DeadSocket: Socket { func close() {} - func read(bytes: Int) throws -> [CChar] { + func read(bytes: Int) throws -> [Byte] { return [] } @@ -227,7 +227,7 @@ class ReconnectableSocket: Socket { func close() {} - func read(bytes: Int) throws -> [CChar] { + func read(bytes: Int) throws -> [Byte] { return [] } diff --git a/Tests/Redbird/StringTests.swift b/Tests/Redbird/StringTests.swift index df67a75..6488a8d 100644 --- a/Tests/Redbird/StringTests.swift +++ b/Tests/Redbird/StringTests.swift @@ -65,10 +65,10 @@ class StringTests: XCTestCase { XCTAssertTrue(does) } - func testCCharArrayView() { + func testByteArrayView() { - let chars = "Yol lo".ccharArrayView() - let exp = [89, 111, 108, 32, 108, 111].map { CChar($0) } + let chars = "Yol lo".byteArrayView() + let exp: [Byte] = [89, 111, 108, 32, 108, 111] XCTAssertEqual(chars, exp) } diff --git a/Tests/Redbird/XCTestManifests.swift b/Tests/Redbird/XCTestManifests.swift index ecbc24a..69df4a6 100644 --- a/Tests/Redbird/XCTestManifests.swift +++ b/Tests/Redbird/XCTestManifests.swift @@ -94,7 +94,7 @@ extension StringTests { ("testHasPrefix", testHasPrefix), ("testHasSuffix", testHasSuffix), ("testContainsCharacter", testContainsCharacter), - ("testCCharArrayView", testCCharArrayView), + ("testByteArrayView", testByteArrayView), ("testSplitAround_NotFound", testSplitAround_NotFound), ("testSplitAround_Middle", testSplitAround_Middle), ("testSplitAround_Start", testSplitAround_Start),