User Tools

Site Tools


Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revision Previous revision
reversing_a_string [2015/02/03 11:24]
will
reversing_a_string [2015/02/03 23:59] (current)
will Algorithm description and unicode issues.
Line 1: Line 1:
 ====== Reversing a string ====== ====== Reversing a string ======
  
-Two things make reversing a string more complicated than [[reversing_an_array|reversing an array]], first strings are immutable in Javascript, and second, and more importantly handling Unicode correctly. Ignoring these issues the algorithms for reversing an array would work on strings.+Two things make reversing a string more complicated than [[reversing_an_array|reversing an array]], first strings are immutable in Javascript, and second, and more importantlyhandling Unicode correctly. Ignoring these issues the algorithms for reversing an array would work on strings.
  
 Since strings are immutable there is no way they can be reversed in-place, they have to be reversed by creating a new string by concatenation. Since strings are immutable there is no way they can be reversed in-place, they have to be reversed by creating a new string by concatenation.
  
-Unicode is more complex see [[https://​github.com/​mathiasbynens/​esrever]] for a more full analysis. ​+Unicode is more complexsee [[https://​github.com/​mathiasbynens/​esrever]] and [[https://​mathiasbynens.be/​notes/​javascript-unicode]] for a more full analysis.
  
 +Javascript strings store astral code points as two characters or surrogate pairs. When reversing, the ordering of these surrogate pairs need to be retained. Secondly combining marks like n + ˜ (''​n\u0303''​) are also handled by multiple characters in the raw string but are combined into a single glyph. Combining marks also need to retain their ordering. The esrever algorithm below flips the surrogate pairs and combining mark ordering before doing a basic reversion of the string. This ensures these keep their correct ordering in the final string.
  
 [algorithm reversing a string] [algorithm reversing a string]
 +
 +For non-Javascript programming languages the same Unicode traps exist, and they need to be handled carefully.
reversing_a_string.txt · Last modified: 2015/02/03 23:59 by will