gitee.com/quant1x/gox@v1.7.6/text/encoding/euc-jp.go (about)

     1  package encoding
     2  
     3  // Converters for the EUC-JP encoding
     4  
     5  import (
     6  	"sync"
     7  )
     8  
     9  func init() {
    10  	RegisterCharset(&Charset{
    11  		Name:    "EUC-JP",
    12  		Aliases: []string{"extended_unix_code_packed_format_for_japanese", "cseucpkdfmtjapanese"},
    13  		NewDecoder: func() Decoder {
    14  			eucJPOnce.Do(makeEUCJPTable)
    15  			return eucJPTable.Decoder()
    16  		},
    17  		NewEncoder: func() Encoder {
    18  			eucJPOnce.Do(makeEUCJPTable)
    19  			return eucJPTable.Encoder()
    20  		},
    21  	})
    22  }
    23  
    24  var eucJPOnce sync.Once
    25  var eucJPTable MBCSTable
    26  
    27  func makeEUCJPTable() {
    28  	var b [3]byte
    29  
    30  	b[0] = 0x8f
    31  	for jis0212, unicode := range jis0212ToUnicode {
    32  		if unicode == 0 {
    33  			continue
    34  		}
    35  
    36  		b[1] = byte(jis0212>>8) | 128
    37  		b[2] = byte(jis0212) | 128
    38  		eucJPTable.AddCharacter(rune(unicode), string(b[:3]))
    39  	}
    40  
    41  	for jis0208, unicode := range jis0208ToUnicode {
    42  		if unicode == 0 {
    43  			continue
    44  		}
    45  
    46  		b[0] = byte(jis0208>>8) | 128
    47  		b[1] = byte(jis0208) | 128
    48  		eucJPTable.AddCharacter(rune(unicode), string(b[:2]))
    49  	}
    50  
    51  	b[0] = 0x8e
    52  	for i := 128; i < 256; i++ {
    53  		unicode := jis0201ToUnicode[i]
    54  		if unicode == 0 {
    55  			continue
    56  		}
    57  
    58  		b[1] = byte(i)
    59  		eucJPTable.AddCharacter(rune(unicode), string(b[:2]))
    60  	}
    61  
    62  	for i := '\x00'; i < 128; i++ {
    63  		var unicode rune
    64  		if i < 32 || i == 127 {
    65  			unicode = i
    66  		} else {
    67  			unicode = rune(jis0201ToUnicode[i])
    68  			if unicode == 0 {
    69  				continue
    70  			}
    71  		}
    72  
    73  		eucJPTable.AddCharacter(unicode, string(byte(i)))
    74  	}
    75  }