utils

package module
v1.0.0 Latest Latest
Warning

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

Go to latest
Published: Oct 19, 2022 License: MIT Imports: 18 Imported by: 50

README

Fiber Utils

Release Discord Test Security Linter

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

Benchmarks

Environment: goos: darwin goarch: amd64 pkg: github.com/gofiber/utils cpu: Intel(R) Core(TM) i7-9750H CPU @ 2.60GHz

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

Benchmark_ToLowerBytes/fiber-12                29715831        36.44 ns/op       0 B/op       0 allocs/op
Benchmark_ToLowerBytes/fiber-12                33316479        36.28 ns/op       0 B/op       0 allocs/op
Benchmark_ToLowerBytes/default-12              11894427        96.98 ns/op      80 B/op       1 allocs/op
Benchmark_ToLowerBytes/default-12              12217050        97.43 ns/op      80 B/op       1 allocs/op

Benchmark_ToUpperBytes/fiber-12                22042162        46.92 ns/op       0 B/op       0 allocs/op
Benchmark_ToUpperBytes/fiber-12                25859680        46.43 ns/op       0 B/op       0 allocs/op
Benchmark_ToUpperBytes/default-12              10015346        117.2 ns/op      80 B/op       1 allocs/op
Benchmark_ToUpperBytes/default-12              10185375        117.8 ns/op      80 B/op       1 allocs/op

Benchmark_EqualFoldBytes/fiber-12              22944849        47.14 ns/op       0 B/op       0 allocs/op
Benchmark_EqualFoldBytes/fiber-12              26006342        46.82 ns/op       0 B/op       0 allocs/op
Benchmark_EqualFoldBytes/default-12             5222006        222.5 ns/op       0 B/op       0 allocs/op
Benchmark_EqualFoldBytes/default-12             5349398        223.2 ns/op       0 B/op       0 allocs/op

Benchmark_EqualFold/fiber-12                   24761037        48.63 ns/op       0 B/op       0 allocs/op
Benchmark_EqualFold/fiber-12                   24159073        48.63 ns/op       0 B/op       0 allocs/op
Benchmark_EqualFold/default-12                  6322188        191.5 ns/op       0 B/op       0 allocs/op
Benchmark_EqualFold/default-12                  6319070        193.5 ns/op       0 B/op       0 allocs/op

Benchmark_UUID/fiber-12                        22061482        49.13 ns/op      48 B/op       1 allocs/op
Benchmark_UUID/fiber-12                        24123198        48.40 ns/op      48 B/op       1 allocs/op
Benchmark_UUID/default-12                       3581961        336.9 ns/op     168 B/op       6 allocs/op
Benchmark_UUID/default-12                       3465946        344.8 ns/op     168 B/op       6 allocs/op

Benchmark_ConvertToBytes/fiber-12              53392819        23.19 ns/op       0 B/op       0 allocs/op
Benchmark_ConvertToBytes/fiber-12              51117225        23.32 ns/op       0 B/op       0 allocs/op

Benchmark_UnsafeString/unsafe-12               1000000000     0.5672 ns/op       0 B/op       0 allocs/op
Benchmark_UnsafeString/unsafe-12               1000000000     0.5683 ns/op       0 B/op       0 allocs/op
Benchmark_UnsafeString/default-12              64000897        18.45 ns/op      16 B/op       1 allocs/op
Benchmark_UnsafeString/default-12              64138909        18.13 ns/op      16 B/op       1 allocs/op

Benchmark_UnsafeBytes/unsafe-12                474777096       2.539 ns/op       0 B/op       0 allocs/op
Benchmark_UnsafeBytes/unsafe-12                469340781       2.535 ns/op       0 B/op       0 allocs/op
Benchmark_UnsafeBytes/default-12               53125656        22.15 ns/op      16 B/op       1 allocs/op
Benchmark_UnsafeBytes/default-12               52615048        22.33 ns/op      16 B/op       1 allocs/op

Benchmark_ToString-12                          22981430        51.72 ns/op      40 B/op       2 allocs/op
Benchmark_ToString-12                          22956476        52.93 ns/op      40 B/op       2 allocs/op

Benchmark_GetMIME/fiber-12                     15782622        74.99 ns/op       0 B/op       0 allocs/op
Benchmark_GetMIME/fiber-12                     13992375        93.13 ns/op       0 B/op       0 allocs/op
Benchmark_GetMIME/default-12                    6825952        147.0 ns/op       0 B/op       0 allocs/op
Benchmark_GetMIME/default-12                    9158227        132.5 ns/op       0 B/op       0 allocs/op

ParseVendorSpecificContentType
Benchmark_Parse.../vendorContentType-12        21334663        50.24 ns/op      16 B/op       1 allocs/op
Benchmark_Parse.../vendorContentType-12        23121808        51.20 ns/op      16 B/op       1 allocs/op
Benchmark_Parse.../defaultContentType-12       154423909       6.772 ns/op       0 B/op       0 allocs/op
Benchmark_Parse.../defaultContentType-12       183285117       6.662 ns/op       0 B/op       0 allocs/op

