Latest 1.1.0
Homepage https://github.com/s-aska/MutableDataScanner
License MIT
Platforms ios 8.0, osx 10.10, requires ARC
Authors

Build Status
Carthage compatible
[]()
[]()
[]()

A simple text scanner which can parse NSMutableData using delimiter.

Faster because it does not have to do a NSData <-> String conversion.

It can be easily and reliably parse of the Twitter Streaming API and other stream.

Performance

Test: https://github.com/s-aska/MutableDataScanner/blob/master/MutableDataScannerTests/MutableDataScannerTests.swift

  • MutableDataScanner specific delimiter … average: 0.071
  • MutableDataScanner line delimiter ( CRLF or LF ) … average: 0.092
  • String#componentsSeparatedByString … average: 0.304

Usage

for Twitter Streaming API

See Processing streaming data for information about the Parsing responses you will receive from the streaming API.

import Foundation
import MutableDataScanner
import SwiftyJSON

class TwitterAPIStreamingRequest: NSObject, NSURLSessionDataDelegate {

    let scanner = MutableDataScanner(delimiter: "rn")

    // ...

    func connection(connection: NSURLConnection, didReceiveData data: NSData) {
        self.scanner.appendData(data)
        while let data = self.scanner.next() {
            if data.length > 0 {
                let json = JSON(data: data)
            }
        }
    }
}

Bytes

  • read(length: Int) -> NSData?
let data = "0123456789abcdefghijklmnopqrstuvwxyz".dataUsingEncoding(NSUTF8StringEncoding)!
let scanner = MutableDataScanner(data: data)

scanner.read(length: 10) // => 0123456789
scanner.read(length: 10) // => abcdefghij
scanner.read(length: 10) // => klmnopqrst
scanner.read(length: 10) // => uvwxyz
scanner.read(length: 10) // => (nil)
  • read(offset: Int, length: Int) -> NSData?
let data = "0123456789abcdefghijklmnopqrstuvwxyz".dataUsingEncoding(NSUTF8StringEncoding)!
let scanner = MutableDataScanner(data: data)

scanner.read(offset: 5, length: 5) // => 56789
scanner.read(offset: 5, length: 5) // => fghij
scanner.read(offset: 5, length: 5) // => pqrst
scanner.read(offset: 5, length: 5) // => z
scanner.read(offset: 5, length: 5) // => (nil)

Delimiter

Specify the delimiter in the properties.

  • init(data: NSMutableData = default, delimiter: NSData)
  • init(data: NSMutableData = default, delimiter: String)
  • next() -> NSData?
  • hasNext() -> Bool
let data = "0123456789nabcdefghijklmnopqrstuvwxyzn0123".dataUsingEncoding(NSUTF8StringEncoding)!
let scanner = MutableDataScanner(data: data, delimiter: "n")

scanner.data // => 0123456789nabcdefghijklmnopqrstuvwxyzn0123

scanner.hasNext() // => true
scanner.next() // => 0123456789
scanner.data // => abcdefghijklmnopqrstuvwxyzn0123

scanner.hasNext() // => true
scanner.next() // => abcdefghijklmnopqrstuvwxyz
scanner.data // => 0123

scanner.hasNext() // => false
scanner.next() // => (nil)

Specify the delimiter in the arguments.

  • next(delimiter: NSData) -> NSData?
  • hasNext(delimiter: NSData) -> Bool
  • next(delimiter: String) -> NSData?
  • hasNext(delimiter: String) -> Bool
let data = "0123456789nabcdefghijklmnopqrstuvwxyzn0123".dataUsingEncoding(NSUTF8StringEncoding)!
let scanner = MutableDataScanner(data: data)

scanner.data // => 0123456789nabcdefghijklmnopqrstuvwxyzn0123

scanner.hasNext("rn") // => false
scanner.hasNext("n") // => true
scanner.next("rn") // => (nil)
scanner.next("n") // => 0123456789
scanner.data // => abcdefghijklmnopqrstuvwxyzn0123

CRLF or LF

  • nextLine() -> NSData?
  • hasNextLine -> Bool
let data = "0123456789rnabcdefghijklmnopqrstuvwxyzn0123".dataUsingEncoding(NSUTF8StringEncoding)!
let scanner = MutableDataScanner(data: data)

scanner.data // => 0123456789rnabcdefghijklmnopqrstuvwxyzn0123

scanner.hasNextLine() // => true
scanner.nextLine() // => 0123456789
scanner.data // => abcdefghijklmnopqrstuvwxyzn0123

scanner.hasNextLine() // => true
scanner.nextLine() // => abcdefghijklmnopqrstuvwxyz
scanner.data // => 0123

scanner.hasNextLine() // => false
scanner.nextLine() // => (nil)

Requirements

  • iOS 8.0+ / Mac OS X 10.10+
  • Xcode 8+ / Swift 3+

Installation

Carthage

Add the following line to your Cartfile

github "s-aska/MutableDataScanner"

CocoaPods

Add the following line to your Podfile

use_frameworks!
pod 'MutableDataScanner'

License

MutableDataScanner is released under the MIT license. See LICENSE for details.

Latest podspec

{
    "name": "MutableDataScanner",
    "version": "1.1.0",
    "summary": "A simple text scanner which can parse NSMutableData using delimiter.",
    "description": "A simple text scanner which can parse NSMutableData using delimiter.nFaster because it does not have to do a NSData <-> String conversion.nIt can be easily and reliably parse of the Twitter Streaming API and other stream.",
    "homepage": "https://github.com/s-aska/MutableDataScanner",
    "license": "MIT",
    "authors": {
        "aska": "[email protected]"
    },
    "social_media_url": "https://twitter.com/su_aska",
    "source": {
        "git": "https://github.com/s-aska/MutableDataScanner.git",
        "tag": "1.1.0"
    },
    "platforms": {
        "ios": "8.0",
        "osx": "10.10"
    },
    "requires_arc": true,
    "source_files": "MutableDataScanner/*.swift",
    "pushed_with_swift_version": "3.0"
}

Pin It on Pinterest

Share This