0

I would like to split an extremely large string, up to 8mb, in 64kb chunks. At the moment I am using the following code:

//1
var regData:String= "string up to 8mb"
var count=((countElements(self.regData!))/65536)

//2
for var index = 0; index < count; ++index {
    arr.append(self.regData!.substringWithRange(Range<String.Index>(start: advance(self.regData!.startIndex, 0),end: advance(self.regData!.startIndex, 65536))))
    self.regData!.removeRange(Range<String.Index>(start: self.regData!.startIndex, end:advance(self.regData!.startIndex, 65536)))
    println(index)
 }
//3
println("exit loop")
arr.append(self.regData!)
  1. I calculate how many 64 kb chunks I have.
  2. In the for loop I get the first 64kb. I collect them in an array. Now I have to delete the first 64kb strings because of step 3.
  3. If I I have less than 64kb I get an error in my loop. Therefore my last step is outside the loop.

The code works fine, but it is extremely slow. I need to speed up my code. Do you have any idea how to do it.

Thanks at all.

2
  • what is your reason to not use NSStream ? Commented Feb 17, 2015 at 10:39
  • I am new in ios programming :D... I have to look at NSStream Commented Feb 17, 2015 at 10:40

1 Answer 1

1

It might be more effective if you don't modify the original string, and just use two indices (from and to) to traverse through the string:

let regData = "string up to 8mb"
let chunkSize = 65536

var array = [String]()
var from = regData.startIndex // start of current chunk
let end = regData.endIndex    // end of string
while from != end {
    // advance "from" by "chunkSize", but not beyond "end":
    let to = from.advancedBy(chunkSize, limit: end)
    array.append(regData.substringWithRange(from ..< to))
    from = to
}

Note that this gives substrings of 65536 characters. Since a Swift character represents a "Unicode grapheme cluster", this will not correspond to 64kB of data. If you need that then you should convert the string to NSData and split that into chunks.

(Updated for Swift 2.)

Sign up to request clarification or add additional context in comments.

2 Comments

I will try this and give a feedback :D. Thanks
Thanks this is very fast :D... Can you give me an example how to handle with NSStream?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.