// Copyright 2012 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

// The trie in this file is used to associate the first full character in an
// UTF-8 string to a collation element. All but the last byte in a UTF-8 byte
// sequence are used to lookup offsets in the index table to be used for the
// next byte. The last byte is used to index into a table of collation elements.
// For a full description, see go.text/collate/build/trie.go.

package colltab

const blockSize = 64

type Trie struct {
	Index0  []uint16 // index for first byte (0xC0-0xFF)
	Values0 []uint32 // index for first byte (0x00-0x7F)
	Index   []uint16
	Values  []uint32
}

const (
	t1 = 0x00 // 0000 0000
	tx = 0x80 // 1000 0000
	t2 = 0xC0 // 1100 0000
	t3 = 0xE0 // 1110 0000
	t4 = 0xF0 // 1111 0000
	t5 = 0xF8 // 1111 1000
	t6 = 0xFC // 1111 1100
	te = 0xFE // 1111 1110
)

func ( *Trie) ( uint16,  byte) Elem {
	return Elem(.Values[int()<<6+int()])
}

// lookup returns the trie value for the first UTF-8 encoding in s and
// the width in bytes of this encoding. The size will be 0 if s does not
// hold enough bytes to complete the encoding. len(s) must be greater than 0.
func ( *Trie) ( []byte) ( Elem,  int) {
	 := [0]
	switch {
	case  < tx:
		return Elem(.Values0[]), 1
	case  < t2:
		return 0, 1
	case  < t3:
		if len() < 2 {
			return 0, 0
		}
		 := .Index0[]
		 := [1]
		if  < tx || t2 <=  {
			return 0, 1
		}
		return .lookupValue(, ), 2
	case  < t4:
		if len() < 3 {
			return 0, 0
		}
		 := .Index0[]
		 := [1]
		if  < tx || t2 <=  {
			return 0, 1
		}
		 := int()<<6 + int()
		 = .Index[]
		 := [2]
		if  < tx || t2 <=  {
			return 0, 2
		}
		return .lookupValue(, ), 3
	case  < t5:
		if len() < 4 {
			return 0, 0
		}
		 := .Index0[]
		 := [1]
		if  < tx || t2 <=  {
			return 0, 1
		}
		 := int()<<6 + int()
		 = .Index[]
		 := [2]
		if  < tx || t2 <=  {
			return 0, 2
		}
		 = int()<<6 + int()
		 = .Index[]
		 := [3]
		if  < tx || t2 <=  {
			return 0, 3
		}
		return .lookupValue(, ), 4
	}
	// Illegal rune
	return 0, 1
}

// The body of lookupString is a verbatim copy of that of lookup.
func ( *Trie) ( string) ( Elem,  int) {
	 := [0]
	switch {
	case  < tx:
		return Elem(.Values0[]), 1
	case  < t2:
		return 0, 1
	case  < t3:
		if len() < 2 {
			return 0, 0
		}
		 := .Index0[]
		 := [1]
		if  < tx || t2 <=  {
			return 0, 1
		}
		return .lookupValue(, ), 2
	case  < t4:
		if len() < 3 {
			return 0, 0
		}
		 := .Index0[]
		 := [1]
		if  < tx || t2 <=  {
			return 0, 1
		}
		 := int()<<6 + int()
		 = .Index[]
		 := [2]
		if  < tx || t2 <=  {
			return 0, 2
		}
		return .lookupValue(, ), 3
	case  < t5:
		if len() < 4 {
			return 0, 0
		}
		 := .Index0[]
		 := [1]
		if  < tx || t2 <=  {
			return 0, 1
		}
		 := int()<<6 + int()
		 = .Index[]
		 := [2]
		if  < tx || t2 <=  {
			return 0, 2
		}
		 = int()<<6 + int()
		 = .Index[]
		 := [3]
		if  < tx || t2 <=  {
			return 0, 3
		}
		return .lookupValue(, ), 4
	}
	// Illegal rune
	return 0, 1
}