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

Add mimetypes.mimesLongest #16480

Merged
merged 7 commits into from
Jan 2, 2021
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
7 changes: 5 additions & 2 deletions changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@
- `writeStackTrace` is available in JS backend now.

- `strscans.scanf` now supports parsing single characters.
- `strscans.scanTuple` added which uses `strscans.scanf` internally, returning a tuple which can be unpacked for easier usage of `scanf`.
- `strscans.scanTuple` added which uses `strscans.scanf` internally, returning a tuple which can be unpacked for easier usage of `scanf`.

- Added `setutils.toSet` that can take any iterable and convert it to a built-in set,
if the iterable yields a built-in settable type.
Expand All @@ -70,12 +70,15 @@
and `lists.toDoublyLinkedList` convert from `openArray`s; `lists.copy` implements
shallow copying; `lists.add` concatenates two lists - an O(1) variation that consumes
its argument, `addMoved`, is also supplied.

- Added `sequtils` import to `prelude`.

- Added `euclDiv` and `euclMod` to `math`.
- Added `httpcore.is1xx` and missing HTTP codes.

- 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`.

juancarlospaco marked this conversation as resolved.
Show resolved Hide resolved

## 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)
Copy link
Member

@timotheecour timotheecour Jan 13, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@juancarlospaco this is bad for several reasons:

  • runnableExamples inside a private proc is never run/compiled
  • the code is in fact invalid (>= not defined on array)
  • even if it were valid (say it returned a tuple), it's bad because (23, 73) >= (22, 100)

and as reported elsewhere it causes this regression: #16703 (EDIT: which itself is caused by #16790)

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