package brotli

/* Copyright 2013 Google Inc. All Rights Reserved.

   Distributed under MIT license.
   See file LICENSE for detail or copy at https://opensource.org/licenses/MIT
*/

/* Heuristics for deciding about the UTF8-ness of strings. */

const kMinUTF8Ratio float64 = 0.75

/* Returns 1 if at least min_fraction of the bytes between pos and
   pos + length in the (data, mask) ring-buffer is UTF8-encoded, otherwise
   returns 0. */
func parseAsUTF8( *int,  []byte,  uint) uint {
	/* ASCII */
	if [0]&0x80 == 0 {
		* = int([0])
		if * > 0 {
			return 1
		}
	}

	/* 2-byte UTF8 */
	if  > 1 && [0]&0xE0 == 0xC0 && [1]&0xC0 == 0x80 {
		* = (int([0])&0x1F)<<6 | int([1])&0x3F
		if * > 0x7F {
			return 2
		}
	}

	/* 3-byte UFT8 */
	if  > 2 && [0]&0xF0 == 0xE0 && [1]&0xC0 == 0x80 && [2]&0xC0 == 0x80 {
		* = (int([0])&0x0F)<<12 | (int([1])&0x3F)<<6 | int([2])&0x3F
		if * > 0x7FF {
			return 3
		}
	}

	/* 4-byte UFT8 */
	if  > 3 && [0]&0xF8 == 0xF0 && [1]&0xC0 == 0x80 && [2]&0xC0 == 0x80 && [3]&0xC0 == 0x80 {
		* = (int([0])&0x07)<<18 | (int([1])&0x3F)<<12 | (int([2])&0x3F)<<6 | int([3])&0x3F
		if * > 0xFFFF && * <= 0x10FFFF {
			return 4
		}
	}

	/* Not UTF8, emit a special symbol above the UTF8-code space */
	* = 0x110000 | int([0])

	return 1
}

/* Returns 1 if at least min_fraction of the data is UTF8-encoded.*/
func isMostlyUTF8( []byte,  uint,  uint,  uint,  float64) bool {
	var  uint = 0
	var  uint = 0
	for  <  {
		var  int
		 := [(+)&:]
		var  uint = parseAsUTF8(&, , -)
		 += 
		if  < 0x110000 {
			 += 
		}
	}

	return float64() > *float64()
}