Page 1 of 1
Function to turn camelCase into separate words?
Posted: Sun Oct 18, 2015 11:39 am
by Jasoco
I'm looking to make a function to split a camelCase string into separate words based on which letters are capitalized. So "camelCase" would become "Camel Case". I'm sure I could figure it out myself but I am probably too tired right now to concentrate because I'm not completely sure without creating a lot of code for parsing.
I guess I'd go through the string and take note of the location of each capital letter, then split it into separate words based on those letters locations, then add spaces between them and concat them all back together. Maybe someone has already created a function to do this?
Re: Function to turn camelCase into separate words?
Posted: Sun Oct 18, 2015 11:47 am
by bartbes
How about..
Code: Select all
function splitCamel(s)
local function split(char)
return " " .. char
end
return (s:gsub("[A-Z]", split):gsub("^.", string.upper))
end
Re: Function to turn camelCase into separate words?
Posted: Sun Oct 18, 2015 5:20 pm
by Jasoco
That works! Wow, I really do not know how to use gsub. I would have probably used so much more code than needed.
Thanks!
Re: Function to turn camelCase into separate words?
Posted: Wed Oct 21, 2015 1:43 pm
by Onenmaru
bartbes wrote:How about..
I'm not trying to be rude, but you definitely don't want to redefine a static helper function each time you need it. Your example would be more appropriate as..
Code: Select all
do
local function split(char)
return " " .. char
end
function splitCamel(s)
return (s:gsub("[A-Z]", split):gsub("^.", string.upper))
end
end
The problem can really just be solved with gsub's
nth match syntax though. Such as:
Code: Select all
function splitCamel(s)
return (s:gsub("%u", " %1"):gsub("^.", string.upper))
end
Re: Function to turn camelCase into separate words?
Posted: Wed Oct 21, 2015 7:04 pm
by airstruck
Onenmaru wrote:The problem can really just be solved with gsub's nth match syntax though.
Might also make sense to use frontier pattern depending on desired behavior for repeated caps.
Code: Select all
> print(("myFriedKFCChicken"):gsub("%u", " %1"):gsub("^.", string.upper))
My Fried K F C Chicken 1
Code: Select all
> print(("myFriedKFCChicken"):gsub(".%f[%l]", " %1"):gsub("%l%f[%u]", "%1 "):gsub("^.", string.upper))
My Fried KFC Chicken 1
Re: Function to turn camelCase into separate words?
Posted: Wed Oct 21, 2015 7:22 pm
by s-ol
For reference, this is how it is done in Rails' ActiveSupport (in Ruby):
Code: Select all
def underscore(camel_cased_word)
return camel_cased_word unless camel_cased_word =~ /[A-Z-]|::/
word = camel_cased_word.to_s.gsub(/::/, '/')
word.gsub!(/(?:(?<=([A-Za-z\d]))|\b)(#{inflections.acronym_regex})(?=\b|[^a-z])/) { "#{$1 && '_'}#{$2.downcase}" }
word.gsub!(/([A-Z\d]+)([A-Z][a-z])/,'\1_\2')
word.gsub!(/([a-z\d])([A-Z])/,'\1_\2')
word.tr!("-", "_")
word.downcase!
word
end
https://github.com/rails/rails/blob/861 ... ods.rb#L90
Re: Function to turn camelCase into separate words?
Posted: Wed Oct 21, 2015 11:06 pm
by airstruck
S0lll0s wrote:For reference, this is how it is done in Rails' ActiveSupport (in Ruby)
Won't do much good as far as Lua's pattern matching is concerned (no alternation, lookbehinds of other fancy stuff). That's why I suggested frontier pattern, does the same job as the lookbehind in that example.
Re: Function to turn camelCase into separate words?
Posted: Thu Oct 22, 2015 9:44 am
by s-ol
airstruck wrote:S0lll0s wrote:For reference, this is how it is done in Rails' ActiveSupport (in Ruby)
Won't do much good as far as Lua's pattern matching is concerned (no alternation, lookbehinds of other fancy stuff). That's why I suggested frontier pattern, does the same job as the lookbehind in that example.
I didn't mean that you should try to port it to lua, it was meant more as an algorithmic reference.