]> Cypherpunks repositories - gostls13.git/commitdiff
time: make month/day name comparisons case insenstive
authorPaul Borman <borman@google.com>
Tue, 4 Oct 2011 19:52:30 +0000 (12:52 -0700)
committerRob Pike <r@golang.org>
Tue, 4 Oct 2011 19:52:30 +0000 (12:52 -0700)
Fixes #2324.

R=r, r
CC=golang-dev
https://golang.org/cl/5180044

src/pkg/time/format.go
src/pkg/time/time_test.go

index 0701cc925a30816cd84a739349418d30e109523e..50e96a5c2591b724f7d98d3a82e44c44ce260543 100644 (file)
@@ -232,9 +232,27 @@ var longMonthNames = []string{
        "December",
 }
 
+// match returns true if s1 and s2 match ignoring case.
+// It is assumed s1 and s2 are the same length.
+func match(s1, s2 string) bool {
+       for i := 0; i < len(s1); i++ {
+               c1 := s1[i]
+               c2 := s2[i]
+               if c1 != c2 {
+                       // Switch to lower-case; 'a'-'A' is known to be a single bit.
+                       c1 |= 'a' - 'A'
+                       c2 |= 'a' - 'A'
+                       if c1 != c2 || c1 < 'a' || c1 > 'z' {
+                               return false
+                       }
+               }
+       }
+       return true
+}
+
 func lookup(tab []string, val string) (int, string, os.Error) {
        for i, v := range tab {
-               if len(val) >= len(v) && val[0:len(v)] == v {
+               if len(val) >= len(v) && match(val[0:len(v)], v) {
                        return i, val[len(v):], nil
                }
        }
index fe0f3482aa80bc6e2beb5eb20621d9e80a74180e..353976c96936f1e9d589e502eb5a9909a8193be0 100644 (file)
@@ -252,6 +252,9 @@ var parseTests = []ParseTest{
        // Amount of white space should not matter.
        {"ANSIC", ANSIC, "Thu Feb 4 21:00:57 2010", false, true, 1, 0},
        {"ANSIC", ANSIC, "Thu      Feb     4     21:00:57     2010", false, true, 1, 0},
+       // Case should not matter
+       {"ANSIC", ANSIC, "THU FEB 4 21:00:57 2010", false, true, 1, 0},
+       {"ANSIC", ANSIC, "thu feb 4 21:00:57 2010", false, true, 1, 0},
        // Fractional seconds.
        {"millisecond", "Mon Jan _2 15:04:05.000 2006", "Thu Feb  4 21:00:57.012 2010", false, true, 1, 3},
        {"microsecond", "Mon Jan _2 15:04:05.000000 2006", "Thu Feb  4 21:00:57.012345 2010", false, true, 1, 6},