Search code examples
unicodecharacterrebolrebol3codepoint

Comparing characters in Rebol 3


I am trying to compare characters to see if they match. I can't figure out why it doesn't work. I'm expecting true on the output, but I'm getting false.

character: "a"
word: "aardvark"

(first word) = character ; expecting true, getting false

Solution

  • So "a" in Rebol is not a character, it is actually a string.

    A single unicode character is its own independent type, with its own literal syntax, e.g. #"a". For example, it can be converted back and forth from INTEGER! to get a code point, which the single-letter string "a" cannot:

    >> to integer! #"a"
    == 97
    
    >> to integer! "a"  
    ** Script error: cannot MAKE/TO integer! from: "a"
    ** Where: to
    ** Near: to integer! "a"
    

    A string is not a series of one-character STRING!s, it's a series of CHAR!. So what you want is therefore:

    character: #"a"
    word: "aardvark"
    
    (first word) = character ;-- true!
    

    (Note: Interestingly, binary conversions of both a single character string and that character will be equivalent:

    >> to binary! "μ"
    == #{CEBC}
    
    >> to binary! #"μ"
    == #{CEBC}
    

    ...those are UTF-8 byte representations.)