Skip to content

Commit

Permalink
Fixed wrong width calculation for variation selectors combined with r…
Browse files Browse the repository at this point in the history
…egular characters. Fixes #50
  • Loading branch information
rivo committed Feb 8, 2024
1 parent 601bbb3 commit 03509a9
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 27 deletions.
20 changes: 8 additions & 12 deletions grapheme.go
Original file line number Diff line number Diff line change
Expand Up @@ -253,16 +253,14 @@ func FirstGraphemeCluster(b []byte, state int) (cluster, rest []byte, width, new
return b[:length], b[length:], width, state | (prop << shiftGraphemePropState)
}

if r == vs16 {
width = 2
} else if firstProp != prExtendedPictographic && firstProp != prRegionalIndicator && firstProp != prL {
width += runeWidth(r, prop)
} else if firstProp == prExtendedPictographic {
if firstProp == prExtendedPictographic {
if r == vs15 {
width = 1
} else {
} else if r == vs16 {
width = 2
}
} else if firstProp != prRegionalIndicator && firstProp != prL {
width += runeWidth(r, prop)
}

length += l
Expand Down Expand Up @@ -315,16 +313,14 @@ func FirstGraphemeClusterInString(str string, state int) (cluster, rest string,
return str[:length], str[length:], width, state | (prop << shiftGraphemePropState)
}

if r == vs16 {
width = 2
} else if firstProp != prExtendedPictographic && firstProp != prRegionalIndicator && firstProp != prL {
width += runeWidth(r, prop)
} else if firstProp == prExtendedPictographic {
if firstProp == prExtendedPictographic {
if r == vs15 {
width = 1
} else {
} else if r == vs16 {
width = 2
}
} else if firstProp != prRegionalIndicator && firstProp != prL {
width += runeWidth(r, prop)
}

length += l
Expand Down
20 changes: 8 additions & 12 deletions step.go
Original file line number Diff line number Diff line change
Expand Up @@ -150,16 +150,14 @@ func Step(b []byte, state int) (cluster, rest []byte, boundaries int, newState i
return b[:length], b[length:], boundary, graphemeState | (wordState << shiftWordState) | (sentenceState << shiftSentenceState) | (lineState << shiftLineState) | (prop << shiftPropState)
}

if r == vs16 {
width = 2
} else if firstProp != prExtendedPictographic && firstProp != prRegionalIndicator && firstProp != prL {
width += runeWidth(r, prop)
} else if firstProp == prExtendedPictographic {
if firstProp == prExtendedPictographic {
if r == vs15 {
width = 1
} else {
} else if r == vs16 {
width = 2
}
} else if firstProp != prRegionalIndicator && firstProp != prL {
width += runeWidth(r, prop)
}

length += l
Expand Down Expand Up @@ -226,16 +224,14 @@ func StepString(str string, state int) (cluster, rest string, boundaries int, ne
return str[:length], str[length:], boundary, graphemeState | (wordState << shiftWordState) | (sentenceState << shiftSentenceState) | (lineState << shiftLineState) | (prop << shiftPropState)
}

if r == vs16 {
width = 2
} else if firstProp != prExtendedPictographic && firstProp != prRegionalIndicator && firstProp != prL {
width += runeWidth(r, prop)
} else if firstProp == prExtendedPictographic {
if firstProp == prExtendedPictographic {
if r == vs15 {
width = 1
} else {
} else if r == vs16 {
width = 2
}
} else if firstProp != prRegionalIndicator && firstProp != prL {
width += runeWidth(r, prop)
}

length += l
Expand Down
6 changes: 3 additions & 3 deletions width_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -277,8 +277,8 @@ var widthTestCases = []struct {
{"\u00a9\u061c", 1},
{"\u00a9\u000a", 1},
{"\u00a9\u000d", 1},
{"\u00a9\u0300", 2}, // This is really 1 but we can't handle it.
{"\u00a9\u200d", 2},
{"\u00a9\u0300", 1},
{"\u00a9\u200d", 1},
{"\u00a9a", 2},
{"\u00a9\u1b05", 2},
{"\u00a9\u2985", 2},
Expand Down Expand Up @@ -341,7 +341,7 @@ var widthTestCases = []struct {
{"\u263a\ufe0f", 2}, // White smiling face (with variation selector 16 = emoji presentation)
{"\u231b", 2}, // Hourglass
{"\u231b\ufe0e", 1}, // Hourglass (with variation selector 15 = text presentation)
{"1\ufe0f", 2}, // Emoji presentation of digit one.
{"1\ufe0f", 1}, // Emoji presentation of digit one.
}

// String width tests using the StringWidth function.
Expand Down

0 comments on commit 03509a9

Please sign in to comment.