Wait. So you consider commonPrefix returning malformed string to be fine? I have lost you here. For example, for code sample given above, output is: ========== Пи П[\D0] ========== Problem is if you use == on code unit you can match only part of valid symbol.