OLD | NEW |
1 // Copyright 2015 The Chromium Authors. All rights reserved. | 1 // Copyright 2015 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 package datastore | 5 package datastore |
6 | 6 |
7 import ( | 7 import ( |
8 "bytes" | 8 "bytes" |
9 "encoding/base64" | 9 "encoding/base64" |
10 "errors" | 10 "errors" |
(...skipping 915 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
926 // not be performed. | 926 // not be performed. |
927 fastCmp(o byteSequence) (int, bool) | 927 fastCmp(o byteSequence) (int, bool) |
928 } | 928 } |
929 | 929 |
930 func cmpByteSequence(a, b byteSequence) int { | 930 func cmpByteSequence(a, b byteSequence) int { |
931 if v, ok := a.fastCmp(b); ok { | 931 if v, ok := a.fastCmp(b); ok { |
932 return v | 932 return v |
933 } | 933 } |
934 | 934 |
935 // Byte-by-byte "slow" comparison. | 935 // Byte-by-byte "slow" comparison. |
936 » ld := a.len() - b.len() | 936 » ln := a.len() |
937 » if ld < 0 { | 937 » if bln := b.len(); bln < ln { |
938 » » ld = -ld | 938 » » ln = bln |
939 } | 939 } |
940 » for i := 0; i < ld; i++ { | 940 |
| 941 » for i := 0; i < ln; i++ { |
941 av, bv := a.get(i), b.get(i) | 942 av, bv := a.get(i), b.get(i) |
942 switch { | 943 switch { |
943 case av < bv: | 944 case av < bv: |
944 return -1 | 945 return -1 |
945 case av > bv: | 946 case av > bv: |
946 return 1 | 947 return 1 |
947 } | 948 } |
948 } | 949 } |
949 | 950 |
950 » return ld | 951 » return a.len() - b.len() |
951 } | 952 } |
952 | 953 |
953 // bytesByteSequence is a byteSequence implementation for a byte slice. | 954 // bytesByteSequence is a byteSequence implementation for a byte slice. |
954 type bytesByteSequence []byte | 955 type bytesByteSequence []byte |
955 | 956 |
956 func (s bytesByteSequence) len() int { return len(s) } | 957 func (s bytesByteSequence) len() int { return len(s) } |
957 func (s bytesByteSequence) get(i int) byte { return s[i] } | 958 func (s bytesByteSequence) get(i int) byte { return s[i] } |
958 func (s bytesByteSequence) value() interface{} { return []byte(s) } | 959 func (s bytesByteSequence) value() interface{} { return []byte(s) } |
959 func (s bytesByteSequence) string() string { return string(s) } | 960 func (s bytesByteSequence) string() string { return string(s) } |
960 func (s bytesByteSequence) bytes() []byte { return []byte(s) } | 961 func (s bytesByteSequence) bytes() []byte { return []byte(s) } |
(...skipping 14 matching lines...) Expand all Loading... |
975 func (s stringByteSequence) bytes() []byte { return []byte(s) } | 976 func (s stringByteSequence) bytes() []byte { return []byte(s) } |
976 func (s stringByteSequence) fastCmp(o byteSequence) (int, bool) { | 977 func (s stringByteSequence) fastCmp(o byteSequence) (int, bool) { |
977 if t, ok := o.(stringByteSequence); ok { | 978 if t, ok := o.(stringByteSequence); ok { |
978 // This pattern is used for string comparison in strings.Compare
. | 979 // This pattern is used for string comparison in strings.Compare
. |
979 if string(s) == string(t) { | 980 if string(s) == string(t) { |
980 return 0, true | 981 return 0, true |
981 } | 982 } |
982 if string(s) < string(t) { | 983 if string(s) < string(t) { |
983 return -1, true | 984 return -1, true |
984 } | 985 } |
985 » » return 0, true | 986 » » return 1, true |
986 } | 987 } |
987 return 0, false | 988 return 0, false |
988 } | 989 } |
OLD | NEW |