iswiftapp / iswift

Objective-C to Swift Converter
30 stars 3 forks source link

String Formatting #161

Closed charlieMonroe closed 8 years ago

charlieMonroe commented 8 years ago

Thanks for fixing the precision formatting, however, there are more format modifiers:

@implementation ABC

-(void)xy {
    NSString *statementID = [NSString stringWithFormat:@"%05li", (unsigned long)[file bankStatementID]];
}

@end

Currently gives:

class ABC {
    func xy() {
        let statementID = "%05li"
    }
}

Discarding the formatting completely. As mentioned before, I believe there should be a check of the number of formatting tokens in the format string and the number of arguments of the formatting method - and if they don't match, either throw an error, or fall back to the String(format: ...) initializer.

drkameleon commented 8 years ago

Hmm... I know what you mean...

Basically, the regex matching "precision" currently is: %[0-9]?\.?[0-9]+[fg] (meaning: which are going to fall bac to the String(format: ...) initializer.

Suggestions? (Apart from the example you mentioned I mean...)

charlieMonroe commented 8 years ago

Well, this isn't precision per sea, it's a padding formatting. Generally speaking C-style formatting can get really wild :)

See http://stackoverflow.com/a/29403060/1800936

According to this person, the regex for a placeholder is pretty insane.

For now, though, I believe that changing the regex to %[0-9.]+l?[fgid] should be sufficient. But this is based on my needs - there certainly are people using much more complex formatting.

I can't see into your code, so I don't know if it's possible, but when converting such an expression, I'd suggest:

drkameleon commented 8 years ago

Well, this isn't precision per sea, it's a padding formatting.

Well, true... lol.


Thanks a lot for your input. I'm looking into it right now. ;-)

drkameleon commented 8 years ago

Marking the issue as fixed of the upcoming 2.4 - but I'll guess we'll have to re-visit. But... what can I say... that's what I'm here for... ;-)


Example:

NSString *statementID = [NSString stringWithFormat:@"%05li", (unsigned long)[file bankStatementID]];

Output:

var statementID: String = String(format: "%05li", CUnsignedLong(file.bankStatementID()))