utils

package
v1.1.0 Latest Latest
Warning

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

Go to latest
Published: Oct 19, 2023 License: MIT Imports: 23 Imported by: 0

README

A collection of common functions but with better performance, less allocations and no dependencies created for Fiber.

// go test -benchmem -run=^$ -bench=Benchmark_ -count=2

Benchmark_ToLowerBytes/fiber-16                 42847654                25.7 ns/op             0 B/op          0 allocs/op
Benchmark_ToLowerBytes/fiber-16                 46143196                25.7 ns/op             0 B/op          0 allocs/op
Benchmark_ToLowerBytes/default-16               17387322                67.4 ns/op            48 B/op          1 allocs/op
Benchmark_ToLowerBytes/default-16               17906491                67.4 ns/op            48 B/op          1 allocs/op

Benchmark_ToUpperBytes/fiber-16                 46143729                25.7 ns/op             0 B/op          0 allocs/op
Benchmark_ToUpperBytes/fiber-16                 47989250                25.6 ns/op             0 B/op          0 allocs/op
Benchmark_ToUpperBytes/default-16               15580854                76.7 ns/op            48 B/op          1 allocs/op
Benchmark_ToUpperBytes/default-16               15381202                76.9 ns/op            48 B/op          1 allocs/op

Benchmark_TrimRightBytes/fiber-16               70572459                16.3 ns/op             8 B/op          1 allocs/op
Benchmark_TrimRightBytes/fiber-16               74983597                16.3 ns/op             8 B/op          1 allocs/op
Benchmark_TrimRightBytes/default-16             16212578                74.1 ns/op            40 B/op          2 allocs/op
Benchmark_TrimRightBytes/default-16             16434686                74.1 ns/op            40 B/op          2 allocs/op

Benchmark_TrimLeftBytes/fiber-16                74983128                16.3 ns/op             8 B/op          1 allocs/op
Benchmark_TrimLeftBytes/fiber-16                74985002                16.3 ns/op             8 B/op          1 allocs/op
Benchmark_TrimLeftBytes/default-16              21047868                56.5 ns/op            40 B/op          2 allocs/op
Benchmark_TrimLeftBytes/default-16              21048015                56.5 ns/op            40 B/op          2 allocs/op

Benchmark_TrimBytes/fiber-16                    54533307                21.9 ns/op            16 B/op          1 allocs/op
Benchmark_TrimBytes/fiber-16                    54532812                21.9 ns/op            16 B/op          1 allocs/op
Benchmark_TrimBytes/default-16                  14282517                84.6 ns/op            48 B/op          2 allocs/op
Benchmark_TrimBytes/default-16                  14114508                84.7 ns/op            48 B/op          2 allocs/op

Benchmark_EqualFolds/fiber-16                   36355153                32.6 ns/op             0 B/op          0 allocs/op
Benchmark_EqualFolds/fiber-16                   36355593                32.6 ns/op             0 B/op          0 allocs/op
Benchmark_EqualFolds/default-16                 15186220                78.1 ns/op             0 B/op          0 allocs/op
Benchmark_EqualFolds/default-16                 15186412                78.3 ns/op             0 B/op          0 allocs/op

Benchmark_UUID/fiber-16                         23994625                49.8 ns/op            48 B/op          1 allocs/op
Benchmark_UUID/fiber-16                         23994768                50.1 ns/op            48 B/op          1 allocs/op
Benchmark_UUID/default-16                        3233772                 371 ns/op           208 B/op          6 allocs/op
Benchmark_UUID/default-16                        3251295                 370 ns/op           208 B/op          6 allocs/op

Benchmark_GetString/unsafe-16                 1000000000               0.709 ns/op             0 B/op          0 allocs/op
Benchmark_GetString/unsafe-16                 1000000000               0.713 ns/op             0 B/op          0 allocs/op
Benchmark_GetString/default-16                  59986202                19.0 ns/op            16 B/op          1 allocs/op
Benchmark_GetString/default-16                  63142939                19.0 ns/op            16 B/op          1 allocs/op

Benchmark_GetBytes/unsafe-16                   508360195                2.36 ns/op             0 B/op          0 allocs/op
Benchmark_GetBytes/unsafe-16                   508359979                2.35 ns/op             0 B/op          0 allocs/op
Benchmark_GetBytes/default-16                   46143019                25.7 ns/op            16 B/op          1 allocs/op
Benchmark_GetBytes/default-16                   44434734                25.6 ns/op            16 B/op          1 allocs/op

Benchmark_GetMIME/fiber-16                      21423750                56.3 ns/op             0 B/op          0 allocs/op
Benchmark_GetMIME/fiber-16                      21423559                55.4 ns/op             0 B/op          0 allocs/op
Benchmark_GetMIME/default-16                     6735282                 173 ns/op             0 B/op          0 allocs/op
Benchmark_GetMIME/default-16                     6895002                 172 ns/op             0 B/op          0 allocs/op

Benchmark_StatusMessage/fiber-16              1000000000               0.766 ns/op             0 B/op          0 allocs/op
Benchmark_StatusMessage/fiber-16              1000000000               0.767 ns/op             0 B/op          0 allocs/op
Benchmark_StatusMessage/default-16             159538528                7.50 ns/op             0 B/op          0 allocs/op
Benchmark_StatusMessage/default-16             159750830                7.51 ns/op             0 B/op          0 allocs/op

Benchmark_ToUpper/fiber-16                      22217408                53.3 ns/op            48 B/op          1 allocs/op
Benchmark_ToUpper/fiber-16                      22636554                53.2 ns/op            48 B/op          1 allocs/op
Benchmark_ToUpper/default-16                    11108600                 108 ns/op            48 B/op          1 allocs/op
Benchmark_ToUpper/default-16                    11108580                 108 ns/op            48 B/op          1 allocs/op

Benchmark_ToLower/fiber-16                      23994720                49.8 ns/op            48 B/op          1 allocs/op
Benchmark_ToLower/fiber-16                      23994768                50.1 ns/op            48 B/op          1 allocs/op
Benchmark_ToLower/default-16                    10808376                 110 ns/op            48 B/op          1 allocs/op
Benchmark_ToLower/default-16                    10617034                 110 ns/op            48 B/op          1 allocs/op

Benchmark_TrimRight/fiber-16                   413699521                2.94 ns/op             0 B/op          0 allocs/op
Benchmark_TrimRight/fiber-16                   415131687                2.91 ns/op             0 B/op          0 allocs/op
Benchmark_TrimRight/default-16                  23994577                49.1 ns/op            32 B/op          1 allocs/op
Benchmark_TrimRight/default-16                  24484249                49.4 ns/op            32 B/op          1 allocs/op

Benchmark_TrimLeft/fiber-16                    379661170                3.13 ns/op             0 B/op          0 allocs/op
Benchmark_TrimLeft/fiber-16                    382079941                3.16 ns/op             0 B/op          0 allocs/op
Benchmark_TrimLeft/default-16                   27900877                41.9 ns/op            32 B/op          1 allocs/op
Benchmark_TrimLeft/default-16                   28564898                42.0 ns/op            32 B/op          1 allocs/op

Benchmark_Trim/fiber-16                        236632856                 4.96 ns/op            0 B/op          0 allocs/op
Benchmark_Trim/fiber-16                        237570085                 4.93 ns/op            0 B/op          0 allocs/op
Benchmark_Trim/default-16                       18457221                 66.0 ns/op           32 B/op          1 allocs/op
Benchmark_Trim/default-16                       18177328                 65.9 ns/op           32 B/op          1 allocs/op
Benchmark_Trim/default.trimspace-16            188933770                 6.33 ns/op            0 B/op          0 allocs/op
Benchmark_Trim/default.trimspace-16            184007649                 6.42 ns/op            0 B/op          0 allocs/op

Benchmark_ConvertToBytes/fiber-8                43773547                24.43 ns/op            0 B/op          0 allocs/op
Benchmark_ConvertToBytes/fiber-8                45849477                25.33 ns/op            0 B/op          0 allocs/op

Documentation

Index

Constants

View Source
const MIMEOctetStream = "application/octet-stream"

Variables

View Source
var (

	// Timestamp please start the timer function before you use this value
	// please load the value with atomic `atomic.LoadUint32(&utils.Timestamp)`
	Timestamp uint32
)

Functions

func AssertEqual

func AssertEqual(tb testing.TB, expected, actual interface{}, description ...string)

AssertEqual checks if values are equal

func ByteSize

func ByteSize(bytes uint64) string

ByteSize returns a human-readable byte string of the form 10M, 12.5K, and so forth. The unit that results in the smallest number greater than or equal to 1 is always chosen.

func ConvertToBytes

func ConvertToBytes(humanReadableString string) int

ConvertToBytes returns integer size of bytes from human-readable string, ex. 42kb, 42M Returns 0 if string is unrecognized

