Nigel Tao | a382727 | 2020-03-12 14:33:27 +1100 | [diff] [blame] | 1 | // 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 Tao | 788479d | 2021-08-22 10:52:51 +1000 | [diff] [blame] | 15 | //go:build ignore |
Nigel Tao | a382727 | 2020-03-12 14:33:27 +1100 | [diff] [blame] | 16 | // +build ignore |
| 17 | |
| 18 | package main |
| 19 | |
| 20 | // base38-encode.go prints the base38 encoding of each argument. |
| 21 | // |
| 22 | // Usage: go run base38-encode.go gif json |
| 23 | |
| 24 | import ( |
| 25 | "fmt" |
| 26 | "os" |
| 27 | "strings" |
| 28 | |
| 29 | "github.com/google/wuffs/lib/base38" |
| 30 | ) |
| 31 | |
| 32 | func main() { |
| 33 | if err := main1(); err != nil { |
| 34 | os.Stderr.WriteString(err.Error() + "\n") |
| 35 | os.Exit(1) |
| 36 | } |
| 37 | } |
| 38 | |
| 39 | func main1() error { |
| 40 | args := os.Args |
| 41 | if len(args) > 1 { |
| 42 | for _, arg := range args[1:] { |
| 43 | arg := (strings.ToLower(arg) + " ")[:4] |
| 44 | if code, ok := base38.Encode(arg); ok { |
| 45 | code0 := fmt.Sprintf("0x%06X", code) |
| 46 | code1 := fmt.Sprintf("0x%08X", code<<10) |
| 47 | fmt.Printf("%s code: %s %s code<<10: %s %s\n", |
| 48 | arg, code0, underscore(code0), code1, underscore(code1)) |
| 49 | } else { |
| 50 | fmt.Printf("%s -\n", arg) |
| 51 | } |
| 52 | } |
| 53 | } |
| 54 | return nil |
| 55 | } |
| 56 | |
| 57 | func underscore(s string) string { |
| 58 | if n := len(s) - 4; n > 0 { |
| 59 | return s[:n] + "_" + s[n:] |
| 60 | } |
| 61 | return s |
| 62 | } |