Initial commit

This commit is contained in:
Gustavo Cordova Avila
2024-03-25 16:32:35 -07:00
commit b02bd3aaff
6 changed files with 81 additions and 0 deletions

35
src/snake_case.nim Normal file
View File

@@ -0,0 +1,35 @@
## Convert strings to/from SNAKE_CASE
proc toSnakeCase(str: string): string =
## Convert a camelCase string to SNAKE_CASE
if str.len > 0:
var prv: char = '\0'
for ch in str:
if ch in {'A'..'Z'}:
if result.len > 0 and prv notin {'A'..'Z'}:
result.add '_'
result.add ch
elif ch in {'a'..'z'}:
result.add (ch.byte - 32).char
elif ch in {'0'..'9'}:
result.add ch
elif ch in {'-', '_', '.', '+'}:
if result.len > 0 and result[^1] != '_':
result.add '_'
proc fromSnakeCase(str: string): string =
## Convert from SNAKE_CASE to camelCase
var usc: bool = false
for ch in str:
if ch in {'A'..'Z'}:
if usc:
result.add ch
usc = false
else:
result.add (ch.byte + 32).char
elif ch in {'0'..'9', 'a'..'z'}:
result.add ch
elif ch in {'_', '-', '.', '+'}:
usc = true
# Fin.