-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: Piotr Piotrowski <piotr@synadia.com>
- Loading branch information
Showing
4 changed files
with
274 additions
and
261 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
// | ||
// Data+Parser.swift | ||
// NatsSwift | ||
// | ||
|
||
import Foundation | ||
|
||
extension Data { | ||
func removePrefix(_ prefix: Data) -> Data { | ||
guard self.starts(with: prefix) else { return self } | ||
return self.dropFirst(prefix.count) | ||
} | ||
|
||
func getMessageType() -> NatsOperation? { | ||
guard self.count > 2 else { return nil } | ||
for operation in NatsOperation.allOperations() { | ||
if self.starts(with: operation.rawBytes) { | ||
return operation | ||
} | ||
} | ||
return nil | ||
} | ||
|
||
func starts(with bytes: [UInt8]) -> Bool { | ||
guard self.count >= bytes.count else { return false } | ||
return self.prefix(bytes.count).elementsEqual(bytes) | ||
} | ||
|
||
func parseOutMessages() -> [Data] { | ||
var messages = [Data]() | ||
let crlf = Data([UInt8(ascii: "\r"), UInt8(ascii: "\n")]) | ||
var startIndex = self.startIndex | ||
|
||
while startIndex < self.endIndex, let range = self[startIndex...].range(of: crlf) { | ||
let lineEndIndex = range.lowerBound | ||
let nextLineStartIndex = self.index(range.upperBound, offsetBy: 0, limitedBy: self.endIndex) ?? self.endIndex | ||
|
||
let lineData = self[startIndex..<lineEndIndex] | ||
|
||
if let messageType = lineData.getMessageType() { | ||
if messageType == .message { | ||
// For .message type, include this line and the following line | ||
if nextLineStartIndex < self.endIndex, let nextLineRange = self[nextLineStartIndex...].range(of: crlf) { | ||
let nextLineEndIndex = nextLineRange.lowerBound | ||
let messageData = self[startIndex..<nextLineEndIndex] | ||
messages.append(messageData) | ||
startIndex = self.index(nextLineRange.upperBound, offsetBy: 0, limitedBy: self.endIndex) ?? self.endIndex | ||
continue | ||
} | ||
} else { | ||
// For other types, include only this line | ||
messages.append(lineData) | ||
} | ||
} | ||
|
||
// Move to the start of the next line | ||
startIndex = nextLineStartIndex | ||
} | ||
|
||
return messages | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.