File: pattern_set.go

package info (click to toggle)
golang-golang-x-arch 0.13.0-1
  • links: PTS, VCS
  • area: main
  • in suites: sid, trixie
  • size: 6,932 kB
  • sloc: ansic: 1,975; makefile: 59
file content (95 lines) | stat: -rw-r--r-- 2,406 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
// Copyright 2018 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 xeddata

import (
	"sort"
	"strings"
)

// PatternSet wraps instruction PATTERN properties providing set operations on them.
type PatternSet map[string]bool

// NewPatternSet decodes pattern string into PatternSet.
func NewPatternSet(pattern string) PatternSet {
	pset := make(PatternSet)
	for _, f := range strings.Fields(pattern) {
		pset[f] = true
	}
	return pset
}

// PatternAliases is extendable map of pattern keys aliases.
// Maps human-readable key to XED property.
//
// Used in PatternSet.Is.
var PatternAliases = map[string]string{
	"VEX":     "VEXVALID=1",
	"EVEX":    "VEXVALID=2",
	"XOP":     "VEXVALID=3",
	"MemOnly": "MOD!=3",
	"RegOnly": "MOD=3",
}

// String returns pattern printer representation.
// All properties are sorted.
func (pset PatternSet) String() string {
	var keys []string
	for k := range pset {
		keys = append(keys, k)
	}
	sort.Strings(keys)
	return strings.Join(keys, " ")
}

// Is reports whether set contains key k.
// In contrast with direct pattern set lookup, it does
// check if PatternAliases[k] is available to be used instead of k in lookup.
func (pset PatternSet) Is(k string) bool {
	if alias := PatternAliases[k]; alias != "" {
		return pset[alias]
	}
	return pset[k]
}

// Replace inserts newKey if oldKey is defined.
// oldKey is removed if insertion is performed.
func (pset PatternSet) Replace(oldKey, newKey string) {
	if pset[oldKey] {
		pset[newKey] = true
		delete(pset, oldKey)
	}
}

// Index returns index from keys of first matching key.
// Returns -1 if does not contain any of given keys.
func (pset PatternSet) Index(keys ...string) int {
	for i, k := range keys {
		if pset[k] {
			return i
		}
	}
	return -1
}

// Match is like MatchOrDefault("", keyval...).
func (pset PatternSet) Match(keyval ...string) string {
	return pset.MatchOrDefault("", keyval...)
}

// MatchOrDefault returns first matching key associated value.
// Returns defaultValue if no match is found.
//
// Keyval structure can be described as {"k1", "v1", ..., "kN", "vN"}.
func (pset PatternSet) MatchOrDefault(defaultValue string, keyval ...string) string {
	for i := 0; i < len(keyval); i += 2 {
		key := keyval[i+0]
		val := keyval[i+1]
		if pset[key] {
			return val
		}
	}
	return defaultValue
}