index: unreverse time

Change-Id: I1ae6a0c0695e0dfa6626999879dfbb9a05c787df
This commit is contained in:
Brad Fitzpatrick 2011-11-27 11:41:20 -05:00
parent 5ea8cf8d26
commit 7ffb3fb66c
2 changed files with 28 additions and 4 deletions

View File

@ -21,9 +21,14 @@ import (
) )
func TestReverseTimeString(t *testing.T) { 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" want := "rt7988-88-72T98:76:54Z"
if got != want { 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)
} }
} }

View File

@ -16,12 +16,29 @@ limitations under the License.
package index 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 { func reverseTimeString(s string) string {
b := make([]byte, 0, len(s)+2) b := make([]byte, 0, len(s)+2)
b = append(b, 'r') b = append(b, 'r')
b = append(b, 't') 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++ { for i := 0; i < len(s); i++ {
c := s[i] c := s[i]
if c >= '0' && c <= '9' { if c >= '0' && c <= '9' {
@ -30,5 +47,7 @@ func reverseTimeString(s string) string {
b = append(b, c) b = append(b, c)
} }
} }
return string(b) return b
} }