Function to turn camelCase into separate words?

Questions about the LÖVE API, installing LÖVE and other support related questions go here.
Forum rules
Before you make a thread asking for help, read this.
Post Reply
User avatar
Jasoco
Inner party member
Posts: 3727
Joined: Mon Jun 22, 2009 9:35 am
Location: Pennsylvania, USA
Contact:

Function to turn camelCase into separate words?

Post 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?
User avatar
bartbes
Sex machine
Posts: 4946
Joined: Fri Aug 29, 2008 10:35 am
Location: The Netherlands
Contact:

Re: Function to turn camelCase into separate words?

Post 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
User avatar
Jasoco
Inner party member
Posts: 3727
Joined: Mon Jun 22, 2009 9:35 am
Location: Pennsylvania, USA
Contact:

Re: Function to turn camelCase into separate words?

Post 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!
Onenmaru
Prole
Posts: 2
Joined: Sat Sep 29, 2012 6:47 am

Re: Function to turn camelCase into separate words?

Post by Onenmaru »

bartbes wrote:How about..

Code: Select all

-snip-
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
User avatar
airstruck
Party member
Posts: 650
Joined: Thu Jun 04, 2015 7:11 pm
Location: Not being time thief.

Re: Function to turn camelCase into separate words?

Post 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
User avatar
s-ol
Party member
Posts: 1077
Joined: Mon Sep 15, 2014 7:41 pm
Location: Cologne, Germany
Contact:

Re: Function to turn camelCase into separate words?

Post 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

s-ol.nu /blog  -  p.s-ol.be /st8.lua  -  g.s-ol.be /gtglg /curcur

Code: Select all

print( type(love) )
if false then
  baby:hurt(me)
end
User avatar
airstruck
Party member
Posts: 650
Joined: Thu Jun 04, 2015 7:11 pm
Location: Not being time thief.

Re: Function to turn camelCase into separate words?

Post 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.
User avatar
s-ol
Party member
Posts: 1077
Joined: Mon Sep 15, 2014 7:41 pm
Location: Cologne, Germany
Contact:

Re: Function to turn camelCase into separate words?

Post 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.

s-ol.nu /blog  -  p.s-ol.be /st8.lua  -  g.s-ol.be /gtglg /curcur

Code: Select all

print( type(love) )
if false then
  baby:hurt(me)
end
Post Reply

Who is online

Users browsing this forum: Bing [Bot], Google [Bot] and 4 guests