Skip to content

Commit

Permalink
Add mimetypes.mimesLongest (#16480)
Browse files Browse the repository at this point in the history
* Allow single alloc mimetypes ops

* Allow single alloc mimetypes ops

* Update lib/pure/mimetypes.nim

Co-authored-by: flywind <[email protected]>

* #16480 (comment)

* #16480 (comment)

* #16480 (comment)

* update changelog

Co-authored-by: flywind <[email protected]>
  • Loading branch information
juancarlospaco and ringabout authored Jan 2, 2021
1 parent 73a8b95 commit 1d2e2b5
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 1 deletion.
3 changes: 3 additions & 0 deletions changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,9 @@
- Added `httpcore.is1xx` and missing HTTP codes.
- Added `jsconsole.jsAssert` for JavaScript target.

- Added `mimetypes.mimesExtMaxLen` thats equal to the length of the longest "ext" from `mimes`.
- Added `mimetypes.mimesMaxLen` thats equal to the length of the longest "mime" from `mimes`.


## Language changes

Expand Down
29 changes: 28 additions & 1 deletion lib/pure/mimetypes.nim
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
#

## This module implements a mimetypes database
import strtabs
import strtabs, std/private/since
from strutils import startsWith, toLowerAscii, strip

type
Expand Down Expand Up @@ -1917,6 +1917,33 @@ func register*(mimedb: var MimeDB, ext: string, mimetype: string) =
{.noSideEffect.}:
mimedb.mimes[ext.toLowerAscii()] = mimetype.toLowerAscii()


since (1, 5):
func mimesLongest(): array[2, int] {.compiletime.} =
runnableExamples:
static:
doAssert mimesLongest() >= (ext: 24, mime: 73)
var currentKeyLength, currentValLength: int
for item in mimes:
currentKeyLength = item[0].len
currentValLength = item[1].len
if currentKeyLength > result[0]: result[0] = currentKeyLength
if currentValLength > result[1]: result[1] = currentValLength

const
ctValue = mimesLongest() # Use 2 const instead of func, save tuple unpack.
mimesExtMaxLen*: int = ctValue[0] ## \
## The length of the longest "ext" from `mimes`,
## this is useful for optimizations with `newStringOfCap` and `newString`.
mimesMaxLen*: int = ctValue[1] ## \
## The length of the longest "mime" from `mimes`,
## this is useful for optimizations with `newStringOfCap` and `newString`.
##
## See also:
## * `newStringOfCap <system.html#newStringOfCap>`_
## * `newString <system.html#newString>`_


runnableExamples:
static:
block:
Expand Down

0 comments on commit 1d2e2b5

Please sign in to comment.