|
1 | 1 | package shiftjis_transformer
|
2 | 2 |
|
3 | 3 | import (
|
| 4 | + "bytes" |
4 | 5 | "reflect"
|
| 6 | + "strings" |
5 | 7 |
|
| 8 | + "github.com/tomtwinkle/garbledreplacer" |
6 | 9 | "golang.org/x/text/encoding/japanese"
|
7 | 10 | "golang.org/x/text/transform"
|
8 | 11 | )
|
9 | 12 |
|
10 | 13 | var encoder = japanese.ShiftJIS.NewEncoder()
|
11 | 14 | var decoder = japanese.ShiftJIS.NewDecoder()
|
12 | 15 |
|
| 16 | +var charMap = map[rune]rune{ |
| 17 | + '−': '-', // Convert full-width minus to half-width hyphen |
| 18 | + '〜': '~', // Convert full-width tilde to half-width tilde |
| 19 | + '-': '-', // Convert full-width dash to half-width hyphen |
| 20 | + ' ': ' ', // Convert full-width space to half-width space |
| 21 | + '”': '"', // Convert full-width double quote to half-width |
| 22 | + '“': '"', // Convert full-width double quote to half-width |
| 23 | + '‘': '\'', // Convert full-width single quote to half-width |
| 24 | + '’': '\'', // Convert full-width single quote to half-width |
| 25 | + '¥': '\\', // Convert full-width yen mark to half-width backslash |
| 26 | + '(': '(', // Convert full-width left parenthesis to half-width |
| 27 | + ')': ')', // Convert full-width right parenthesis to half-width |
| 28 | + '[': '[', // Convert full-width left square bracket to half-width |
| 29 | + ']': ']', // Convert full-width right square bracket to half-width |
| 30 | + '{': '{', // Convert full-width left curly brace to half-width |
| 31 | + '}': '}', // Convert full-width right curly brace to half-width |
| 32 | + '<': '<', // Convert full-width less than to half-width |
| 33 | + '>': '>', // Convert full-width greater than to half-width |
| 34 | + '=': '=', // Convert full-width equals to half-width |
| 35 | + '+': '+', // Convert full-width plus to half-width |
| 36 | + ';': ';', // Convert full-width semicolon to half-width |
| 37 | + ':': ':', // Convert full-width colon to half-width |
| 38 | + '*': '*', // Convert full-width asterisk to half-width |
| 39 | + '&': '&', // Convert full-width ampersand to half-width |
| 40 | + '%': '%', // Convert full-width percent to half-width |
| 41 | + '#': '#', // Convert full-width hash to half-width |
| 42 | + '@': '@', // Convert full-width at sign to half-width |
| 43 | + '!': '!', // Convert full-width exclamation mark to half-width |
| 44 | + '?': '?', // Convert full-width question mark to half-width |
| 45 | + '|': '|', // Convert full-width vertical bar to half-width |
| 46 | +} |
| 47 | + |
| 48 | +// replace specific characters in the string with the mapping table |
| 49 | +func replaceUnsupportedChars(input string) string { |
| 50 | + return strings.Map(func(r rune) rune { |
| 51 | + if newR, ok := charMap[r]; ok { |
| 52 | + return newR |
| 53 | + } |
| 54 | + return r |
| 55 | + }, input) |
| 56 | +} |
| 57 | + |
13 | 58 | func EncodeToShiftJISFromUTF8(input interface{}) error {
|
14 | 59 | val := reflect.ValueOf(input).Elem()
|
15 | 60 | for i := 0; i < val.NumField(); i++ {
|
16 | 61 | if val.Field(i).Kind() == reflect.String {
|
17 |
| - encoded, _, err := transform.String(encoder, val.Field(i).String()) |
| 62 | + original := val.Field(i).String() |
| 63 | + replaced := replaceUnsupportedChars(original) |
| 64 | + |
| 65 | + var buf bytes.Buffer |
| 66 | + w := transform.NewWriter(&buf, garbledreplacer.NewTransformer(japanese.ShiftJIS, '?')) |
| 67 | + _, err := w.Write([]byte(replaced)) |
18 | 68 | if err != nil {
|
19 | 69 | return err
|
20 | 70 | }
|
21 |
| - val.Field(i).SetString(encoded) |
| 71 | + val.Field(i).SetString(buf.String()) |
22 | 72 | }
|
23 | 73 | }
|
24 | 74 | return nil
|
|
0 commit comments