Re: Swift: How to determine if a Character represents whitespace?

2015-04-06 Thread Roland King
I have no idea how a linguistic tagger determines whitespace and whether it uses the same definition for whitespace as NSCharacterSet does. Given that it's multi-language-aware I wouldn't be shocked to find it uses some entirely different way of enumerating textual elements. On 6 Apr 2015,

Re: Swift: How to determine if a Character represents whitespace?

2015-04-03 Thread Charles Jenkins
I imagine you’re right, that they’re NString indexes packaged up into a frustrating return type. After sleeping on it, though, I imagined that even if complex grapheme clusters WERE to make count( attrStr.string ) return a different result than attrStr.length, it would probably never be due to

Re: Swift: How to determine if a Character represents whitespace?

2015-04-03 Thread Marco S Hyman
extension Character { func isMemberOfSet( set:NSCharacterSet ) - Bool { // The for loop only executes once; // its purpose is to convert Character to a type // you can actually do something with for char in String( self ).utf16 { if set.characterIsMember(

Re: Swift: How to determine if a Character represents whitespace?

2015-04-03 Thread Quincey Morris
On Apr 3, 2015, at 04:00 , Charles Jenkins cejw...@gmail.com wrote: for char in String( self ).utf16 { if set.characterIsMember( char ) { return true } Now we’re back to the place we started. This code is wrong. It fails for any code point that isn’t representable a

Re: Swift: How to determine if a Character represents whitespace?

2015-04-03 Thread Marco S Hyman
On Apr 3, 2015, at 11:04 AM, Quincey Morris quinceymor...@rivergatesoftware.com wrote: On Apr 3, 2015, at 04:00 , Charles Jenkins cejw...@gmail.com wrote: for char in String( self ).utf16 { if set.characterIsMember( char ) { return true } Now we’re back to the

Re: Swift: How to determine if a Character represents whitespace?

2015-04-03 Thread Quincey Morris
On Apr 3, 2015, at 11:19 , Marco S Hyman m...@snafu.org wrote: The original code will return true only if all code points map to white space. The “failure” I was talking about is something a bit different. It has two problems: 1. For Unicode code points that are represented by 2 code values,

Re: Swift: How to determine if a Character represents whitespace?

2015-04-03 Thread Charles Jenkins
So my Character.isMemberOfSet() is a poor general-purpose method, and I need to ditch it. I like your code. I had to modify it a bit so it wouldn’t fall on strings composed entirely of whitespace: let testString =     \n\n \t\t let attrStr = NSAttributedString( string:testString ) let str = 

Re: Swift: How to determine if a Character represents whitespace?

2015-04-03 Thread Quincey Morris
On Apr 3, 2015, at 13:18 , Charles Jenkins cejw...@gmail.com wrote: is there a way in the playground for use to test addresses to make sure attrStr.string as NSString doesn’t perform a copy? I doubt it. This is the best I could come up with in a couple of minutes: import Cocoa let

Re: Swift: How to determine if a Character represents whitespace?

2015-04-03 Thread Roland King
ok here’s my try, assuming NSLinguisticTagger knows what it’s doing. And yes it’s a bit stupid to use a linguistic tagger to do something like that but .. whatever var str = Some String WIth Whitespace var lt = NSLinguisticTagger( tagSchemes: [NSLinguisticTagSchemeTokenType], options: 0 )

Re: Swift: How to determine if a Character represents whitespace?

2015-04-02 Thread Quincey Morris
On Apr 2, 2015, at 19:28 , Charles Jenkins cejw...@gmail.com wrote: I can indeed call attrStr.string.rangeOfCharacterFromSet(). But in typical Swift string fashion, the return type is as unfriendly as possible: RangeString.Index? — as if the NSString were a Swift string. I finally read the

Re: Swift: How to determine if a Character represents whitespace?

2015-04-02 Thread Quincey Morris
On Apr 2, 2015, at 19:28 , Charles Jenkins cejw...@gmail.com wrote: So after doing two anchored searches, one at the beginning and one at the end of the string, if I get two different ranges, I’m stuck with two values that aren’t subtractable to determine the length of the NSRange I need in

Re: Swift: How to determine if a Character represents whitespace?

2015-04-02 Thread Charles Jenkins
Amen, brother. Given my attributed string “attrStr,” I can indeed call attrStr.string.rangeOfCharacterFromSet(). But in typical Swift string fashion, the return type is as unfriendly as possible: RangeString.Index? — as if the NSString were a Swift string. So after doing two anchored

Re: Swift: How to determine if a Character represents whitespace?

2015-04-02 Thread Charles Jenkins
I kept my original question as brief as I could, but let me tell you what problem I’m trying to solve, and maybe someone will have good advice I haven’t yet considered. I’m trying to code in pure Swift. I have an NSAttributedString which can potentially be very large, and I want to save off

Re: Swift: How to determine if a Character represents whitespace?

2015-04-02 Thread Uli Kusterer
On 02 Apr 2015, at 13:54, Charles Jenkins cejw...@gmail.com mailto:cejw...@gmail.com wrote: What would be nice is a way to count leading and trailing characters in place while the thing is still an NSAttributedString--without using NSAttributedString.string to convert to a Swift string in

Re: Swift: How to determine if a Character represents whitespace?

2015-04-02 Thread Ken Thomases
On Apr 2, 2015, at 6:54 AM, Charles Jenkins cejw...@gmail.com wrote: What would be nice is a way to count leading and trailing characters in place while the thing is still an NSAttributedString--without using NSAttributedString.string to convert to a Swift string in the first place.

Re: Swift: How to determine if a Character represents whitespace?

2015-04-02 Thread Jens Alfke
On Apr 2, 2015, at 4:54 AM, Charles Jenkins cejw...@gmail.com wrote: What would be nice is a way to count leading and trailing characters in place while the thing is still an NSAttributedString--without using NSAttributedString.string to convert to a Swift string in the first place. If

Re: Swift: How to determine if a Character represents whitespace?

2015-04-02 Thread Charles Jenkins
The documentation certainly says that, Ken, but stick this code in a playground and see that you can’t examine the characters via index no matter whether you assume it to be String or NSString: let whitespaceSet = NSCharacterSet.whitespaceAndNewlineCharacterSet() let attrStr =

Re: Swift: How to determine if a Character represents whitespace?

2015-04-02 Thread Charles Jenkins
Oops. My documentation viewer was set up wrong. characterAtIndex() is indeed supposed to be available in Swift. Don’t know what I’ve done wrong that I can’t use it in a playground. --  Charles On April 2, 2015 at 10:18:00, Charles Jenkins (cejw...@gmail.com) wrote: The documentation

Re: Swift: How to determine if a Character represents whitespace?

2015-04-02 Thread Quincey Morris
On Apr 2, 2015, at 04:54 , Charles Jenkins cejw...@gmail.com wrote: Swift has a built-in func stringByTrimmingCharactersInSet(set: NSCharacterSet) - String There is something wacky going on here — or not. (I know you don’t want to use this particular method, but I’m just using it as an

Re: Swift: How to determine if a Character represents whitespace?

2015-04-01 Thread Quincey Morris
On Apr 1, 2015, at 21:17 , Charles Jenkins cejw...@gmail.com wrote: for ch in String(char).utf16 { if !set.characterIsMember(ch) { found = false } } Except that this code can’t possibly be right, in general. 1. A ‘unichar’ is a UTF-16 code value, but it’s not a Unicode code point.

Re: Swift: How to determine if a Character represents whitespace?

2015-04-01 Thread Charles Srstka
On Apr 1, 2015, at 8:14 PM, Charles Jenkins cejw...@gmail.com wrote: Given this code: let someCharacter = str[str.endIndex.predecessor()] How can I determine if someCharacter is whitespace? import Foundation func isChar(char: Character, inSet set: NSCharacterSet) - Bool { // this

Re: Swift: How to determine if a Character represents whitespace?

2015-04-01 Thread Charles Jenkins
Thank you very much. :-) I had been trying to figure out how to use NSCharacterSet, but I didn’t know the bit about converting to UTF-16 string first.     —  Charles On April 1, 2015 at 9:52:47 PM, Charles Srstka (cocoa...@charlessoft.com) wrote: On Apr 1, 2015, at 8:14 PM, Charles Jenkins