@@ -632,6 +632,19 @@ func TestWrapNonPositiveWidth(t *testing.T) {
632632 }
633633}
634634
635+ func TestWrapCRLFNonPositiveWidth (t * testing.T ) {
636+ // The ASCII fast path has to keep CRLF together as the segmenter
637+ // does, even at a width that breaks before every other cluster.
638+ c := & Condition {}
639+ for _ , s := range []string {"a\r \n b" , "\r \n " , "ab\r \n \r \n cd" , "a\r b" , "a\r " } {
640+ for _ , w := range []int {0 , - 1 , 1 , 2 } {
641+ if got , want := c .Wrap (s , w ), clusterWrap (c , s , w ); got != want {
642+ t .Errorf ("Wrap(%q, %d) = %q, cluster loop = %q" , s , w , got , want )
643+ }
644+ }
645+ }
646+ }
647+
635648func TestCreateLUTRebuildsAfterFlagChange (t * testing.T ) {
636649 savedEA , savedLut := DefaultCondition .EastAsianWidth , DefaultCondition .combinedLut
637650 defer func () {
@@ -699,8 +712,8 @@ func TestWrapGraphemeCluster(t *testing.T) {
699712// taken out, the behaviour the fast path has to reproduce exactly.
700713func clusterWidth (c * Condition , s string ) int {
701714 width := 0
702- g := graphemes .FromString (s )
703- for g .Next () {
715+ g := graphemes .FromString (clusterText ( s ) )
716+ for g .Next () && g . Start () < len ( s ) {
704717 width += c .graphemeWidth (g .Value ())
705718 }
706719 return width
@@ -709,8 +722,8 @@ func clusterWidth(c *Condition, s string) int {
709722func clusterWrap (c * Condition , s string , w int ) string {
710723 width := 0
711724 var out strings.Builder
712- g := graphemes .FromString (s )
713- for g .Next () {
725+ g := graphemes .FromString (clusterText ( s ) )
726+ for g .Next () && g . Start () < len ( s ) {
714727 cluster := g .Value ()
715728 if strings .HasSuffix (cluster , "\n " ) {
716729 out .WriteString (cluster )
@@ -831,13 +844,8 @@ func TestInvalidUTF8MatchesClusterLoop(t *testing.T) {
831844 b .WriteString (invalidPieces [r .Intn (len (invalidPieces ))])
832845 }
833846 s := b .String ()
834- // StringWidth answers a single byte from a shortcut of its
835- // own, which has never agreed with the segmenter on a byte
836- // that is not valid UTF-8.
837- if len (s ) > 1 {
838- if got , want := c .StringWidth (s ), clusterWidth (c , s ); got != want {
839- t .Fatalf ("StringWidth(%q) = %d, cluster loop = %d" , s , got , want )
840- }
847+ if got , want := c .StringWidth (s ), clusterWidth (c , s ); got != want {
848+ t .Fatalf ("StringWidth(%q) = %d, cluster loop = %d" , s , got , want )
841849 }
842850 w := 1 + r .Intn (6 )
843851 if got , want := c .Wrap (s , w ), clusterWrap (c , s , w ); got != want {
@@ -858,6 +866,62 @@ func TestInvalidUTF8MatchesClusterLoop(t *testing.T) {
858866 }
859867}
860868
869+ func TestIncompleteSequenceAtTheEnd (t * testing.T ) {
870+ // The bytes of a sequence cut short at the end are measured as they
871+ // are when anything follows them, rather than vanishing into the
872+ // cluster before them.
873+ r := rand .New (rand .NewSource (10 ))
874+ for _ , c := range []* Condition {{}, {EastAsianWidth : true , StrictEmojiNeutral : true }} {
875+ for i := 0 ; i < 20000 ; i ++ {
876+ var b strings.Builder
877+ for j := 1 + r .Intn (6 ); j > 0 ; j -- {
878+ b .WriteString (invalidPieces [r .Intn (len (invalidPieces ))])
879+ }
880+ s := b .String ()
881+ if got , want := c .StringWidth (s ), c .StringWidth (s + " " )- 1 ; got != want {
882+ t .Fatalf ("StringWidth(%q) = %d, want %d as with a space after it" , s , got , want )
883+ }
884+ }
885+ }
886+ if got := Truncate ("あ\xe3 \x81 " , 2 , "" ); got != "あ" {
887+ t .Errorf ("Truncate(%q, 2, \" \" ) = %q, want %q" , "あ\xe3 \x81 " , got , "あ" )
888+ }
889+ }
890+
891+ func TestClusterCapCoversOnlyTheGlyph (t * testing.T ) {
892+ // The two-cell cap is for sequences drawn as one glyph. A letter in
893+ // front of a skin tone modifier, or a Prepend rune in front of the
894+ // rest, is drawn on its own and keeps its cell.
895+ c := & Condition {}
896+ for _ , tt := range []struct {
897+ s string
898+ want int
899+ }{
900+ {"👍🏼" , 2 },
901+ {"🇯🇵" , 2 },
902+ {"👨\u200d 👩\u200d 👧\u200d 👦" , 2 },
903+ {"🏳\ufe0f \u200d 🌈" , 2 },
904+ {"\u1100 \u1161 \u11a8 " , 2 },
905+ {"🏼" , 2 },
906+ {" 🏼" , 3 },
907+ {"a🏼" , 3 },
908+ {"\u0890 \u0601 " , 3 },
909+ {"\u0601 👍" , 3 },
910+ } {
911+ if got := c .StringWidth (tt .s ); got != tt .want {
912+ t .Errorf ("StringWidth(%+q) = %d, want %d" , tt .s , got , tt .want )
913+ }
914+ }
915+ for _ , s := range []string {"🏼" , "\u0890 \u0601 " } {
916+ if got := c .StringWidth (c .FillLeft (s , 4 )); got != 4 {
917+ t .Errorf ("StringWidth(FillLeft(%+q, 4)) = %d, want 4" , s , got )
918+ }
919+ if got := c .StringWidth (c .FillRight (s , 4 )); got != 4 {
920+ t .Errorf ("StringWidth(FillRight(%+q, 4)) = %d, want 4" , s , got )
921+ }
922+ }
923+ }
924+
861925// The three Truncate functions with the fast path taken out, the behaviour
862926// it has to reproduce exactly.
863927func clusterTruncate (c * Condition , s string , w int , tail string ) string {
@@ -867,8 +931,8 @@ func clusterTruncate(c *Condition, s string, w int, tail string) string {
867931 w -= c .StringWidth (tail )
868932 var width int
869933 pos := len (s )
870- g := graphemes .FromString (s )
871- for g .Next () {
934+ g := graphemes .FromString (clusterText ( s ) )
935+ for g .Next () && g . Start () < len ( s ) {
872936 chWidth := c .graphemeWidth (g .Value ())
873937 if width + chWidth > w {
874938 pos = g .Start ()
@@ -885,8 +949,8 @@ func clusterTruncateLeft(c *Condition, s string, w int, prefix string) string {
885949 }
886950 var width int
887951 pos := len (s )
888- g := graphemes .FromString (s )
889- for g .Next () {
952+ g := graphemes .FromString (clusterText ( s ) )
953+ for g .Next () && g . Start () < len ( s ) {
890954 chWidth := c .graphemeWidth (g .Value ())
891955 if width + chWidth > w {
892956 if width < w {
@@ -913,8 +977,8 @@ func clusterTruncatePrefix(c *Condition, s string, w int, prefix string) string
913977 w -= c .StringWidth (prefix )
914978 var width int
915979 var pos int
916- g := graphemes .FromString (s )
917- for g .Next () {
980+ g := graphemes .FromString (clusterText ( s ) )
981+ for g .Next () && g . Start () < len ( s ) {
918982 chWidth := c .graphemeWidth (g .Value ())
919983 if sw - (width + chWidth ) <= w {
920984 pos = g .End ()
0 commit comments