summaryrefslogtreecommitdiffstats
path: root/lib/str.go
blob: 2f6ece4af05b6f477060570d1005d684141cd320 (plain) (blame)
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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
// Copyright © 2021 siddharth <s@ricketyspace.net>
// SPDX-License-Identifier: ISC

package lib

// Key: ASCII character; Value: score
var AsciiScores map[byte]int = make(map[byte]int, 0)

// Printable ASCII characters orrdered by frequency.
// (https://mdickens.me/typing/letter_frequency.html)
var PrintableAscii []byte = []byte{
	' ', 'e', 't', 'a', 'o', 'i', 'n', 's', 'r', 'h', 'l', 'd',
	'c', 'u', 'm', 'f', 'g', 'p', 'y', 'w', 'b', ',', '.',
	'v', 'k', '-', '"', '_', '\'', 'x', ')', '(', ';', '0', 'j',
	'1', 'q', '=', '2', ':', 'z', '/', '*', '!', '?', '$', '3',
	'5', '>', '{', '}', '4', '9', '[', ']', '8', '6', '7', '\\',
	'+', '|', '&', '<', '%', '@', '#', '^', '`', '~',
}

func init() {
	// Initialize AsciiScores
	for pos, a := range PrintableAscii {
		AsciiScores[a] = 255 - pos
		// Also add the uppercase version of ascii
		// character if it exists.
		au := ByteToUpper(a)
		if a != au {
			AsciiScores[au] = 255 - pos
		}
	}
}

func FillStr(a string, l int) string {
	b := ""
	if l < 1 {
		return b
	}
	for i := 0; i < l; i++ {
		b += a
	}
	return b
}

func FillBytes(c byte, l int) []byte {
	if l < 1 {
		return make([]byte, 0)
	}
	bs := make([]byte, l)
	for i := 0; i < l; i++ {
		bs[i] = c
	}
	return bs
}

func StrToBytes(s string) []byte {
	bs := make([]byte, len(s))
	for i := 0; i < len(s); i++ {
		bs[i] = byte(s[i])
	}
	return bs
}

func BytesToStr(bs []byte) string {
	s := ""
	for i := 0; i < len(bs); i++ {
		s += string(bs[i])
	}
	return s
}

// Strip space and newline characters from string.
func StripSpaceChars(s string) string {
	ss := ""
	for i := 0; i < len(s); i++ {
		if s[i] == ' ' {
			continue
		}
		if s[i] == '\n' {
			continue
		}
		if s[i] == 0 { // NUL character
			continue
		}
		ss += string(s[i])
	}
	return ss
}

func AlphaPunchScore(bs []byte) int {
	s := 0
	for i := 0; i < len(bs); i++ {
		s += AsciiScores[bs[i]]
	}
	return s
}

func NumToChar(n int64) byte {
	if 0 <= n && n <= 9 {
		return byte(48 + n)
	}
	return 0
}

func NumToStr(n int64) string {
	s := ""
	for n != 0 {
		s = string(NumToChar(n%10)) + s
		n /= 10
	}
	return s
}

func StrSplitAt(c byte, s string) []string {
	l := make([]string, 0)

	acc := ""
	for i := 0; i < len(s); i++ {
		if s[i] == c {
			l = append(l, acc)
			acc = ""
		} else {
			acc += string(s[i])
		}
	}
	if len(acc) > 0 {
		l = append(l, acc)
	}
	return l
}

func StrToUpper(s string) string {
	us := ""
	for i := 0; i < len(s); i++ {
		us += string(ByteToUpper(s[i]))
	}
	return us
}

func ByteToUpper(b byte) byte {
	if 'a' <= b && b <= 'z' {
		return 'A' + (b - 'a')
	} else {
		return b
	}
}

// Returns true if string 's' has string 'n' in it.
func StrHas(s, n string) bool {
	for i := 0; i < len(s); i++ {
		if s[i:i+len(n)] == n {
			return true
		}
	}
	return false
}

// Converts a string to an integer.
func StrToNum(s string) (int, error) {
	var negative bool
	var n int

	if len(s) < 1 {
		return 0, CPError{"invalid number"}
	}
	if s[0] == '-' {
		negative = true
		s = s[1:]
	}
	u := 1
	for i := len(s) - 1; i >= 0; i-- {
		b := s[i]
		if b >= '0' && b <= '9' {
			n += int(b-48) * u
			u *= 10
		} else {
			return 0, CPError{"invalid number"}
		}
	}
	if negative {
		n *= -1
	}
	return n, nil
}