Added initial output of generator scripts.
[cl-glfw.git] / generators / string-lispify.rb
blob8aeca16bc17b1238b9cd458db980cef15488bec7
2 class String
3   def lispify prefix=nil
4     s=self.dup
5     # remove prefix if it's there
6     s=s.slice(prefix.length..-1) if prefix and s[0...prefix.length]==prefix
8     # prefix underscore->alpha or uppercase->lowercase with dash
9     s.gsub!(/(_+[a-zA-Z]|[A-Z])(?=[a-z])/) do |m|
10       '-'+m.downcase[-1,1]
11     end
12     # split off any numerical groups
13     s.gsub!(/\d+/,"-\\0")
14     # split off any two or more-letter acronyms at the end (eg. EXT NV ARB ATI SGIX etc)
15     s.sub!(/[A-Z]{2,}$/,"-\\0")
16     # group together any bouts of -s and _s into a single dash
17     s.gsub!(/[-_]+/,"-")
18     s.downcase!
19     if s[0,1]=='-'
20       s[1..-1]
21     else
22       s
23     end
24   end
25 end
27 class Array
28   GL_SUFFIXES=Regexp.new('('+["IBM","3DFX","SGIS","SUNX","HP","GREMEDY","APPLE","MESA","SUN","INTEL","NV","ARB","SGIX","EXT","ATI"].join('|')+')$')
30   def lispify
31     stems={}
32     split=
33     collect do |e|
34       [e,*e.sub(GL_SUFFIXES,'-\\1').split('-')]
35     end.collect do |orig,e,vendor_suffix|
36       stem,arg_suffix=e.sub(/([0-9])?(u?(i|s|b)|h|f|d)v?$/,'-\\0').split('-')
37       stems[stem]||={}
38       if arg_suffix && arg_suffix!=''
39         stems[stem][arg_suffix]||=0
40         stems[stem][arg_suffix]+=1
41       end
42       #pp [orig,stem,arg_suffix||'',vendor_suffix]
43       [orig,stem,arg_suffix||'',vendor_suffix]
44     end
45     split.collect do |orig,e,arg_suffix,vendor_suffix|
46       shared_stem=stems[e].keys.length > 1
47       e_split=(shared_stem ? e : e+arg_suffix).gsub(/([a-z])([A-Z0-9])/,'\\1-\\2').split('-')
48       [orig,*e_split].concat([shared_stem ? arg_suffix : '',vendor_suffix])
49     end
50   end
51 end