Skip to content

Instantly share code, notes, and snippets.

@flaviut
Created May 25, 2014 20:44
Show Gist options
  • Save flaviut/3aec4e853582c69499c3 to your computer and use it in GitHub Desktop.
Save flaviut/3aec4e853582c69499c3 to your computer and use it in GitHub Desktop.
import strutils
proc mangle*(name: string): string =
result = ""
case name[0]
of Letters:
result.add(name[0].toLower)
of Digits:
result.add("N" & name[0])
else:
result = "HEX" & toHex(ord(name[0]), 2)
for i in 1..(name.len-1):
let c = name[i]
case c
of 'A'..'Z':
add(result, c.toLower)
of '_':
discard
of 'a'..'z', '0'..'9':
add(result, c)
else:
add(result, "HEX" & toHex(ord(c), 2))
proc mangleField(name: string): string =
result = mangle(name)
if name[0] in 'a'..'z':
result[0] = name[0].toUpper
echo mangleField("A")
echo mangleField("a")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment