Source File
bidirule.go
Belonging Package
golang.org/x/text/secure/bidirule
package bidirule
import (
)
var ErrInvalid = errors.New("bidirule: failed Bidi Rule")
type ruleState uint8
const (
ruleInitial ruleState = iota
ruleLTR
ruleLTRFinal
ruleRTL
ruleRTLFinal
ruleInvalid
)
type ruleTransition struct {
next ruleState
mask uint16
}
ruleInitial: {
{ruleLTRFinal, 1 << bidi.L},
{ruleRTLFinal, 1<<bidi.R | 1<<bidi.AL},
},
{ruleLTRFinal, 1<<bidi.L | 1<<bidi.EN},
{ruleLTR, 1<<bidi.ES | 1<<bidi.CS | 1<<bidi.ET | 1<<bidi.ON | 1<<bidi.BN},
},
ruleInvalid: {
{ruleInvalid, 0},
{ruleInvalid, 0},
},
}
func ( string) bool {
var Transformer
if , := .advanceString(); ! || < len() {
return false
}
return .isFinal()
}
func () *Transformer {
return &Transformer{}
}
func ( *Transformer) () { * = Transformer{} }
func ( *Transformer) ( []byte, bool) ( int, error) {
if .state == ruleInvalid && .isRTL() {
return 0, ErrInvalid
}
, := .advance()
switch {
case !:
= ErrInvalid
case < len():
if ! {
= transform.ErrShortSrc
break
}
= ErrInvalid
case !.isFinal():
= ErrInvalid
}
return ,
}
var asciiTable [128]bidi.Properties
func () {
for := range asciiTable {
, := bidi.LookupRune(rune())
asciiTable[] =
}
}
func ( *Transformer) ( []byte) ( int, bool) {
var bidi.Properties
var int
for < len() {
if [] < utf8.RuneSelf {
, = asciiTable[[]], 1
} else {
, = bidi.Lookup([:])
if <= 1 {
:= uint16(1 << .Class())
.seen |=
if .seen&exclusiveRTL == exclusiveRTL {
.state = ruleInvalid
return , false
}
switch := transitions[.state]; {
case [0].mask& != 0:
.state = [0].next
case [1].mask& != 0:
.state = [1].next
default:
.state = ruleInvalid
if .isRTL() {
return , false
}
}
+=
}
return , true
}
func ( *Transformer) ( string) ( int, bool) {
var bidi.Properties
var int
for < len() {
if [] < utf8.RuneSelf {
, = asciiTable[[]], 1
} else {
, = bidi.LookupString([:])
if <= 1 {
if == 1 {
return , false // invalid UTF-8
}
return , true // incomplete UTF-8 encoding
}
:= uint16(1 << .Class())
.seen |=
if .seen&exclusiveRTL == exclusiveRTL {
.state = ruleInvalid
return , false
}
switch := transitions[.state]; {
case [0].mask& != 0:
.state = [0].next
case [1].mask& != 0:
.state = [1].next
default:
.state = ruleInvalid
if .isRTL() {
return , false
}
}
+=
}
return , true
![]() |
The pages are generated with Golds v0.3.2-preview. (GOOS=darwin GOARCH=amd64) Golds is a Go 101 project developed by Tapir Liu. PR and bug reports are welcome and can be submitted to the issue list. Please follow @Go100and1 (reachable from the left QR code) to get the latest news of Golds. |