fjcaetano / NSStringMask

NSStringMask allows you to apply masks or formats to NSStrings using NSRegularExpression to input your format.
http://fjcaetano.github.io/NSStringMask/
MIT License
241 stars 36 forks source link

Terminated due to memory issue on version 1.2.2 #17

Open evertoncunha opened 7 years ago

evertoncunha commented 7 years ago

I'm getting "terminated due to memory issue", on version 1.2.2. It's working fine on v1.2.1.

static func maskTitle(readable: String) -> String {
    //let pattern = "(\\d{5}\\.\\d{5} )(\\d{5}\\.\\d{6} )(\\d{5}\\.\\d{6} )(\\d{1} ).*"

    var mask: String = ""

    let count = readable.characters.count
    if count >= 6 {
      mask += "(\\d{5})\\."
    }
    if count >= 11 {
      mask += "(\\d{5}) "
    }
    if count >= 16 {
      mask += "(\\d{5})\\."
    }
    if count >= 22 {
      mask += "(\\d{6}) "
    }
    if count >= 27 {
      mask += "(\\d{5})\\."
    }
    if count >= 33 {
      mask += "(\\d{6}) "
    }
    if count >= 34 {
      mask += "(\\d{1}) "
    }
    mask += "(.*)"

    return NSStringMask.maskString(readable, withPattern: mask) ?? ""
  }

You can try to pass any value, like maskTitle(readable: "1")

fjcaetano commented 7 years ago

What are you trying to accomplish with the (.*) at the end? Could you provide an example of a valid formatted string for your patter?

evertoncunha commented 7 years ago

It's a pattern for 🇧🇷 barcode payments while you type. Example: 55555 55555.5 55555.55555 55555.55555 5 55555.55555 55555.666666 55555.666666 1 00000000000014

The (.*) at the end is to accept any number after any case of the pattern.

fjcaetano commented 7 years ago

You really shouldn't have a group that captures and formats without a limited count. It does fall into an infinite loop in the do-while in [NSStringMask validCharactersForString:(NSString*)]. Could you try limiting the characters captured by that (.*)? Also, by using the dot-star pattern, you'd be capturing any characters, not only digits, spaces and dots.

Perhaps there should be a warning or some other verification at execution time for unlimited repetitions patterns.

evertoncunha commented 7 years ago

I do a "numbers only" filter previous this function. But the strange thing is that it works fine at version 1.2.1. I'll stick with the previous version then 🤔 .

fjcaetano commented 7 years ago

That is super weird. I couldn't see anything obvious that might have broken it. I'll get back to this when I get the chance, so I'll leave your issue open for now.