PSA: Umlauts are not always what they seem to be

Updated . Posted . Visible to the public. Repeats.

When you have a string containing umlauts which don't behave as expected (are not matched with a regexp, can't be found with an SQL query, do not print correctly on LaTeX documents, etc), you may be encountering umlauts which are not actually umlaut characters.

They look, depending on the font, like their "real" umlaut counterpart:

  • ä ↔ ä
  • ö ↔ ö
  • ü ↔ ü

However, they are not the same:

'ä' == 'ä' # false
'ä'.size # 1
'ä'.size # 2

Looking at how those strings are constructed reveals what is going on:

'ä'.unpack('U*') #  [228]
'ä'.unpack('U*') # [97, 776]

As you can see, the 2nd representation is actually a combination of "a" (character code 97) and a negative-space unicode character which puts the dots on top of it Show archive.org snapshot .

This is terrible.

Ruby 2.2 is your friend

Ruby 2.2 brings String#unicode_normalize Show archive.org snapshot and its bang and query brothers.

'ä' == 'ä' # false 
'ä' == 'ä'.unicode_normalize # true 

Also see

The Big List of Naughty Strings Show archive.org snapshot is an evolving list of strings which have a high probability of causing issues when used as user-input data.

Arne Hartherz
Last edit
Michael Leimstädtner
Keywords
unicode, mimicry, utf-8
License
Source code in this card is licensed under the MIT License.
Posted by Arne Hartherz to makandra dev (2014-06-03 09:46)