Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Grapheme cluster support #5

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
module github.com/liggitt/tabwriter

go 1.21.4
Empty file added go.sum
Empty file.
10 changes: 9 additions & 1 deletion tabwriter.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
package tabwriter

import (
"github.com/liggitt/tabwriter/unicode"
"io"
"unicode/utf8"
)
Expand Down Expand Up @@ -199,6 +200,9 @@ const (

// Remember maximum widths seen per column even after Flush() is called.
RememberWidths

// Combine multiple runes into individual user-perceived characters ("grapheme clusters" in Unicode specification)
GraphemeClusterWidth
)

// A Writer must be initialized with a call to Init. The first parameter (output)
Expand Down Expand Up @@ -439,7 +443,11 @@ func (b *Writer) append(text []byte) {

// Update the cell width.
func (b *Writer) updateWidth() {
b.cell.width += utf8.RuneCount(b.buf[b.pos:])
if b.flags&GraphemeClusterWidth != 0 {
b.cell.width += unicode.MonospaceWidth(b.buf[b.pos:])
} else {
b.cell.width += utf8.RuneCount(b.buf[b.pos:])
}
b.pos = len(b.buf)
}

Expand Down
39 changes: 39 additions & 0 deletions tabwriter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -361,6 +361,45 @@ var tests = []struct {
"1....2....3....4....\n",
},

{
"10c",
5, 0, 0, '.', GraphemeClusterWidth,
"1\t2\t3\t4\t\n",
"1....2....3....4....\n",
},

// Emoji Variation Selector - all width of 1
{
"10d",
5, 0, 0, '.', GraphemeClusterWidth,
"\u2716\t\u2716\ufe0e\t\u2716\ufe0f\t\n\u263a\t\u263a\ufe0e\t\u263a\ufe0f\t\n",
"✖....✖︎....✖️....\n☺....☺︎....☺️....\n",
},

// Emoji Flags - pirate flag is width 2, rest are width 1
{
"10e",
5, 0, 0, '.', GraphemeClusterWidth,
"\U0001F3F4\u200D\u2620\t\U0001F3F3\u200D\U0001F308\t\U0001F1E9\U0001F1EA\t\n",
"🏴‍☠️...🏳️‍🌈....🇩🇪....\n",
},

// Emoji Speedboat - width 1, rest are width 2
{
"10e",
5, 0, 0, '.', GraphemeClusterWidth,
"\U0001F6E5\t\U0001F4A3\t\U0001F468\u200D\U0001F467\u200D\U0001F466\t\n",
"🛥....💣...👨‍👧‍👦...\n",
},

// East Asian characters should be width 2
{
"10f",
5, 0, 0, '.', GraphemeClusterWidth,
"容器\t容器\t容器\t\n",
"容器.容器.容器.\n",
},

{
"11",
8, 0, 1, '.', 0,
Expand Down
2,578 changes: 2,578 additions & 0 deletions unicode/eastasianwidth.go

Large diffs are not rendered by default.

307 changes: 307 additions & 0 deletions unicode/emojipresentation.go

Large diffs are not rendered by default.

120 changes: 120 additions & 0 deletions unicode/grapheme.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,120 @@
// From https://github.com/rivo/uniseg/blob/master/grapheme.go
//
// Licensed under MIT License
//
// Copyright (c) 2019 Oliver Kuederle
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.

package unicode

import "unicode/utf8"

// The number of bits the grapheme property must be shifted to make place for
// grapheme states.
const shiftGraphemePropState = 4

// The bit mask used to extract the state returned by the [Step] function, after
// shifting. These values must correspond to the shift constants.
const maskGraphemeState = 0xf

// FirstGraphemeCluster returns the first grapheme cluster found in the given
// byte slice according to the rules of [Unicode Standard Annex #29, Grapheme
// Cluster Boundaries]. This function can be called continuously to extract all
// grapheme clusters from a byte slice, as illustrated in the example below.
//
// If you don't know the current state, for example when calling the function
// for the first time, you must pass -1. For consecutive calls, pass the state
// and rest slice returned by the previous call.
//
// The "rest" slice is the sub-slice of the original byte slice "b" starting
// after the last byte of the identified grapheme cluster. If the length of the
// "rest" slice is 0, the entire byte slice "b" has been processed. The
// "cluster" byte slice is the sub-slice of the input slice containing the
// identified grapheme cluster.
//
// The returned width is the width of the grapheme cluster for most monospace
// fonts where a value of 1 represents one character cell.
//
// Given an empty byte slice "b", the function returns nil values.
//
// While slightly less convenient than using the Graphemes class, this function
// has much better performance and makes no allocations. It lends itself well to
// large byte slices.
//
// [Unicode Standard Annex #29, Grapheme Cluster Boundaries]: http://unicode.org/reports/tr29/#Grapheme_Cluster_Boundaries
func FirstGraphemeCluster(b []byte, state int) (cluster, rest []byte, width, newState int) {
// An empty byte slice returns nothing.
if len(b) == 0 {
return
}

// Extract the first rune.
r, length := utf8.DecodeRune(b)
if len(b) <= length { // If we're already past the end, there is nothing else to parse.
var prop int
if state < 0 {
prop = property(graphemeCodePoints, r)
} else {
prop = state >> shiftGraphemePropState
}
return b, nil, runeWidth(r, prop), grAny | (prop << shiftGraphemePropState)
}

// If we don't know the state, determine it now.
var firstProp int
if state < 0 {
state, firstProp, _ = transitionGraphemeState(state, r)
} else {
firstProp = state >> shiftGraphemePropState
}
width += runeWidth(r, firstProp)

// Transition until we find a boundary.
for {
var (
prop int
boundary bool
)

r, l := utf8.DecodeRune(b[length:])
state, prop, boundary = transitionGraphemeState(state&maskGraphemeState, r)

if boundary {
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 r == vs15 {
width = 1
} else {
width = 2
}
}

length += l
if len(b) <= length {
return b, nil, width, grAny | (prop << shiftGraphemePropState)
}
}
}
Loading