Skip to content

Commit

Permalink
changed type to byte from cchar
Browse files Browse the repository at this point in the history
  • Loading branch information
czechboy0 committed May 5, 2016
1 parent dbea4c6 commit 09fe3cb
Show file tree
Hide file tree
Showing 10 changed files with 43 additions and 41 deletions.
16 changes: 8 additions & 8 deletions Sources/Redbird/ClientSocket.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}
Expand Down Expand Up @@ -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
Expand All @@ -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 {
Expand Down Expand Up @@ -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()
Expand Down
4 changes: 2 additions & 2 deletions Sources/Redbird/Errors.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
12 changes: 6 additions & 6 deletions Sources/Redbird/Extensions.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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<CChar>(repeating: 0, count: count)
while idx < count { out[idx] = ptr[idx]; idx += 1 }
var out = Array<Byte>(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())
Expand All @@ -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
Expand Down
2 changes: 1 addition & 1 deletion Sources/Redbird/Formatters.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
20 changes: 10 additions & 10 deletions Sources/Redbird/Parsers.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand All @@ -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)

Expand All @@ -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()
Expand All @@ -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()
Expand All @@ -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()
Expand All @@ -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)
Expand Down Expand Up @@ -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
Expand All @@ -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)
Expand Down
4 changes: 3 additions & 1 deletion Sources/Redbird/Redbird.swift
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ public struct RedbirdConfig {
}
}

public typealias Byte = UInt8

///Redis client object
public class Redbird {

Expand Down Expand Up @@ -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
Expand Down
12 changes: 6 additions & 6 deletions Tests/Redbird/ParsingTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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
Expand All @@ -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")
}
Expand Down Expand Up @@ -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")
}
Expand Down
6 changes: 3 additions & 3 deletions Tests/Redbird/RedbirdTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}

Expand All @@ -210,7 +210,7 @@ class DeadSocket: Socket {

func close() {}

func read(bytes: Int) throws -> [CChar] {
func read(bytes: Int) throws -> [Byte] {
return []
}

Expand All @@ -227,7 +227,7 @@ class ReconnectableSocket: Socket {

func close() {}

func read(bytes: Int) throws -> [CChar] {
func read(bytes: Int) throws -> [Byte] {
return []
}

Expand Down
6 changes: 3 additions & 3 deletions Tests/Redbird/StringTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}

Expand Down
2 changes: 1 addition & 1 deletion Tests/Redbird/XCTestManifests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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),
Expand Down

0 comments on commit 09fe3cb

Please sign in to comment.