package yaml

import (
	
	
	
	
	
	
)

type resolveMapItem struct {
	value interface{}
	tag   string
}

var resolveTable = make([]byte, 256)
var resolveMap = make(map[string]resolveMapItem)

func () {
	 := resolveTable
	[int('+')] = 'S' // Sign
	[int('-')] = 'S'
	for ,  := range "0123456789" {
		[int()] = 'D' // Digit
	}
	for ,  := range "yYnNtTfFoO~" {
		[int()] = 'M' // In map
	}
	[int('.')] = '.' // Float (potentially in map)

	var  = []struct {
		   interface{}
		 string
		   []string
	}{
		{true, yaml_BOOL_TAG, []string{"y", "Y", "yes", "Yes", "YES"}},
		{true, yaml_BOOL_TAG, []string{"true", "True", "TRUE"}},
		{true, yaml_BOOL_TAG, []string{"on", "On", "ON"}},
		{false, yaml_BOOL_TAG, []string{"n", "N", "no", "No", "NO"}},
		{false, yaml_BOOL_TAG, []string{"false", "False", "FALSE"}},
		{false, yaml_BOOL_TAG, []string{"off", "Off", "OFF"}},
		{nil, yaml_NULL_TAG, []string{"", "~", "null", "Null", "NULL"}},
		{math.NaN(), yaml_FLOAT_TAG, []string{".nan", ".NaN", ".NAN"}},
		{math.Inf(+1), yaml_FLOAT_TAG, []string{".inf", ".Inf", ".INF"}},
		{math.Inf(+1), yaml_FLOAT_TAG, []string{"+.inf", "+.Inf", "+.INF"}},
		{math.Inf(-1), yaml_FLOAT_TAG, []string{"-.inf", "-.Inf", "-.INF"}},
		{"<<", yaml_MERGE_TAG, []string{"<<"}},
	}

	 := resolveMap
	for ,  := range  {
		for ,  := range . {
			[] = resolveMapItem{., .}
		}
	}
}

const longTagPrefix = "tag:yaml.org,2002:"

TODO This can easily be made faster and produce less garbage.
	if strings.HasPrefix(, longTagPrefix) {
		return "!!" + [len(longTagPrefix):]
	}
	return 
}

func ( string) string {
	if strings.HasPrefix(, "!!") {
		return longTagPrefix + [2:]
	}
	return 
}

func ( string) bool {
	switch  {
	case "", yaml_STR_TAG, yaml_BOOL_TAG, yaml_INT_TAG, yaml_FLOAT_TAG, yaml_NULL_TAG, yaml_TIMESTAMP_TAG:
		return true
	}
	return false
}

var yamlStyleFloat = regexp.MustCompile(`^[-+]?(\.[0-9]+|[0-9]+(\.[0-9]*)?)([eE][-+]?[0-9]+)?$`)

func ( string,  string) ( string,  interface{}) {
	if !resolvableTag() {
		return , 
	}

	defer func() {
		switch  {
		case "", , yaml_STR_TAG, yaml_BINARY_TAG:
			return
		case yaml_FLOAT_TAG:
			if  == yaml_INT_TAG {
				switch v := .(type) {
				case int64:
					 = yaml_FLOAT_TAG
					 = float64()
					return
				case int:
					 = yaml_FLOAT_TAG
					 = float64()
					return
				}
			}
		}
		failf("cannot decode %s `%s` as a %s", shortTag(), , shortTag())
	}()
Any data is accepted as a !!str or !!binary. Otherwise, the prefix is enough of a hint about what it might be.
	 := byte('N')
	if  != "" {
		 = resolveTable[[0]]
	}
Handle things we can lookup in a map.
		if ,  := resolveMap[];  {
			return .tag, .value
		}
Base 60 floats are a bad idea, were dropped in YAML 1.2, and are purposefully unsupported here. They're still quoted on the way out for compatibility with other parser, though.

		switch  {
We've already checked the map above.

Not in the map, so maybe a normal float.
			,  := strconv.ParseFloat(, 64)
			if  == nil {
				return yaml_FLOAT_TAG, 
			}

Int, float, or timestamp. Only try values as a timestamp if the value is unquoted or there's an explicit !!timestamp tag.
			if  == "" ||  == yaml_TIMESTAMP_TAG {
				,  := parseTimestamp()
				if  {
					return yaml_TIMESTAMP_TAG, 
				}
			}

			 := strings.Replace(, "_", "", -1)
			,  := strconv.ParseInt(, 0, 64)
			if  == nil {
				if  == int64(int()) {
					return yaml_INT_TAG, int()
				} else {
					return yaml_INT_TAG, 
				}
			}
			,  := strconv.ParseUint(, 0, 64)
			if  == nil {
				return yaml_INT_TAG, 
			}
			if yamlStyleFloat.MatchString() {
				,  := strconv.ParseFloat(, 64)
				if  == nil {
					return yaml_FLOAT_TAG, 
				}
			}
			if strings.HasPrefix(, "0b") {
				,  := strconv.ParseInt([2:], 2, 64)
				if  == nil {
					if  == int64(int()) {
						return yaml_INT_TAG, int()
					} else {
						return yaml_INT_TAG, 
					}
				}
				,  := strconv.ParseUint([2:], 2, 64)
				if  == nil {
					return yaml_INT_TAG, 
				}
			} else if strings.HasPrefix(, "-0b") {
				,  := strconv.ParseInt("-" + [3:], 2, 64)
				if  == nil {
					if true ||  == int64(int()) {
						return yaml_INT_TAG, int()
					} else {
						return yaml_INT_TAG, 
					}
				}
			}
		default:
			panic("resolveTable item not yet handled: " + string(rune()) + " (with " +  + ")")
		}
	}
	return yaml_STR_TAG, 
}
encodeBase64 encodes s as base64 that is broken up into multiple lines as appropriate for the resulting length.
func ( string) string {
	const  = 70
	 := base64.StdEncoding.EncodedLen(len())
	 := / + 1
	 := make([]byte, *2+)
	 := [0:]
	 := [:]
	base64.StdEncoding.Encode(, []byte())
	 := 0
	for  := 0;  < len();  +=  {
		 :=  + 
		if  > len() {
			 = len()
		}
		 += copy([:], [:])
		if  > 1 {
			[] = '\n'
			++
		}
	}
	return string([:])
}
This is a subset of the formats allowed by the regular expression defined at http://yaml.org/type/timestamp.html.
var allowedTimestampFormats = []string{
	"2006-1-2T15:4:5.999999999Z07:00", // RCF3339Nano with short date fields.
	"2006-1-2t15:4:5.999999999Z07:00", // RFC3339Nano with short date fields and lower-case "t".
	"2006-1-2 15:4:5.999999999",       // space separated with no time zone
Notable exception: time.Parse cannot handle: "2001-12-14 21:59:43.10 -5" from the set of examples.
}
parseTimestamp parses s as a timestamp string and returns the timestamp and reports whether it succeeded. Timestamp formats are defined at http://yaml.org/type/timestamp.html
TODO write code to check all the formats supported by http://yaml.org/type/timestamp.html instead of using time.Parse.
Quick check: all date formats start with YYYY-.
	 := 0
	for ;  < len(); ++ {
		if  := [];  < '0' ||  > '9' {
			break
		}
	}
	if  != 4 ||  == len() || [] != '-' {
		return time.Time{}, false
	}
	for ,  := range allowedTimestampFormats {
		if ,  := time.Parse(, );  == nil {
			return , true
		}
	}
	return time.Time{}, false