Benchmark_StatusMessage/fiber-12             1000000000       0.9796 ns/op       0 B/op       0 allocs/op
Benchmark_StatusMessage/fiber-12             1000000000       0.9706 ns/op       0 B/op       0 allocs/op
Benchmark_StatusMessage/default-12            380260562        2.989 ns/op       0 B/op       0 allocs/op
Benchmark_StatusMessage/default-12            403639642        3.124 ns/op       0 B/op       0 allocs/op

Benchmark_IsIPv4/fiber-12                      53576214        21.07 ns/op       0 B/op       0 allocs/op
Benchmark_IsIPv4/fiber-12                      62672907        22.04 ns/op       0 B/op       0 allocs/op
Benchmark_IsIPv4/default-12                    21204613        62.23 ns/op      16 B/op       1 allocs/op
Benchmark_IsIPv4/default-12                    21399847        56.61 ns/op      16 B/op       1 allocs/op

Benchmark_IsIPv6/fiber-12                      16754995        72.46 ns/op       0 B/op       0 allocs/op
Benchmark_IsIPv6/fiber-12                      17080897        74.31 ns/op       0 B/op       0 allocs/op
Benchmark_IsIPv6/default-12                     8160195        124.5 ns/op      16 B/op       1 allocs/op
Benchmark_IsIPv6/default-12                     9415326        119.8 ns/op      16 B/op       1 allocs/op

Benchmark_ToUpper/fiber-12                     13175154        81.67 ns/op      80 B/op       1 allocs/op
Benchmark_ToUpper/fiber-12                     14285533        77.27 ns/op      80 B/op       1 allocs/op
Benchmark_ToUpper/default-12                    5332206        231.8 ns/op      80 B/op       1 allocs/op
Benchmark_ToUpper/default-12                    5364650        236.0 ns/op      80 B/op       1 allocs/op

Benchmark_ToLower/fiber-12                     12996409        80.24 ns/op      80 B/op       1 allocs/op
Benchmark_ToLower/fiber-12                     16539536        69.27 ns/op      80 B/op       1 allocs/op
Benchmark_ToLower/default-12                    5132185        222.5 ns/op      80 B/op       1 allocs/op
Benchmark_ToLower/default-12                    5158561        225.3 ns/op      80 B/op       1 allocs/op

Benchmark_CalculateTimestamp/fiber-12        1000000000       0.2634 ns/op       0 B/op       0 allocs/op
Benchmark_CalculateTimestamp/fiber-12        1000000000       0.2935 ns/op       0 B/op       0 allocs/op
Benchmark_CalculateTimestamp/default-12        15740576        73.79 ns/op       0 B/op       0 allocs/op
Benchmark_CalculateTimestamp/default-12        15789036        71.12 ns/op       0 B/op       0 allocs/op

See all the benchmarks under https://gofiber.github.io/utils/

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 ByteSize added in v0.0.7

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 added in v1.0.0

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 added in v1.0.0

func CopyBytes(b []byte) []byte

CopyBytes copies a slice to make it immutable

func CopyString added in v1.0.0

func CopyString(s string) string

CopyString copies a string to make it immutable

func EqualFold added in v1.0.0

func EqualFold[S byteSeq](b, s S) bool

EqualFold tests ascii strings or bytes for equality case-insensitively

func FunctionName added in v0.0.6

func FunctionName(fn any) string

FunctionName returns function name

func GetArgument

func GetArgument(arg string) bool

GetArgument check if key is in arguments

func GetMIME

func GetMIME(extension string) (mime string)

GetMIME returns the content-type of a file extension

func IncrementIPRange added in v1.0.0

func IncrementIPRange(ip net.IP)

IncrementIPRange Find available next IP address

func IsIPv4 added in v1.0.0

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 added in v1.0.0

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 added in v1.0.0

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 added in v1.0.0

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 added in v0.0.2

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 added in v1.0.0

func ToString(arg any, 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 UUID added in v0.0.4

func UUID() string

UUID generates an universally unique identifier (UUID)

func UUIDv4 added in v1.0.0

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 added in v0.1.0

func UnsafeBytes(s string) (bs []byte)

#nosec G103 UnsafeBytes returns a byte pointer without allocation

func UnsafeString added in v0.1.0

func UnsafeString(b []byte) string

#nosec G103 UnsafeString returns a string pointer without allocation

Types

type JSONMarshal added in v1.0.0

type JSONMarshal func(v any) ([]byte, error)

JSONMarshal returns the JSON encoding of v.

type JSONUnmarshal added in v1.0.0

type JSONUnmarshal func(data []byte, v any) 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 added in v1.0.0

type XMLMarshal func(v any) ([]byte, error)

XMLMarshal returns the XML encoding of v.

type XMLUnmarshal added in v1.0.0

type XMLUnmarshal func(data []byte, v any) error

XMLUnmarshal parses the XML-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.

Jump to

Keyboard shortcuts

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