Bladeren bron

Reimplement titleize without regex

Zed 4 jaren geleden
bovenliggende
commit
0e5da8c305
2 gewijzigde bestanden met toevoegingen van 13 en 13 verwijderingen
  1. 11 0
      src/formatters.nim
  2. 2 13
      src/routes/preferences.nim

+ 11 - 0
src/formatters.nim

@@ -1,5 +1,6 @@
 # SPDX-License-Identifier: AGPL-3.0-only
 import strutils, strformat, times, uri, tables, xmltree, htmlparser, htmlgen
+import std/enumerate
 import regex
 import types, utils, query
 
@@ -179,3 +180,13 @@ proc getLocation*(u: Profile | Tweet): (string, string) =
 
 proc getSuspended*(username: string): string =
   &"User \"{username}\" has been suspended"
+
+proc titleize*(str: string): string =
+  const
+    lowercase = {'a'..'z'}
+    delims = {' ', '('}
+
+  result = str
+  for i, c in enumerate(str):
+    if c in lowercase and (i == 0 or str[i - 1] in delims):
+      result[i] = c.toUpperAscii

+ 2 - 13
src/routes/preferences.nim

@@ -1,25 +1,14 @@
 # SPDX-License-Identifier: AGPL-3.0-only
-import strutils, uri, os, re, algorithm
+import strutils, uri, os, algorithm
 
 import jester
 
 import router_utils
-import ../types
+import ".."/[types, formatters]
 import ../views/[general, preferences]
 
 export preferences
 
-let titleizeRegex = re"(?<![A-z])[a-z]"
-
-proc titleize(str: string): string =
-  result = str
-  var idx = 0
-  while idx != -1:
-    idx = str.find(titleizeRegex, start = idx)
-    if idx != -1:
-      result[idx] = str[idx].toUpperAscii
-      inc idx
-
 proc findThemes*(dir: string): seq[string] =
   for kind, path in walkDir(dir / "css" / "themes"):
     let theme = path.splitFile.name