Skip to content

Commit

Permalink
🧹 chore: sync utils from https://github.com/gofiber/fiber
Browse files Browse the repository at this point in the history
  • Loading branch information
efectn committed Apr 18, 2023
1 parent 95aa1ff commit 9de5365
Show file tree
Hide file tree
Showing 7 changed files with 52 additions and 20 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ jobs:
Tests:
strategy:
matrix:
go-version: [1.18.x, 1.19.x, 1.20.x]
go-version: [1.19.x, 1.20.x]
platform: [ubuntu-latest, windows-latest, macos-latest]
runs-on: ${{ matrix.platform }}
steps:
Expand Down
4 changes: 2 additions & 2 deletions bytes.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,15 @@

package utils

// ToLowerBytes converts ascii slice to lower-case in-place.
// ToLowerBytes converts ascii slice to lower-case
func ToLowerBytes(b []byte) []byte {
for i := 0; i < len(b); i++ {
b[i] = toLowerTable[b[i]]
}
return b
}

// ToUpperBytes converts ascii slice to upper-case in-place.
// ToUpperBytes converts ascii slice to upper-case
func ToUpperBytes(b []byte) []byte {
for i := 0; i < len(b); i++ {
b[i] = toUpperTable[b[i]]
Expand Down
20 changes: 12 additions & 8 deletions convert.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,21 +13,25 @@ import (
"unsafe"
)

const MaxStringLen = 0x7fff0000 // Maximum string length for UnsafeBytes. (decimal: 2147418112)

// #nosec G103
// UnsafeString returns a string pointer without allocation
func UnsafeString(b []byte) string {
return *(*string)(unsafe.Pointer(&b))
}

// #nosec G103
// UnsafeBytes returns a byte pointer without allocation
func UnsafeBytes(s string) (bs []byte) {
sh := (*reflect.StringHeader)(unsafe.Pointer(&s))
bh := (*reflect.SliceHeader)(unsafe.Pointer(&bs))
bh.Data = sh.Data
bh.Len = sh.Len
bh.Cap = sh.Len
return
// UnsafeBytes returns a byte pointer without allocation.
// String length shouldn't be more than 2147418112.
func UnsafeBytes(s string) []byte {
if s == "" {
return nil
}

return (*[MaxStringLen]byte)(unsafe.Pointer(
(*reflect.StringHeader)(unsafe.Pointer(&s)).Data),
)[:len(s):len(s)]
}

// CopyString copies a string to make it immutable
Expand Down
29 changes: 21 additions & 8 deletions http.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,24 +4,37 @@

package utils

import "strings"
import (
"mime"
"strings"
)

const MIMEOctetStream = "application/octet-stream"

// GetMIME returns the content-type of a file extension
func GetMIME(extension string) (mime string) {
func GetMIME(extension string) string {
if len(extension) == 0 {
return mime
return ""
}
var foundMime string
if extension[0] == '.' {
mime = mimeExtensions[extension[1:]]
foundMime = mimeExtensions[extension[1:]]
} else {
mime = mimeExtensions[extension]
foundMime = mimeExtensions[extension]
}
if len(mime) == 0 {
return MIMEOctetStream

if len(foundMime) == 0 {
if extension[0] != '.' {
foundMime = mime.TypeByExtension("." + extension)
} else {
foundMime = mime.TypeByExtension(extension)
}

if foundMime == "" {
return MIMEOctetStream
}
}
return mime
return foundMime
}

// ParseVendorSpecificContentType check if content type is vendor specific and
Expand Down
9 changes: 9 additions & 0 deletions http_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,18 @@ func Test_GetMIME(t *testing.T) {

res = GetMIME("unknown")
require.Equal(t, MIMEOctetStream, res)

// empty case
res = GetMIME("")
require.Equal(t, "", res)

err := mime.AddExtensionType(".mjs", "application/javascript")
if err == nil {
res = GetMIME(".mjs")
require.Equal(t, "application/javascript", res)
}
require.NoError(t, err)

}

// go test -v -run=^$ -bench=Benchmark_GetMIME -benchmem -count=2
Expand Down
2 changes: 1 addition & 1 deletion ips.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ func IsIPv4(s string) bool {
}
}

if ci == 0 || n > 0xFF || (ci > 1 && s[0] == '0') {
if ci == 0 || (ci > 1 && s[0] == '0') {
return false
}

Expand Down
6 changes: 6 additions & 0 deletions ips_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,12 @@ func Test_IsIPv4(t *testing.T) {
require.Equal(t, false, IsIPv4(""))
require.Equal(t, false, IsIPv4("2345:0425:2CA1::0567:5673:23b5"))
require.Equal(t, false, IsIPv4("invalid"))
require.Equal(t, false, IsIPv4("189.12.34.260"))
require.Equal(t, false, IsIPv4("189.12.260.260"))
require.Equal(t, false, IsIPv4("189.260.260.260"))
require.Equal(t, false, IsIPv4("999.999.999.999"))
require.Equal(t, false, IsIPv4("9999.9999.9999.9999"))

}

// go test -v -run=^$ -bench=UnsafeString -benchmem -count=2
Expand Down

0 comments on commit 9de5365

Please sign in to comment.