-
Notifications
You must be signed in to change notification settings - Fork 4
/
strings.go
77 lines (68 loc) · 1.46 KB
/
strings.go
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
// Copyright (c) 2022, Cogent Core. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
// This is initially adapted from https://github.com/vulkan-go/asche
// Copyright © 2017 Maxim Kupriianov <[email protected]>, under the MIT License
package vgpu
import (
"strings"
"unicode"
)
func CheckExisting(actual, required []string) (existing []string, missing int) {
existing = make([]string, 0, len(required))
for j := range required {
req := SafeString(required[j])
for i := range actual {
if SafeString(actual[i]) == req {
existing = append(existing, req)
}
}
}
missing = len(required) - len(existing)
return existing, missing
}
var end = "\x00"
var endChar byte = '\x00'
func SafeString(s string) string {
if len(s) == 0 {
return end
}
if s[len(s)-1] != endChar {
return s + end
}
return s
}
func SafeStrings(list []string) []string {
for i := range list {
list[i] = SafeString(list[i])
}
return list
}
func CleanString(s string) string {
s = SafeString(s)
ss := ""
lastSpace := false
for _, r := range s {
if unicode.IsLetter(r) || unicode.IsNumber(r) {
ss += string(r)
lastSpace = false
} else {
if !lastSpace {
ss += " "
lastSpace = true
}
}
}
if lastSpace {
return ss[:len(ss)-1]
}
return ss
}
func HasAllStrings(s string, strs []string) bool {
for _, ss := range strs {
if !strings.Contains(s, ss) {
return false
}
}
return true
}