forked from fyne-io/fyne
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Reduce string allocations in mimetype code
The code handling mimetypes was using strings.Split() to split the string and that allocates. We can use strings.IndexByte() to get an index and subslice instead. I also refactored some code to avoid duplicating mimetype split logic. The new internal/repository/mime package exists to avoid cycling imports. NOTE: The split logic is slightly different because a second / would be includes as part of the subtype now but as far as I can see from reading the spec (https://specifications.freedesktop.org/shared-mime-info-spec/shared-mime-info-spec-0.21.html) it should not be a problem as the format is "mainType/subType;weight" so there should not be any more separators.
- Loading branch information
Showing
5 changed files
with
61 additions
and
35 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters