From 17d49510c41c17e0e7ecfaa617f1b361aab91186 Mon Sep 17 00:00:00 2001 From: John Date: Wed, 12 Jun 2019 21:02:00 +0800 Subject: [PATCH] add example for gcharset; fix issue in gxml --- g/encoding/gxml/gxml.go | 10 +++------- geg/encoding/gcharset/gcharset.go | 17 +++++++++++++++++ 2 files changed, 20 insertions(+), 7 deletions(-) create mode 100644 geg/encoding/gcharset/gcharset.go diff --git a/g/encoding/gxml/gxml.go b/g/encoding/gxml/gxml.go index 374641a11..772e8c1cb 100644 --- a/g/encoding/gxml/gxml.go +++ b/g/encoding/gxml/gxml.go @@ -9,7 +9,7 @@ package gxml import ( "fmt" - "github.com/gogf/gf/g/encoding/internal" + "github.com/gogf/gf/g/encoding/gcharset" "github.com/gogf/gf/g/text/gregex" "github.com/gogf/gf/third/github.com/clbanning/mxj" "strings" @@ -61,16 +61,12 @@ func convert(xml []byte) (res []byte, err error) { xmlEncode = matchStr[1] } xmlEncode = strings.ToUpper(xmlEncode) - s := internal.GetCharset(xmlEncode) - if s == false { - return nil, fmt.Errorf("not support charset:%s\n", xmlEncode) - } - res, err = gregex.Replace(patten, []byte(""), xml) + res, err = gregex.Replace(patten, []byte(""), xml) if err != nil { return nil, err } if xmlEncode != "UTF-8" && xmlEncode != "UTF8" { - dst, err := internal.Convert("UTF-8", xmlEncode, string(res)) + dst, err := gcharset.Convert("UTF-8", xmlEncode, string(res)) if err != nil { return nil, err } diff --git a/geg/encoding/gcharset/gcharset.go b/geg/encoding/gcharset/gcharset.go new file mode 100644 index 000000000..073e0f730 --- /dev/null +++ b/geg/encoding/gcharset/gcharset.go @@ -0,0 +1,17 @@ +package main + +import ( + "fmt" + "github.com/gogf/gf/g/encoding/gcharset" +) + +func main() { + src := "~{;(F#,6@WCN^O`GW!#" + srcCharset := "GB2312" + dstCharset := "UTF-8" + str, err := gcharset.Convert(dstCharset, srcCharset, src) + if err != nil { + panic(err) + } + fmt.Println(str) +}