complete use of parser
This commit is contained in:
parent
8bae7d06d2
commit
fb9b19820b
@ -16,7 +16,7 @@ class SocketParserTest: XCTestCase {
|
||||
"2/swift,[\"testArrayEmitReturn\",[\"test3\",\"test4\"]]": ("/swift", ["testArrayEmitReturn", ["test3", "test4"]], [], -1),
|
||||
"51-/swift,[\"testMultipleItemsWithBufferEmitReturn\",[1,2],{\"test\":\"bob\"},25,\"polo\",{\"_placeholder\":true,\"num\":0}]": ("/swift", ["testMultipleItemsWithBufferEmitReturn", [1, 2], ["test": "bob"], 25, "polo", "~~0"], [], -1),
|
||||
"3/swift,0[[\"test3\",\"test4\"]]": ("/swift", [["test3", "test4"]], [], 0),
|
||||
"61-/swift,9[[1,2],{\"test\":\"bob\"},25,\"polo\",{\"_placeholder\":true,\"num\":0}]": ("/swift", [ [1, 2], ["test": "bob"], 25, "polo", "~~0"], [], 9),
|
||||
"61-/swift,19[[1,2],{\"test\":\"bob\"},25,\"polo\",{\"_placeholder\":true,\"num\":0}]": ("/swift", [ [1, 2], ["test": "bob"], 25, "polo", "~~0"], [], 19),
|
||||
"4/swift,": ("/swift", [], [], -1),
|
||||
"10": ("/swift", [], [], -1)]
|
||||
|
||||
@ -46,7 +46,7 @@ class SocketParserTest: XCTestCase {
|
||||
}
|
||||
|
||||
func testNameSpaceBinaryAckParse() {
|
||||
let message = "61-/swift,9[[1,2],{\"test\":\"bob\"},25,\"polo\",{\"_placeholder\":true,\"num\":0}]"
|
||||
let message = "61-/swift,19[[1,2],{\"test\":\"bob\"},25,\"polo\",{\"_placeholder\":true,\"num\":0}]"
|
||||
validateParseResult(message)
|
||||
}
|
||||
|
||||
|
||||
@ -122,9 +122,7 @@ class SocketParser {
|
||||
return SocketPacket(type: type, nsp: "/")
|
||||
}
|
||||
|
||||
var id: Int?
|
||||
var nsp:String?
|
||||
var i = 0
|
||||
var placeholders = -1
|
||||
|
||||
if type == .BinaryEvent || type == .BinaryAck {
|
||||
@ -134,41 +132,34 @@ class SocketParser {
|
||||
NSLog("Error parsing \(str)")
|
||||
return nil
|
||||
}
|
||||
|
||||
i = parser.currentIndex - 1
|
||||
}
|
||||
if parser.currentCharacter == "/" {
|
||||
nsp = parser.readUntilStringOccurence(",")
|
||||
i = parser.currentIndex
|
||||
parser.currentIndex++
|
||||
}
|
||||
|
||||
if parser.currentIndex + 1 >= parser.messageCharacters.count {
|
||||
return SocketPacket(type: type, id: id ?? -1,
|
||||
if parser.currentIndex >= parser.messageCharacters.count {
|
||||
return SocketPacket(type: type, id: -1,
|
||||
nsp: nsp ?? "/", placeholders: placeholders)
|
||||
}
|
||||
|
||||
let next = String(messageCharacters[i + 1])
|
||||
|
||||
if Int(next) != nil {
|
||||
var c = ""
|
||||
while ++i < messageCharacters.count {
|
||||
if let int = Int(String(messageCharacters[i])) {
|
||||
c += String(int)
|
||||
var idString = ""
|
||||
while parser.currentIndex < messageCharacters.count {
|
||||
if let next = parser.read(1), let int = Int(next) {
|
||||
idString += String(int)
|
||||
} else {
|
||||
--i
|
||||
parser.currentIndex -= 2
|
||||
break
|
||||
}
|
||||
}
|
||||
|
||||
id = Int(c)
|
||||
}
|
||||
|
||||
if ++i < messageCharacters.count {
|
||||
let d = str[str.startIndex.advancedBy(i)...str.startIndex.advancedBy(str.characters.count-1)]
|
||||
if parser.currentIndex < messageCharacters.count {
|
||||
let d = str[str.startIndex.advancedBy(parser.currentIndex + 1)...str.startIndex.advancedBy(str.characters.count-1)]
|
||||
let noPlaceholders = d["(\\{\"_placeholder\":true,\"num\":(\\d*)\\})"] ~= "\"~~$2\""
|
||||
let data = SocketParser.parseData(noPlaceholders) as? [AnyObject] ?? [noPlaceholders]
|
||||
|
||||
return SocketPacket(type: type, data: data, id: id ?? -1,
|
||||
return SocketPacket(type: type, data: data, id: Int(idString) ?? -1,
|
||||
nsp: nsp ?? "/", placeholders: placeholders)
|
||||
}
|
||||
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user