2019-06-06 17:59:49 -05:00
|
|
|
package shell
|
|
|
|
|
2024-01-25 00:39:14 -06:00
|
|
|
/*
|
2019-06-06 17:59:49 -05:00
|
|
|
perl 'chomp'
|
|
|
|
|
|
|
|
send it anything, always get back a string
|
|
|
|
*/
|
|
|
|
|
2024-01-10 18:39:58 -06:00
|
|
|
import (
|
2024-01-25 00:39:14 -06:00
|
|
|
"bytes"
|
2024-01-10 18:39:58 -06:00
|
|
|
"fmt"
|
|
|
|
"reflect"
|
|
|
|
"strings"
|
|
|
|
|
|
|
|
"go.wit.com/log"
|
|
|
|
)
|
2019-06-06 17:59:49 -05:00
|
|
|
|
|
|
|
// import "github.com/davecgh/go-spew/spew"
|
|
|
|
|
|
|
|
func chompBytesBuffer(buf *bytes.Buffer) string {
|
|
|
|
var bytesSplice []byte
|
|
|
|
bytesSplice = buf.Bytes()
|
|
|
|
|
|
|
|
return Chomp(string(bytesSplice))
|
|
|
|
}
|
|
|
|
|
|
|
|
// TODO: obviously this is stupidly wrong
|
|
|
|
// TODO: fix this to trim fucking everything
|
|
|
|
// really world? 8 fucking years of this language
|
|
|
|
// and I'm fucking writing this? jesus. how the
|
|
|
|
// hell is everyone else doing this? Why isn't
|
|
|
|
// this already in the strings package?
|
|
|
|
func perlChomp(s string) string {
|
|
|
|
// lots of stuff in go moves around the whole block of whatever it is so lots of things are padded with NULL values
|
|
|
|
s = strings.Trim(s, "\x00") // removes NULL (needed!)
|
|
|
|
|
|
|
|
// TODO: christ. make some fucking regex that takes out every NULL, \t, ' ", \n, and \r
|
|
|
|
s = strings.Trim(s, "\n")
|
|
|
|
s = strings.Trim(s, "\n")
|
|
|
|
s = strings.TrimSuffix(s, "\r")
|
|
|
|
s = strings.TrimSuffix(s, "\n")
|
|
|
|
|
2024-01-25 00:39:14 -06:00
|
|
|
s = strings.TrimSpace(s) // this is like 'chomp' in perl
|
|
|
|
s = strings.TrimSuffix(s, "\n") // this is like 'chomp' in perl
|
2019-06-06 17:59:49 -05:00
|
|
|
return s
|
|
|
|
}
|
|
|
|
|
|
|
|
// TODO: fix this to chomp \n \r NULL \t and ' '
|
|
|
|
func Chomp(a interface{}) string {
|
|
|
|
// switch reflect.TypeOf(a) {
|
|
|
|
switch t := a.(type) {
|
2024-01-25 00:39:14 -06:00
|
|
|
case string:
|
|
|
|
var s string
|
|
|
|
s = a.(string)
|
|
|
|
return perlChomp(s)
|
|
|
|
case []uint8:
|
|
|
|
// log.Printf("shell.Chomp() FOUND []uint8")
|
|
|
|
var tmp []uint8
|
|
|
|
tmp = a.([]uint8)
|
2019-06-06 17:59:49 -05:00
|
|
|
|
2024-01-25 00:39:14 -06:00
|
|
|
s := string(tmp)
|
|
|
|
return perlChomp(s)
|
|
|
|
case uint64:
|
|
|
|
// log.Printf("shell.Chomp() FOUND []uint64")
|
|
|
|
s := fmt.Sprintf("%d", a.(uint64))
|
|
|
|
return perlChomp(s)
|
|
|
|
case int64:
|
|
|
|
// log.Printf("shell.Chomp() FOUND []int64")
|
|
|
|
s := fmt.Sprintf("%d", a.(int64))
|
|
|
|
return perlChomp(s)
|
|
|
|
case *bytes.Buffer:
|
|
|
|
// log.Printf("shell.Chomp() FOUND *bytes.Buffer")
|
|
|
|
var tmp *bytes.Buffer
|
|
|
|
tmp = a.(*bytes.Buffer)
|
|
|
|
if tmp == nil {
|
|
|
|
return ""
|
|
|
|
}
|
2019-06-06 17:59:49 -05:00
|
|
|
|
2024-01-25 00:39:14 -06:00
|
|
|
var bytesSplice []byte
|
|
|
|
bytesSplice = tmp.Bytes()
|
|
|
|
return Chomp(string(bytesSplice))
|
|
|
|
default:
|
|
|
|
tmp := fmt.Sprint("shell.Chomp() NO HANDLER FOR TYPE: %T", a)
|
|
|
|
handleError(fmt.Errorf(tmp), -1)
|
|
|
|
log.Warn("shell.Chomp() NEED TO MAKE CONVERTER FOR type =", reflect.TypeOf(t))
|
2019-06-06 17:59:49 -05:00
|
|
|
}
|
2019-06-06 21:32:12 -05:00
|
|
|
tmp := "shell.Chomp() THIS SHOULD NEVER HAPPEN"
|
|
|
|
handleError(fmt.Errorf(tmp), -1)
|
2019-06-06 17:59:49 -05:00
|
|
|
return ""
|
|
|
|
}
|
2024-02-22 15:55:16 -06:00
|
|
|
|
|
|
|
// this is stuff from a long time ago that there must be a replacement for
|
|
|
|
func RemoveFirstElement(slice []string) (string, []string) {
|
|
|
|
if len(slice) == 0 {
|
|
|
|
return "", slice // Return the original slice if it's empty
|
|
|
|
}
|
|
|
|
return slice[0], slice[1:] // Return the slice without the first element
|
|
|
|
}
|