Commit 49116220 authored by Russ Cox's avatar Russ Cox

exp/template/html: use rune

Nothing terribly interesting here.

R=mikesamuel, nigeltao, r
CC=golang-dev
https://golang.org/cl/5307044
parent 8f571817
...@@ -35,19 +35,19 @@ func endsWithCSSKeyword(b []byte, kw string) bool { ...@@ -35,19 +35,19 @@ func endsWithCSSKeyword(b []byte, kw string) bool {
} }
// isCSSNmchar returns whether rune is allowed anywhere in a CSS identifier. // isCSSNmchar returns whether rune is allowed anywhere in a CSS identifier.
func isCSSNmchar(rune int) bool { func isCSSNmchar(r rune) bool {
// Based on the CSS3 nmchar production but ignores multi-rune escape // Based on the CSS3 nmchar production but ignores multi-rune escape
// sequences. // sequences.
// http://www.w3.org/TR/css3-syntax/#SUBTOK-nmchar // http://www.w3.org/TR/css3-syntax/#SUBTOK-nmchar
return 'a' <= rune && rune <= 'z' || return 'a' <= r && r <= 'z' ||
'A' <= rune && rune <= 'Z' || 'A' <= r && r <= 'Z' ||
'0' <= rune && rune <= '9' || '0' <= r && r <= '9' ||
'-' == rune || r == '-' ||
'_' == rune || r == '_' ||
// Non-ASCII cases below. // Non-ASCII cases below.
0x80 <= rune && rune <= 0xd7ff || 0x80 <= r && r <= 0xd7ff ||
0xe000 <= rune && rune <= 0xfffd || 0xe000 <= r && r <= 0xfffd ||
0x10000 <= rune && rune <= 0x10ffff 0x10000 <= r && r <= 0x10ffff
} }
// decodeCSS decodes CSS3 escapes given a sequence of stringchars. // decodeCSS decodes CSS3 escapes given a sequence of stringchars.
...@@ -81,11 +81,11 @@ func decodeCSS(s []byte) []byte { ...@@ -81,11 +81,11 @@ func decodeCSS(s []byte) []byte {
for j < len(s) && j < 7 && isHex(s[j]) { for j < len(s) && j < 7 && isHex(s[j]) {
j++ j++
} }
rune := hexDecode(s[1:j]) r := hexDecode(s[1:j])
if rune > unicode.MaxRune { if r > unicode.MaxRune {
rune, j = rune/16, j-1 r, j = r/16, j-1
} }
n := utf8.EncodeRune(b[len(b):cap(b)], rune) n := utf8.EncodeRune(b[len(b):cap(b)], r)
// The optional space at the end allows a hex // The optional space at the end allows a hex
// sequence to be followed by a literal hex. // sequence to be followed by a literal hex.
// string(decodeCSS([]byte(`\A B`))) == "\nB" // string(decodeCSS([]byte(`\A B`))) == "\nB"
...@@ -105,17 +105,17 @@ func isHex(c byte) bool { ...@@ -105,17 +105,17 @@ func isHex(c byte) bool {
} }
// hexDecode decodes a short hex digit sequence: "10" -> 16. // hexDecode decodes a short hex digit sequence: "10" -> 16.
func hexDecode(s []byte) int { func hexDecode(s []byte) rune {
n := 0 n := rune(0)
for _, c := range s { for _, c := range s {
n <<= 4 n <<= 4
switch { switch {
case '0' <= c && c <= '9': case '0' <= c && c <= '9':
n |= int(c - '0') n |= rune(c - '0')
case 'a' <= c && c <= 'f': case 'a' <= c && c <= 'f':
n |= int(c-'a') + 10 n |= rune(c-'a') + 10
case 'A' <= c && c <= 'F': case 'A' <= c && c <= 'F':
n |= int(c-'A') + 10 n |= rune(c-'A') + 10
default: default:
panic(fmt.Sprintf("Bad hex digit in %q", s)) panic(fmt.Sprintf("Bad hex digit in %q", s))
} }
...@@ -251,11 +251,11 @@ func cssValueFilter(args ...interface{}) string { ...@@ -251,11 +251,11 @@ func cssValueFilter(args ...interface{}) string {
case '-': case '-':
// Disallow <!-- or -->. // Disallow <!-- or -->.
// -- should not appear in valid identifiers. // -- should not appear in valid identifiers.
if i != 0 && '-' == b[i-1] { if i != 0 && b[i-1] == '-' {
return filterFailsafe return filterFailsafe
} }
default: default:
if c < 0x80 && isCSSNmchar(int(c)) { if c < 0x80 && isCSSNmchar(rune(c)) {
id = append(id, c) id = append(id, c)
} }
} }
......
...@@ -35,7 +35,7 @@ func TestEndsWithCSSKeyword(t *testing.T) { ...@@ -35,7 +35,7 @@ func TestEndsWithCSSKeyword(t *testing.T) {
func TestIsCSSNmchar(t *testing.T) { func TestIsCSSNmchar(t *testing.T) {
tests := []struct { tests := []struct {
rune int rune rune
want bool want bool
}{ }{
{0, false}, {0, false},
...@@ -114,11 +114,11 @@ func TestDecodeCSS(t *testing.T) { ...@@ -114,11 +114,11 @@ func TestDecodeCSS(t *testing.T) {
func TestHexDecode(t *testing.T) { func TestHexDecode(t *testing.T) {
for i := 0; i < 0x200000; i += 101 /* coprime with 16 */ { for i := 0; i < 0x200000; i += 101 /* coprime with 16 */ {
s := strconv.Itob(i, 16) s := strconv.Itob(i, 16)
if got := hexDecode([]byte(s)); got != i { if got := int(hexDecode([]byte(s))); got != i {
t.Errorf("%s: want %d but got %d", s, i, got) t.Errorf("%s: want %d but got %d", s, i, got)
} }
s = strings.ToUpper(s) s = strings.ToUpper(s)
if got := hexDecode([]byte(s)); got != i { if got := int(hexDecode([]byte(s))); got != i {
t.Errorf("%s: want %d but got %d", s, i, got) t.Errorf("%s: want %d but got %d", s, i, got)
} }
} }
......
...@@ -139,7 +139,7 @@ var htmlNospaceNormReplacementTable = []string{ ...@@ -139,7 +139,7 @@ var htmlNospaceNormReplacementTable = []string{
func htmlReplacer(s string, replacementTable []string, badRunes bool) string { func htmlReplacer(s string, replacementTable []string, badRunes bool) string {
written, b := 0, new(bytes.Buffer) written, b := 0, new(bytes.Buffer)
for i, r := range s { for i, r := range s {
if r < len(replacementTable) { if int(r) < len(replacementTable) {
if repl := replacementTable[r]; len(repl) != 0 { if repl := replacementTable[r]; len(repl) != 0 {
b.WriteString(s[written:i]) b.WriteString(s[written:i])
b.WriteString(repl) b.WriteString(repl)
......
...@@ -85,7 +85,7 @@ func nextJSCtx(s []byte, preceding jsCtx) jsCtx { ...@@ -85,7 +85,7 @@ func nextJSCtx(s []byte, preceding jsCtx) jsCtx {
// Look for an IdentifierName and see if it is a keyword that // Look for an IdentifierName and see if it is a keyword that
// can precede a regular expression. // can precede a regular expression.
j := n j := n
for j > 0 && isJSIdentPart(int(s[j-1])) { for j > 0 && isJSIdentPart(rune(s[j-1])) {
j-- j--
} }
if regexpPrecederKeywords[string(s[j:])] { if regexpPrecederKeywords[string(s[j:])] {
...@@ -234,7 +234,7 @@ func replace(s string, replacementTable []string) string { ...@@ -234,7 +234,7 @@ func replace(s string, replacementTable []string) string {
for i, r := range s { for i, r := range s {
var repl string var repl string
switch { switch {
case r < len(replacementTable) && replacementTable[r] != "": case int(r) < len(replacementTable) && replacementTable[r] != "":
repl = replacementTable[r] repl = replacementTable[r]
case r == '\u2028': case r == '\u2028':
repl = `\u2028` repl = `\u2028`
...@@ -329,17 +329,17 @@ var jsRegexpReplacementTable = []string{ ...@@ -329,17 +329,17 @@ var jsRegexpReplacementTable = []string{
// It does not handle all the non-Latin letters, joiners, and combining marks, // It does not handle all the non-Latin letters, joiners, and combining marks,
// but it does handle every codepoint that can occur in a numeric literal or // but it does handle every codepoint that can occur in a numeric literal or
// a keyword. // a keyword.
func isJSIdentPart(rune int) bool { func isJSIdentPart(r rune) bool {
switch { switch {
case '$' == rune: case r == '$':
return true return true
case '0' <= rune && rune <= '9': case '0' <= r && r <= '9':
return true return true
case 'A' <= rune && rune <= 'Z': case 'A' <= r && r <= 'Z':
return true return true
case '_' == rune: case r == '_':
return true return true
case 'a' <= rune && rune <= 'z': case 'a' <= r && r <= 'z':
return true return true
} }
return false return false
......
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