Skip to content

Commit

Permalink
fix #16703; revert most of #16480; add tests/stdlib/tmimetypes.nim (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
timotheecour authored Jan 13, 2021
1 parent 165d397 commit fee5fbc
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 50 deletions.
3 changes: 0 additions & 3 deletions changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -84,9 +84,6 @@
- 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`.

- Added `posix_utils.osReleaseFile` to get system identification from `os-release` file on Linux and the BSDs.
https://www.freedesktop.org/software/systemd/man/os-release.html

Expand Down
66 changes: 19 additions & 47 deletions lib/pure/mimetypes.nim
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,25 @@
#

## This module implements a mimetypes database
import strtabs, std/private/since

runnableExamples:
var m = newMimetypes()
doAssert m.getMimetype("mp4") == "video/mp4"
doAssert m.getExt("text/html") == "html"
## Values can be uppercase too.
doAssert m.getMimetype("MP4") == "video/mp4"
doAssert m.getExt("TEXT/HTML") == "html"
## If values are invalid then ``default`` is returned.
doAssert m.getMimetype("INVALID") == "text/plain"
doAssert m.getExt("INVALID/NONEXISTENT") == "txt"
doAssert m.getMimetype("") == "text/plain"
doAssert m.getExt("") == "txt"
## Register new Mimetypes.
m.register(ext = "fakext", mimetype = "text/fakelang")
doAssert m.getMimetype("fakext") == "text/fakelang"
doAssert m.getMimetype("FaKeXT") == "text/fakelang"

import strtabs
from strutils import startsWith, toLowerAscii, strip

type
Expand Down Expand Up @@ -1916,49 +1934,3 @@ func register*(mimedb: var MimeDB, ext: string, mimetype: string) =
assert mimetype.strip.len > 0, "mimetype argument can not be empty 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:
var m = newMimetypes()
doAssert m.getMimetype("mp4") == "video/mp4"
doAssert m.getExt("text/html") == "html"
## Values can be uppercase too.
doAssert m.getMimetype("MP4") == "video/mp4"
doAssert m.getExt("TEXT/HTML") == "html"
## If values are invalid then ``default`` is returned.
doAssert m.getMimetype("INVALID") == "text/plain"
doAssert m.getExt("INVALID/NONEXISTENT") == "txt"
doAssert m.getMimetype("") == "text/plain"
doAssert m.getExt("") == "txt"
## Register new Mimetypes.
m.register(ext = "fakext", mimetype = "text/fakelang")
doAssert m.getMimetype("fakext") == "text/fakelang"
doAssert m.getMimetype("FaKeXT") == "text/fakelang"
13 changes: 13 additions & 0 deletions tests/stdlib/tmimetypes.nim
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
discard """
targets: "c js"
"""

import std/mimetypes
template main() =
var m = newMimetypes()
doAssert m.getMimetype("mp4") == "video/mp4"
# see also `runnableExamples`.
# xxx we should have a way to avoid duplicating code between runnableExamples and tests

static: main()
main()

0 comments on commit fee5fbc

Please sign in to comment.