-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.go
More file actions
59 lines (46 loc) · 1.87 KB
/
main.go
File metadata and controls
59 lines (46 loc) · 1.87 KB
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
// The `so/strings` package provides string manipulation
// functions, similar to Go's `strings` package.
package main
import (
"solod.dev/so/fmt"
"solod.dev/so/mem"
"solod.dev/so/strings"
)
func main() {
contains := strings.Contains("test", "es")
fmt.Printf("Contains(test, es) = %d\n", contains)
count := strings.Count("test", "t")
fmt.Printf("Count(test, t) = %d\n", int32(count))
hasPrefix := strings.HasPrefix("test", "te")
fmt.Printf("HasPrefix(test, te) = %d\n", hasPrefix)
hasSuffix := strings.HasSuffix("test", "st")
fmt.Printf("HasSuffix(test, st) = %d\n", hasSuffix)
index := strings.Index("test", "e")
fmt.Printf("Index(test, e) = %d\n", int32(index))
// Some functions allocate new strings to return the result.
// Use mem.FreeString to free them and avoid memory leaks.
joined := strings.Join(mem.System, []string{"a", "b"}, "-")
defer mem.FreeString(mem.System, joined)
fmt.Printf("Join(a, b, -) = %s\n", joined)
repeated := strings.Repeat(mem.System, "a", 5)
defer mem.FreeString(mem.System, repeated)
fmt.Printf("Repeat(a, 5) = %s\n", repeated)
replacedAll := strings.ReplaceAll(mem.System, "foo", "o", "0")
defer mem.FreeString(mem.System, replacedAll)
fmt.Printf("ReplaceAll(foo, o, 0) = %s\n", replacedAll)
replacedOnce := strings.Replace(mem.System, "foo", "o", "0", 1)
defer mem.FreeString(mem.System, replacedOnce)
fmt.Printf("Replace(foo, o, 0, 1) = %s\n", replacedOnce)
splitted := strings.Split(mem.System, "a-b-c-d-e", "-")
defer mem.FreeSlice(mem.System, splitted)
fmt.Println("Split(a-b-c-d-e, -):")
for i, s := range splitted {
fmt.Printf(" [%d] = %s\n", i, s)
}
lowered := strings.ToLower(mem.System, "TEST")
defer mem.FreeString(mem.System, lowered)
fmt.Printf("ToLower(TEST) = %s\n", lowered)
uppered := strings.ToUpper(mem.System, "test")
defer mem.FreeString(mem.System, uppered)
fmt.Printf("ToUpper(test) = %s\n", uppered)
}