]> Cypherpunks repositories - gostls13.git/commitdiff
unicode/utf8: remove dependence on unicode.
authorRob Pike <r@golang.org>
Tue, 6 Mar 2012 00:14:45 +0000 (11:14 +1100)
committerRob Pike <r@golang.org>
Tue, 6 Mar 2012 00:14:45 +0000 (11:14 +1100)
The dependency was there only to pull in two constants.
Now we define them locally and verify equality in the test.

R=golang-dev, bradfitz
CC=golang-dev
https://golang.org/cl/5754046

src/pkg/unicode/utf8/utf8.go
src/pkg/unicode/utf8/utf8_test.go

index 631533a5a3579c859d9ac9fcc3914d6df881ec3e..57ea19e96d4e0b2f9b7b9c112aae6d4efa3bab77 100644 (file)
@@ -6,13 +6,16 @@
 // UTF-8. It includes functions to translate between runes and UTF-8 byte sequences.
 package utf8
 
-import "unicode" // only needed for a couple of constants
+// The conditions RuneError==unicode.ReplacementChar and
+// MaxRune==unicode.MaxRune are verified in the tests.
+// Defining them locally avoids this package depending on package unicode.
 
 // Numbers fundamental to the encoding.
 const (
-       RuneError = unicode.ReplacementChar // the "error" Rune or "replacement character".
-       RuneSelf  = 0x80                    // characters below Runeself are represented as themselves in a single byte.
-       UTFMax    = 4                       // maximum number of bytes of a UTF-8 encoded Unicode character.
+       RuneError = '\uFFFD'     // the "error" Rune or "Unicode replacement character"
+       RuneSelf  = 0x80         // characters below Runeself are represented as themselves in a single byte.
+       MaxRune   = '\U0010FFFF' // Maximum valid Unicode code point.
+       UTFMax    = 4            // maximum number of bytes of a UTF-8 encoded Unicode character.
 )
 
 const (
@@ -309,7 +312,7 @@ func EncodeRune(p []byte, r rune) int {
                return 2
        }
 
-       if uint32(r) > unicode.MaxRune {
+       if uint32(r) > MaxRune {
                r = RuneError
        }
 
index 63514265bb9187e5ca02509d6c5db672bfbed9ef..abf5a468eac298f28c6b335e89f63995ad4b4084 100644 (file)
@@ -7,9 +7,20 @@ package utf8_test
 import (
        "bytes"
        "testing"
+       "unicode"
        . "unicode/utf8"
 )
 
+// Validate the constants redefined from unicode.
+func init() {
+       if MaxRune != unicode.MaxRune {
+               panic("utf8.MaxRune is wrong")
+       }
+       if RuneError != unicode.ReplacementChar {
+               panic("utf8.RuneError is wrong")
+       }
+}
+
 type Utf8Map struct {
        r   rune
        str string