summary refs log tree commit diff stats
diff options
context:
space:
mode:
authorJuan Carlos <juancarlospaco@gmail.com>2021-01-02 04:10:38 -0300
committerGitHub <noreply@github.com>2021-01-02 08:10:38 +0100
commit1d2e2b5a5b70cc20c79fc3d02a8f3e220b00b3a3 (patch)
treec87a883fcfc5cb7ab94083e680875b886b525a23
parent73a8b950cb6abf36a0d29c210bb7db302ae68325 (diff)
downloadNim-1d2e2b5a5b70cc20c79fc3d02a8f3e220b00b3a3.tar.gz
Add mimetypes.mimesLongest (#16480)
* Allow single alloc mimetypes ops

* Allow single alloc mimetypes ops

* Update lib/pure/mimetypes.nim

Co-authored-by: flywind <43030857+xflywind@users.noreply.github.com>

* https://github.com/nim-lang/Nim/pull/16480#issuecomment-752630190

* https://github.com/nim-lang/Nim/pull/16480#issuecomment-752630190

* https://github.com/nim-lang/Nim/pull/16480#issuecomment-753349661

* update changelog

Co-authored-by: flywind <43030857+xflywind@users.noreply.github.com>
-rw-r--r--changelog.md3
-rw-r--r--lib/pure/mimetypes.nim29
2 files changed, 31 insertions, 1 deletions
diff --git a/changelog.md b/changelog.md
index c6a1d17d9..b10b9607a 100644
--- a/changelog.md
+++ b/changelog.md
@@ -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
 
diff --git a/lib/pure/mimetypes.nim b/lib/pure/mimetypes.nim
index 206783929..880ccdff1 100644
--- a/lib/pure/mimetypes.nim
+++ b/lib/pure/mimetypes.nim
@@ -8,7 +8,7 @@
 #
 
 ## This module implements a mimetypes database
-import strtabs
+import strtabs, std/private/since
 from strutils import startsWith, toLowerAscii, strip
 
 type
@@ -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: