aboutsummaryrefslogtreecommitdiff
path: root/go/cmd/chardump/main.go
diff options
context:
space:
mode:
authorMitchell Riedstra <mitch@riedstra.dev>2022-12-28 18:18:39 -0500
committerMitchell Riedstra <mitch@riedstra.dev>2022-12-28 18:46:42 -0500
commitd7eec191b9b48a52fbca9b6a90357da0e36680ba (patch)
tree10c57675415bf8899ec1898180046ceb0e99b6bb /go/cmd/chardump/main.go
parentd0ad22b295eae397f39de3a9542ade6320a0591b (diff)
downloaddotfiles-0.0.1.tar.gz
dotfiles-0.0.1.tar.xz
Add a couple of tiny little go utilitiesv0.0.1
Diffstat (limited to 'go/cmd/chardump/main.go')
-rw-r--r--go/cmd/chardump/main.go41
1 files changed, 41 insertions, 0 deletions
diff --git a/go/cmd/chardump/main.go b/go/cmd/chardump/main.go
new file mode 100644
index 0000000..c275205
--- /dev/null
+++ b/go/cmd/chardump/main.go
@@ -0,0 +1,41 @@
+package main
+
+import (
+ "bytes"
+ "fmt"
+ "io"
+ "log"
+ "os"
+ "strconv"
+)
+
+var Logger = log.New(os.Stderr, "", 0)
+
+func dumpString(w io.Writer, s string) {
+ n := 0
+ for _, char := range s { // I don't care about the byte number, just chars...
+ // https://go.dev/ref/spec#Rune_literals
+ fmt.Fprintf(w,
+ "Number: %-4d Char: %-6s Unicode: %U Hex Bytes: %+v\n",
+ n,
+ strconv.QuoteRune(char),
+ char,
+ []byte(string(char)))
+ n++
+ }
+}
+
+func main() {
+ buf := &bytes.Buffer{}
+
+ n, err := io.Copy(buf, os.Stdin)
+ if err != io.EOF && err != nil {
+ Logger.Fatalf("Failed to read stdin: %s\n", err)
+ }
+
+ Logger.Println("------------------------")
+ Logger.Printf("Read in %d bytes\n", n)
+ Logger.Println("------------------------")
+
+ dumpString(os.Stdout, buf.String())
+}