github.com/pavlo67/common@v0.5.3/common/httplib/win1251.go (about)

     1  package httplib
     2  
     3  import (
     4  	"bytes"
     5  	"errors"
     6  	"io/ioutil"
     7  	"regexp"
     8  
     9  	"golang.org/x/text/encoding/charmap"
    10  	"golang.org/x/text/transform"
    11  
    12  	"github.com/pavlo67/common/common/strlib"
    13  )
    14  
    15  func Win1251ToUTF8(data []byte) ([]byte, error) {
    16  
    17  	if strlib.ReCheckUTF8.MatchString(string(data)) {
    18  		return data, errors.New("it is not win1251 filelib")
    19  	}
    20  	rInUTF8 := transform.NewReader(bytes.NewReader(data), charmap.Windows1251.NewDecoder())
    21  	newData, err := ioutil.ReadAll(rInUTF8)
    22  	if err != nil {
    23  		return nil, err
    24  	}
    25  	return newData, nil
    26  }
    27  
    28  var re1251Char = regexp.MustCompile(`(?ism)charset=windows-1251`)
    29  
    30  func ChangeCharsetWin1251(data []byte) []byte {
    31  	return []byte(re1251Char.ReplaceAllString(string(data), "charset=utf-8"))
    32  }