示例#1
0
func ExampleCollator_Strings() {
	c := collate.New(locale.Und)
	strings := []string{
		"ad",
		"ab",
		"äb",
		"ac",
	}
	c.SortStrings(strings)
	fmt.Println(strings)
	// Output: [ab äb ac ad]
}
示例#2
0
func TestSort(t *testing.T) {
	c := collate.New(locale.En)
	strings := []string{
		"bcd",
		"abc",
		"ddd",
	}
	c.Sort(sorter(strings))
	res := fmt.Sprint(strings)
	want := "[abc bcd ddd]"
	if res != want {
		t.Errorf("found %s; want %s", res, want)
	}
}
示例#3
0
func testCollator(c *collate.Collator) {
	c0 := collate.New(locale.Und)

	// iterator over all characters for all locales and check
	// whether Key is equal.
	buf := collate.Buffer{}

	// Add all common and not too uncommon runes to the test set.
	for i := rune(0); i < 0x30000; i++ {
		testInput.add(string(i))
	}
	for i := rune(0xE0000); i < 0xF0000; i++ {
		testInput.add(string(i))
	}
	for _, str := range testInput.values() {
		k0 := c0.KeyFromString(&buf, str)
		k := c.KeyFromString(&buf, str)
		if !bytes.Equal(k0, k) {
			failOnError(fmt.Errorf("test:%U: keys differ (%x vs %x)", []rune(str), k0, k))
		}
		buf.Reset()
	}
	fmt.Println("PASS")
}
示例#4
0
func newGoCollator(loc string) (Collator, error) {
	c := &goCollator{c: collate.New(locale.Make(loc))}
	return c, nil
}
示例#5
0
//  License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
//  either express or implied. See the License for the specific language governing permissions
//  and limitations under the License.

// this code originall taken from walrus
// https://github.com/couchbaselabs/walrus

package ast

import (
	"fmt"

	"code.google.com/p/go.exp/locale/collate"
)

var icuCollator = collate.New("icu")

// CouchDB-compatible collation/comparison of JSON values.
// See: http://wiki.apache.org/couchdb/View_collation#Collation_Specification
func CollateJSON(key1, key2 interface{}) int {
	type1 := collationType(key1)
	type2 := collationType(key2)
	if type1 != type2 {
		return type1 - type2
	}
	switch type1 {
	case 0, 1, 2:
		return 0
	case 3:
		n1 := collationToFloat64(key1)
		n2 := collationToFloat64(key2)