-
Notifications
You must be signed in to change notification settings - Fork 34
/
Copy pathstring_util.go
85 lines (76 loc) · 2.35 KB
/
string_util.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
78
79
80
81
82
83
84
85
package main
import (
"github.com/mattn/go-runewidth"
"github.com/nsf/termbox-go"
)
func isASCII(val byte) bool {
return (val >= 0x20 && val < 0x7f)
}
func isCode(val byte) bool {
return val == 0x09 || val == 0x0A || val == 0x0D
}
func isPrintable(val byte) bool {
return isASCII(val) || isCode(val)
}
func drawStringAtPoint(str string, x int, y int, fg termbox.Attribute, bg termbox.Attribute) int {
x_pos := x
for _, runeValue := range str {
termbox.SetCell(x_pos, y, runeValue, fg, bg)
x_pos += runewidth.RuneWidth(runeValue)
}
return x_pos - x
}
func binaryStringForInteger8(value uint8) string {
new_string := make([]byte, 1)
new_string[0] = value
return string(new_string[0:1])
}
func binaryStringForInteger16(value uint16, big_endian bool) string {
new_string := make([]byte, 2)
if big_endian {
new_string[0] = uint8(value >> 8)
new_string[1] = uint8(value & 0xFF)
} else {
new_string[0] = uint8(value & 0xFF)
new_string[1] = uint8(value >> 8)
}
return string(new_string[0:2])
}
func binaryStringForInteger32(value uint32, big_endian bool) string {
new_string := make([]byte, 4)
if big_endian {
new_string[0] = uint8(value >> 24)
new_string[1] = uint8((value >> 16) & 0xFF)
new_string[2] = uint8((value >> 8) & 0xFF)
new_string[3] = uint8(value & 0xFF)
} else {
new_string[0] = uint8(value & 0xFF)
new_string[1] = uint8((value >> 8) & 0xFF)
new_string[2] = uint8((value >> 16) & 0xFF)
new_string[3] = uint8(value >> 24)
}
return string(new_string[0:4])
}
func binaryStringForInteger64(value uint64, big_endian bool) string {
new_string := make([]byte, 8)
if big_endian {
new_string[0] = uint8((value >> 56) & 0xFF)
new_string[1] = uint8((value >> 48) & 0xFF)
new_string[2] = uint8((value >> 40) & 0xFF)
new_string[3] = uint8((value >> 32) & 0xFF)
new_string[4] = uint8((value >> 24) & 0xFF)
new_string[5] = uint8((value >> 16) & 0xFF)
new_string[6] = uint8((value >> 8) & 0xFF)
new_string[7] = uint8(value & 0xFF)
} else {
new_string[0] = uint8(value & 0xFF)
new_string[1] = uint8((value >> 8) & 0xFF)
new_string[2] = uint8((value >> 16) & 0xFF)
new_string[3] = uint8((value >> 24) & 0xFF)
new_string[4] = uint8((value >> 32) & 0xFF)
new_string[5] = uint8((value >> 40) & 0xFF)
new_string[6] = uint8((value >> 48) & 0xFF)
new_string[7] = uint8((value >> 56) & 0xFF)
}
return string(new_string[0:8])
}