| 1 | // Copyright 2015 The Go Authors. All rights reserved. |
| 2 | // Use of this source code is governed by a BSD-style |
| 3 | // license that can be found in the LICENSE file. |
| 4 | |
| 5 | //go:generate go run gen.go gen_trieval.go gen_ranges.go |
| 6 | |
| 7 | // Package bidi contains functionality for bidirectional text support. |
| 8 | // |
| 9 | // See https://www.unicode.org/reports/tr9. |
| 10 | // |
| 11 | // NOTE: UNDER CONSTRUCTION. This API may change in backwards incompatible ways |
| 12 | // and without notice. |
| 13 | package bidiscip-go gomod golang.org/x/text . `golang.org/x/text/unicode/bidi`/ // import "golang.org/x/text/unicode/bidi" |
| 14 | |
| 15 | // TODO |
| 16 | // - Transformer for reordering? |
| 17 | // - Transformer (validator, really) for Bidi Rule. |
| 18 | |
| 19 | import ( |
| 20 | "bytesscip-go gomod github.com/golang/go/src go1.25.0 bytes/" |
| 21 | ) |
| 22 | |
| 23 | // This API tries to avoid dealing with embedding levels for now. Under the hood |
| 24 | // these will be computed, but the question is to which extent the user should |
| 25 | // know they exist. We should at some point allow the user to specify an |
| 26 | // embedding hierarchy, though. |
| 27 | |
| 28 | // A Direction indicates the overall flow of text. |
| 29 | type Directionscip-go gomod golang.org/x/text . `golang.org/x/text/unicode/bidi`/Direction# int |
| 30 | |
| 31 | const ( |
| 32 | // LeftToRight indicates the text contains no right-to-left characters and |
| 33 | // that either there are some left-to-right characters or the option |
| 34 | // DefaultDirection(LeftToRight) was passed. |
| 35 | LeftToRightscip-go gomod golang.org/x/text . `golang.org/x/text/unicode/bidi`/LeftToRight. Directionscip-go gomod golang.org/x/text . `golang.org/x/text/unicode/bidi`/Direction# = iota |
| 36 | |
| 37 | // RightToLeft indicates the text contains no left-to-right characters and |
| 38 | // that either there are some right-to-left characters or the option |
| 39 | // DefaultDirection(RightToLeft) was passed. |
| 40 | RightToLeftscip-go gomod golang.org/x/text . `golang.org/x/text/unicode/bidi`/RightToLeft. |
| 41 | |
| 42 | // Mixed indicates text contains both left-to-right and right-to-left |
| 43 | // characters. |
| 44 | Mixedscip-go gomod golang.org/x/text . `golang.org/x/text/unicode/bidi`/Mixed. |
| 45 | |
| 46 | // Neutral means that text contains no left-to-right and right-to-left |
| 47 | // characters and that no default direction has been set. |
| 48 | Neutralscip-go gomod golang.org/x/text . `golang.org/x/text/unicode/bidi`/Neutral. |
| 49 | ) |
| 50 | |
| 51 | type optionsscip-go gomod golang.org/x/text . `golang.org/x/text/unicode/bidi`/options# struct { |
| 52 | defaultDirectionscip-go gomod golang.org/x/text . `golang.org/x/text/unicode/bidi`/options#defaultDirection. Directionscip-go gomod golang.org/x/text . `golang.org/x/text/unicode/bidi`/Direction# |
| 53 | } |
| 54 | |
| 55 | // An Option is an option for Bidi processing. |
| 56 | type Optionscip-go gomod golang.org/x/text . `golang.org/x/text/unicode/bidi`/Option# func(*optionsscip-go gomod golang.org/x/text . `golang.org/x/text/unicode/bidi`/options#) |
| 57 | |
| 58 | // ICU allows the user to define embedding levels. This may be used, for example, |
| 59 | // to use hierarchical structure of markup languages to define embeddings. |
| 60 | // The following option may be a way to expose this functionality in this API. |
| 61 | // // LevelFunc sets a function that associates nesting levels with the given text. |
| 62 | // // The levels function will be called with monotonically increasing values for p. |
| 63 | // func LevelFunc(levels func(p int) int) Option { |
| 64 | // panic("unimplemented") |
| 65 | // } |
| 66 | |
| 67 | // DefaultDirection sets the default direction for a Paragraph. The direction is |
| 68 | // overridden if the text contains directional characters. |
| 69 | func DefaultDirectionscip-go gomod golang.org/x/text . `golang.org/x/text/unicode/bidi`/DefaultDirection().(d Directionscip-go gomod golang.org/x/text . `golang.org/x/text/unicode/bidi`/Direction#) Optionscip-go gomod golang.org/x/text . `golang.org/x/text/unicode/bidi`/Option# { |
| 70 | return func(opts *optionsscip-go gomod golang.org/x/text . `golang.org/x/text/unicode/bidi`/options#) { |
| 71 | opts.defaultDirectionscip-go gomod golang.org/x/text . `golang.org/x/text/unicode/bidi`/options#defaultDirection. = d |
| 72 | } |
| 73 | } |
| 74 | |
| 75 | // A Paragraph holds a single Paragraph for Bidi processing. |
| 76 | type Paragraphscip-go gomod golang.org/x/text . `golang.org/x/text/unicode/bidi`/Paragraph# struct { |
| 77 | pscip-go gomod golang.org/x/text . `golang.org/x/text/unicode/bidi`/Paragraph#p. []byte |
| 78 | oscip-go gomod golang.org/x/text . `golang.org/x/text/unicode/bidi`/Paragraph#o. Orderingscip-go gomod golang.org/x/text . `golang.org/x/text/unicode/bidi`/Ordering# |
| 79 | optsscip-go gomod golang.org/x/text . `golang.org/x/text/unicode/bidi`/Paragraph#opts. []Optionscip-go gomod golang.org/x/text . `golang.org/x/text/unicode/bidi`/Option# |
| 80 | typesscip-go gomod golang.org/x/text . `golang.org/x/text/unicode/bidi`/Paragraph#types. []Classscip-go gomod golang.org/x/text . `golang.org/x/text/unicode/bidi`/Class# |
| 81 | pairTypesscip-go gomod golang.org/x/text . `golang.org/x/text/unicode/bidi`/Paragraph#pairTypes. []bracketTypescip-go gomod golang.org/x/text . `golang.org/x/text/unicode/bidi`/bracketType# |
| 82 | pairValuesscip-go gomod golang.org/x/text . `golang.org/x/text/unicode/bidi`/Paragraph#pairValues. []rune |
| 83 | runesscip-go gomod golang.org/x/text . `golang.org/x/text/unicode/bidi`/Paragraph#runes. []rune |
| 84 | optionsscip-go gomod golang.org/x/text . `golang.org/x/text/unicode/bidi`/Paragraph#options. optionsscip-go gomod golang.org/x/text . `golang.org/x/text/unicode/bidi`/options# |
| 85 | } |
| 86 | |
| 87 | // Initialize the p.pairTypes, p.pairValues and p.types from the input previously |
| 88 | // set by p.SetBytes() or p.SetString(). Also limit the input up to (and including) a paragraph |
| 89 | // separator (bidi class B). |
| 90 | // |
| 91 | // The function p.Order() needs these values to be set, so this preparation could be postponed. |
| 92 | // But since the SetBytes and SetStrings functions return the length of the input up to the paragraph |
| 93 | // separator, the whole input needs to be processed anyway and should not be done twice. |
| 94 | // |
| 95 | // The function has the same return values as SetBytes() / SetString() |
| 96 | func (p *Paragraphscip-go gomod golang.org/x/text . `golang.org/x/text/unicode/bidi`/Paragraph#) prepareInputscip-go gomod golang.org/x/text . `golang.org/x/text/unicode/bidi`/Paragraph#prepareInput().() (n int, err error) { |
| 97 | p.runesscip-go gomod golang.org/x/text . `golang.org/x/text/unicode/bidi`/Paragraph#runes. = bytesscip-go gomod github.com/golang/go/src go1.25.0 bytes/.Runesscip-go gomod github.com/golang/go/src go1.25.0 bytes/Runes().(p.pscip-go gomod golang.org/x/text . `golang.org/x/text/unicode/bidi`/Paragraph#p.) |
| 98 | bytecount := 0 |
| 99 | // clear slices from previous SetString or SetBytes |
| 100 | p.pairTypesscip-go gomod golang.org/x/text . `golang.org/x/text/unicode/bidi`/Paragraph#pairTypes. = nil |
| 101 | p.pairValuesscip-go gomod golang.org/x/text . `golang.org/x/text/unicode/bidi`/Paragraph#pairValues. = nil |
| 102 | p.typesscip-go gomod golang.org/x/text . `golang.org/x/text/unicode/bidi`/Paragraph#types. = nil |
| 103 | |
| 104 | for _, r := range p.runesscip-go gomod golang.org/x/text . `golang.org/x/text/unicode/bidi`/Paragraph#runes. { |
| 105 | props, i := LookupRunescip-go gomod golang.org/x/text . `golang.org/x/text/unicode/bidi`/LookupRune().(r) |
| 106 | bytecount += i |
| 107 | cls := props.Classscip-go gomod golang.org/x/text . `golang.org/x/text/unicode/bidi`/Properties#Class().() |
| 108 | if cls == Bscip-go gomod golang.org/x/text . `golang.org/x/text/unicode/bidi`/B. { |
| 109 | return bytecount, nil |
| 110 | } |
| 111 | p.typesscip-go gomod golang.org/x/text . `golang.org/x/text/unicode/bidi`/Paragraph#types. = append(p.typesscip-go gomod golang.org/x/text . `golang.org/x/text/unicode/bidi`/Paragraph#types., cls) |
| 112 | if props.IsOpeningBracketscip-go gomod golang.org/x/text . `golang.org/x/text/unicode/bidi`/Properties#IsOpeningBracket().() { |
| 113 | p.pairTypesscip-go gomod golang.org/x/text . `golang.org/x/text/unicode/bidi`/Paragraph#pairTypes. = append(p.pairTypesscip-go gomod golang.org/x/text . `golang.org/x/text/unicode/bidi`/Paragraph#pairTypes., bpOpenscip-go gomod golang.org/x/text . `golang.org/x/text/unicode/bidi`/bpOpen.) |
| 114 | p.pairValuesscip-go gomod golang.org/x/text . `golang.org/x/text/unicode/bidi`/Paragraph#pairValues. = append(p.pairValuesscip-go gomod golang.org/x/text . `golang.org/x/text/unicode/bidi`/Paragraph#pairValues., r) |
| 115 | } else if props.IsBracketscip-go gomod golang.org/x/text . `golang.org/x/text/unicode/bidi`/Properties#IsBracket().() { |
| 116 | // this must be a closing bracket, |
| 117 | // since IsOpeningBracket is not true |
| 118 | p.pairTypesscip-go gomod golang.org/x/text . `golang.org/x/text/unicode/bidi`/Paragraph#pairTypes. = append(p.pairTypesscip-go gomod golang.org/x/text . `golang.org/x/text/unicode/bidi`/Paragraph#pairTypes., bpClosescip-go gomod golang.org/x/text . `golang.org/x/text/unicode/bidi`/bpClose.) |
| 119 | p.pairValuesscip-go gomod golang.org/x/text . `golang.org/x/text/unicode/bidi`/Paragraph#pairValues. = append(p.pairValuesscip-go gomod golang.org/x/text . `golang.org/x/text/unicode/bidi`/Paragraph#pairValues., r) |
| 120 | } else { |
| 121 | p.pairTypesscip-go gomod golang.org/x/text . `golang.org/x/text/unicode/bidi`/Paragraph#pairTypes. = append(p.pairTypesscip-go gomod golang.org/x/text . `golang.org/x/text/unicode/bidi`/Paragraph#pairTypes., bpNonescip-go gomod golang.org/x/text . `golang.org/x/text/unicode/bidi`/bpNone.) |
| 122 | p.pairValuesscip-go gomod golang.org/x/text . `golang.org/x/text/unicode/bidi`/Paragraph#pairValues. = append(p.pairValuesscip-go gomod golang.org/x/text . `golang.org/x/text/unicode/bidi`/Paragraph#pairValues., 0) |
| 123 | } |
| 124 | } |
| 125 | return bytecount, nil |
| 126 | } |
| 127 | |
| 128 | // SetBytes configures p for the given paragraph text. It replaces text |
| 129 | // previously set by SetBytes or SetString. If b contains a paragraph separator |
| 130 | // it will only process the first paragraph and report the number of bytes |
| 131 | // consumed from b including this separator. Error may be non-nil if options are |
| 132 | // given. |
| 133 | func (p *Paragraphscip-go gomod golang.org/x/text . `golang.org/x/text/unicode/bidi`/Paragraph#) SetBytesscip-go gomod golang.org/x/text . `golang.org/x/text/unicode/bidi`/Paragraph#SetBytes().(b []byte, opts ...Optionscip-go gomod golang.org/x/text . `golang.org/x/text/unicode/bidi`/Option#) (n int, err error) { |
| 134 | p.pscip-go gomod golang.org/x/text . `golang.org/x/text/unicode/bidi`/Paragraph#p. = b |
| 135 | p.optsscip-go gomod golang.org/x/text . `golang.org/x/text/unicode/bidi`/Paragraph#opts. = opts |
| 136 | return p.prepareInputscip-go gomod golang.org/x/text . `golang.org/x/text/unicode/bidi`/Paragraph#prepareInput().() |
| 137 | } |
| 138 | |
| 139 | // SetString configures s for the given paragraph text. It replaces text |
| 140 | // previously set by SetBytes or SetString. If s contains a paragraph separator |
| 141 | // it will only process the first paragraph and report the number of bytes |
| 142 | // consumed from s including this separator. Error may be non-nil if options are |
| 143 | // given. |
| 144 | func (p *Paragraphscip-go gomod golang.org/x/text . `golang.org/x/text/unicode/bidi`/Paragraph#) SetStringscip-go gomod golang.org/x/text . `golang.org/x/text/unicode/bidi`/Paragraph#SetString().(s string, opts ...Optionscip-go gomod golang.org/x/text . `golang.org/x/text/unicode/bidi`/Option#) (n int, err error) { |
| 145 | p.pscip-go gomod golang.org/x/text . `golang.org/x/text/unicode/bidi`/Paragraph#p. = []byte(s) |
| 146 | p.optsscip-go gomod golang.org/x/text . `golang.org/x/text/unicode/bidi`/Paragraph#opts. = opts |
| 147 | return p.prepareInputscip-go gomod golang.org/x/text . `golang.org/x/text/unicode/bidi`/Paragraph#prepareInput().() |
| 148 | } |
| 149 | |
| 150 | // IsLeftToRight reports whether the principle direction of rendering for this |
| 151 | // paragraphs is left-to-right. If this returns false, the principle direction |
| 152 | // of rendering is right-to-left. |
| 153 | func (p *Paragraphscip-go gomod golang.org/x/text . `golang.org/x/text/unicode/bidi`/Paragraph#) IsLeftToRightscip-go gomod golang.org/x/text . `golang.org/x/text/unicode/bidi`/Paragraph#IsLeftToRight().() bool { |
| 154 | return p.Directionscip-go gomod golang.org/x/text . `golang.org/x/text/unicode/bidi`/Paragraph#Direction().() == LeftToRightscip-go gomod golang.org/x/text . `golang.org/x/text/unicode/bidi`/LeftToRight. |
| 155 | } |
| 156 | |
| 157 | // Direction returns the direction of the text of this paragraph. |
| 158 | // |
| 159 | // The direction may be LeftToRight, RightToLeft, Mixed, or Neutral. |
| 160 | func (p *Paragraphscip-go gomod golang.org/x/text . `golang.org/x/text/unicode/bidi`/Paragraph#) Directionscip-go gomod golang.org/x/text . `golang.org/x/text/unicode/bidi`/Paragraph#Direction().() Directionscip-go gomod golang.org/x/text . `golang.org/x/text/unicode/bidi`/Direction# { |
| 161 | return p.oscip-go gomod golang.org/x/text . `golang.org/x/text/unicode/bidi`/Paragraph#o..Directionscip-go gomod golang.org/x/text . `golang.org/x/text/unicode/bidi`/Ordering#Direction().() |
| 162 | } |
| 163 | |
| 164 | // TODO: what happens if the position is > len(input)? This should return an error. |
| 165 | |
| 166 | // RunAt reports the Run at the given position of the input text. |
| 167 | // |
| 168 | // This method can be used for computing line breaks on paragraphs. |
| 169 | func (p *Paragraphscip-go gomod golang.org/x/text . `golang.org/x/text/unicode/bidi`/Paragraph#) RunAtscip-go gomod golang.org/x/text . `golang.org/x/text/unicode/bidi`/Paragraph#RunAt().(pos int) Runscip-go gomod golang.org/x/text . `golang.org/x/text/unicode/bidi`/Run# { |
| 170 | c := 0 |
| 171 | runNumber := 0 |
| 172 | for i, r := range p.oscip-go gomod golang.org/x/text . `golang.org/x/text/unicode/bidi`/Paragraph#o..runesscip-go gomod golang.org/x/text . `golang.org/x/text/unicode/bidi`/Ordering#runes. { |
| 173 | c += len(r) |
| 174 | if pos < c { |
| 175 | runNumber = i |
| 176 | } |
| 177 | } |
| 178 | return p.oscip-go gomod golang.org/x/text . `golang.org/x/text/unicode/bidi`/Paragraph#o..Runscip-go gomod golang.org/x/text . `golang.org/x/text/unicode/bidi`/Ordering#Run().(runNumber) |
| 179 | } |
| 180 | |
| 181 | func calculateOrderingscip-go gomod golang.org/x/text . `golang.org/x/text/unicode/bidi`/calculateOrdering().(levels []levelscip-go gomod golang.org/x/text . `golang.org/x/text/unicode/bidi`/level#, runes []rune) Orderingscip-go gomod golang.org/x/text . `golang.org/x/text/unicode/bidi`/Ordering# { |
| 182 | var curDir Directionscip-go gomod golang.org/x/text . `golang.org/x/text/unicode/bidi`/Direction# |
| 183 | |
| 184 | prevDir := Neutralscip-go gomod golang.org/x/text . `golang.org/x/text/unicode/bidi`/Neutral. |
| 185 | prevI := 0 |
| 186 | |
| 187 | o := Orderingscip-go gomod golang.org/x/text . `golang.org/x/text/unicode/bidi`/Ordering#{} |
| 188 | // lvl = 0,2,4,...: left to right |
| 189 | // lvl = 1,3,5,...: right to left |
| 190 | for i, lvl := range levels { |
| 191 | if lvl%2 == 0 { |
| 192 | curDir = LeftToRightscip-go gomod golang.org/x/text . `golang.org/x/text/unicode/bidi`/LeftToRight. |
| 193 | } else { |
| 194 | curDir = RightToLeftscip-go gomod golang.org/x/text . `golang.org/x/text/unicode/bidi`/RightToLeft. |
| 195 | } |
| 196 | if curDir != prevDir { |
| 197 | if i > 0 { |
| 198 | o.runesscip-go gomod golang.org/x/text . `golang.org/x/text/unicode/bidi`/Ordering#runes. = append(o.runesscip-go gomod golang.org/x/text . `golang.org/x/text/unicode/bidi`/Ordering#runes., runes[prevI:i]) |
| 199 | o.directionsscip-go gomod golang.org/x/text . `golang.org/x/text/unicode/bidi`/Ordering#directions. = append(o.directionsscip-go gomod golang.org/x/text . `golang.org/x/text/unicode/bidi`/Ordering#directions., prevDir) |
| 200 | o.startposscip-go gomod golang.org/x/text . `golang.org/x/text/unicode/bidi`/Ordering#startpos. = append(o.startposscip-go gomod golang.org/x/text . `golang.org/x/text/unicode/bidi`/Ordering#startpos., prevI) |
| 201 | } |
| 202 | prevI = i |
| 203 | prevDir = curDir |
| 204 | } |
| 205 | } |
| 206 | o.runesscip-go gomod golang.org/x/text . `golang.org/x/text/unicode/bidi`/Ordering#runes. = append(o.runesscip-go gomod golang.org/x/text . `golang.org/x/text/unicode/bidi`/Ordering#runes., runes[prevI:]) |
| 207 | o.directionsscip-go gomod golang.org/x/text . `golang.org/x/text/unicode/bidi`/Ordering#directions. = append(o.directionsscip-go gomod golang.org/x/text . `golang.org/x/text/unicode/bidi`/Ordering#directions., prevDir) |
| 208 | o.startposscip-go gomod golang.org/x/text . `golang.org/x/text/unicode/bidi`/Ordering#startpos. = append(o.startposscip-go gomod golang.org/x/text . `golang.org/x/text/unicode/bidi`/Ordering#startpos., prevI) |
| 209 | return o |
| 210 | } |
| 211 | |
| 212 | // Order computes the visual ordering of all the runs in a Paragraph. |
| 213 | func (p *Paragraphscip-go gomod golang.org/x/text . `golang.org/x/text/unicode/bidi`/Paragraph#) Orderscip-go gomod golang.org/x/text . `golang.org/x/text/unicode/bidi`/Paragraph#Order().() (Orderingscip-go gomod golang.org/x/text . `golang.org/x/text/unicode/bidi`/Ordering#, error) { |
| 214 | if len(p.typesscip-go gomod golang.org/x/text . `golang.org/x/text/unicode/bidi`/Paragraph#types.) == 0 { |
| 215 | return Orderingscip-go gomod golang.org/x/text . `golang.org/x/text/unicode/bidi`/Ordering#{}, nil |
| 216 | } |
| 217 | |
| 218 | for _, fn := range p.optsscip-go gomod golang.org/x/text . `golang.org/x/text/unicode/bidi`/Paragraph#opts. { |
| 219 | fn(&p.optionsscip-go gomod golang.org/x/text . `golang.org/x/text/unicode/bidi`/Paragraph#options.) |
| 220 | } |
| 221 | lvl := levelscip-go gomod golang.org/x/text . `golang.org/x/text/unicode/bidi`/level#(-1) |
| 222 | if p.optionsscip-go gomod golang.org/x/text . `golang.org/x/text/unicode/bidi`/Paragraph#options..defaultDirectionscip-go gomod golang.org/x/text . `golang.org/x/text/unicode/bidi`/options#defaultDirection. == RightToLeftscip-go gomod golang.org/x/text . `golang.org/x/text/unicode/bidi`/RightToLeft. { |
| 223 | lvl = 1 |
| 224 | } |
| 225 | para, err := newParagraphscip-go gomod golang.org/x/text . `golang.org/x/text/unicode/bidi`/newParagraph().(p.typesscip-go gomod golang.org/x/text . `golang.org/x/text/unicode/bidi`/Paragraph#types., p.pairTypesscip-go gomod golang.org/x/text . `golang.org/x/text/unicode/bidi`/Paragraph#pairTypes., p.pairValuesscip-go gomod golang.org/x/text . `golang.org/x/text/unicode/bidi`/Paragraph#pairValues., lvl) |
| 226 | if err != nil { |
| 227 | return Orderingscip-go gomod golang.org/x/text . `golang.org/x/text/unicode/bidi`/Ordering#{}, err |
| 228 | } |
| 229 | |
| 230 | levels := para.getLevelsscip-go gomod golang.org/x/text . `golang.org/x/text/unicode/bidi`/paragraph#getLevels().([]int{len(p.typesscip-go gomod golang.org/x/text . `golang.org/x/text/unicode/bidi`/Paragraph#types.)}) |
| 231 | |
| 232 | p.oscip-go gomod golang.org/x/text . `golang.org/x/text/unicode/bidi`/Paragraph#o. = calculateOrderingscip-go gomod golang.org/x/text . `golang.org/x/text/unicode/bidi`/calculateOrdering().(levels, p.runesscip-go gomod golang.org/x/text . `golang.org/x/text/unicode/bidi`/Paragraph#runes.) |
| 233 | return p.oscip-go gomod golang.org/x/text . `golang.org/x/text/unicode/bidi`/Paragraph#o., nil |
| 234 | } |
| 235 | |
| 236 | // Line computes the visual ordering of runs for a single line starting and |
| 237 | // ending at the given positions in the original text. |
| 238 | func (p *Paragraphscip-go gomod golang.org/x/text . `golang.org/x/text/unicode/bidi`/Paragraph#) Linescip-go gomod golang.org/x/text . `golang.org/x/text/unicode/bidi`/Paragraph#Line().(start, end int) (Orderingscip-go gomod golang.org/x/text . `golang.org/x/text/unicode/bidi`/Ordering#, error) { |
| 239 | lineTypes := p.typesscip-go gomod golang.org/x/text . `golang.org/x/text/unicode/bidi`/Paragraph#types.[start:end] |
| 240 | para, err := newParagraphscip-go gomod golang.org/x/text . `golang.org/x/text/unicode/bidi`/newParagraph().(lineTypes, p.pairTypesscip-go gomod golang.org/x/text . `golang.org/x/text/unicode/bidi`/Paragraph#pairTypes.[start:end], p.pairValuesscip-go gomod golang.org/x/text . `golang.org/x/text/unicode/bidi`/Paragraph#pairValues.[start:end], -1) |
| 241 | if err != nil { |
| 242 | return Orderingscip-go gomod golang.org/x/text . `golang.org/x/text/unicode/bidi`/Ordering#{}, err |
| 243 | } |
| 244 | levels := para.getLevelsscip-go gomod golang.org/x/text . `golang.org/x/text/unicode/bidi`/paragraph#getLevels().([]int{len(lineTypes)}) |
| 245 | o := calculateOrderingscip-go gomod golang.org/x/text . `golang.org/x/text/unicode/bidi`/calculateOrdering().(levels, p.runesscip-go gomod golang.org/x/text . `golang.org/x/text/unicode/bidi`/Paragraph#runes.[start:end]) |
| 246 | return o, nil |
| 247 | } |
| 248 | |
| 249 | // An Ordering holds the computed visual order of runs of a Paragraph. Calling |
| 250 | // SetBytes or SetString on the originating Paragraph invalidates an Ordering. |
| 251 | // The methods of an Ordering should only be called by one goroutine at a time. |
| 252 | type Orderingscip-go gomod golang.org/x/text . `golang.org/x/text/unicode/bidi`/Ordering# struct { |
| 253 | runesscip-go gomod golang.org/x/text . `golang.org/x/text/unicode/bidi`/Ordering#runes. [][]rune |
| 254 | directionsscip-go gomod golang.org/x/text . `golang.org/x/text/unicode/bidi`/Ordering#directions. []Directionscip-go gomod golang.org/x/text . `golang.org/x/text/unicode/bidi`/Direction# |
| 255 | startposscip-go gomod golang.org/x/text . `golang.org/x/text/unicode/bidi`/Ordering#startpos. []int |
| 256 | } |
| 257 | |
| 258 | // Direction reports the directionality of the runs. |
| 259 | // |
| 260 | // The direction may be LeftToRight, RightToLeft, Mixed, or Neutral. |
| 261 | func (o *Orderingscip-go gomod golang.org/x/text . `golang.org/x/text/unicode/bidi`/Ordering#) Directionscip-go gomod golang.org/x/text . `golang.org/x/text/unicode/bidi`/Ordering#Direction().() Directionscip-go gomod golang.org/x/text . `golang.org/x/text/unicode/bidi`/Direction# { |
| 262 | return o.directionsscip-go gomod golang.org/x/text . `golang.org/x/text/unicode/bidi`/Ordering#directions.[0] |
| 263 | } |
| 264 | |
| 265 | // NumRuns returns the number of runs. |
| 266 | func (o *Orderingscip-go gomod golang.org/x/text . `golang.org/x/text/unicode/bidi`/Ordering#) NumRunsscip-go gomod golang.org/x/text . `golang.org/x/text/unicode/bidi`/Ordering#NumRuns().() int { |
| 267 | return len(o.runesscip-go gomod golang.org/x/text . `golang.org/x/text/unicode/bidi`/Ordering#runes.) |
| 268 | } |
| 269 | |
| 270 | // Run returns the ith run within the ordering. |
| 271 | func (o *Orderingscip-go gomod golang.org/x/text . `golang.org/x/text/unicode/bidi`/Ordering#) Runscip-go gomod golang.org/x/text . `golang.org/x/text/unicode/bidi`/Ordering#Run().(i int) Runscip-go gomod golang.org/x/text . `golang.org/x/text/unicode/bidi`/Run# { |
| 272 | r := Runscip-go gomod golang.org/x/text . `golang.org/x/text/unicode/bidi`/Run#{ |
| 273 | runesscip-go gomod golang.org/x/text . `golang.org/x/text/unicode/bidi`/Run#runes.: o.runesscip-go gomod golang.org/x/text . `golang.org/x/text/unicode/bidi`/Ordering#runes.[i], |
| 274 | directionscip-go gomod golang.org/x/text . `golang.org/x/text/unicode/bidi`/Run#direction.: o.directionsscip-go gomod golang.org/x/text . `golang.org/x/text/unicode/bidi`/Ordering#directions.[i], |
| 275 | startposscip-go gomod golang.org/x/text . `golang.org/x/text/unicode/bidi`/Run#startpos.: o.startposscip-go gomod golang.org/x/text . `golang.org/x/text/unicode/bidi`/Ordering#startpos.[i], |
| 276 | } |
| 277 | return r |
| 278 | } |
| 279 | |
| 280 | // TODO: perhaps with options. |
| 281 | // // Reorder creates a reader that reads the runes in visual order per character. |
| 282 | // // Modifiers remain after the runes they modify. |
| 283 | // func (l *Runs) Reorder() io.Reader { |
| 284 | // panic("unimplemented") |
| 285 | // } |
| 286 | |
| 287 | // A Run is a continuous sequence of characters of a single direction. |
| 288 | type Runscip-go gomod golang.org/x/text . `golang.org/x/text/unicode/bidi`/Run# struct { |
| 289 | runesscip-go gomod golang.org/x/text . `golang.org/x/text/unicode/bidi`/Run#runes. []rune |
| 290 | directionscip-go gomod golang.org/x/text . `golang.org/x/text/unicode/bidi`/Run#direction. Directionscip-go gomod golang.org/x/text . `golang.org/x/text/unicode/bidi`/Direction# |
| 291 | startposscip-go gomod golang.org/x/text . `golang.org/x/text/unicode/bidi`/Run#startpos. int |
| 292 | } |
| 293 | |
| 294 | // String returns the text of the run in its original order. |
| 295 | func (r *Runscip-go gomod golang.org/x/text . `golang.org/x/text/unicode/bidi`/Run#) Stringscip-go gomod golang.org/x/text . `golang.org/x/text/unicode/bidi`/Run#String().() string { |
| 296 | return string(r.runesscip-go gomod golang.org/x/text . `golang.org/x/text/unicode/bidi`/Run#runes.) |
| 297 | } |
| 298 | |
| 299 | // Bytes returns the text of the run in its original order. |
| 300 | func (r *Runscip-go gomod golang.org/x/text . `golang.org/x/text/unicode/bidi`/Run#) Bytesscip-go gomod golang.org/x/text . `golang.org/x/text/unicode/bidi`/Run#Bytes().() []byte { |
| 301 | return []byte(r.Stringscip-go gomod golang.org/x/text . `golang.org/x/text/unicode/bidi`/Run#String().()) |
| 302 | } |
| 303 | |
| 304 | // TODO: methods for |
| 305 | // - Display order |
| 306 | // - headers and footers |
| 307 | // - bracket replacement. |
| 308 | |
| 309 | // Direction reports the direction of the run. |
| 310 | func (r *Runscip-go gomod golang.org/x/text . `golang.org/x/text/unicode/bidi`/Run#) Directionscip-go gomod golang.org/x/text . `golang.org/x/text/unicode/bidi`/Run#Direction().() Directionscip-go gomod golang.org/x/text . `golang.org/x/text/unicode/bidi`/Direction# { |
| 311 | return r.directionscip-go gomod golang.org/x/text . `golang.org/x/text/unicode/bidi`/Run#direction. |
| 312 | } |
| 313 | |
| 314 | // Pos returns the position of the Run within the text passed to SetBytes or SetString of the |
| 315 | // originating Paragraph value. |
| 316 | func (r *Runscip-go gomod golang.org/x/text . `golang.org/x/text/unicode/bidi`/Run#) Posscip-go gomod golang.org/x/text . `golang.org/x/text/unicode/bidi`/Run#Pos().() (start, end int) { |
| 317 | return r.startposscip-go gomod golang.org/x/text . `golang.org/x/text/unicode/bidi`/Run#startpos., r.startposscip-go gomod golang.org/x/text . `golang.org/x/text/unicode/bidi`/Run#startpos. + len(r.runesscip-go gomod golang.org/x/text . `golang.org/x/text/unicode/bidi`/Run#runes.) - 1 |
| 318 | } |
| 319 | |
| 320 | // AppendReverse reverses the order of characters of in, appends them to out, |
| 321 | // and returns the result. Modifiers will still follow the runes they modify. |
| 322 | // Brackets are replaced with their counterparts. |
| 323 | func AppendReversescip-go gomod golang.org/x/text . `golang.org/x/text/unicode/bidi`/AppendReverse().(out, in []byte) []byte { |
| 324 | ret := make([]byte, len(in)+len(out)) |
| 325 | copy(ret, out) |
| 326 | inRunes := bytesscip-go gomod github.com/golang/go/src go1.25.0 bytes/.Runesscip-go gomod github.com/golang/go/src go1.25.0 bytes/Runes().(in) |
| 327 | |
| 328 | for i, r := range inRunes { |
| 329 | prop, _ := LookupRunescip-go gomod golang.org/x/text . `golang.org/x/text/unicode/bidi`/LookupRune().(r) |
| 330 | if prop.IsBracketscip-go gomod golang.org/x/text . `golang.org/x/text/unicode/bidi`/Properties#IsBracket().() { |
| 331 | inRunes[i] = prop.reverseBracketscip-go gomod golang.org/x/text . `golang.org/x/text/unicode/bidi`/Properties#reverseBracket().(r) |
| 332 | } |
| 333 | } |
| 334 | |
| 335 | for i, j := 0, len(inRunes)-1; i < j; i, j = i+1, j-1 { |
| 336 | inRunes[i], inRunes[j] = inRunes[j], inRunes[i] |
| 337 | } |
| 338 | copy(ret[len(out):], string(inRunes)) |
| 339 | |
| 340 | return ret |
| 341 | } |
| 342 | |
| 343 | // ReverseString reverses the order of characters in s and returns a new string. |
| 344 | // Modifiers will still follow the runes they modify. Brackets are replaced with |
| 345 | // their counterparts. |
| 346 | func ReverseStringscip-go gomod golang.org/x/text . `golang.org/x/text/unicode/bidi`/ReverseString().(s string) string { |
| 347 | input := []rune(s) |
| 348 | li := len(input) |
| 349 | ret := make([]rune, li) |
| 350 | for i, r := range input { |
| 351 | prop, _ := LookupRunescip-go gomod golang.org/x/text . `golang.org/x/text/unicode/bidi`/LookupRune().(r) |
| 352 | if prop.IsBracketscip-go gomod golang.org/x/text . `golang.org/x/text/unicode/bidi`/Properties#IsBracket().() { |
| 353 | ret[li-i-1] = prop.reverseBracketscip-go gomod golang.org/x/text . `golang.org/x/text/unicode/bidi`/Properties#reverseBracket().(r) |
| 354 | } else { |
| 355 | ret[li-i-1] = r |
| 356 | } |
| 357 | } |
| 358 | return string(ret) |
| 359 | } |