Debian Codegraph @

common/utilities.go

raw
1 package common
2
3 import (
4 "strings"
5 "unicode"
6
7 "github.com/pkg/errors"
8 )
9
10 // IndexOf searches a string for the first occurrence of another string, starting
11 // at a given offset.
12 // @param str1 String to search.
13 // @param str2 String to search for.
14 // @param off Integer offset or -1 if not found.
15 func IndexOf(str1, str2 string, off int) int {
16 index := strings.Index(str1[off:], str2)
17 if index == -1 {
18 return -1
19 }
20 return index + off
21 }
22
23 // ContainsWildcards searches string for special characters * and ?
24 // @param string String to be searched
25 // @return true if string contains wildcard, false otherwise
26 func ContainsWildcards(str string) bool {
27 prev := ' '
28 for _, s := range str {
29 if s == '*' || s == '?' {
30 if prev != '\\' {
31 return true
32 }
33 }
34 prev = s
35 }
36 return false
37 }
38
39 // ContainsQuestions searches string for special characters ?
40 // @param string String to be searched
41 // @return true if string contains wildcard, false otherwise
42 func ContainsQuestions(str string) bool {
43 prev := ' '
44 for _, s := range str {
45 if s == '?' {
46 if prev != '\\' {
47 return true
48 }
49 }
50 prev = s
51 }
52 return false
53 }
54
55 // CountEscapeCharacters counts the number of escape characters in the string beginning and ending
56 // at the given indices
57 // @param str string to search
58 // @param start beginning index
59 // @param end ending index
60 // @return number of escape characters in string
61 func CountEscapeCharacters(str string) (result int) {
62 active := false
63 for _, s := range str {
64 if !active && s == '\\' {
65 result++
66 active = true
67 } else {
68 active = false
69 }
70 }
71 return result
72 }
73
74 // LengthWithEscapeCharacters counts the number of characters with escape characters
75 func LengthWithEscapeCharacters(str string) (result int) {
76 return len(str) - CountEscapeCharacters(str)
77 }
78
79 // GetUnescapedColonIndex searches a string for the first unescaped colon and returns the index of that colon
80 // @param str string to search
81 // @return index of first unescaped colon, or 0 if not found
82 func GetUnescapedColonIndex(str string) (idx int) {
83 for i, s := range str {
84 if s == ':' {
85 if i == 0 || str[i-1] != '\\' {
86 idx = i
87 break
88 }
89 }
90 }
91 return idx
92 }
93
94 // IsAlphanum returns true if the string contains only
95 // alphanumeric characters or the underscore character,
96 // false otherwise.
97 // @param c the string in question
98 // @return true if c is alphanumeric or underscore, false if not
99 func IsAlphanum(s string) bool {
100 for _, r := range s {
101 if !IsAlpha(r) && !unicode.IsDigit(r) && r != '_' {
102 return false
103 }
104 }
105 return true
106 }
107
108 // IsAlpha returns true if the rune contains only 'a'..'z' and 'A'..'Z'.
109 func IsAlpha(r rune) bool {
110 if (r < 'a' || r > 'z') && (r < 'A' || r > 'Z') {
111 return false
112 }
113 return true
114 }
115
116 // ValidateURI is not part of the reference implementation pseudo code
117 // found in the CPE 2.3 specification. It enforces two rules in the
118 // specification:
119 // URI must start with the characters "cpe:/"
120 // A URI may not contain more than 7 components
121 // If either rule is violated, a ParseErr is thrown.
122 func ValidateURI(in string) error {
123 in = strings.ToLower(in)
124 // make sure uri starts with cpe:/
125 if !strings.HasPrefix(in, "cpe:/") {
126 return errors.Wrapf(ErrParse, "Error: URI must start with 'cpe:/'. Given: %s", in)
127 }
128 // make sure uri doesn't contain more than 7 colons
129 if count := strings.Count(in, ":"); count > 7 {
130 return errors.Wrapf(ErrParse, "Error parsing URI. Found %d extra components in: %s", count-7, in)
131 }
132 return nil
133 }
134
135 // ValidateFS is not part of the reference implementation pseudo code
136 // found in the CPE 2.3 specification. It enforces three rules found in the
137 // specification:
138 // Formatted string must start with the characters "cpe:2.3:"
139 // A formatted string must contain 11 components
140 // A formatted string must not contain empty components
141 // If any rule is violated, a ParseException is thrown.
142 func ValidateFS(in string) error {
143 in = strings.ToLower(in)
144 if !strings.HasPrefix(in, "cpe:2.3:") {
145 return errors.Wrapf(ErrParse, "Error: Formatted String must start with \"cpe:2.3\". Given: %s", in)
146 }
147 // make sure fs contains exactly 12 unquoted colons
148 count := 0
149 for i := 0; i != len(in); i++ {
150 if in[i] == ':' {
151 if i == 0 || in[i-1] != '\\' {
152 count++
153 } else {
154 continue
155 }
156 if i < len(in)-1 && in[i+1] == ':' {
157 return errors.Wrap(ErrParse, "Error parsing formatted string. Found empty component")
158 }
159 }
160 }
161 if count > 12 {
162 extra := count - 12
163 return errors.Wrapf(ErrParse, "Error parsing formatted string. Found %d components in: %s", extra, in)
164 }
165 if count < 12 {
166 missing := 12 - count
167 return errors.Wrapf(ErrParse, "Error parsing formatted string. Missing %d components in: %s", missing, in)
168 }
169 return nil
170 }