File: strings.go

package info (click to toggle)
golang-github-savsgio-gotils 0.0~git20250408.196191e-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 176 kB
  • sloc: makefile: 2
file content (72 lines) | stat: -rw-r--r-- 1,389 bytes parent folder | download
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
package strings

import (
	"github.com/savsgio/gotils/strconv"
)

// Copy returns a copy of string in a new pointer.
func Copy(s string) string {
	return string(strconv.S2B(s))
}

// CopySlice returns a copy of the slice.
func CopySlice(slice []string) []string {
	dst := make([]string, len(slice))
	copy(dst, slice)

	return dst
}

// IndexOf returns index position in slice from given string
// If value is -1, the string does not found.
func IndexOf(slice []string, s string) int {
	for i, v := range slice {
		if v == s {
			return i
		}
	}

	return -1
}

// Include returns true or false if given string is in slice.
func Include(slice []string, s string) bool {
	return IndexOf(slice, s) != -1
}

// UniqueAppend appends a string if not exist in the slice.
func UniqueAppend(slice []string, s ...string) []string {
	for i := range s {
		if IndexOf(slice, s[i]) != -1 {
			continue
		}

		slice = append(slice, s[i])
	}

	return slice
}

// EqualSlices checks if the slices are equal.
func EqualSlices(slice1, slice2 []string) bool {
	if len(slice1) != len(slice2) {
		return false
	}

	for i := range slice1 {
		if slice1[i] != slice2[i] {
			return false
		}
	}

	return true
}

// ReverseSlice reverses a string slice.
func ReverseSlice(slice []string) []string {
	for i, j := 0, len(slice)-1; i < j; i, j = i+1, j-1 {
		slice[i], slice[j] = slice[j], slice[i]
	}

	return slice
}