aboutsummaryrefslogtreecommitdiff
path: root/go/cmd/chardump/main.go
blob: c275205fcda8a722188637f9bcc1b64cecfba0b0 (plain) (blame)
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
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())
}