From 93b8438e597602cdfb6742246cd406bd1e479535 Mon Sep 17 00:00:00 2001 From: Paul Borman Date: Tue, 4 Oct 2011 12:52:30 -0700 Subject: [PATCH] time: make month/day name comparisons case insenstive Fixes #2324. R=r, r CC=golang-dev https://golang.org/cl/5180044 --- src/pkg/time/format.go | 20 +++++++++++++++++++- src/pkg/time/time_test.go | 3 +++ 2 files changed, 22 insertions(+), 1 deletion(-) diff --git a/src/pkg/time/format.go b/src/pkg/time/format.go index 0701cc925a..50e96a5c25 100644 --- a/src/pkg/time/format.go +++ b/src/pkg/time/format.go @@ -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 } } diff --git a/src/pkg/time/time_test.go b/src/pkg/time/time_test.go index fe0f3482aa..353976c969 100644 --- a/src/pkg/time/time_test.go +++ b/src/pkg/time/time_test.go @@ -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}, -- 2.48.1