Commit cfa036ae authored by Russ Cox's avatar Russ Cox

old/regexp, old/template, template: use rune

Nothing terribly interesting here.

R=r, gri
CC=golang-dev
https://golang.org/cl/5308042
parent 49116220
...@@ -119,7 +119,7 @@ type instr struct { ...@@ -119,7 +119,7 @@ type instr struct {
index int // used only in debugging; could be eliminated index int // used only in debugging; could be eliminated
next *instr // the instruction to execute after this one next *instr // the instruction to execute after this one
// Special fields valid only for some items. // Special fields valid only for some items.
char int // iChar char rune // iChar
braNum int // iBra, iEbra braNum int // iBra, iEbra
cclass *charClass // iCharClass cclass *charClass // iCharClass
left *instr // iAlt, other branch left *instr // iAlt, other branch
...@@ -172,8 +172,8 @@ type Regexp struct { ...@@ -172,8 +172,8 @@ type Regexp struct {
type charClass struct { type charClass struct {
negate bool // is character class negated? ([^a-z]) negate bool // is character class negated? ([^a-z])
// slice of int, stored pairwise: [a-z] is (a,z); x is (x,x): // slice of int, stored pairwise: [a-z] is (a,z); x is (x,x):
ranges []int ranges []rune
cmin, cmax int cmin, cmax rune
} }
func (cclass *charClass) print() { func (cclass *charClass) print() {
...@@ -192,7 +192,7 @@ func (cclass *charClass) print() { ...@@ -192,7 +192,7 @@ func (cclass *charClass) print() {
} }
} }
func (cclass *charClass) addRange(a, b int) { func (cclass *charClass) addRange(a, b rune) {
// range is a through b inclusive // range is a through b inclusive
cclass.ranges = append(cclass.ranges, a, b) cclass.ranges = append(cclass.ranges, a, b)
if a < cclass.cmin { if a < cclass.cmin {
...@@ -203,7 +203,7 @@ func (cclass *charClass) addRange(a, b int) { ...@@ -203,7 +203,7 @@ func (cclass *charClass) addRange(a, b int) {
} }
} }
func (cclass *charClass) matches(c int) bool { func (cclass *charClass) matches(c rune) bool {
if c < cclass.cmin || c > cclass.cmax { if c < cclass.cmin || c > cclass.cmax {
return cclass.negate return cclass.negate
} }
...@@ -219,7 +219,7 @@ func (cclass *charClass) matches(c int) bool { ...@@ -219,7 +219,7 @@ func (cclass *charClass) matches(c int) bool {
func newCharClass() *instr { func newCharClass() *instr {
i := &instr{kind: iCharClass} i := &instr{kind: iCharClass}
i.cclass = new(charClass) i.cclass = new(charClass)
i.cclass.ranges = make([]int, 0, 4) i.cclass.ranges = make([]rune, 0, 4)
i.cclass.cmin = 0x10FFFF + 1 // MaxRune + 1 i.cclass.cmin = 0x10FFFF + 1 // MaxRune + 1
i.cclass.cmax = -1 i.cclass.cmax = -1
return i return i
...@@ -235,7 +235,7 @@ type parser struct { ...@@ -235,7 +235,7 @@ type parser struct {
re *Regexp re *Regexp
nlpar int // number of unclosed lpars nlpar int // number of unclosed lpars
pos int pos int
ch int ch rune
} }
func (p *parser) error(err Error) { func (p *parser) error(err Error) {
...@@ -244,9 +244,9 @@ func (p *parser) error(err Error) { ...@@ -244,9 +244,9 @@ func (p *parser) error(err Error) {
const endOfText = -1 const endOfText = -1
func (p *parser) c() int { return p.ch } func (p *parser) c() rune { return p.ch }
func (p *parser) nextc() int { func (p *parser) nextc() rune {
if p.pos >= len(p.re.expr) { if p.pos >= len(p.re.expr) {
p.ch = endOfText p.ch = endOfText
} else { } else {
...@@ -264,7 +264,7 @@ func newParser(re *Regexp) *parser { ...@@ -264,7 +264,7 @@ func newParser(re *Regexp) *parser {
return p return p
} }
func special(c int) bool { func special(c rune) bool {
for _, r := range `\.+*?()|[]^$` { for _, r := range `\.+*?()|[]^$` {
if c == r { if c == r {
return true return true
...@@ -273,7 +273,7 @@ func special(c int) bool { ...@@ -273,7 +273,7 @@ func special(c int) bool {
return false return false
} }
func ispunct(c int) bool { func ispunct(c rune) bool {
for _, r := range "!\"#$%&'()*+,-./:;<=>?@[\\]^_`{|}~" { for _, r := range "!\"#$%&'()*+,-./:;<=>?@[\\]^_`{|}~" {
if c == r { if c == r {
return true return true
...@@ -285,16 +285,16 @@ func ispunct(c int) bool { ...@@ -285,16 +285,16 @@ func ispunct(c int) bool {
var escapes = []byte("abfnrtv") var escapes = []byte("abfnrtv")
var escaped = []byte("\a\b\f\n\r\t\v") var escaped = []byte("\a\b\f\n\r\t\v")
func escape(c int) int { func escape(c rune) int {
for i, b := range escapes { for i, b := range escapes {
if int(b) == c { if rune(b) == c {
return i return i
} }
} }
return -1 return -1
} }
func (p *parser) checkBackslash() int { func (p *parser) checkBackslash() rune {
c := p.c() c := p.c()
if c == '\\' { if c == '\\' {
c = p.nextc() c = p.nextc()
...@@ -304,7 +304,7 @@ func (p *parser) checkBackslash() int { ...@@ -304,7 +304,7 @@ func (p *parser) checkBackslash() int {
case ispunct(c): case ispunct(c):
// c is as delivered // c is as delivered
case escape(c) >= 0: case escape(c) >= 0:
c = int(escaped[escape(c)]) c = rune(escaped[escape(c)])
default: default:
p.error(ErrBadBackslash) p.error(ErrBadBackslash)
} }
...@@ -319,7 +319,7 @@ func (p *parser) charClass() *instr { ...@@ -319,7 +319,7 @@ func (p *parser) charClass() *instr {
cc.negate = true cc.negate = true
p.nextc() p.nextc()
} }
left := -1 left := rune(-1)
for { for {
switch c := p.c(); c { switch c := p.c(); c {
case ']', endOfText: case ']', endOfText:
...@@ -751,8 +751,8 @@ func (a *matchArena) addState(s []state, inst *instr, prefixed bool, match *matc ...@@ -751,8 +751,8 @@ func (a *matchArena) addState(s []state, inst *instr, prefixed bool, match *matc
// input abstracts different representations of the input text. It provides // input abstracts different representations of the input text. It provides
// one-character lookahead. // one-character lookahead.
type input interface { type input interface {
step(pos int) (rune int, width int) // advance one rune step(pos int) (r rune, width int) // advance one rune
canCheckPrefix() bool // can we look ahead without losing info? canCheckPrefix() bool // can we look ahead without losing info?
hasPrefix(re *Regexp) bool hasPrefix(re *Regexp) bool
index(re *Regexp, pos int) int index(re *Regexp, pos int) int
} }
...@@ -766,7 +766,7 @@ func newInputString(str string) *inputString { ...@@ -766,7 +766,7 @@ func newInputString(str string) *inputString {
return &inputString{str: str} return &inputString{str: str}
} }
func (i *inputString) step(pos int) (int, int) { func (i *inputString) step(pos int) (rune, int) {
if pos < len(i.str) { if pos < len(i.str) {
return utf8.DecodeRuneInString(i.str[pos:len(i.str)]) return utf8.DecodeRuneInString(i.str[pos:len(i.str)])
} }
...@@ -794,7 +794,7 @@ func newInputBytes(str []byte) *inputBytes { ...@@ -794,7 +794,7 @@ func newInputBytes(str []byte) *inputBytes {
return &inputBytes{str: str} return &inputBytes{str: str}
} }
func (i *inputBytes) step(pos int) (int, int) { func (i *inputBytes) step(pos int) (rune, int) {
if pos < len(i.str) { if pos < len(i.str) {
return utf8.DecodeRune(i.str[pos:len(i.str)]) return utf8.DecodeRune(i.str[pos:len(i.str)])
} }
...@@ -824,7 +824,7 @@ func newInputReader(r io.RuneReader) *inputReader { ...@@ -824,7 +824,7 @@ func newInputReader(r io.RuneReader) *inputReader {
return &inputReader{r: r} return &inputReader{r: r}
} }
func (i *inputReader) step(pos int) (int, int) { func (i *inputReader) step(pos int) (rune, int) {
if !i.atEOT && pos != i.pos { if !i.atEOT && pos != i.pos {
return endOfText, 0 return endOfText, 0
...@@ -886,7 +886,7 @@ func (re *Regexp) doExecute(i input, pos int) []int { ...@@ -886,7 +886,7 @@ func (re *Regexp) doExecute(i input, pos int) []int {
atBOT: pos == 0, atBOT: pos == 0,
atEOT: nextChar == endOfText, atEOT: nextChar == endOfText,
} }
for c, startPos := 0, pos; c != endOfText; { for c, startPos := rune(0), pos; c != endOfText; {
if !found && (pos == startPos || !anchored) { if !found && (pos == startPos || !anchored) {
// prime the pump if we haven't seen a match yet // prime the pump if we haven't seen a match yet
match := arena.noMatch() match := arena.noMatch()
...@@ -966,7 +966,7 @@ func (re *Regexp) doExecute(i input, pos int) []int { ...@@ -966,7 +966,7 @@ func (re *Regexp) doExecute(i input, pos int) []int {
// of the regular expression re. It returns the boolean true if the // of the regular expression re. It returns the boolean true if the
// literal string comprises the entire regular expression. // literal string comprises the entire regular expression.
func (re *Regexp) LiteralPrefix() (prefix string, complete bool) { func (re *Regexp) LiteralPrefix() (prefix string, complete bool) {
c := make([]int, len(re.inst)-2) // minus start and end. c := make([]rune, len(re.inst)-2) // minus start and end.
// First instruction is start; skip that. // First instruction is start; skip that.
i := 0 i := 0
for inst := re.inst[0].next; inst.kind != iEnd; inst = inst.next { for inst := re.inst[0].next; inst.kind != iEnd; inst = inst.next {
...@@ -1141,7 +1141,7 @@ func QuoteMeta(s string) string { ...@@ -1141,7 +1141,7 @@ func QuoteMeta(s string) string {
// A byte loop is correct because all metacharacters are ASCII. // A byte loop is correct because all metacharacters are ASCII.
j := 0 j := 0
for i := 0; i < len(s); i++ { for i := 0; i < len(s); i++ {
if special(int(s[i])) { if special(rune(s[i])) {
b[j] = '\\' b[j] = '\\'
j++ j++
} }
......
...@@ -146,8 +146,8 @@ func (t *Template) parseError(err string, args ...interface{}) { ...@@ -146,8 +146,8 @@ func (t *Template) parseError(err string, args ...interface{}) {
// Is this an exported - upper case - name? // Is this an exported - upper case - name?
func isExported(name string) bool { func isExported(name string) bool {
rune, _ := utf8.DecodeRuneInString(name) r, _ := utf8.DecodeRuneInString(name)
return unicode.IsUpper(rune) return unicode.IsUpper(r)
} }
// -- Lexical analysis // -- Lexical analysis
...@@ -419,7 +419,7 @@ func (t *Template) newVariable(words []string) *variableElement { ...@@ -419,7 +419,7 @@ func (t *Template) newVariable(words []string) *variableElement {
case '"', '`', '\'': case '"', '`', '\'':
v, err := strconv.Unquote(word) v, err := strconv.Unquote(word)
if err == nil && word[0] == '\'' { if err == nil && word[0] == '\'' {
args[i] = []int(v)[0] args[i], _ = utf8.DecodeRuneInString(v)
} else { } else {
args[i], lerr = v, err args[i], lerr = v, err
} }
......
...@@ -644,7 +644,7 @@ func TestTree(t *testing.T) { ...@@ -644,7 +644,7 @@ func TestTree(t *testing.T) {
if err != nil { if err != nil {
t.Fatal("exec error:", err) t.Fatal("exec error:", err)
} }
stripSpace := func(r int) int { stripSpace := func(r rune) rune {
if r == '\t' || r == '\n' { if r == '\t' || r == '\n' {
return -1 return -1
} }
......
...@@ -279,7 +279,7 @@ func JSEscape(w io.Writer, b []byte) { ...@@ -279,7 +279,7 @@ func JSEscape(w io.Writer, b []byte) {
for i := 0; i < len(b); i++ { for i := 0; i < len(b); i++ {
c := b[i] c := b[i]
if !jsIsSpecial(int(c)) { if !jsIsSpecial(rune(c)) {
// fast path: nothing to do // fast path: nothing to do
continue continue
} }
...@@ -307,12 +307,12 @@ func JSEscape(w io.Writer, b []byte) { ...@@ -307,12 +307,12 @@ func JSEscape(w io.Writer, b []byte) {
} }
} else { } else {
// Unicode rune. // Unicode rune.
rune, size := utf8.DecodeRune(b[i:]) r, size := utf8.DecodeRune(b[i:])
if unicode.IsPrint(rune) { if unicode.IsPrint(r) {
w.Write(b[i : i+size]) w.Write(b[i : i+size])
} else { } else {
// TODO(dsymonds): Do this without fmt? // TODO(dsymonds): Do this without fmt?
fmt.Fprintf(w, "\\u%04X", rune) fmt.Fprintf(w, "\\u%04X", r)
} }
i += size - 1 i += size - 1
} }
...@@ -332,12 +332,12 @@ func JSEscapeString(s string) string { ...@@ -332,12 +332,12 @@ func JSEscapeString(s string) string {
return b.String() return b.String()
} }
func jsIsSpecial(rune int) bool { func jsIsSpecial(r rune) bool {
switch rune { switch r {
case '\\', '\'', '"', '<', '>': case '\\', '\'', '"', '<', '>':
return true return true
} }
return rune < ' ' || utf8.RuneSelf <= rune return r < ' ' || utf8.RuneSelf <= r
} }
// JSEscaper returns the escaped JavaScript equivalent of the textual // JSEscaper returns the escaped JavaScript equivalent of the textual
......
...@@ -131,21 +131,21 @@ type lexer struct { ...@@ -131,21 +131,21 @@ type lexer struct {
} }
// next returns the next rune in the input. // next returns the next rune in the input.
func (l *lexer) next() (rune int) { func (l *lexer) next() (r rune) {
if l.pos >= len(l.input) { if l.pos >= len(l.input) {
l.width = 0 l.width = 0
return eof return eof
} }
rune, l.width = utf8.DecodeRuneInString(l.input[l.pos:]) r, l.width = utf8.DecodeRuneInString(l.input[l.pos:])
l.pos += l.width l.pos += l.width
return rune return r
} }
// peek returns but does not consume the next rune in the input. // peek returns but does not consume the next rune in the input.
func (l *lexer) peek() int { func (l *lexer) peek() rune {
rune := l.next() r := l.next()
l.backup() l.backup()
return rune return r
} }
// backup steps back one rune. Can only be called once per call of next. // backup steps back one rune. Can only be called once per call of next.
...@@ -468,7 +468,7 @@ Loop: ...@@ -468,7 +468,7 @@ Loop:
} }
// isSpace reports whether r is a space character. // isSpace reports whether r is a space character.
func isSpace(r int) bool { func isSpace(r rune) bool {
switch r { switch r {
case ' ', '\t', '\n', '\r': case ' ', '\t', '\n', '\r':
return true return true
...@@ -477,6 +477,6 @@ func isSpace(r int) bool { ...@@ -477,6 +477,6 @@ func isSpace(r int) bool {
} }
// isAlphaNumeric reports whether r is an alphabetic, digit, or underscore. // isAlphaNumeric reports whether r is an alphabetic, digit, or underscore.
func isAlphaNumeric(r int) bool { func isAlphaNumeric(r rune) bool {
return r == '_' || unicode.IsLetter(r) || unicode.IsDigit(r) return r == '_' || unicode.IsLetter(r) || unicode.IsDigit(r)
} }
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment