]> Cypherpunks repositories - gostls13.git/commitdiff
IsSpace
authorRob Pike <r@golang.org>
Tue, 1 Sep 2009 01:12:40 +0000 (18:12 -0700)
committerRob Pike <r@golang.org>
Tue, 1 Sep 2009 01:12:40 +0000 (18:12 -0700)
R=rsc
DELTA=39  (39 added, 0 deleted, 0 changed)
OCL=34153
CL=34167

src/pkg/unicode/letter.go
src/pkg/unicode/letter_test.go

index f67b7e5cf0f9cbb63019c7abd74b4353cb4fc28a..45b68f485df14ef3a0b6bcfc34504dd110059b46 100644 (file)
@@ -117,6 +117,18 @@ func IsLetter(rune int) bool {
        return Is(Letter, rune);
 }
 
+// IsSpace reports whether the rune is a white space character.
+func IsSpace(rune int) bool {
+       if rune <= 0xFF {       // quick Latin-1 check
+               switch rune {
+               case '\t', '\n', '\v', '\f', '\r', ' ', 0x85, 0xA0:
+                       return true;
+               }
+               return false;
+       }
+       return Is(White_Space, rune);
+}
+
 // To maps the rune to the specified case: UpperCase, LowerCase, or TitleCase
 func To(_case int, rune int) int {
        if _case < 0 || MaxCase <= _case {
index 0ccb29f0a20d04f909f47338f49958b74cd9c71d..f5cb72f7f8b3b708b290675ec1a7769373a6dd36 100644 (file)
@@ -92,6 +92,20 @@ var notletterTest = []int{
        0x10ffff,
 }
 
+// Contains all the special cased Latin-1 chars.
+var spaceTest = []int{
+       0x09,
+       0x0a,
+       0x0b,
+       0x0c,
+       0x0d,
+       0x20,
+       0x85,
+       0xA0,
+       0x2000,
+       0x3000,
+}
+
 type caseT struct {
        cas, in, out int
 }
@@ -291,3 +305,16 @@ func TestToTitleCase(t *testing.T) {
                }
        }
 }
+
+func TestIsSpace(t *testing.T) {
+       for _, c := range spaceTest {
+               if !IsSpace(c) {
+                       t.Errorf("IsSpace(U+%04X) = false; want true", c);
+               }
+       }
+       for _, c := range letterTest {
+               if IsSpace(c) {
+                       t.Errorf("IsSpace(U+%04X) = true; want false", c);
+               }
+       }
+}