func CopyBytes

func CopyBytes(b []byte) []byte

CopyBytes copies a slice to make it immutable

func CopyString

func CopyString(s string) string

CopyString copies a string to make it immutable

func EqualFold

func EqualFold(b, s string) bool

EqualFold tests ascii strings for equality case-insensitively

func EqualFoldBytes

func EqualFoldBytes(b, s []byte) bool

EqualFoldBytes tests ascii slices for equality case-insensitively

func FunctionName

func FunctionName(fn interface{}) string

FunctionName returns function name

func GetArgument

func GetArgument(arg string) bool

GetArgument check if key is in arguments

func GetBytes deprecated

func GetBytes(s string) []byte

Deprecated: Please use UnsafeBytes instead

func GetMIME

func GetMIME(extension string) string

GetMIME returns the content-type of a file extension

func GetString deprecated

func GetString(b []byte) string

Deprecated: Please use UnsafeString instead

func ImmutableString deprecated

func ImmutableString(s string) string

Deprecated: Please use CopyString instead

func IncrementIPRange

func IncrementIPRange(ip net.IP)

IncrementIPRange Find available next IP address

func IsIPv4

func IsIPv4(s string) bool

IsIPv4 works the same way as net.ParseIP, but without check for IPv6 case and without returning net.IP slice, whereby IsIPv4 makes no allocations.

func IsIPv6

func IsIPv6(s string) bool

IsIPv6 works the same way as net.ParseIP, but without check for IPv4 case and without returning net.IP slice, whereby IsIPv6 makes no allocations.

func ParseVendorSpecificContentType

func ParseVendorSpecificContentType(cType string) string

ParseVendorSpecificContentType check if content type is vendor specific and if it is parsable to any known types. If its not vendor specific then returns the original content type.

func StartTimeStampUpdater

func StartTimeStampUpdater()

StartTimeStampUpdater starts a concurrent function which stores the timestamp to an atomic value per second, which is much better for performance than determining it at runtime each time

func StatusMessage

func StatusMessage(status int) string

StatusMessage returns the correct message for the provided HTTP statuscode

func ToLower

func ToLower(b string) string

ToLower converts ascii string to lower-case

func ToLowerBytes

func ToLowerBytes(b []byte) []byte

ToLowerBytes converts ascii slice to lower-case in-place.

func ToString

func ToString(arg interface{}, timeFormat ...string) string

ToString Change arg to string

func ToUpper

func ToUpper(b string) string

ToUpper converts ascii string to upper-case

func ToUpperBytes

func ToUpperBytes(b []byte) []byte

ToUpperBytes converts ascii slice to upper-case in-place.

func Trim

func Trim(s string, cutset byte) string

Trim is the equivalent of strings.Trim

func TrimBytes

func TrimBytes(b []byte, cutset byte) []byte

TrimBytes is the equivalent of bytes.Trim

func TrimLeft

func TrimLeft(s string, cutset byte) string

TrimLeft is the equivalent of strings.TrimLeft

func TrimLeftBytes

func TrimLeftBytes(b []byte, cutset byte) []byte

TrimLeftBytes is the equivalent of bytes.TrimLeft

func TrimRight

func TrimRight(s string, cutset byte) string

TrimRight is the equivalent of strings.TrimRight

func TrimRightBytes

func TrimRightBytes(b []byte, cutset byte) []byte

TrimRightBytes is the equivalent of bytes.TrimRight

func UUID

func UUID() string

UUID generates an universally unique identifier (UUID)

func UUIDv4

func UUIDv4() string

UUIDv4 returns a Random (Version 4) UUID. The strength of the UUIDs is based on the strength of the crypto/rand package.

func UnsafeBytes

func UnsafeBytes(s string) []byte

UnsafeBytes returns a byte pointer without allocation.

func UnsafeString

func UnsafeString(b []byte) string

UnsafeString returns a string pointer without allocation

Types

type JSONMarshal

type JSONMarshal func(v interface{}) ([]byte, error)

JSONMarshal returns the JSON encoding of v.

type JSONUnmarshal

type JSONUnmarshal func(data []byte, v interface{}) error

JSONUnmarshal parses the JSON-encoded data and stores the result in the value pointed to by v. If v is nil or not a pointer, Unmarshal returns an InvalidUnmarshalError.

type XMLMarshal

type XMLMarshal func(v interface{}) ([]byte, error)

XMLMarshal returns the XML encoding of v.

Jump to

Keyboard shortcuts

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