From 5e06941c63425ab10e08e4d533939c7309cfe6ce Mon Sep 17 00:00:00 2001 From: Laurenz Date: Tue, 5 Oct 2021 17:57:30 +0200 Subject: [PATCH] Iterate over grapheme clusters instead of chars --- Cargo.toml | 1 + src/eval/str.rs | 6 ++++-- tests/typ/code/for.typ | 6 +++--- 3 files changed, 8 insertions(+), 5 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index c1841af9b..c7fa703c4 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -29,6 +29,7 @@ rustybuzz = "0.4" serde = { version = "1", features = ["derive", "rc"] } ttf-parser = "0.12" unicode-bidi = "0.3.5" +unicode-segmentation = "1.8" unicode-xid = "0.2" xi-unicode = "0.3" anyhow = { version = "1", optional = true } diff --git a/src/eval/str.rs b/src/eval/str.rs index 59e0887ae..800d17093 100644 --- a/src/eval/str.rs +++ b/src/eval/str.rs @@ -3,6 +3,8 @@ use std::convert::TryFrom; use std::fmt::{self, Debug, Formatter, Write}; use std::ops::{Add, AddAssign, Deref}; +use unicode_segmentation::UnicodeSegmentation; + use crate::diag::StrResult; use crate::util::EcoString; @@ -41,9 +43,9 @@ impl 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 + '_ { - self.chars().map(Into::into) + self.graphemes(true).map(Into::into) } /// Repeat this string `n` times. diff --git a/tests/typ/code/for.typ b/tests/typ/code/for.typ index 63dab9b88..2569f5a7f 100644 --- a/tests/typ/code/for.typ +++ b/tests/typ/code/for.typ @@ -58,15 +58,15 @@ #test(out, (1, 2, 3, 4, 5, "a", 6, "b", 7)) -// Chars of string. +// Grapheme clusters of string. #let first = true -#let joined = for c in "abc" { +#let joined = for c in "abc👩‍👩‍👦‍👦" { if not first { ", " } first = false c } -#test(joined, "a, b, c") +#test(joined, "a, b, c, 👩‍👩‍👦‍👦") // Return value. #test(for v in "" [], none)