Skip to content

Commit fb21995

Browse files
committed
x/text: bidi incorrect brackets matching
Brackets are now ordered correctly in the bidi algorithm. While parsing the input, the pairValues slice is created with correct bracket data. The algorithm in matchOpener from brackets.go checks if the opening and closing values are equal. Therefore the unicode value of the closing bracket needs to be changed to the opening bracket.
1 parent 566b44f commit fb21995

File tree

2 files changed

+40
-8
lines changed

2 files changed

+40
-8
lines changed

unicode/bidi/bidi.go

+8-8
Original file line numberDiff line numberDiff line change
@@ -109,17 +109,17 @@ func (p *Paragraph) prepareInput() (n int, err error) {
109109
return bytecount, nil
110110
}
111111
p.types = append(p.types, cls)
112-
if props.IsOpeningBracket() {
112+
113+
switch {
114+
case !props.IsBracket():
115+
p.pairTypes = append(p.pairTypes, bpNone)
116+
p.pairValues = append(p.pairValues, 0)
117+
case props.IsOpeningBracket():
113118
p.pairTypes = append(p.pairTypes, bpOpen)
114119
p.pairValues = append(p.pairValues, r)
115-
} else if props.IsBracket() {
116-
// this must be a closing bracket,
117-
// since IsOpeningBracket is not true
120+
default:
118121
p.pairTypes = append(p.pairTypes, bpClose)
119-
p.pairValues = append(p.pairValues, r)
120-
} else {
121-
p.pairTypes = append(p.pairTypes, bpNone)
122-
p.pairValues = append(p.pairValues, 0)
122+
p.pairValues = append(p.pairValues, props.reverseBracket(r))
123123
}
124124
}
125125
return bytecount, nil

unicode/bidi/bidi_test.go

+32
Original file line numberDiff line numberDiff line change
@@ -345,3 +345,35 @@ func TestAppendReverse(t *testing.T) {
345345
}
346346

347347
}
348+
349+
func TestBracket(t *testing.T) {
350+
str := `ع a (b)`
351+
p := Paragraph{}
352+
p.SetString(str, DefaultDirection(LeftToRight))
353+
order, err := p.Order()
354+
if err != nil {
355+
log.Fatal(err)
356+
}
357+
358+
expectedRuns := []runInformation{
359+
{"ع ", RightToLeft, 0, 1},
360+
{"a (b)", LeftToRight, 2, 6},
361+
}
362+
363+
if nr, want := order.NumRuns(), len(expectedRuns); nr != want {
364+
t.Errorf("order.NumRuns() = %d; want %d", nr, want)
365+
}
366+
367+
for i, want := range expectedRuns {
368+
r := order.Run(i)
369+
if got := r.String(); got != want.str {
370+
t.Errorf("Run(%d) = %q; want %q", i, got, want.str)
371+
}
372+
if s, e := r.Pos(); s != want.start || e != want.end {
373+
t.Errorf("Run(%d).start = %d, .end = %d; want start = %d, end = %d", i, s, e, want.start, want.end)
374+
}
375+
if d := r.Direction(); d != want.dir {
376+
t.Errorf("Run(%d).Direction = %d; want %d", i, d, want.dir)
377+
}
378+
}
379+
}

0 commit comments

Comments
 (0)