// bidi implements the Unicode Bidi algorithm. // // The implementation is inspired by x/text/unicode/bidi, providing // a similar API.
package bidi import ( ucd ) // 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. type Paragraph struct { text []rune // input values initialTypes []ucd.BidiClass pairTypes []bracketType pairValues []rune embeddingLevel Level // default: = implicitLevel; // at the paragraph levels resultTypes []ucd.BidiClass resultLevels []Level // Index of matching PDI for isolate initiator characters. For other // characters, the value of matchingPDI will be set to -1. For isolate // initiators with no matching PDI, matchingPDI will be set to the length of // the input string. matchingPDI []int // Index of matching isolate initiator for PDI characters. For other // characters, and for PDIs with no matching isolate initiator, the value of // matchingIsolateInitiator will be set to -1. matchingIsolateInitiator []int runForCharacter []int // exclusive indice of output runs: the run is at text[previousEnd:end] runsEnd []int } // Run is a slice of text with a constant direction. type Run struct { // Start and End indicate the subslice of the input text : text[Start:End] Start, End int Level Level } // IsLeftToRight returns `true` for a RTL run. func ( Run) () bool { return .Level%2 == 0 } // Runs holds the output of the Bidi algorithm. type Runs struct { levels []Level runEnds []int } // NumRuns returns the number of runs. func ( *Runs) () int { return len(.runEnds) } // Run returns the ith run of segmented text. // This method panics if [i] is not in the range [0,NumRuns()[ func ( *Runs) ( int) Run { := 0 if != 0 { = .runEnds[-1] } := .runEnds[] := .levels[] return Run{, , } } // 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) ( []rune, Direction) Runs { .text = append(.text[:0], ...) return .segment() } // SegmentString is the same as [Segment], but avoid allocations // if [text] is a string func ( *Paragraph) ( string, Direction) Runs { .text = .text[:0] for , := range { .text = append(.text, ) } return .segment() } // SegmentBytes is the same as [Segment], but avoid allocations // if [text] is a byte slice. func ( *Paragraph) ( []byte, Direction) Runs { .text = .text[:0] // The Go compiler should optimize this without allocating a string. for , := range string() { .text = append(.text, ) } return .segment() } func ( *Paragraph) ( Direction) Runs { := .prepareInput() if len(.initialTypes) == 0 { return Runs{} } // Try to short-circuit the full algorithm for full LTR text : // this mask is very conservative, perhaps there is a better condition here ? const = ucd.BD_CS | ucd.BD_EN | ucd.BD_ES | ucd.BD_L | ucd.BD_WS if != RightToLeft && & ^ == 0 { // full RTL, level 0 setLevels(.resultLevels, 0) return .buildRuns() } := implicitLevel switch { case LeftToRight: = 0 case RightToLeft: = 1 } .embeddingLevel = .run() .computeLevels() return .buildRuns() } // depends only on [resultLevels] func ( *Paragraph) () Runs { var bool // lvl = 0,2,4,...: left to right // lvl = 1,3,5,...: right to left for , := range .resultLevels { := %2 != 0 if == 0 { = } else if != { // close the current run .runsEnd = append(.runsEnd, ) = } } // close the last run .runsEnd = append(.runsEnd, len(.resultLevels)) return Runs{levels: .resultLevels, runEnds: .runsEnd} } func max(, Level) Level { if < { return } return } // A Direction indicates the overall flow of text. type Direction uint8 const ( Neutral Direction = iota LeftToRight RightToLeft ) // Initialize the p.pairTypes, p.pairValues and p.types from the input previously // set by p.SetBytes() or p.SetString(). Also limit the input up to (and including) a paragraph // separator (bidi class B). func ( *Paragraph) () ( ucd.BidiClass) { // reset storage .runsEnd = .runsEnd[:0] if := len(.text); cap(.pairTypes) >= { .pairTypes = .pairTypes[:] .pairValues = .pairValues[:] .initialTypes = .initialTypes[:] .resultTypes = .resultTypes[:] .resultLevels = .resultLevels[:] .matchingPDI = .matchingPDI[:] .matchingIsolateInitiator = .matchingIsolateInitiator[:] .runForCharacter = .runForCharacter[:] } else { .pairTypes = make([]bracketType, ) .pairValues = make([]rune, ) .initialTypes = make([]ucd.BidiClass, ) .resultTypes = make([]ucd.BidiClass, ) .resultLevels = make([]Level, ) .matchingPDI = make([]int, ) .matchingIsolateInitiator = make([]int, ) .runForCharacter = make([]int, ) } // Try to short-circuit the full algorithm for single direction text = ucd.BidiClass(0) for , := range .text { , := ucd.LookupBidiClass() |= if == ucd.BD_B { // Unlikely, but trim the arrays and exit .text = .text[:] .pairTypes = .pairTypes[:] .pairValues = .pairValues[:] .initialTypes = .initialTypes[:] .resultTypes = .resultTypes[:] .resultLevels = .resultLevels[:] .matchingPDI = .matchingPDI[:] .matchingIsolateInitiator = .matchingIsolateInitiator[:] .runForCharacter = .runForCharacter[:] return } .initialTypes[] = .resultTypes[] = if .IsOpening() { .pairTypes[] = bpOpen .pairValues[] = } else if .IsBracket() { // this must be a closing bracket, // since IsOpeningBracket is not true .pairTypes[] = bpClose .pairValues[] = .Reverse() } else { .pairTypes[] = bpNone .pairValues[] = 0 } } return }