blob: f2501292078d1c0db12bf1af0333da8093beedcc [file] [log] [blame]
Nigel Taoa3827272020-03-12 14:33:27 +11001// 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// base38-encode.go prints the base38 encoding of each argument.
20//
21// Usage: go run base38-encode.go gif json
22
23import (
24 "fmt"
25 "os"
26 "strings"
27
28 "github.com/google/wuffs/lib/base38"
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 args := os.Args
40 if len(args) > 1 {
41 for _, arg := range args[1:] {
42 arg := (strings.ToLower(arg) + " ")[:4]
43 if code, ok := base38.Encode(arg); ok {
44 code0 := fmt.Sprintf("0x%06X", code)
45 code1 := fmt.Sprintf("0x%08X", code<<10)
46 fmt.Printf("%s code: %s %s code<<10: %s %s\n",
47 arg, code0, underscore(code0), code1, underscore(code1))
48 } else {
49 fmt.Printf("%s -\n", arg)
50 }
51 }
52 }
53 return nil
54}
55
56func underscore(s string) string {
57 if n := len(s) - 4; n > 0 {
58 return s[:n] + "_" + s[n:]
59 }
60 return s
61}