As a human user of an editor, I don’t count code points as defined by maxLength and minLength. Instead, I count the number of characters I see. These grapheme clusters (Unicode Standard Annex #29) are the smallest visually meaningful unit of a character string.
Graphemes can consist of a single Unicode code point or multiple code points. The longest code point sequences are defined in the Emoji specification, with up to 10 code points. But even in more mundane use cases, such as DIN 91379, there are 147 graphemes in the Latin block that consist of 2 or 3 code points.
Proposed Validation Extension
The validation keywords maxGraphemes and minGraphemes are introduced.
maxGraphemes (integer): maximum number of Unicode grapheme clusters in a string
minGraphemes (integer): minimum number of Unicode grapheme clusters in a string
Implementation Notes
Unlike maxLength and minLength, the time complexity is not O(1) but O(n), since most implementations must traverse the entire string to count the graphemes.
Support
Swift - Native - String is composed of Character values, which are Extended Grapheme Clusters by default
Java - Native (since JDK 20) - java.text.BreakIterator.getCharacterInstance(Locale) - since Java 20
.NET (C#) - Native - System.Globalization.StringInfo (GetTextElementEnumerator)
Ruby - Native (since 2.6) - String#each_grapheme_cluster, #grapheme_clusters
TypeScript/JavaScript - Native (modern engines) - Intl.Segmenter with granularity: "grapheme"
PHP - Native via extension - intl extension: grapheme_strlen(), grapheme_substr() (ICU-based)
Perl - Native (core regex) - \X in regex; alternatively Unicode::GCString
Rust - Via crate - no std support, but unicode-segmentation (UnicodeSegmentation::graphemes())
Go - Via library - no stdlib support, but github.com/rivo/uniseg or golang.org/x/text/unicode/segment
R - Via package - no base R support, but stringi::stri_split_boundaries(type="character")
C - Via library - no language feature; relies on ICU (ubrk_* functions) or utf8proc
Reference
max- and minGraphemes are also included in Lexcion from the atproto world.
As a human user of an editor, I don’t count code points as defined by
maxLengthandminLength. Instead, I count the number of characters I see. These grapheme clusters (Unicode Standard Annex #29) are the smallest visually meaningful unit of a character string.Graphemes can consist of a single Unicode code point or multiple code points. The longest code point sequences are defined in the Emoji specification, with up to 10 code points. But even in more mundane use cases, such as DIN 91379, there are 147 graphemes in the Latin block that consist of 2 or 3 code points.
Proposed Validation Extension
The validation keywords
maxGraphemesandminGraphemesare introduced.maxGraphemes(integer): maximum number of Unicode grapheme clusters in a stringminGraphemes(integer): minimum number of Unicode grapheme clusters in a stringImplementation Notes
Unlike
maxLengthandminLength, the time complexity is not O(1) but O(n), since most implementations must traverse the entire string to count the graphemes.Support
Swift - Native - String is composed of Character values, which are Extended Grapheme Clusters by default
Java - Native (since JDK 20) - java.text.BreakIterator.getCharacterInstance(Locale) - since Java 20
.NET (C#) - Native - System.Globalization.StringInfo (GetTextElementEnumerator)
Ruby - Native (since 2.6) - String#each_grapheme_cluster, #grapheme_clusters
TypeScript/JavaScript - Native (modern engines) - Intl.Segmenter with granularity: "grapheme"
PHP - Native via extension - intl extension: grapheme_strlen(), grapheme_substr() (ICU-based)
Perl - Native (core regex) - \X in regex; alternatively Unicode::GCString
Rust - Via crate - no std support, but unicode-segmentation (UnicodeSegmentation::graphemes())
Go - Via library - no stdlib support, but github.com/rivo/uniseg or golang.org/x/text/unicode/segment
R - Via package - no base R support, but stringi::stri_split_boundaries(type="character")
C - Via library - no language feature; relies on ICU (ubrk_* functions) or utf8proc
Reference
max- andminGraphemesare also included in Lexcion from the atproto world.