> On 3 Sep 2016, at 14:52, Quincey Morris <quinceymor...@rivergatesoftware.com> > wrote: > > 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.
I tried: let a = “ü” //(u with Umlaut) print(“\(a) has \(a.characters.count) characters, \(a.unicodeScalars.count) unicodeScalars, \(a.utf16.count) utf16") → ü has 1 characters, 1 unicodeScalars, 1 utf16 let nsa = a as NSString print(“\(a) as NSString = \”\(nsa)\” with length \(nsa.length)") → ü as NSString = "ü" with length 1 let scom1 = String(format: "%9s", nsa.UTF8String) print(“\(a) formatted %9s = \"\(scom1)\" with \(scom1.characters.count) characters, \(scom1.unicodeScalars.count) unicodeScalars, \(scom1.utf16.count) utf16") → ü formatted %9s = " √º" with 9 characters, 9 unicodeScalars, 9 utf16 That is: even for Roman scripts (but not plain Ascii) UTF8String does not work in Swift. I seem to remember that this used to work ok in Objective-C. Just tried it: NSString *str = @“ü”; NSLog(@"NSLog \"%9s\"", str.UTF8String); → NSLog “ √º” (same garbage as in Swift) fprintf(stderr,"fprintf = \"%9s\"\n", str.UTF8String); → fprintf = “ ü” (correct) [NSString stringWithFormat: str.UTF8String] has the same garbage. This is kind of worrying. By the way: what I really wanted to do is: let s = String(format: “%9d”, someString.endIndex ) but: error: argument type 'Index' (aka 'String.CharacterView.Index') does not conform to expected type 'CVarArgType' So I came up with let s = String( someString.endIndex ) and print this with %9s, which works ok as “s” is plain Ascii. _______________________________________________ Cocoa-dev mailing list (Cocoa-dev@lists.apple.com) 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: https://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com This email sent to arch...@mail-archive.com