bidi

package
v1.0.1 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Apr 26, 2026 License: BSD-3-Clause, Unlicense Imports: 3 Imported by: 0

Documentation

Overview

bidi implements the Unicode Bidi algorithm.

The implementation is inspired by x/text/unicode/bidi, providing a similar API.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Direction

type Direction uint8

A Direction indicates the overall flow of text.

const (
	Neutral Direction = iota
	LeftToRight
	RightToLeft
)

type Level

type Level int8

Level is the embedding level of a character. Even embedding levels indicate left-to-right order and odd levels indicate right-to-left order.

type Paragraph

type Paragraph struct {
	// contains filtered or unexported fields
}

Paragraph is the main entry point of the package.

It stores internal data required to segment a string, and should be reused to reduce allocations.

func (*Paragraph) Segment

func (p *Paragraph) Segment(text []rune, defaultDirection Direction) Runs

Segment applies the Bidi algorithm. The returned runs are only valid until the next call to [Segment], [SegmentString] or [SegmentBytes].

[defaultDirection] is the default direction for the input text. The direction is overridden if the text contains directional characters.

func (*Paragraph) SegmentBytes

func (p *Paragraph) SegmentBytes(text []byte, defaultDirection Direction) Runs

SegmentBytes is the same as [Segment], but avoid allocations if [text] is a byte slice.

func (*Paragraph) SegmentString

func (p *Paragraph) SegmentString(text string, defaultDirection Direction) Runs

SegmentString is the same as [Segment], but avoid allocations if [text] is a string

type Run

type Run struct {
	// Start and End indicate the subslice of the input text : text[Start:End]
	Start, End int
	Level      Level
}

Run is a slice of text with a constant direction.

func (Run) IsLeftToRight

func (r Run) IsLeftToRight() bool

IsLeftToRight returns `true` for a RTL run.

type Runs

type Runs struct {
	// contains filtered or unexported fields
}

Runs holds the output of the Bidi algorithm.

func (*Runs) NumRuns

func (r *Runs) NumRuns() int

NumRuns returns the number of runs.

func (*Runs) Run

func (r *Runs) Run(i int) Run

Run returns the ith run of segmented text. This method panics if [i] is not in the range [0,NumRuns()[

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL