Iterate over grapheme clusters instead of chars

This commit is contained in:
Laurenz 2021-10-05 17:57:30 +02:00
parent 00be5d36c4
commit 5e06941c63
3 changed files with 8 additions and 5 deletions

View File

@ -29,6 +29,7 @@ rustybuzz = "0.4"
serde = { version = "1", features = ["derive", "rc"] } serde = { version = "1", features = ["derive", "rc"] }
ttf-parser = "0.12" ttf-parser = "0.12"
unicode-bidi = "0.3.5" unicode-bidi = "0.3.5"
unicode-segmentation = "1.8"
unicode-xid = "0.2" unicode-xid = "0.2"
xi-unicode = "0.3" xi-unicode = "0.3"
anyhow = { version = "1", optional = true } anyhow = { version = "1", optional = true }

View File

@ -3,6 +3,8 @@ use std::convert::TryFrom;
use std::fmt::{self, Debug, Formatter, Write}; use std::fmt::{self, Debug, Formatter, Write};
use std::ops::{Add, AddAssign, Deref}; use std::ops::{Add, AddAssign, Deref};
use unicode_segmentation::UnicodeSegmentation;
use crate::diag::StrResult; use crate::diag::StrResult;
use crate::util::EcoString; use crate::util::EcoString;
@ -41,9 +43,9 @@ impl Str {
self.0.as_str() self.0.as_str()
} }
/// Return an iterator over the chars as strings. /// Return an iterator over the grapheme clusters as strings.
pub fn iter(&self) -> impl Iterator<Item = Str> + '_ { pub fn iter(&self) -> impl Iterator<Item = Str> + '_ {
self.chars().map(Into::into) self.graphemes(true).map(Into::into)
} }
/// Repeat this string `n` times. /// Repeat this string `n` times.

View File

@ -58,15 +58,15 @@
#test(out, (1, 2, 3, 4, 5, "a", 6, "b", 7)) #test(out, (1, 2, 3, 4, 5, "a", 6, "b", 7))
// Chars of string. // Grapheme clusters of string.
#let first = true #let first = true
#let joined = for c in "abc" { #let joined = for c in "abc👩‍👩‍👦‍👦" {
if not first { ", " } if not first { ", " }
first = false first = false
c c
} }
#test(joined, "a, b, c") #test(joined, "a, b, c, 👩‍👩‍👦‍👦")
// Return value. // Return value.
#test(for v in "" [], none) #test(for v in "" [], none)