@@ -108,6 +108,7 @@ public int CellCount()
108108 {
109109 return 0 ;
110110 }
111+ if ( Text == "\n " ) return 1 ;
111112
112113 return Cell . GetCellLength ( Text ) ;
113114 }
@@ -160,10 +161,12 @@ public Segment StripLineEndings()
160161 if ( offset > 0 )
161162 {
162163 var accumulated = 0 ;
163- foreach ( var character in Text )
164+ var enumerator = StringInfo . GetTextElementEnumerator ( Text ) ;
165+ while ( enumerator . MoveNext ( ) )
164166 {
165- index ++ ;
166- accumulated += Cell . GetCellLength ( character ) ;
167+ var cluster = enumerator . GetTextElement ( ) ;
168+ accumulated += Cell . GetCellLength ( cluster ) ;
169+ index += cluster . Length ;
167170 if ( accumulated >= offset )
168171 {
169172 break ;
@@ -429,16 +432,18 @@ public static List<Segment> Truncate(IEnumerable<Segment> segments, int maxWidth
429432
430433 var builder = new StringBuilder ( ) ;
431434 var accumulatedCellWidth = 0 ;
432- foreach ( var character in segment . Text )
435+ var truncateEnumerator = StringInfo . GetTextElementEnumerator ( segment . Text ) ;
436+ while ( truncateEnumerator . MoveNext ( ) )
433437 {
434- var characterWidth = UnicodeCalculator . GetWidth ( character ) ;
435- if ( accumulatedCellWidth + characterWidth > maxWidth )
438+ var cluster = truncateEnumerator . GetTextElement ( ) ;
439+ var clusterWidth = Cell . GetCellLength ( cluster ) ;
440+ if ( accumulatedCellWidth + clusterWidth > maxWidth )
436441 {
437442 break ;
438443 }
439444
440- builder . Append ( character ) ;
441- accumulatedCellWidth += characterWidth ;
445+ builder . Append ( cluster ) ;
446+ accumulatedCellWidth += clusterWidth ;
442447 }
443448
444449 if ( builder . Length == 0 )
@@ -571,17 +576,20 @@ internal static List<string> SplitSegment(string text, int maxCellLength)
571576
572577 var length = 0 ;
573578 var sb = new StringBuilder ( ) ;
574- foreach ( var ch in text )
579+ var splitEnumerator = StringInfo . GetTextElementEnumerator ( text ) ;
580+ while ( splitEnumerator . MoveNext ( ) )
575581 {
576- if ( length + UnicodeCalculator . GetWidth ( ch ) > maxCellLength )
582+ var cluster = splitEnumerator . GetTextElement ( ) ;
583+ var clusterWidth = Cell . GetCellLength ( cluster ) ;
584+ if ( length + clusterWidth > maxCellLength )
577585 {
578586 list . Add ( sb . ToString ( ) ) ;
579587 sb . Clear ( ) ;
580588 length = 0 ;
581589 }
582590
583- length += UnicodeCalculator . GetWidth ( ch ) ;
584- sb . Append ( ch ) ;
591+ length += clusterWidth ;
592+ sb . Append ( cluster ) ;
585593 }
586594
587595 list . Add ( sb . ToString ( ) ) ;
0 commit comments