unsafeConvert

package module
v1.6.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Sep 16, 2023 License: Apache-2.0 Imports: 5 Imported by: 47

README

Unsafe string↔[]byte conversion library

The library functions for unsafely convert between a string and a slice of bytes. You probably shouldn’t use it unless you need to squeeze extra performance from your performance-critical code path.

See https://mina86.com/2017/golang-string-and-bytes/ for some more info.

Bench

20230916

goarch: amd64
pkg: github.com/3JoB/unsafeConvert
cpu: 12th Gen Intel(R) Core(TM) i7-12700H
Benchmark_StringToByte_G_Lite-20            	506960354	         2.390 ns/op	       0 B/op	       0 allocs/op
Benchmark_StringToByte_Copy_U_Lite-20       	100000000	        10.06 ns/op	      16 B/op	       1 allocs/op
Benchmark_StringToByte_Slice_U_Lite-20      	1000000000	         0.2269 ns/op	       0 B/op	       0 allocs/op
Benchmark_StringToByte_Bytes_U_Lite-20      	52430136	        23.88 ns/op	      64 B/op	       1 allocs/op
Benchmark_StringToByte_Pointer_U_Lite-20    	1000000000	         0.1159 ns/op	       0 B/op	       0 allocs/op
Benchmark_StringToByte_G_Big-20             	 1000000	      1086 ns/op	   10880 B/op	       1 allocs/op
Benchmark_StringToByte_Copy_U_Big-20        	 1117935	      1073 ns/op	   10880 B/op	       1 allocs/op
Benchmark_StringToByte_Slice_U_Big-20       	1000000000	         0.4338 ns/op	       0 B/op	       0 allocs/op
Benchmark_StringToByte_Bytes_U_Big-20       	 1000000	      1138 ns/op	   10880 B/op	       1 allocs/op
Benchmark_StringToByte_U_Pointer_Big-20     	1000000000	         0.1117 ns/op	       0 B/op	       0 allocs/op
Benchmark_ByteToString_G_Lite-20            	613644068	         1.979 ns/op	       0 B/op	       0 allocs/op
Benchmark_ByteToString_Slice_U_Lite-20      	1000000000	         0.2174 ns/op	       0 B/op	       0 allocs/op
Benchmark_ByteToString_Strings_U_Lite-20    	63382401	        17.89 ns/op	      16 B/op	       1 allocs/op
Benchmark_ByteToString_Pointer_U_Lite-20    	1000000000	         0.1119 ns/op	       0 B/op	       0 allocs/op
Benchmark_ByteToString_G_Big-20             	 1000000	      1139 ns/op	   10880 B/op	       1 allocs/op
Benchmark_ByteToString_Slice_U_Big-20       	1000000000	         0.4358 ns/op	       0 B/op	       0 allocs/op
Benchmark_ByteToString_U_Pointer_Big-20     	1000000000	         0.1100 ns/op	       0 B/op	       0 allocs/op
Benchmark_ByteToString_U_Strings_Big-20     	 1000000	      1126 ns/op	   10880 B/op	       1 allocs/op
Benchmark_Float32_U-20                      	18850288	        58.05 ns/op	      40 B/op	       2 allocs/op
Benchmark_Float32_FMT-20                    	11351810	       114.2 ns/op	      16 B/op	       2 allocs/op
Benchmark_IOTA_U-20                         	186808952	         6.235 ns/op	       0 B/op	       0 allocs/op
Benchmark_Iota_G-20                         	88849399	        13.01 ns/op	       5 B/op	       1 allocs/op
Benchmark_Atoi_U-20                         	135130189	         8.779 ns/op	       0 B/op	       0 allocs/op
Benchmark_Atoi_G-20                         	342945426	         3.713 ns/op	       0 B/op	       0 allocs/op

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func AnyString added in v1.6.0

func AnyString[T ~string | ~[]byte](v T) string

Note that this method is extremely dangerous, please do not use this method unless absolutely necessary!

Before generics were used, this method could cause memory usage exceptions, leading to system unresponsiveness (no matter how much memory is installed). This problem has been fixed by adding generics, but there is no guarantee that memory exceptions will not continue to occur in the future.

----------------------------------------------------------------

This method is used to convert any type to a string type. However, please note that not all types can be converted.

Abusing this method can lead to program crashes or even system crashes. So before converting, please test in a virtual environment whether the type you want to convert can be converted, in order to avoid loss.

func Atoi added in v1.6.0

func Atoi(s string) (int, error)

func ByteBytes added in v1.6.0

func ByteBytes(v string) []byte

func ByteCopy added in v1.5.0

func ByteCopy(s string) []byte

func BytePointer added in v1.5.0

func BytePointer(v string) []byte

func ByteSlice added in v1.5.0

func ByteSlice(v string) []byte

func Float32 added in v1.6.0

func Float32(f float32) string

func Float64 added in v1.6.0

func Float64(f float64) string

func Int64ToString added in v1.1.0

func Int64ToString(v int64) string

func IntToString added in v1.1.0

func IntToString(v int) string

func Itoa added in v1.6.0

func Itoa(i int) string

func StringPointer added in v1.5.0

func StringPointer(v []byte) string

func StringSlice added in v1.5.0

func StringSlice(v []byte) string

func StringStrings added in v1.6.0

func StringStrings(v []byte) string

func StringToInt added in v1.1.0

func StringToInt(v string) int

func StringToInt64 added in v1.1.0

func StringToInt64(v string) int64

Types

This section is empty.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL