amuz.es/src/go/misc@v1.0.1/han/euckr.go (about)

     1  package han
     2  
     3  import (
     4  	"github.com/valyala/fasthttp"
     5  	"go.uber.org/multierr"
     6  	"golang.org/x/text/encoding/korean"
     7  	"golang.org/x/text/transform"
     8  	"net/url"
     9  	"strings"
    10  	"unicode"
    11  )
    12  
    13  const (
    14  	hexConstUpper = "0123456789ABCDEF"
    15  )
    16  
    17  // ConvertFormEUCKRFasthttp is translate method from euc-kr encoded fasthttp.Args to utf-8 encoded fasthttp.Args.
    18  func ConvertFormEUCKRFasthttp(rawParsed *fasthttp.Args) (parsed *fasthttp.Args, err error) {
    19  	var (
    20  		//rawKey, rawValue string
    21  		decoder = korean.EUCKR.NewDecoder()
    22  	)
    23  
    24  	parsed = &fasthttp.Args{}
    25  	rawParsed.VisitAll(func(k, v []byte) {
    26  		if rawKey, decodeErr := decoder.Bytes(k); decodeErr != nil {
    27  			err = multierr.Append(err, decodeErr)
    28  		} else if rawValue, decodeErr := decoder.Bytes(v); decodeErr != nil {
    29  			err = multierr.Append(err, decodeErr)
    30  		} else {
    31  			parsed.SetBytesKV(rawKey, rawValue)
    32  		}
    33  	})
    34  
    35  	return
    36  }
    37  
    38  // ConvertFormEUCKR is translate method from euc-kr encoded url.Values to utf-8 encoded url.Values.
    39  func ConvertFormEUCKR(rawParsed url.Values) (parsed url.Values, err error) {
    40  	var (
    41  		rawKey, rawValue string
    42  		decoder          = korean.EUCKR.NewDecoder()
    43  	)
    44  
    45  	parsed = make(url.Values, len(rawParsed))
    46  	for k, v := range rawParsed {
    47  		rawKey, err = decoder.String(k)
    48  		if err != nil {
    49  			return
    50  		}
    51  		valueList := make([]string, 0, len(v))
    52  		for _, vi := range v {
    53  			rawValue, err = decoder.String(vi)
    54  			if err != nil {
    55  				return
    56  			}
    57  			valueList = append(valueList, rawValue)
    58  		}
    59  		parsed[rawKey] = valueList
    60  	}
    61  	return
    62  }
    63  
    64  // ConvertToEUCKRUrlEncoded is translate method from utf-8 encoded string to euc-kr encoded string.
    65  func ConvertToEUCKRUrlEncoded(str string) (converted string, err error) {
    66  	var (
    67  		buf     strings.Builder
    68  		encoder = korean.EUCKR.NewEncoder()
    69  		size    int
    70  	)
    71  	for _, r := range str {
    72  		switch {
    73  		case unicode.IsNumber(r) ||
    74  			unicode.Is(unicode.Latin, r) ||
    75  			r == '-' ||
    76  			r == '_' ||
    77  			r == '.':
    78  			buf.WriteRune(r)
    79  		case unicode.IsSpace(r):
    80  			buf.WriteByte('+')
    81  		case r == '$' ||
    82  			r == '&' ||
    83  			r == '+' ||
    84  			r == ',' ||
    85  			r == '/' ||
    86  			r == ':' ||
    87  			r == ';' ||
    88  			r == '=' ||
    89  			r == '?' ||
    90  			r == '@':
    91  			buf.WriteByte('%')
    92  			buf.WriteByte(hexConstUpper[byte(r)>>4])
    93  			buf.WriteByte(hexConstUpper[byte(r)&15])
    94  		default:
    95  			converted, size, err = transform.String(encoder, string(r))
    96  			if err != nil {
    97  				return
    98  			}
    99  			buf.WriteByte('%')
   100  			buf.WriteByte(hexConstUpper[(converted[0]>>4)&0xf])
   101  			buf.WriteByte(hexConstUpper[converted[0]&0xf])
   102  			if size > 1 {
   103  				buf.WriteByte('%')
   104  				buf.WriteByte(hexConstUpper[(converted[1]>>4)&0xf])
   105  				buf.WriteByte(hexConstUpper[converted[1]&0xf])
   106  			}
   107  		}
   108  	}
   109  	converted = buf.String()
   110  	return
   111  }
   112  
   113  // ConvertFromEUCKRUrlEncoded is translate method from euc-kr encoded string to utf-8 encoded string.
   114  func ConvertFromEUCKRUrlEncoded(str string) (converted string, err error) {
   115  	dst, err := url.QueryUnescape(str)
   116  	if err != nil {
   117  		return
   118  	}
   119  	converted, _, err = transform.String(korean.EUCKR.NewDecoder(), dst)
   120  	return
   121  }