// Copyright 2016 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.

package runtime

// Numbers fundamental to the encoding.
const (
	runeError = '\uFFFD'     // the "error" Rune or "Unicode replacement character"
	runeSelf  = 0x80         // characters below runeSelf are represented as themselves in a single byte.
	maxRune   = '\U0010FFFF' // Maximum valid Unicode code point.
)

// Code points in the surrogate range are not valid for UTF-8.
const (
	surrogateMin = 0xD800
	surrogateMax = 0xDFFF
)

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

	maskx = 0x3F // 0011 1111
	mask2 = 0x1F // 0001 1111
	mask3 = 0x0F // 0000 1111
	mask4 = 0x07 // 0000 0111

	rune1Max = 1<<7 - 1
	rune2Max = 1<<11 - 1
	rune3Max = 1<<16 - 1

	// The default lowest and highest continuation byte.
	locb = 0x80 // 1000 0000
	hicb = 0xBF // 1011 1111
)

// countrunes returns the number of runes in s.
func ( string) int {
	 := 0
	for range  {
		++
	}
	return 
}

// decoderune returns the non-ASCII rune at the start of
// s[k:] and the index after the rune in s.
//
// decoderune assumes that caller has checked that
// the to be decoded rune is a non-ASCII rune.
//
// If the string appears to be incomplete or decoding problems
// are encountered (runeerror, k + 1) is returned to ensure
// progress when decoderune is used to iterate over a string.
func ( string,  int) ( rune,  int) {
	 = 

	if  >= len() {
		return runeError,  + 1
	}

	 = [:]

	switch {
	case t2 <= [0] && [0] < t3:
		// 0080-07FF two byte sequence
		if len() > 1 && (locb <= [1] && [1] <= hicb) {
			 = rune([0]&mask2)<<6 | rune([1]&maskx)
			 += 2
			if rune1Max <  {
				return
			}
		}
	case t3 <= [0] && [0] < t4:
		// 0800-FFFF three byte sequence
		if len() > 2 && (locb <= [1] && [1] <= hicb) && (locb <= [2] && [2] <= hicb) {
			 = rune([0]&mask3)<<12 | rune([1]&maskx)<<6 | rune([2]&maskx)
			 += 3
			if rune2Max <  && !(surrogateMin <=  &&  <= surrogateMax) {
				return
			}
		}
	case t4 <= [0] && [0] < t5:
		// 10000-1FFFFF four byte sequence
		if len() > 3 && (locb <= [1] && [1] <= hicb) && (locb <= [2] && [2] <= hicb) && (locb <= [3] && [3] <= hicb) {
			 = rune([0]&mask4)<<18 | rune([1]&maskx)<<12 | rune([2]&maskx)<<6 | rune([3]&maskx)
			 += 4
			if rune3Max <  &&  <= maxRune {
				return
			}
		}
	}

	return runeError,  + 1
}

// encoderune writes into p (which must be large enough) the UTF-8 encoding of the rune.
// It returns the number of bytes written.
func ( []byte,  rune) int {
	// Negative values are erroneous. Making it unsigned addresses the problem.
	switch  := uint32(); {
	case  <= rune1Max:
		[0] = byte()
		return 1
	case  <= rune2Max:
		_ = [1] // eliminate bounds checks
		[0] = t2 | byte(>>6)
		[1] = tx | byte()&maskx
		return 2
	case  > maxRune, surrogateMin <=  &&  <= surrogateMax:
		 = runeError
		fallthrough
	case  <= rune3Max:
		_ = [2] // eliminate bounds checks
		[0] = t3 | byte(>>12)
		[1] = tx | byte(>>6)&maskx
		[2] = tx | byte()&maskx
		return 3
	default:
		_ = [3] // eliminate bounds checks
		[0] = t4 | byte(>>18)
		[1] = tx | byte(>>12)&maskx
		[2] = tx | byte(>>6)&maskx
		[3] = tx | byte()&maskx
		return 4
	}
}