summaryrefslogtreecommitdiffstats
path: root/lib/str.go
blob: 9112a149b9a052eebc79ad2748fc9d65bbd2831b (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
// Copyright © 2020 rsiddharth <s@ricketyspace.net>
// SPDX-License-Identifier: ISC

package lib

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
}

// 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
		}
		ss += string(s[i])
	}
	return ss
}

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

// Returns true if byte 'c' is a non-numeric character in the English language.
func isAlphaPunch(c byte) bool {
	switch {
	case 'A' <= c && c <= 'Z':
		return true
	case 'a' <= c && c <= 'z':
		return true
	case c == ' ' || c == '.':
		return true
	case c == ',' || c == '\'':
		return true
	case c == '"':
		return true
	}
	return false
}