-
Notifications
You must be signed in to change notification settings - Fork 23
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #110 from xushiwei/t
x/byteutil
- Loading branch information
Showing
7 changed files
with
170 additions
and
17 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
//go:build go1.21 | ||
// +build go1.21 | ||
|
||
/* | ||
Copyright 2024 Qiniu Limited (qiniu.com) | ||
Licensed under the Apache License, Version 2.0 (the "License"); | ||
you may not use this file except in compliance with the License. | ||
You may obtain a copy of the License at | ||
http://www.apache.org/licenses/LICENSE-2.0 | ||
Unless required by applicable law or agreed to in writing, software | ||
distributed under the License is distributed on an "AS IS" BASIS, | ||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
See the License for the specific language governing permissions and | ||
limitations under the License. | ||
*/ | ||
|
||
package byteutil | ||
|
||
import ( | ||
"unsafe" | ||
) | ||
|
||
// Bytes returns a byte slice whose underlying data is s. | ||
func Bytes(s string) []byte { | ||
// Although unsafe.SliceData/String was introduced in go1.20, but | ||
// the go version in go.mod is 1.18 so we cannot use them. | ||
return unsafe.Slice(unsafe.StringData(s), len(s)) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
//go:build !go1.21 | ||
// +build !go1.21 | ||
|
||
/* | ||
Copyright 2024 Qiniu Limited (qiniu.com) | ||
Licensed under the Apache License, Version 2.0 (the "License"); | ||
you may not use this file except in compliance with the License. | ||
You may obtain a copy of the License at | ||
http://www.apache.org/licenses/LICENSE-2.0 | ||
Unless required by applicable law or agreed to in writing, software | ||
distributed under the License is distributed on an "AS IS" BASIS, | ||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
See the License for the specific language governing permissions and | ||
limitations under the License. | ||
*/ | ||
|
||
package byteutil | ||
|
||
import ( | ||
"reflect" | ||
"unsafe" | ||
) | ||
|
||
// Bytes returns a byte slice whose underlying data is s. | ||
func Bytes(s string) (b []byte) { | ||
bh := (*reflect.SliceHeader)(unsafe.Pointer(&b)) | ||
sh := *(*reflect.StringHeader)(unsafe.Pointer(&s)) | ||
bh.Data, bh.Len, bh.Cap = sh.Data, sh.Len, sh.Len | ||
return | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,27 +1,40 @@ | ||
/* | ||
Copyright 2019 Qiniu Limited (qiniu.com) | ||
Licensed under the Apache License, Version 2.0 (the "License"); | ||
you may not use this file except in compliance with the License. | ||
You may obtain a copy of the License at | ||
http://www.apache.org/licenses/LICENSE-2.0 | ||
Unless required by applicable law or agreed to in writing, software | ||
distributed under the License is distributed on an "AS IS" BASIS, | ||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
See the License for the specific language governing permissions and | ||
limitations under the License. | ||
*/ | ||
|
||
package jsonutil | ||
|
||
import ( | ||
"encoding/json" | ||
"reflect" | ||
"unsafe" | ||
) | ||
|
||
// ---------------------------------------------------------- | ||
"github.com/qiniu/x/byteutil" | ||
"github.com/qiniu/x/stringutil" | ||
) | ||
|
||
// Unmarshal parses the JSON-encoded data and stores the result in the value pointed to by v. | ||
func Unmarshal(data string, v interface{}) error { | ||
|
||
sh := *(*reflect.StringHeader)(unsafe.Pointer(&data)) | ||
arr := (*[1 << 30]byte)(unsafe.Pointer(sh.Data)) | ||
return json.Unmarshal(arr[:sh.Len], v) | ||
b := byteutil.Bytes(data) | ||
return json.Unmarshal(b, v) | ||
} | ||
|
||
// ---------------------------------------------------------- | ||
|
||
// Stringify converts a value into string. | ||
func Stringify(v interface{}) string { | ||
b, _ := json.Marshal(v) | ||
return string(b) | ||
return stringutil.String(b) | ||
} | ||
|
||
// ---------------------------------------------------------- |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
//go:build !go1.21 | ||
// +build !go1.21 | ||
|
||
/* | ||
Copyright 2024 Qiniu Limited (qiniu.com) | ||
Licensed under the Apache License, Version 2.0 (the "License"); | ||
you may not use this file except in compliance with the License. | ||
You may obtain a copy of the License at | ||
http://www.apache.org/licenses/LICENSE-2.0 | ||
Unless required by applicable law or agreed to in writing, software | ||
distributed under the License is distributed on an "AS IS" BASIS, | ||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
See the License for the specific language governing permissions and | ||
limitations under the License. | ||
*/ | ||
|
||
package stringutil | ||
|
||
import ( | ||
"reflect" | ||
"unsafe" | ||
) | ||
|
||
// String returns a string value whose underlying bytes is b. | ||
// | ||
// Since Go strings are immutable, the bytes passed to String | ||
// must not be modified afterwards. | ||
func String(b []byte) (s string) { | ||
sh := (*reflect.StringHeader)(unsafe.Pointer(&s)) | ||
bh := *(*reflect.SliceHeader)(unsafe.Pointer(&b)) | ||
sh.Data, sh.Len = bh.Data, bh.Len | ||
return | ||
} |