blob: 40ed2a88462cb250004f9bc39cdd8b33e0630c0f [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
15// +build ignore
16
17package main
18
19// print-json-ascii-escapes.go prints the JSON escapes for ASCII characters.
20// For example, the JSON escapes for the ASCII "horizontal tab" and
21// "substitute" control characters are "\t" and "\u001A".
22//
23// Usage: go run print-json-ascii-escapes.go
24
25import (
26 "fmt"
27 "os"
28)
29
30func main() {
31 if err := main1(); err != nil {
32 os.Stderr.WriteString(err.Error() + "\n")
33 os.Exit(1)
34 }
35}
36
37func main1() error {
38 for c := 0; c < 0x80; c++ {
39 s := ""
40 switch c {
41 case '\b':
42 s = "\\b"
43 case '\f':
44 s = "\\f"
45 case '\n':
46 s = "\\n"
47 case '\r':
48 s = "\\r"
49 case '\t':
50 s = "\\t"
51 case '"':
52 s = "\\\""
53 case '\\':
54 s = "\\\\"
55 default:
56 if c > 0x20 {
57 s = string(c)
58 } else {
59 const hex = "0123456789ABCDEF"
60 s = string([]byte{
61 '\\',
62 'u',
63 '0',
64 '0',
65 hex[c>>4],
66 hex[c&0x0F],
67 })
68 }
69 }
70 b := make([]byte, 8)
71 b[0] = byte(len(s))
72 copy(b[1:], s)
73 description := s
74 if c == 0x7F {
75 description = "<DEL>"
76 }
77 for _, x := range b {
78 fmt.Printf("0x%02X, ", x)
79 }
80 fmt.Printf("// 0x%02X: %q\n", c, description)
81 }
82 return nil
83}