Skip to content

Commit a7c0236

Browse files
deepakjoismpvl
authored andcommitted
bidi: support for changes to bidi algorithm in Unicode 8.0
Code changes to support: - updated rules X5a,X5b and X6a in Unicode 8.0 - max depth for nested brackets in Unicode 8.0 - updated definitions BD14 and BD15 in Unicode 8.0 - clarifications to rule N0 in Unicode 8.0 To makes the tests pass again: - use par.getLevels() instead of using par.resultLevels directly in test file Change-Id: I63bdba11d91932e62e830ce9b71b1a6c6261abb0 Reviewed-on: https://go-review.googlesource.com/29630 Reviewed-by: Sam Whited <[email protected]> Reviewed-by: Marcel van Lohuizen <[email protected]> Run-TryBot: Marcel van Lohuizen <[email protected]> TryBot-Result: Gobot Gobot <[email protected]>
1 parent 9c8be9c commit a7c0236

File tree

3 files changed

+40
-9
lines changed

3 files changed

+40
-9
lines changed

unicode/bidi/bracket.go

+35-7
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ func resolvePairedBrackets(s *isolatingRunSequence) {
8484
dirEmbed = R
8585
}
8686
p.locateBrackets(s.p.pairTypes, s.p.pairValues)
87-
p.resolveBrackets(dirEmbed)
87+
p.resolveBrackets(dirEmbed, s.p.initialTypes)
8888
}
8989

9090
type bracketPairer struct {
@@ -125,6 +125,8 @@ func (p *bracketPairer) matchOpener(pairValues []rune, opener, closer int) bool
125125
return pairValues[p.indexes[opener]] == pairValues[p.indexes[closer]]
126126
}
127127

128+
const maxPairingDepth = 63
129+
128130
// locateBrackets locates matching bracket pairs according to BD16.
129131
//
130132
// This implementation uses a linked list instead of a stack, because, while
@@ -136,11 +138,17 @@ func (p *bracketPairer) locateBrackets(pairTypes []bracketType, pairValues []run
136138
for i, index := range p.indexes {
137139

138140
// look at the bracket type for each character
139-
switch pairTypes[index] {
140-
case bpNone:
141+
if pairTypes[index] == bpNone || p.codesIsolatedRun[i] != ON {
141142
// continue scanning
142-
143+
continue
144+
}
145+
switch pairTypes[index] {
143146
case bpOpen:
147+
// check if maximum pairing depth reached
148+
if p.openers.Len() == maxPairingDepth {
149+
p.openers.Init()
150+
return
151+
}
144152
// remember opener location, most recent first
145153
p.openers.PushFront(i)
146154

@@ -270,7 +278,7 @@ func (p *bracketPairer) classBeforePair(loc bracketPair) Class {
270278
}
271279

272280
// assignBracketType implements rule N0 for a single bracket pair.
273-
func (p *bracketPairer) assignBracketType(loc bracketPair, dirEmbed Class) {
281+
func (p *bracketPairer) assignBracketType(loc bracketPair, dirEmbed Class, initialTypes []Class) {
274282
// rule "N0, a", inspect contents of pair
275283
dirPair := p.classifyPairContent(loc, dirEmbed)
276284

@@ -295,13 +303,33 @@ func (p *bracketPairer) assignBracketType(loc bracketPair, dirEmbed Class) {
295303
// direction
296304

297305
// set the bracket types to the type found
306+
p.setBracketsToType(loc, dirPair, initialTypes)
307+
}
308+
309+
func (p *bracketPairer) setBracketsToType(loc bracketPair, dirPair Class, initialTypes []Class) {
298310
p.codesIsolatedRun[loc.opener] = dirPair
299311
p.codesIsolatedRun[loc.closer] = dirPair
312+
313+
for i := loc.opener + 1; i < loc.closer; i++ {
314+
index := p.indexes[i]
315+
if initialTypes[index] != NSM {
316+
break
317+
}
318+
p.codesIsolatedRun[i] = dirPair
319+
}
320+
321+
for i := loc.closer + 1; i < len(p.indexes); i++ {
322+
index := p.indexes[i]
323+
if initialTypes[index] != NSM {
324+
break
325+
}
326+
p.codesIsolatedRun[i] = dirPair
327+
}
300328
}
301329

302330
// resolveBrackets implements rule N0 for a list of pairs.
303-
func (p *bracketPairer) resolveBrackets(dirEmbed Class) {
331+
func (p *bracketPairer) resolveBrackets(dirEmbed Class, initialTypes []Class) {
304332
for _, loc := range p.pairPositions {
305-
p.assignBracketType(loc, dirEmbed)
333+
p.assignBracketType(loc, dirEmbed, initialTypes)
306334
}
307335
}

unicode/bidi/core.go

+3
Original file line numberDiff line numberDiff line change
@@ -309,6 +309,9 @@ func (p *paragraph) determineExplicitEmbeddingLevels() {
309309
}
310310
if isIsolate {
311311
p.resultLevels[i] = stack.lastEmbeddingLevel()
312+
if stack.lastDirectionalOverrideStatus() != ON {
313+
p.resultTypes[i] = stack.lastDirectionalOverrideStatus()
314+
}
312315
}
313316

314317
var newLevel level

unicode/bidi/core_test.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ func TestBidiCore(t *testing.T) {
5858
par := newParagraph(types, pairTypes, pairValues, lev)
5959

6060
if *testLevels {
61-
levels := par.resultLevels
61+
levels := par.getLevels([]int{len(types)})
6262
for i, s := range wantLevels {
6363
if s == "x" {
6464
continue
@@ -150,7 +150,7 @@ func TestBidiCharacters(t *testing.T) {
150150
}
151151

152152
if *testLevels {
153-
gotLevels := getLevelStrings(types, par.resultLevels)
153+
gotLevels := getLevelStrings(types, par.getLevels([]int{len(types)}))
154154
if got, want := fmt.Sprint(gotLevels), fmt.Sprint(wantLevels); got != want {
155155
t.Errorf("%04X %q:%d: got %v; want %v\nval: %x\npair: %v", runes, string(runes), parLevel, got, want, pairValues, pairTypes)
156156
}

0 commit comments

Comments
 (0)