summaryrefslogtreecommitdiff
path: root/chomp.go
diff options
context:
space:
mode:
authorJeff Carr <[email protected]>2024-01-25 00:39:14 -0600
committerJeff Carr <[email protected]>2024-01-25 00:39:14 -0600
commit41fe4a4659d50d70a835224405490588019d24ff (patch)
treedd6677fb17d11a4233568fddf0d3a141434bba10 /chomp.go
parente24c2c2eb3feb5083a74a9b2cb396bcbcb0ac52d (diff)
new homev0.13.0
Signed-off-by: Jeff Carr <[email protected]>
Diffstat (limited to 'chomp.go')
-rw-r--r--chomp.go74
1 files changed, 36 insertions, 38 deletions
diff --git a/chomp.go b/chomp.go
index 9bf331a..db37ecb 100644
--- a/chomp.go
+++ b/chomp.go
@@ -1,16 +1,16 @@
package shell
-/*
+/*
perl 'chomp'
send it anything, always get back a string
*/
import (
+ "bytes"
"fmt"
"reflect"
"strings"
- "bytes"
"go.wit.com/log"
)
@@ -24,14 +24,12 @@ func chompBytesBuffer(buf *bytes.Buffer) string {
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!)
@@ -42,8 +40,8 @@ func perlChomp(s string) string {
s = strings.TrimSuffix(s, "\r")
s = strings.TrimSuffix(s, "\n")
- s = strings.TrimSpace(s) // this is like 'chomp' in perl
- s = strings.TrimSuffix(s, "\n") // this is like 'chomp' in perl
+ s = strings.TrimSpace(s) // this is like 'chomp' in perl
+ s = strings.TrimSuffix(s, "\n") // this is like 'chomp' in perl
return s
}
@@ -51,40 +49,40 @@ func perlChomp(s string) string {
func Chomp(a interface{}) string {
// switch reflect.TypeOf(a) {
switch t := a.(type) {
- 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)
+ 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)
- 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 ""
- }
+ 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 ""
+ }
- 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))
+ 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))
}
tmp := "shell.Chomp() THIS SHOULD NEVER HAPPEN"
handleError(fmt.Errorf(tmp), -1)