proc camel2snake*(s: string): string {.noSideEffect, procvar.} =
## CanBeFun => can_be_fun
result = newStringOfCap(s.len)
for i in 0..<len(s):
if s[i] in {'A'..'Z'}:
if i > 0:
result.add('_')
result.add(chr(ord(s[i]) + (ord('a') - ord('A'))))
else:
result.add(s[i])
echo camel2snake "CanBeFun"
I wonder if we have iterators for strings as in Ruby, something like "for c in string" maybe? Have not seen that in strutils or manual.
irb(main):003:0> 'Stefan'.each_char.with_index{|c, i| print c, i}; print "\n" S0t1e2f3a4n5 => nil irb(main):004:0> 'Stefan'.each_char{|c| print c}; print "\n" Stefan => nil
Note that strutils has toLower(c: char): char, so you don't need to fiddle with ord.
You can iterate over characters in a string, e.g.:
for ch in "abc":
echo ch
Great, thanks.
Both, the iterator and toLower seems to not being used much in strutils, I have not seen it at all.