2019-11-10 03:36:53 -05:00
|
|
|
package d2helper
|
2019-10-24 09:31:59 -04:00
|
|
|
|
2019-10-27 17:24:21 -04:00
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"fmt"
|
|
|
|
"strconv"
|
|
|
|
"strings"
|
|
|
|
"unicode/utf16"
|
|
|
|
"unicode/utf8"
|
|
|
|
)
|
2019-10-24 09:31:59 -04:00
|
|
|
|
2019-11-03 22:03:22 -05:00
|
|
|
// AsterToEmpty converts strings beginning with "*" to "", for use when handling columns where an asterix can be used to comment out entries
|
|
|
|
func AsterToEmpty(text string) string {
|
|
|
|
if strings.HasPrefix(text, "*") {
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
return text
|
|
|
|
}
|
|
|
|
|
|
|
|
// EmptyToZero converts empty strings to "0" and leaves non-empty strings as is, for use before converting numerical data which equates empty to zero
|
|
|
|
func EmptyToZero(text string) string {
|
|
|
|
if text == "" || text == " " {
|
|
|
|
return "0"
|
|
|
|
}
|
|
|
|
return text
|
|
|
|
}
|
|
|
|
|
2019-10-24 09:31:59 -04:00
|
|
|
// StringToInt converts a string to an integer
|
|
|
|
func StringToInt(text string) int {
|
|
|
|
result, err := strconv.Atoi(text)
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
return result
|
|
|
|
}
|
|
|
|
|
2019-11-05 22:23:48 -05:00
|
|
|
// SafeStringToInt converts a string to an integer, or returns -1 on falure
|
|
|
|
func SafeStringToInt(text string) int {
|
|
|
|
result, err := strconv.Atoi(text)
|
|
|
|
if err != nil {
|
|
|
|
return -1
|
|
|
|
}
|
|
|
|
return result
|
|
|
|
}
|
|
|
|
|
2019-11-03 22:03:22 -05:00
|
|
|
// StringToUint converts a string to a uint32
|
|
|
|
func StringToUint(text string) uint {
|
|
|
|
result, err := strconv.ParseUint(text, 10, 32)
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
return uint(result)
|
|
|
|
}
|
|
|
|
|
2019-10-24 09:31:59 -04:00
|
|
|
// StringToUint8 converts a string to an uint8
|
|
|
|
func StringToUint8(text string) uint8 {
|
|
|
|
result, err := strconv.Atoi(text)
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
if result < 0 || result > 255 {
|
2019-11-03 22:03:22 -05:00
|
|
|
panic(fmt.Sprintf("value %d out of range of byte", result))
|
2019-10-24 09:31:59 -04:00
|
|
|
}
|
|
|
|
return uint8(result)
|
|
|
|
}
|
|
|
|
|
|
|
|
// StringToInt8 converts a string to an int8
|
|
|
|
func StringToInt8(text string) int8 {
|
|
|
|
result, err := strconv.Atoi(text)
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
if result < -128 || result > 122 {
|
2019-11-03 22:03:22 -05:00
|
|
|
panic(fmt.Sprintf("value %d out of range of a signed byte", result))
|
2019-10-24 09:31:59 -04:00
|
|
|
}
|
|
|
|
return int8(result)
|
|
|
|
}
|
2019-10-27 17:24:21 -04:00
|
|
|
|
2019-11-03 22:56:26 -05:00
|
|
|
// StringToFloat64 converts a string to a float64
|
|
|
|
func StringToFloat64(text string) float64 {
|
|
|
|
result, err := strconv.ParseFloat(text, 64)
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
return float64(result)
|
|
|
|
}
|
|
|
|
|
2019-10-27 17:24:21 -04:00
|
|
|
func Utf16BytesToString(b []byte) (string, error) {
|
|
|
|
|
|
|
|
if len(b)%2 != 0 {
|
|
|
|
return "", fmt.Errorf("Must have even length byte slice")
|
|
|
|
}
|
|
|
|
|
|
|
|
u16s := make([]uint16, 1)
|
|
|
|
|
|
|
|
ret := &bytes.Buffer{}
|
|
|
|
|
|
|
|
b8buf := make([]byte, 4)
|
|
|
|
|
|
|
|
lb := len(b)
|
|
|
|
for i := 0; i < lb; i += 2 {
|
|
|
|
u16s[0] = uint16(b[i]) + (uint16(b[i+1]) << 8)
|
|
|
|
r := utf16.Decode(u16s)
|
|
|
|
n := utf8.EncodeRune(b8buf, r[0])
|
|
|
|
ret.Write(b8buf[:n])
|
|
|
|
}
|
|
|
|
|
|
|
|
return ret.String(), nil
|
|
|
|
}
|
|
|
|
|
2019-11-01 00:34:23 -04:00
|
|
|
func CombineStrings(input []string) string {
|
|
|
|
return strings.Join(input, "\n")
|
|
|
|
}
|
|
|
|
|
2019-10-27 17:24:21 -04:00
|
|
|
func SplitIntoLinesWithMaxWidth(fullSentence string, maxChars int) []string {
|
|
|
|
lines := make([]string, 0)
|
|
|
|
line := ""
|
|
|
|
totalLength := 0
|
|
|
|
words := strings.Split(fullSentence, " ")
|
|
|
|
for _, word := range words {
|
|
|
|
totalLength += 1 + len(word)
|
|
|
|
if totalLength > maxChars {
|
|
|
|
totalLength = len(word)
|
|
|
|
lines = append(lines, line)
|
|
|
|
line = ""
|
|
|
|
} else {
|
|
|
|
line += " "
|
|
|
|
}
|
|
|
|
line += word
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(line) > 0 {
|
|
|
|
lines = append(lines, line)
|
|
|
|
}
|
|
|
|
|
|
|
|
return lines
|
|
|
|
}
|