Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Reduce string allocations in mimetype code #4440

Merged
merged 2 commits into from
Dec 7, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions internal/repository/mime/mime.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package mime

import "strings"

// Split spits the mimetype into its main type and subtype.
func Split(mimeTypeFull string) (mimeType, mimeSubType string) {
// Replace with strings.Cut() when Go 1.18 is our new base version.
separatorIndex := strings.IndexByte(mimeTypeFull, '/')
if separatorIndex == -1 || mimeTypeFull[separatorIndex+1:] == "" {
return "", "" // Empty or only one part. Ignore.
}

mimeType = mimeTypeFull[:separatorIndex]
mimeSubType = mimeTypeFull[separatorIndex+1:]
return
}
21 changes: 21 additions & 0 deletions internal/repository/mime/mime_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package mime

import (
"testing"

"github.com/stretchr/testify/assert"
)

func TestSplitMimeType(t *testing.T) {
main, sub := Split("text/plain")
assert.Equal(t, "text", main)
assert.Equal(t, "plain", sub)

main, sub = Split("text/")
assert.Empty(t, main)
assert.Empty(t, sub)

main, sub = Split("")
assert.Empty(t, main)
assert.Empty(t, sub)
}
35 changes: 13 additions & 22 deletions storage/filter.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"strings"

"fyne.io/fyne/v2"
"fyne.io/fyne/v2/internal/repository/mime"
)

// FileFilter is an interface that can be implemented to provide a filter to a file dialog.
Expand Down Expand Up @@ -42,18 +43,21 @@ func NewExtensionFileFilter(extensions []string) FileFilter {

// Matches returns true if a file URI has one of the filtered mimetypes.
func (mt *MimeTypeFileFilter) Matches(uri fyne.URI) bool {
mimeType, mimeSubType := splitMimeType(uri)
mimeType, mimeSubType := mime.Split(uri.MimeType())
for _, mimeTypeFull := range mt.MimeTypes {
mimeTypeSplit := strings.Split(mimeTypeFull, "/")
if len(mimeTypeSplit) <= 1 {
mType, mSubType := mime.Split(mimeTypeFull)
if mType == "" || mSubType == "" {
continue
}
mType := mimeTypeSplit[0]
mSubType := strings.Split(mimeTypeSplit[1], ";")[0]
if mType == mimeType {
if mSubType == mimeSubType || mSubType == "*" {
return true
}

// Replace with strings.Cut() when Go 1.18 is our new base version.
subTypeSeparatorIndex := strings.IndexByte(mSubType, ';')
if subTypeSeparatorIndex != -1 {
mSubType = mSubType[:subTypeSeparatorIndex]
}

if mType == mimeType && (mSubType == mimeSubType || mSubType == "*") {
return true
}
}
return false
Expand All @@ -64,16 +68,3 @@ func (mt *MimeTypeFileFilter) Matches(uri fyne.URI) bool {
func NewMimeTypeFileFilter(mimeTypes []string) FileFilter {
return &MimeTypeFileFilter{MimeTypes: mimeTypes}
}

func splitMimeType(uri fyne.URI) (mimeType, mimeSubType string) {
mimeTypeFull := uri.MimeType()
mimeTypeSplit := strings.Split(mimeTypeFull, "/")
if len(mimeTypeSplit) <= 1 {
mimeType, mimeSubType = "", ""
return
}
mimeType = mimeTypeSplit[0]
mimeSubType = mimeTypeSplit[1]

return
}
8 changes: 6 additions & 2 deletions storage/repository/uri.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@ func (u *uri) Name() string {
}

func (u *uri) MimeType() string {

mimeTypeFull := mime.TypeByExtension(u.Extension())
if mimeTypeFull == "" {
mimeTypeFull = "text/plain"
Expand All @@ -50,7 +49,12 @@ func (u *uri) MimeType() string {
}
}

return strings.Split(mimeTypeFull, ";")[0]
// Replace with strings.Cut() when Go 1.18 is our new base version.
semicolonIndex := strings.IndexByte(mimeTypeFull, ';')
if semicolonIndex == -1 {
return mimeTypeFull
}
return mimeTypeFull[:semicolonIndex]
}

func (u *uri) Scheme() string {
Expand Down
14 changes: 3 additions & 11 deletions widget/fileicon.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
package widget

import (
"strings"

"fyne.io/fyne/v2"
"fyne.io/fyne/v2/canvas"
"fyne.io/fyne/v2/internal/repository/mime"
"fyne.io/fyne/v2/internal/widget"
"fyne.io/fyne/v2/storage"
"fyne.io/fyne/v2/theme"
Expand Down Expand Up @@ -99,7 +98,8 @@ func (i *FileIcon) lookupIcon(uri fyne.URI) fyne.Resource {
return theme.FolderIcon()
}

switch splitMimeType(uri) {
mainMimeType, _ := mime.Split(uri.MimeType())
switch mainMimeType {
case "application":
return theme.FileApplicationIcon()
case "audio":
Expand Down Expand Up @@ -201,11 +201,3 @@ func trimmedExtension(uri fyne.URI) string {
}
return ext
}

func splitMimeType(uri fyne.URI) string {
mimeTypeSplit := strings.Split(uri.MimeType(), "/")
if len(mimeTypeSplit) <= 1 {
return ""
}
return mimeTypeSplit[0]
}
Loading