[swift-users] CharacterSet vs Set<Character>

Jean-Denis Muys jdmuys at gmail.com
Mon Oct 3 04:28:24 CDT 2016


You are perfectly right. The context is playing around really, but I was more specifically writing a function counting vowels and consonants in an arbitrary string:

func countLetters(s: String) -> (vowels: Int, consonants: Int) {
    let vowels: Set<Character> = Set("AEIOU".characters)
    let consonants: Set<Character> = Set("BCDFGHJKLMNPQRSTVWXYZ".characters)

    var v = 0, c = 0

    for char in s.uppercased().characters {
        if vowels.contains(char) {
            v += 1
        }
        if consonants.contains(char) {
            c += 1
        }
    }

    return (v, c)
}

As you could see, I opted not to use CharacterSet for this case, as it looked too much trouble. 

The current goal is for me to learn Swift. Trying to extrapolate a bit on what might happen in the real world, I would tend to answer your questions thus:

> * Do you plan to use a fixed character set?  Or is the character set itself built at runtime?

The character set is likely to be fixed. Does this really change anything?

> * Do you have specific knowledge of either of the inputs?  Like that they’re all ASCII?  Or normalised in a certain way?

ASCII? Probably not. Latin? perhaps, though not obvious. For example French accented letters would probably have to be handled somehow. Greek or Cyrillic? Perhaps. Other scripts? Unlikely, but what do I know.
Normalisation: it should probably consider all variations of “é” to be the same…
Is this opening a Unicode can of worms? Possibly. I am not knowledgeable enough, but willing to learn.

> * Specifically, where do the characters you’re trying to test (`char` in your example) come from?  Do they represent user input, in which case they can be arbitrary Unicode?  Or something more constrained


User input most probably.

I tried and your suggestion:

> let uchar: UnicodeScalar = “E"

will not work with a Character variable (as opposed to a character literal)

        let uchar: UnicodeScalar = char

(Cannot convert value of type Character to specified type UnicodeScalar)

Thanks,

Jean-Denis 


> On 3 Oct 2016, at 09:43, Quinn The Eskimo! via swift-users <swift-users at swift.org> wrote:
> 
> 
> On 2 Oct 2016, at 19:02, Jean-Denis Muys via swift-users <swift-users at swift.org> wrote:
> 
>> The problem is, I could not find a simple way to convert from a character to a unicodeScalar. 
> 
> As is often the case with string examples, it would help if you posted more about your context.  With the details we have now your code could be written like this:
> 
> let vowels = CharacterSet(charactersIn: "AEIOU")
> let char: UnicodeScalar = "E"
> vowels.contains(char)
> 
> but I’m pretty sure that won’t help in your real app (-:  So, my questions:
> 
> * Do you plan to use a fixed character set?  Or is the character set itself built at runtime?
> 
> * Do you have specific knowledge of either of the inputs?  Like that they’re all ASCII?  Or normalised in a certain way?
> 
> * Specifically, where do the characters you’re trying to test (`char` in your example) come from?  Do they represent user input, in which case they can be arbitrary Unicode?  Or something more constrained
> 
> Share and Enjoy
> --
> Quinn "The Eskimo!"                    <http://www.apple.com/developer/>
> Apple Developer Relations, Developer Technical Support, Core OS/Hardware
> 
> 
> _______________________________________________
> swift-users mailing list
> swift-users at swift.org
> https://lists.swift.org/mailman/listinfo/swift-users

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.swift.org/pipermail/swift-users/attachments/20161003/4df7c145/attachment.html>


More information about the swift-users mailing list