Re: formatted strings in Swift
Re: formatted strings in Swift
- Subject: Re: formatted strings in Swift
- From: Quincey Morris <email@hidden>
- Date: Sat, 03 Sep 2016 00:52:31 -0700
- Feedback-id: 167118m:167118agrif8a:167118snOXEO4QuZ:SMTPCORP
On Sep 3, 2016, at 00:28 , Gerriet M. Denkmann <email@hidden> wrote:
>
> I.e. the size parameter 9 seems to be ignored. (I kind of remember having the same problem in Objective-C).
Yes, it’s an issue with %@, which is a non-IEEE specifier, unlike %s, which is. (http://pubs.opengroup.org/onlinepubs/009695399/functions/printf.html) %@ actually means to insert the result of ‘[argument description]’, which could be any length of string describing any kind of object, so perhaps Apple decided that it made no particular sense to try to jam it in a fixed width.
The other issue I can think of would be how to interpret the field width, since “real” strings can be counted in different ways. Would %9@ mean 9 UTF-16 code units? 9 Unicode code points? 9 grapheme clusters?
There’s probably a similar ambiguity with %9s. It apparently means 9 bytes from/including a C-string, which is not necessarily 9 of anything in a UTF-8 string. Since you work with non-Roman character sets, you’ll probably run into this eventually, if you’re padding strings with spaces.
> let scom1 = String(format: "%9s", ("a" as NSString).UTF8String)
It’s not important, but it’s UTF8String because you’re presumably using Xcode 7.3. I tried it in Xcode 8 beta 6, where the name translation conventions for imported Obj-C SDK symbols are different. In Swift 3, the Obj-C method comes out as utf8String. (And, just for extra fun, the String version of the method comes out at utf8CString, and has a different type that doesn’t work as a vararg.)
Just a few of the many joys of the upcoming Swift 3 transition.
_______________________________________________
Cocoa-dev mailing list (email@hidden)
Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden