From 7ffb3fb66cb8de7a6041c4c838aa6d7792321083 Mon Sep 17 00:00:00 2001 From: Brad Fitzpatrick Date: Sun, 27 Nov 2011 11:41:20 -0500 Subject: [PATCH] index: unreverse time Change-Id: I1ae6a0c0695e0dfa6626999879dfbb9a05c787df --- lib/go/camli/index/index_test.go | 9 +++++++-- lib/go/camli/index/reversetime.go | 23 +++++++++++++++++++++-- 2 files changed, 28 insertions(+), 4 deletions(-) diff --git a/lib/go/camli/index/index_test.go b/lib/go/camli/index/index_test.go index 514795963..f157a0c1d 100644 --- a/lib/go/camli/index/index_test.go +++ b/lib/go/camli/index/index_test.go @@ -21,9 +21,14 @@ import ( ) func TestReverseTimeString(t *testing.T) { - got := reverseTimeString("2011-11-27T01:23:45Z") + in := "2011-11-27T01:23:45Z" + got := reverseTimeString(in) want := "rt7988-88-72T98:76:54Z" if got != want { - t.Errorf("reverseTimeString = %q, want %q", got, want) + t.Fatalf("reverseTimeString = %q, want %q", got, want) + } + back := unreverseTimeString(got) + if back != in { + t.Fatalf("unreverseTimeString = %q, want %q", back, in) } } diff --git a/lib/go/camli/index/reversetime.go b/lib/go/camli/index/reversetime.go index 1cbd555d3..848ec7ce0 100644 --- a/lib/go/camli/index/reversetime.go +++ b/lib/go/camli/index/reversetime.go @@ -16,12 +16,29 @@ limitations under the License. package index -import () +import ( + "fmt" + "strings" +) + +func unreverseTimeString(s string) string { + if !strings.HasPrefix(s, "rt") { + panic(fmt.Sprintf("can't unreverse time string: %q", s)) + } + b := make([]byte, 0, len(s)-2) + b = appendReverseString(b, s[2:]) + return string(b) +} func reverseTimeString(s string) string { b := make([]byte, 0, len(s)+2) b = append(b, 'r') b = append(b, 't') + b = appendReverseString(b, s) + return string(b) +} + +func appendReverseString(b []byte, s string) []byte { for i := 0; i < len(s); i++ { c := s[i] if c >= '0' && c <= '9' { @@ -30,5 +47,7 @@ func reverseTimeString(s string) string { b = append(b, c) } } - return string(b) + return b } + +