Skip to content

Commit 2acf4f8

Browse files
authored
Merge pull request #269 from wolfadex/faster-desymbolify
Make deSymbolify faster
2 parents f1ea78e + 22e245a commit 2acf4f8

1 file changed

Lines changed: 36 additions & 31 deletions

File tree

src/Common.elm

Lines changed: 36 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -244,44 +244,49 @@ deSymbolify replacement str =
244244
|> String.replace "+" "Plus"
245245
)
246246

247-
else if String.startsWith "$" str then
248-
-- This was first identified in the BIMcloud OAS, the fields of `Resource` were prefixed with `$`
249-
deSymbolify replacement (String.dropLeft 1 str)
250-
251247
else
252248
let
253-
removeLeadingUnderscores : String -> String
254-
removeLeadingUnderscores acc =
255-
case String.uncons acc of
256-
Nothing ->
257-
"empty__"
258-
259-
Just ( head, tail ) ->
260-
if head == replacement then
261-
removeLeadingUnderscores tail
249+
replaced : List Char
250+
replaced =
251+
str
252+
|> String.foldl
253+
(\c acc ->
254+
let
255+
code : Int
256+
code =
257+
Char.toCode c
258+
in
259+
if
260+
-- lowercase
261+
(0x61 <= code && code <= 0x7A)
262+
|| -- uppercase
263+
(0x41 <= code && code <= 0x5A)
264+
|| -- digits
265+
(0x30 <= code && code <= 0x39)
266+
then
267+
c :: acc
268+
269+
else if List.isEmpty acc then
270+
acc
271+
272+
else if code == {- '_' -} 95 then
273+
c :: acc
262274

263-
else if Char.isDigit head then
264-
"N" ++ acc
265-
266-
else
267-
acc
275+
else
276+
replacement :: acc
277+
)
278+
[]
268279
in
269-
str
270-
|> replaceSymbolsWith replacement
271-
|> removeLeadingUnderscores
272-
280+
case List.reverse replaced of
281+
[] ->
282+
"empty__"
273283

274-
replaceSymbolsWith : Char -> String -> String
275-
replaceSymbolsWith replacement input =
276-
input
277-
|> String.map
278-
(\c ->
279-
if Char.toCode c /= {- '_' -} 95 && not (Char.isAlphaNum c) then
280-
replacement
284+
(h :: _) as nonEmpty ->
285+
if Char.isDigit h then
286+
"N" ++ String.fromList nonEmpty
281287

282288
else
283-
c
284-
)
289+
String.fromList nonEmpty
285290

286291

287292
initialUppercaseWordToLowercase : String -> String

0 commit comments

Comments
 (0)