10 stable releases

1.0.9 Mar 15, 2024
1.0.8 Dec 18, 2018
1.0.7 Aug 24, 2018
1.0.6 Jan 23, 2018
1.0.3 Dec 27, 2016

#143 in Text processing

Download history 12631/week @ 2024-08-14 13537/week @ 2024-08-21 14100/week @ 2024-08-28 14209/week @ 2024-09-04 13966/week @ 2024-09-11 15020/week @ 2024-09-18 13941/week @ 2024-09-25 14890/week @ 2024-10-02 15118/week @ 2024-10-09 17501/week @ 2024-10-16 16709/week @ 2024-10-23 15570/week @ 2024-10-30 16007/week @ 2024-11-06 18623/week @ 2024-11-13 15528/week @ 2024-11-20 13474/week @ 2024-11-27

66,729 downloads per month
Used in 80 crates (via polars-ops)

MIT/Apache

10KB
63 lines

unicode-reverse

Unicode-aware in-place string reversal for Rust UTF-8 strings.

The reverse_grapheme_clusters_in_place function reverses a string slice in-place without allocating any memory on the heap. It correctly handles multi-byte UTF-8 sequences and grapheme clusters, including combining marks and astral characters such as Emoji.

Example

use unicode_reverse::reverse_grapheme_clusters_in_place;

let mut x = "man\u{0303}ana".to_string();
println!("{}", x); // prints "mañana"

reverse_grapheme_clusters_in_place(&mut x);
println!("{}", x); // prints "anañam"

Background

As described in this article by Mathias Bynens, naively reversing a Unicode string can go wrong in several ways. For example, merely reversing the chars (Unicode Scalar Values) in a string can cause combining marks to become attached to the wrong characters:

let x = "man\u{0303}ana";
println!("{}", x); // prints "mañana"

let y: String = x.chars().rev().collect();
println!("{}", y); // prints "anãnam": Oops! The '~' is now applied to the 'a'.

Reversing the grapheme clusters of the string fixes this problem:

extern crate unicode_segmentation;
use unicode_segmentation::UnicodeSegmentation;

fn main() {
    let x = "man\u{0303}ana";
    let y: String = x.graphemes(true).rev().collect();
    println!("{}", y); // prints "anañam"
}

The reverse_grapheme_clusters_in_place function from this crate performs this same operation, but performs the reversal in-place rather than allocating a new string.

Note: Even grapheme-level reversal may produce unexpected output if the input string contains certain non-printable control codes, such as directional formatting characters. Handling such characters is outside the scope of this crate.

Algorithm

The implementation is very simple. It makes two passes over the string's contents:

  1. For each grapheme cluster, reverse the bytes within the grapheme cluster in-place.
  2. Reverse the bytes of the entire string in-place.

After the second pass, each grapheme cluster has been reversed twice, so its bytes are now back in their original order, but the clusters are now in the opposite order within the string.

no_std

This crate does not depend on libstd, so it can be used in no_std projects.

Dependencies

~355KB