// Flattens the key for formatting along with its labels, removes spaces
335516
func (i *InmemSink) flattenKeyLabels(parts []string, labels []Label) (string, string) {
	buf := &bytes.Buffer{}
	replacer := strings.NewReplacer(" ", "_")

	if len(parts) > 0 {
		replacer.WriteString(buf, parts[0])
	}
	for _, part := range parts[1:] {
		replacer.WriteString(buf, ".")
		replacer.WriteString(buf, part)
	}

	key := buf.String()

	for _, label := range labels {
		replacer.WriteString(buf, fmt.Sprintf(";%s=%s", label.Name, label.Value))
	}

	return buf.String(), key
}
606137
func transposeLabels(labels map[string]string) {
	for k, t := range labelMap {
		if vv, ok := labels[k]; ok {
			labels[t] = vv
			delete(labels, k)
		}
	}
}
1051382
func (e Error) formatParams() string {
	if e.Params == nil || len(e.Params) == 0 {
		return "[]"
	}

	var keyValuePairs []struct {
		key   string
		value string
	}

	for k, v := range e.Params {
		keyValuePairs = append(keyValuePairs, struct {
			key   string
			value string
		}{
			key:   k,
			value: v,
		})
	}
	sort.Slice(keyValuePairs, func(i, j int) bool {
		return keyValuePairs[i].key < keyValuePairs[j].key
	})

	result := "["
	l := len(keyValuePairs)
	for i, kvp := range keyValuePairs {
		result += kvp.key + ":" + kvp.value
		if i < l-1 {
			result += ", "
		}
	}
	result += "]"
	return result
}
403983
func FlattenJson(jsonBytes []byte) (map[string][]string, error) {
	jsonMap := make(map[string]interface{})
	if err := json.Unmarshal(jsonBytes, &jsonMap); err != nil {
		return nil, err
	}
	return FlattenJsonMap(jsonMap)
}
1044324
func (n *regexNode) reduceConcatenation() *regexNode {
	// Eliminate empties and concat adjacent strings/chars

	var optionsLast RegexOptions
	var optionsAt RegexOptions
	var i, j int

	if len(n.children) == 0 {
		return newRegexNode(ntEmpty, n.options)
	}

	wasLastString := false

	for i, j = 0, 0; i < len(n.children); i, j = i+1, j+1 {
		var at, prev *regexNode

		at = n.children[i]

		if j < i {
			n.children[j] = at
		}

		if at.t == ntConcatenate &&
			((at.options & RightToLeft) == (n.options & RightToLeft)) {
			for k := 0; k < len(at.children); k++ {
				at.children[k].next = n
			}

			//insert at.children at i+1 index in n.children
			n.insertChildren(i+1, at.children)

			j--
		} else if at.t == ntMulti || at.t == ntOne {
			// Cannot merge strings if L or I options differ
			optionsAt = at.options & (RightToLeft | IgnoreCase)

			if !wasLastString || optionsLast != optionsAt {
				wasLastString = true
				optionsLast = optionsAt
				continue
			}

			j--
			prev = n.children[j]

			if prev.t == ntOne {
				prev.t = ntMulti
				prev.str = []rune{prev.ch}
			}

			if (optionsAt & RightToLeft) == 0 {
				if at.t == ntOne {
					prev.str = append(prev.str, at.ch)
				} else {
					prev.str = append(prev.str, at.str...)
				}
			} else {
				if at.t == ntOne {
					// insert at the front by expanding our slice, copying the data over, and then setting the value
					prev.str = append(prev.str, 0)
					copy(prev.str[1:], prev.str)
					prev.str[0] = at.ch
				} else {
					//insert at the front...this one we'll make a new slice and copy both into it
					merge := make([]rune, len(prev.str)+len(at.str))
					copy(merge, at.str)
					copy(merge[len(at.str):], prev.str)
					prev.str = merge
				}
			}
		} else if at.t == ntEmpty {
			j--
		} else {
			wasLastString = false
		}
	}

	if j < i {
		// remove indices j through i from the children
		n.removeChildren(j, i)
	}

	return n.stripEnation(ntEmpty)
}
1070555
func (rule *PortRuleL7) Sanitize() error {
	for k := range *rule {
		if k == "" {
			return fmt.Errorf("Empty key not allowed")
		}
	}
	return nil
}
906387
func keyPrefix(key []byte) (pre prefix) {
	if len(key) >= prefixSize {
		return *(*prefix)(unsafe.Pointer(&key[0]))
	}
	copy(pre[:], key)
	return pre
}
1136261
func (s String) Format(sep string) string {
	return strings.Join([]string(s), sep)
}
1272363
func (h *Handle) KeyPrefix() []string {
	h.Lock()
	defer h.Unlock()
	return []string{h.app}
}
1101694
func flatten(q Q) (Q, bool) {
	switch s := q.(type) {
	case *And:
		if len(s.Children) == 1 {
			return s.Children[0], true
		}
		flatChildren, changed := flattenAndOr(s.Children, s)
		return &And{flatChildren}, changed
	case *Or:
		if len(s.Children) == 1 {
			return s.Children[0], true
		}
		flatChildren, changed := flattenAndOr(s.Children, s)
		return &Or{flatChildren}, changed
	case *Not:
		child, changed := flatten(s.Child)
		return &Not{child}, changed
	default:
		return q, false
	}
}
111616
func FlattenClaim(key string, claim interface{}) []string {
	attributes := []string{}
	if slice, ok := claim.([]string); ok {
		for _, data := range slice {
			attributes = append(attributes, key+"="+data)
		}
	}
	if attr, ok := claim.(string); ok {
		attributes = append(attributes, key+"="+attr)
	}
	if kv, ok := claim.(map[string]interface{}); ok {
		for ikey, ivalue := range kv {
			if attr, ok := ivalue.(string); ok {
				attributes = append(attributes, key+":"+ikey+"="+attr)
			}
		}
	}
	return attributes
}
447903
func (kf *KeyFormat) KeyNoPrefix(args ...interface{}) (Prefix, error) {
	key, err := kf.Key(args...)
	if err != nil {
		return nil, err
	}
	return key[len(kf.prefix):], nil
}
416298
func trimSpaces(k AST) AST {
	// trim left hand side of spaces
	for i := 0; i < len(k.Root.raw); i++ {
		if !isWhitespace(k.Root.raw[i]) {
			break
		}

		k.Root.raw = k.Root.raw[1:]
		i--
	}

	// trim right hand side of spaces
	for i := len(k.Root.raw) - 1; i >= 0; i-- {
		if !isWhitespace(k.Root.raw[i]) {
			break
		}

		k.Root.raw = k.Root.raw[:len(k.Root.raw)-1]
	}

	return k
}
159432
func Flatten(list cty.Value) (cty.Value, error) {
	return FlattenFunc.Call([]cty.Value{list})
}
423130
func (p *MemPostings) SortedKeys() []labels.Label {
	p.mtx.RLock()
	keys := make([]labels.Label, 0, len(p.m))

	for n, e := range p.m {
		for v := range e {
			keys = append(keys, labels.Label{Name: n, Value: v})
		}
	}
	p.mtx.RUnlock()

	sort.Slice(keys, func(i, j int) bool {
		if d := strings.Compare(keys[i].Name, keys[j].Name); d != 0 {
			return d < 0
		}
		return keys[i].Value < keys[j].Value
	})
	return keys
}
931757
func Flatten(source map[string]interface{}) map[string]interface{} {
	target := make(map[string]interface{})
	flatten(target, source, nil)
	return target
}
1283940
func normalizeKeys(key []string) {
	for i, k := range key {
		if !validCharactersRE.MatchString(k) {
			// first replace */* with all
			k = strings.Replace(k, allMatcher, allReplacement, -1)

			// now replace all other invalid characters with a safe one.
			key[i] = invalidCharactersRE.ReplaceAllString(k, normalizedToken)
		}
	}
}