fix #16703; revert most of #16480; add tests/stdlib/tmimetypes.nim (#16711)

This commit is contained in:
Timothee Cour
2021-01-13 14:52:59 -08:00
committed by GitHub
parent 165d39738d
commit fee5fbc722
3 changed files with 32 additions and 50 deletions

View File

@@ -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

View File

@@ -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
@@ -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"

View File

@@ -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()