From 308fdccf65f34097cef8f1cccf849b9ff43b8ca8 Mon Sep 17 00:00:00 2001 From: daguang Date: Tue, 19 Apr 2022 17:06:27 +0800 Subject: [PATCH] no longer use unsafe package for string/bytes conversion --- container/gmap/gmap_hash_any_any_map.go | 2 +- container/gmap/gmap_hash_int_any_map.go | 2 +- container/gmap/gmap_hash_int_int_map.go | 2 +- container/gmap/gmap_hash_int_str_map.go | 2 +- container/gmap/gmap_hash_str_any_map.go | 2 +- container/gmap/gmap_hash_str_int_map.go | 2 +- container/gmap/gmap_hash_str_str_map.go | 2 +- container/gmap/gmap_list_map.go | 2 +- container/gtype/byte.go | 7 ++++--- container/gtype/bytes.go | 5 +++-- container/gtype/float32.go | 7 ++++--- container/gtype/float64.go | 7 ++++--- container/gtype/int.go | 7 ++++--- container/gtype/int32.go | 7 ++++--- container/gtype/int64.go | 7 ++++--- container/gtype/string.go | 7 ++++--- container/gtype/uint.go | 7 ++++--- container/gtype/uint32.go | 7 ++++--- container/gtype/uint64.go | 7 ++++--- database/gdb/gdb_type_record.go | 5 +++-- database/gredis/gredis_conn.go | 9 +++++---- encoding/gbase64/gbase64.go | 7 +++---- encoding/gjson/gjson_api_encoding.go | 15 +++++++-------- net/ghttp/ghttp_request_param.go | 13 +++++++------ net/ghttp/internal/client/client_dump.go | 9 ++++----- net/ghttp/internal/client/client_response.go | 4 +--- net/gsmtp/gsmtp.go | 7 +++---- os/gfile/gfile_contents.go | 4 +--- os/gres/gres_func.go | 6 +++--- os/gview/gview_buildin.go | 5 +++-- os/gview/gview_parse.go | 12 ++++++------ util/guid/guid_string.go | 12 ++++++------ 32 files changed, 103 insertions(+), 96 deletions(-) diff --git a/container/gmap/gmap_hash_any_any_map.go b/container/gmap/gmap_hash_any_any_map.go index a42cf26ea..3544d0648 100644 --- a/container/gmap/gmap_hash_any_any_map.go +++ b/container/gmap/gmap_hash_any_any_map.go @@ -460,7 +460,7 @@ func (m *AnyAnyMap) Merge(other *AnyAnyMap) { // String returns the map as a string. func (m *AnyAnyMap) String() string { b, _ := m.MarshalJSON() - return gconv.UnsafeBytesToStr(b) + return string(b) } // MarshalJSON implements the interface MarshalJSON for json.Marshal. diff --git a/container/gmap/gmap_hash_int_any_map.go b/container/gmap/gmap_hash_int_any_map.go index f99fd77d4..5ced8024f 100644 --- a/container/gmap/gmap_hash_int_any_map.go +++ b/container/gmap/gmap_hash_int_any_map.go @@ -458,7 +458,7 @@ func (m *IntAnyMap) Merge(other *IntAnyMap) { // String returns the map as a string. func (m *IntAnyMap) String() string { b, _ := m.MarshalJSON() - return gconv.UnsafeBytesToStr(b) + return string(b) } // MarshalJSON implements the interface MarshalJSON for json.Marshal. diff --git a/container/gmap/gmap_hash_int_int_map.go b/container/gmap/gmap_hash_int_int_map.go index 3f8b8d411..5ca359630 100644 --- a/container/gmap/gmap_hash_int_int_map.go +++ b/container/gmap/gmap_hash_int_int_map.go @@ -429,7 +429,7 @@ func (m *IntIntMap) Merge(other *IntIntMap) { // String returns the map as a string. func (m *IntIntMap) String() string { b, _ := m.MarshalJSON() - return gconv.UnsafeBytesToStr(b) + return string(b) } // MarshalJSON implements the interface MarshalJSON for json.Marshal. diff --git a/container/gmap/gmap_hash_int_str_map.go b/container/gmap/gmap_hash_int_str_map.go index c5f5ac323..df5a655d8 100644 --- a/container/gmap/gmap_hash_int_str_map.go +++ b/container/gmap/gmap_hash_int_str_map.go @@ -429,7 +429,7 @@ func (m *IntStrMap) Merge(other *IntStrMap) { // String returns the map as a string. func (m *IntStrMap) String() string { b, _ := m.MarshalJSON() - return gconv.UnsafeBytesToStr(b) + return string(b) } // MarshalJSON implements the interface MarshalJSON for json.Marshal. diff --git a/container/gmap/gmap_hash_str_any_map.go b/container/gmap/gmap_hash_str_any_map.go index 04d70f664..802cd09db 100644 --- a/container/gmap/gmap_hash_str_any_map.go +++ b/container/gmap/gmap_hash_str_any_map.go @@ -454,7 +454,7 @@ func (m *StrAnyMap) Merge(other *StrAnyMap) { // String returns the map as a string. func (m *StrAnyMap) String() string { b, _ := m.MarshalJSON() - return gconv.UnsafeBytesToStr(b) + return string(b) } // MarshalJSON implements the interface MarshalJSON for json.Marshal. diff --git a/container/gmap/gmap_hash_str_int_map.go b/container/gmap/gmap_hash_str_int_map.go index 7bc03cd34..f01fcaa8d 100644 --- a/container/gmap/gmap_hash_str_int_map.go +++ b/container/gmap/gmap_hash_str_int_map.go @@ -432,7 +432,7 @@ func (m *StrIntMap) Merge(other *StrIntMap) { // String returns the map as a string. func (m *StrIntMap) String() string { b, _ := m.MarshalJSON() - return gconv.UnsafeBytesToStr(b) + return string(b) } // MarshalJSON implements the interface MarshalJSON for json.Marshal. diff --git a/container/gmap/gmap_hash_str_str_map.go b/container/gmap/gmap_hash_str_str_map.go index ae68640c8..cfbc945b2 100644 --- a/container/gmap/gmap_hash_str_str_map.go +++ b/container/gmap/gmap_hash_str_str_map.go @@ -432,7 +432,7 @@ func (m *StrStrMap) Merge(other *StrStrMap) { // String returns the map as a string. func (m *StrStrMap) String() string { b, _ := m.MarshalJSON() - return gconv.UnsafeBytesToStr(b) + return string(b) } // MarshalJSON implements the interface MarshalJSON for json.Marshal. diff --git a/container/gmap/gmap_list_map.go b/container/gmap/gmap_list_map.go index 790998da7..805b565f1 100644 --- a/container/gmap/gmap_list_map.go +++ b/container/gmap/gmap_list_map.go @@ -513,7 +513,7 @@ func (m *ListMap) Merge(other *ListMap) { // String returns the map as a string. func (m *ListMap) String() string { b, _ := m.MarshalJSON() - return gconv.UnsafeBytesToStr(b) + return string(b) } // MarshalJSON implements the interface MarshalJSON for json.Marshal. diff --git a/container/gtype/byte.go b/container/gtype/byte.go index 3b2a8d09b..51f1c7dbe 100644 --- a/container/gtype/byte.go +++ b/container/gtype/byte.go @@ -7,9 +7,10 @@ package gtype import ( - "github.com/gogf/gf/util/gconv" "strconv" "sync/atomic" + + "github.com/gogf/gf/util/gconv" ) // Byte is a struct for concurrent-safe operation for type byte. @@ -60,12 +61,12 @@ func (v *Byte) String() string { // MarshalJSON implements the interface MarshalJSON for json.Marshal. func (v *Byte) MarshalJSON() ([]byte, error) { - return gconv.UnsafeStrToBytes(strconv.FormatUint(uint64(v.Val()), 10)), nil + return []byte(strconv.FormatUint(uint64(v.Val()), 10)), nil } // UnmarshalJSON implements the interface UnmarshalJSON for json.Unmarshal. func (v *Byte) UnmarshalJSON(b []byte) error { - v.Set(gconv.Uint8(gconv.UnsafeBytesToStr(b))) + v.Set(gconv.Uint8(string(b))) return nil } diff --git a/container/gtype/bytes.go b/container/gtype/bytes.go index 9574558cb..64bead8a3 100644 --- a/container/gtype/bytes.go +++ b/container/gtype/bytes.go @@ -9,8 +9,9 @@ package gtype import ( "bytes" "encoding/base64" - "github.com/gogf/gf/util/gconv" "sync/atomic" + + "github.com/gogf/gf/util/gconv" ) // Bytes is a struct for concurrent-safe operation for type []byte. @@ -59,7 +60,7 @@ func (v *Bytes) MarshalJSON() ([]byte, error) { val := v.Val() dst := make([]byte, base64.StdEncoding.EncodedLen(len(val))) base64.StdEncoding.Encode(dst, val) - return gconv.UnsafeStrToBytes(`"` + gconv.UnsafeBytesToStr(dst) + `"`), nil + return []byte(`"` + string(dst) + `"`), nil } // UnmarshalJSON implements the interface UnmarshalJSON for json.Unmarshal. diff --git a/container/gtype/float32.go b/container/gtype/float32.go index 2eaeb8960..efd86c44a 100644 --- a/container/gtype/float32.go +++ b/container/gtype/float32.go @@ -7,11 +7,12 @@ package gtype import ( - "github.com/gogf/gf/util/gconv" "math" "strconv" "sync/atomic" "unsafe" + + "github.com/gogf/gf/util/gconv" ) // Float32 is a struct for concurrent-safe operation for type float32. @@ -73,12 +74,12 @@ func (v *Float32) String() string { // MarshalJSON implements the interface MarshalJSON for json.Marshal. func (v *Float32) MarshalJSON() ([]byte, error) { - return gconv.UnsafeStrToBytes(strconv.FormatFloat(float64(v.Val()), 'g', -1, 32)), nil + return []byte(strconv.FormatFloat(float64(v.Val()), 'g', -1, 32)), nil } // UnmarshalJSON implements the interface UnmarshalJSON for json.Unmarshal. func (v *Float32) UnmarshalJSON(b []byte) error { - v.Set(gconv.Float32(gconv.UnsafeBytesToStr(b))) + v.Set(gconv.Float32(string(b))) return nil } diff --git a/container/gtype/float64.go b/container/gtype/float64.go index ecc80facc..d853d6c69 100644 --- a/container/gtype/float64.go +++ b/container/gtype/float64.go @@ -7,11 +7,12 @@ package gtype import ( - "github.com/gogf/gf/util/gconv" "math" "strconv" "sync/atomic" "unsafe" + + "github.com/gogf/gf/util/gconv" ) // Float64 is a struct for concurrent-safe operation for type float64. @@ -73,12 +74,12 @@ func (v *Float64) String() string { // MarshalJSON implements the interface MarshalJSON for json.Marshal. func (v *Float64) MarshalJSON() ([]byte, error) { - return gconv.UnsafeStrToBytes(strconv.FormatFloat(v.Val(), 'g', -1, 64)), nil + return []byte(strconv.FormatFloat(v.Val(), 'g', -1, 64)), nil } // UnmarshalJSON implements the interface UnmarshalJSON for json.Unmarshal. func (v *Float64) UnmarshalJSON(b []byte) error { - v.Set(gconv.Float64(gconv.UnsafeBytesToStr(b))) + v.Set(gconv.Float64(string(b))) return nil } diff --git a/container/gtype/int.go b/container/gtype/int.go index c1b2543f0..8263da615 100644 --- a/container/gtype/int.go +++ b/container/gtype/int.go @@ -7,9 +7,10 @@ package gtype import ( - "github.com/gogf/gf/util/gconv" "strconv" "sync/atomic" + + "github.com/gogf/gf/util/gconv" ) // Int is a struct for concurrent-safe operation for type int. @@ -60,12 +61,12 @@ func (v *Int) String() string { // MarshalJSON implements the interface MarshalJSON for json.Marshal. func (v *Int) MarshalJSON() ([]byte, error) { - return gconv.UnsafeStrToBytes(strconv.Itoa(v.Val())), nil + return []byte(strconv.Itoa(v.Val())), nil } // UnmarshalJSON implements the interface UnmarshalJSON for json.Unmarshal. func (v *Int) UnmarshalJSON(b []byte) error { - v.Set(gconv.Int(gconv.UnsafeBytesToStr(b))) + v.Set(gconv.Int(string(b))) return nil } diff --git a/container/gtype/int32.go b/container/gtype/int32.go index 63d4a5d23..4e8969cf3 100644 --- a/container/gtype/int32.go +++ b/container/gtype/int32.go @@ -7,9 +7,10 @@ package gtype import ( - "github.com/gogf/gf/util/gconv" "strconv" "sync/atomic" + + "github.com/gogf/gf/util/gconv" ) // Int32 is a struct for concurrent-safe operation for type int32. @@ -60,12 +61,12 @@ func (v *Int32) String() string { // MarshalJSON implements the interface MarshalJSON for json.Marshal. func (v *Int32) MarshalJSON() ([]byte, error) { - return gconv.UnsafeStrToBytes(strconv.Itoa(int(v.Val()))), nil + return []byte(strconv.Itoa(int(v.Val()))), nil } // UnmarshalJSON implements the interface UnmarshalJSON for json.Unmarshal. func (v *Int32) UnmarshalJSON(b []byte) error { - v.Set(gconv.Int32(gconv.UnsafeBytesToStr(b))) + v.Set(gconv.Int32(string(b))) return nil } diff --git a/container/gtype/int64.go b/container/gtype/int64.go index b9c356ebd..1a2aafed6 100644 --- a/container/gtype/int64.go +++ b/container/gtype/int64.go @@ -7,9 +7,10 @@ package gtype import ( - "github.com/gogf/gf/util/gconv" "strconv" "sync/atomic" + + "github.com/gogf/gf/util/gconv" ) // Int64 is a struct for concurrent-safe operation for type int64. @@ -60,12 +61,12 @@ func (v *Int64) String() string { // MarshalJSON implements the interface MarshalJSON for json.Marshal. func (v *Int64) MarshalJSON() ([]byte, error) { - return gconv.UnsafeStrToBytes(strconv.FormatInt(v.Val(), 10)), nil + return []byte(strconv.FormatInt(v.Val(), 10)), nil } // UnmarshalJSON implements the interface UnmarshalJSON for json.Unmarshal. func (v *Int64) UnmarshalJSON(b []byte) error { - v.Set(gconv.Int64(gconv.UnsafeBytesToStr(b))) + v.Set(gconv.Int64(string(b))) return nil } diff --git a/container/gtype/string.go b/container/gtype/string.go index 0e1483b6f..ff30e6c69 100644 --- a/container/gtype/string.go +++ b/container/gtype/string.go @@ -8,8 +8,9 @@ package gtype import ( "bytes" - "github.com/gogf/gf/util/gconv" "sync/atomic" + + "github.com/gogf/gf/util/gconv" ) // String is a struct for concurrent-safe operation for type string. @@ -55,12 +56,12 @@ func (v *String) String() string { // MarshalJSON implements the interface MarshalJSON for json.Marshal. func (v *String) MarshalJSON() ([]byte, error) { - return gconv.UnsafeStrToBytes(`"` + v.Val() + `"`), nil + return []byte(`"` + v.Val() + `"`), nil } // UnmarshalJSON implements the interface UnmarshalJSON for json.Unmarshal. func (v *String) UnmarshalJSON(b []byte) error { - v.Set(gconv.UnsafeBytesToStr(bytes.Trim(b, `"`))) + v.Set(string(bytes.Trim(b, `"`))) return nil } diff --git a/container/gtype/uint.go b/container/gtype/uint.go index b9203a9af..884662479 100644 --- a/container/gtype/uint.go +++ b/container/gtype/uint.go @@ -7,9 +7,10 @@ package gtype import ( - "github.com/gogf/gf/util/gconv" "strconv" "sync/atomic" + + "github.com/gogf/gf/util/gconv" ) // Uint is a struct for concurrent-safe operation for type uint. @@ -60,12 +61,12 @@ func (v *Uint) String() string { // MarshalJSON implements the interface MarshalJSON for json.Marshal. func (v *Uint) MarshalJSON() ([]byte, error) { - return gconv.UnsafeStrToBytes(strconv.FormatUint(uint64(v.Val()), 10)), nil + return []byte(strconv.FormatUint(uint64(v.Val()), 10)), nil } // UnmarshalJSON implements the interface UnmarshalJSON for json.Unmarshal. func (v *Uint) UnmarshalJSON(b []byte) error { - v.Set(gconv.Uint(gconv.UnsafeBytesToStr(b))) + v.Set(gconv.Uint(string(b))) return nil } diff --git a/container/gtype/uint32.go b/container/gtype/uint32.go index 2fbdcb030..f1c98ad00 100644 --- a/container/gtype/uint32.go +++ b/container/gtype/uint32.go @@ -7,9 +7,10 @@ package gtype import ( - "github.com/gogf/gf/util/gconv" "strconv" "sync/atomic" + + "github.com/gogf/gf/util/gconv" ) // Uint32 is a struct for concurrent-safe operation for type uint32. @@ -60,12 +61,12 @@ func (v *Uint32) String() string { // MarshalJSON implements the interface MarshalJSON for json.Marshal. func (v *Uint32) MarshalJSON() ([]byte, error) { - return gconv.UnsafeStrToBytes(strconv.FormatUint(uint64(v.Val()), 10)), nil + return []byte(strconv.FormatUint(uint64(v.Val()), 10)), nil } // UnmarshalJSON implements the interface UnmarshalJSON for json.Unmarshal. func (v *Uint32) UnmarshalJSON(b []byte) error { - v.Set(gconv.Uint32(gconv.UnsafeBytesToStr(b))) + v.Set(gconv.Uint32(string(b))) return nil } diff --git a/container/gtype/uint64.go b/container/gtype/uint64.go index 580163bb5..ee38870a3 100644 --- a/container/gtype/uint64.go +++ b/container/gtype/uint64.go @@ -7,9 +7,10 @@ package gtype import ( - "github.com/gogf/gf/util/gconv" "strconv" "sync/atomic" + + "github.com/gogf/gf/util/gconv" ) // Uint64 is a struct for concurrent-safe operation for type uint64. @@ -60,12 +61,12 @@ func (v *Uint64) String() string { // MarshalJSON implements the interface MarshalJSON for json.Marshal. func (v *Uint64) MarshalJSON() ([]byte, error) { - return gconv.UnsafeStrToBytes(strconv.FormatUint(v.Val(), 10)), nil + return []byte(strconv.FormatUint(v.Val(), 10)), nil } // UnmarshalJSON implements the interface UnmarshalJSON for json.Unmarshal. func (v *Uint64) UnmarshalJSON(b []byte) error { - v.Set(gconv.Uint64(gconv.UnsafeBytesToStr(b))) + v.Set(gconv.Uint64(string(b))) return nil } diff --git a/database/gdb/gdb_type_record.go b/database/gdb/gdb_type_record.go index eff35a1f4..a4e1c24d6 100644 --- a/database/gdb/gdb_type_record.go +++ b/database/gdb/gdb_type_record.go @@ -8,6 +8,7 @@ package gdb import ( "database/sql" + "github.com/gogf/gf/container/gmap" "github.com/gogf/gf/encoding/gparser" "github.com/gogf/gf/internal/empty" @@ -22,13 +23,13 @@ func (r Record) Interface() interface{} { // Json converts `r` to JSON format content. func (r Record) Json() string { content, _ := gparser.VarToJson(r.Map()) - return gconv.UnsafeBytesToStr(content) + return string(content) } // Xml converts `r` to XML format content. func (r Record) Xml(rootTag ...string) string { content, _ := gparser.VarToXml(r.Map(), rootTag...) - return gconv.UnsafeBytesToStr(content) + return string(content) } // Map converts `r` to map[string]interface{}. diff --git a/database/gredis/gredis_conn.go b/database/gredis/gredis_conn.go index a56e9ddd7..ddd8e6ffb 100644 --- a/database/gredis/gredis_conn.go +++ b/database/gredis/gredis_conn.go @@ -8,15 +8,16 @@ package gredis import ( "context" + "github.com/gomodule/redigo/redis" + "reflect" + "time" + "github.com/gogf/gf/container/gvar" "github.com/gogf/gf/errors/gcode" "github.com/gogf/gf/errors/gerror" "github.com/gogf/gf/internal/json" "github.com/gogf/gf/os/gtime" "github.com/gogf/gf/util/gconv" - "github.com/gomodule/redigo/redis" - "reflect" - "time" ) // Do sends a command to the server and returns the received reply. @@ -117,7 +118,7 @@ func (c *Conn) ReceiveVarWithTimeout(timeout time.Duration) (*gvar.Var, error) { func resultToVar(result interface{}, err error) (*gvar.Var, error) { if err == nil { if result, ok := result.([]byte); ok { - return gvar.New(gconv.UnsafeBytesToStr(result)), err + return gvar.New(string(result)), err } // It treats all returned slice as string slice. if result, ok := result.([]interface{}); ok { diff --git a/encoding/gbase64/gbase64.go b/encoding/gbase64/gbase64.go index b88373677..30e104aec 100644 --- a/encoding/gbase64/gbase64.go +++ b/encoding/gbase64/gbase64.go @@ -9,7 +9,6 @@ package gbase64 import ( "encoding/base64" - "github.com/gogf/gf/util/gconv" "io/ioutil" ) @@ -27,7 +26,7 @@ func EncodeString(src string) string { // EncodeToString encodes bytes to string with BASE64 algorithm. func EncodeToString(src []byte) string { - return gconv.UnsafeBytesToStr(Encode(src)) + return string(Encode(src)) } // EncryptFile encodes file content of using BASE64 algorithms. @@ -55,7 +54,7 @@ func EncodeFileToString(path string) (string, error) { if err != nil { return "", err } - return gconv.UnsafeBytesToStr(content), nil + return string(content), nil } // MustEncodeFileToString encodes file content of to string using BASE64 algorithms. @@ -103,7 +102,7 @@ func MustDecodeString(data string) []byte { // DecodeString decodes string with BASE64 algorithm. func DecodeToString(data string) (string, error) { b, err := DecodeString(data) - return gconv.UnsafeBytesToStr(b), err + return string(b), err } // MustDecodeToString decodes string with BASE64 algorithm. diff --git a/encoding/gjson/gjson_api_encoding.go b/encoding/gjson/gjson_api_encoding.go index a0eabdb90..e6fb0b587 100644 --- a/encoding/gjson/gjson_api_encoding.go +++ b/encoding/gjson/gjson_api_encoding.go @@ -12,7 +12,6 @@ import ( "github.com/gogf/gf/encoding/gxml" "github.com/gogf/gf/encoding/gyaml" "github.com/gogf/gf/internal/json" - "github.com/gogf/gf/util/gconv" ) // ======================================================================== @@ -50,7 +49,7 @@ func (j *Json) MustToJson() []byte { } func (j *Json) MustToJsonString() string { - return gconv.UnsafeBytesToStr(j.MustToJson()) + return string(j.MustToJson()) } func (j *Json) MustToJsonIndent() []byte { @@ -62,7 +61,7 @@ func (j *Json) MustToJsonIndent() []byte { } func (j *Json) MustToJsonIndentString() string { - return gconv.UnsafeBytesToStr(j.MustToJsonIndent()) + return string(j.MustToJsonIndent()) } // ======================================================================== @@ -96,7 +95,7 @@ func (j *Json) MustToXml(rootTag ...string) []byte { } func (j *Json) MustToXmlString(rootTag ...string) string { - return gconv.UnsafeBytesToStr(j.MustToXml(rootTag...)) + return string(j.MustToXml(rootTag...)) } func (j *Json) MustToXmlIndent(rootTag ...string) []byte { @@ -108,7 +107,7 @@ func (j *Json) MustToXmlIndent(rootTag ...string) []byte { } func (j *Json) MustToXmlIndentString(rootTag ...string) string { - return gconv.UnsafeBytesToStr(j.MustToXmlIndent(rootTag...)) + return string(j.MustToXmlIndent(rootTag...)) } // ======================================================================== @@ -135,7 +134,7 @@ func (j *Json) MustToYaml() []byte { } func (j *Json) MustToYamlString() string { - return gconv.UnsafeBytesToStr(j.MustToYaml()) + return string(j.MustToYaml()) } // ======================================================================== @@ -162,7 +161,7 @@ func (j *Json) MustToToml() []byte { } func (j *Json) MustToTomlString() string { - return gconv.UnsafeBytesToStr(j.MustToToml()) + return string(j.MustToToml()) } // ======================================================================== @@ -189,5 +188,5 @@ func (j *Json) MustToIni() []byte { } func (j *Json) MustToIniString() string { - return gconv.UnsafeBytesToStr(j.MustToIni()) + return string(j.MustToIni()) } diff --git a/net/ghttp/ghttp_request_param.go b/net/ghttp/ghttp_request_param.go index 8c9689b2e..71f5054eb 100644 --- a/net/ghttp/ghttp_request_param.go +++ b/net/ghttp/ghttp_request_param.go @@ -9,6 +9,11 @@ package ghttp import ( "bytes" "fmt" + "io/ioutil" + "mime/multipart" + "reflect" + "strings" + "github.com/gogf/gf/container/gvar" "github.com/gogf/gf/encoding/gjson" "github.com/gogf/gf/encoding/gurl" @@ -21,10 +26,6 @@ import ( "github.com/gogf/gf/text/gstr" "github.com/gogf/gf/util/gconv" "github.com/gogf/gf/util/gvalid" - "io/ioutil" - "mime/multipart" - "reflect" - "strings" ) const ( @@ -174,7 +175,7 @@ func (r *Request) GetBody() []byte { // GetBodyString retrieves and returns request body content as string. // It can be called multiple times retrieving the same body content. func (r *Request) GetBodyString() string { - return gconv.UnsafeBytesToStr(r.GetBody()) + return string(r.GetBody()) } // GetJson parses current request content as JSON format, and returns the JSON object. @@ -374,7 +375,7 @@ func (r *Request) parseForm() { // It might be JSON/XML content. if s := gstr.Trim(name + strings.Join(values, " ")); len(s) > 0 { if s[0] == '{' && s[len(s)-1] == '}' || s[0] == '<' && s[len(s)-1] == '>' { - r.bodyContent = gconv.UnsafeStrToBytes(s) + r.bodyContent = []byte(s) params = "" break } diff --git a/net/ghttp/internal/client/client_dump.go b/net/ghttp/internal/client/client_dump.go index 9ac718a65..bcb46a5a8 100644 --- a/net/ghttp/internal/client/client_dump.go +++ b/net/ghttp/internal/client/client_dump.go @@ -8,12 +8,11 @@ package client import ( "fmt" - "github.com/gogf/gf/internal/utils" "io/ioutil" "net/http" "net/http/httputil" - "github.com/gogf/gf/util/gconv" + "github.com/gogf/gf/internal/utils" ) // dumpTextFormat is the format of the dumped raw string @@ -31,7 +30,7 @@ func getResponseBody(res *http.Response) string { } bodyContent, _ := ioutil.ReadAll(res.Body) res.Body = utils.NewReadCloser(bodyContent, true) - return gconv.UnsafeBytesToStr(bodyContent) + return string(bodyContent) } // RawRequest returns the raw content of the request. @@ -51,7 +50,7 @@ func (r *Response) RawRequest() string { return fmt.Sprintf( dumpTextFormat, "REQUEST ", - gconv.UnsafeBytesToStr(bs), + string(bs), r.requestBody, ) } @@ -70,7 +69,7 @@ func (r *Response) RawResponse() string { return fmt.Sprintf( dumpTextFormat, "RESPONSE", - gconv.UnsafeBytesToStr(bs), + string(bs), getResponseBody(r.Response), ) } diff --git a/net/ghttp/internal/client/client_response.go b/net/ghttp/internal/client/client_response.go index 324115ebf..54699562c 100644 --- a/net/ghttp/internal/client/client_response.go +++ b/net/ghttp/internal/client/client_response.go @@ -9,8 +9,6 @@ package client import ( "io/ioutil" "net/http" - - "github.com/gogf/gf/util/gconv" ) // Response is the struct for client request response. @@ -65,7 +63,7 @@ func (r *Response) ReadAll() []byte { // ReadAllString retrieves and returns the response content as string. func (r *Response) ReadAllString() string { - return gconv.UnsafeBytesToStr(r.ReadAll()) + return string(r.ReadAll()) } // Close closes the response when it will never be used. diff --git a/net/gsmtp/gsmtp.go b/net/gsmtp/gsmtp.go index 37c8dfd5d..9e4d4fec3 100644 --- a/net/gsmtp/gsmtp.go +++ b/net/gsmtp/gsmtp.go @@ -14,7 +14,6 @@ package gsmtp import ( "encoding/base64" "fmt" - "github.com/gogf/gf/util/gconv" "net/smtp" "strings" ) @@ -85,7 +84,7 @@ func (s *SMTP) SendMail(from, tos, subject, body string, contentType ...string) header := map[string]string{ "From": from, "To": strings.Join(tosArr, ";"), - "Subject": fmt.Sprintf("=?UTF-8?B?%s?=", contentEncoding.EncodeToString(gconv.UnsafeStrToBytes(subject))), + "Subject": fmt.Sprintf("=?UTF-8?B?%s?=", contentEncoding.EncodeToString([]byte(subject))), "MIME-Version": "1.0", "Content-Type": "text/plain; charset=UTF-8", "Content-Transfer-Encoding": "base64", @@ -97,12 +96,12 @@ func (s *SMTP) SendMail(from, tos, subject, body string, contentType ...string) for k, v := range header { message += fmt.Sprintf("%s: %s\r\n", k, v) } - message += "\r\n" + contentEncoding.EncodeToString(gconv.UnsafeStrToBytes(body)) + message += "\r\n" + contentEncoding.EncodeToString([]byte(body)) return smtp.SendMail( address, smtp.PlainAuth("", s.Username, s.Password, server), from, tosArr, - gconv.UnsafeStrToBytes(message), + []byte(message), ) } diff --git a/os/gfile/gfile_contents.go b/os/gfile/gfile_contents.go index 048e15d76..29057ddaa 100644 --- a/os/gfile/gfile_contents.go +++ b/os/gfile/gfile_contents.go @@ -11,8 +11,6 @@ import ( "io" "io/ioutil" "os" - - "github.com/gogf/gf/util/gconv" ) var ( @@ -23,7 +21,7 @@ var ( // GetContents returns the file content of as string. // It returns en empty string if it fails reading. func GetContents(path string) string { - return gconv.UnsafeBytesToStr(GetBytes(path)) + return string(GetBytes(path)) } // GetBytes returns the file content of as []byte. diff --git a/os/gres/gres_func.go b/os/gres/gres_func.go index 4995fbc29..ec68a1297 100644 --- a/os/gres/gres_func.go +++ b/os/gres/gres_func.go @@ -11,10 +11,10 @@ import ( "bytes" "encoding/hex" "fmt" + "github.com/gogf/gf/encoding/gbase64" "github.com/gogf/gf/encoding/gcompress" "github.com/gogf/gf/text/gstr" - "github.com/gogf/gf/util/gconv" "github.com/gogf/gf/os/gfile" ) @@ -116,7 +116,7 @@ func UnpackContent(content string) ([]*File, error) { return nil, err } } else { - data, err = gcompress.UnGzip(gconv.UnsafeStrToBytes(content)) + data, err = gcompress.UnGzip([]byte(content)) if err != nil { return nil, err } @@ -166,7 +166,7 @@ func isHexStr(s string) bool { // hexStrToBytes converts hex string content to []byte. func hexStrToBytes(s string) []byte { - src := gconv.UnsafeStrToBytes(s) + src := []byte(s) dst := make([]byte, hex.DecodedLen(len(src))) hex.Decode(dst, src) return dst diff --git a/os/gview/gview_buildin.go b/os/gview/gview_buildin.go index da22f78fa..b3dd031ac 100644 --- a/os/gview/gview_buildin.go +++ b/os/gview/gview_buildin.go @@ -9,9 +9,10 @@ package gview import ( "context" "fmt" + "strings" + "github.com/gogf/gf/internal/json" "github.com/gogf/gf/util/gutil" - "strings" "github.com/gogf/gf/encoding/ghtml" "github.com/gogf/gf/encoding/gurl" @@ -222,7 +223,7 @@ func (view *View) buildInFuncNl2Br(str interface{}) string { // which encodes and returns `value` as JSON string. func (view *View) buildInFuncJson(value interface{}) (string, error) { b, err := json.Marshal(value) - return gconv.UnsafeBytesToStr(b), err + return string(b), err } // buildInFuncPlus implements build-in template function: plus , diff --git a/os/gview/gview_parse.go b/os/gview/gview_parse.go index c8a4811ee..b32336148 100644 --- a/os/gview/gview_parse.go +++ b/os/gview/gview_parse.go @@ -10,6 +10,11 @@ import ( "bytes" "context" "fmt" + htmltpl "html/template" + "strconv" + "strings" + texttpl "text/template" + "github.com/gogf/gf/encoding/ghash" "github.com/gogf/gf/errors/gcode" "github.com/gogf/gf/errors/gerror" @@ -17,12 +22,7 @@ import ( "github.com/gogf/gf/os/gfsnotify" "github.com/gogf/gf/os/gmlock" "github.com/gogf/gf/text/gstr" - "github.com/gogf/gf/util/gconv" "github.com/gogf/gf/util/gutil" - htmltpl "html/template" - "strconv" - "strings" - texttpl "text/template" "github.com/gogf/gf/os/gres" @@ -71,7 +71,7 @@ func (view *View) Parse(ctx context.Context, file string, params ...Params) (res return nil } if resource != nil { - content = gconv.UnsafeBytesToStr(resource.Content()) + content = string(resource.Content()) } else { content = gfile.GetContentsWithCache(path) } diff --git a/util/guid/guid_string.go b/util/guid/guid_string.go index 666d0ec8d..979feb162 100644 --- a/util/guid/guid_string.go +++ b/util/guid/guid_string.go @@ -7,14 +7,14 @@ package guid import ( - "github.com/gogf/gf/container/gtype" - "github.com/gogf/gf/encoding/ghash" - "github.com/gogf/gf/net/gipv4" - "github.com/gogf/gf/util/gconv" - "github.com/gogf/gf/util/grand" "os" "strconv" "time" + + "github.com/gogf/gf/container/gtype" + "github.com/gogf/gf/encoding/ghash" + "github.com/gogf/gf/net/gipv4" + "github.com/gogf/gf/util/grand" ) const ( @@ -94,7 +94,7 @@ func S(data ...[]byte) string { } else { panic("too many data parts, it should be no more than 2 parts") } - return gconv.UnsafeBytesToStr(b) + return string(b) } // getSequence increases and returns the sequence string in 3 bytes.