blob: 1a30bd19a5695db4a6b2acce7ff65698652d32a3 [file] [log] [blame]
Nigel Tao63441812020-08-21 14:05:48 +10001// Copyright 2020 The Wuffs Authors.
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7// https://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
Nigel Tao788479d2021-08-22 10:52:51 +100015//go:build ignore
Nigel Tao63441812020-08-21 14:05:48 +100016// +build ignore
17
18package main
19
20// print-json-ascii-escapes.go prints the JSON escapes for ASCII characters.
21// For example, the JSON escapes for the ASCII "horizontal tab" and
22// "substitute" control characters are "\t" and "\u001A".
23//
24// Usage: go run print-json-ascii-escapes.go
25
26import (
27 "fmt"
28 "os"
29)
30
31func main() {
32 if err := main1(); err != nil {
33 os.Stderr.WriteString(err.Error() + "\n")
34 os.Exit(1)
35 }
36}
37
38func main1() error {
39 for c := 0; c < 0x80; c++ {
40 s := ""
41 switch c {
42 case '\b':
43 s = "\\b"
44 case '\f':
45 s = "\\f"
46 case '\n':
47 s = "\\n"
48 case '\r':
49 s = "\\r"
50 case '\t':
51 s = "\\t"
52 case '"':
53 s = "\\\""
54 case '\\':
55 s = "\\\\"
56 default:
57 if c > 0x20 {
58 s = string(c)
59 } else {
60 const hex = "0123456789ABCDEF"
61 s = string([]byte{
62 '\\',
63 'u',
64 '0',
65 '0',
66 hex[c>>4],
67 hex[c&0x0F],
68 })
69 }
70 }
71 b := make([]byte, 8)
72 b[0] = byte(len(s))
73 copy(b[1:], s)
74 description := s
75 if c == 0x7F {
76 description = "<DEL>"
77 }
78 for _, x := range b {
79 fmt.Printf("0x%02X, ", x)
80 }
81 fmt.Printf("// 0x%02X: %q\n", c, description)
82 }
83 return nil
84}