summaryrefslogtreecommitdiff
path: root/ext/socket/mkconstants.rb
diff options
context:
space:
mode:
authorakr <akr@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2009-01-01 08:03:36 +0000
committerakr <akr@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2009-01-01 08:03:36 +0000
commit7454eab7013e276282c9bc282b149242b891b334 (patch)
treef860c4fb831f9b9afba8a4e10f47378fe1d1a7a6 /ext/socket/mkconstants.rb
parent0c838b4947a36ea0f706dfbd131b92de9a7b18a3 (diff)
* ext/socket/mkconstants.rb: refine family_to_int.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@21241 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
Diffstat (limited to 'ext/socket/mkconstants.rb')
-rw-r--r--ext/socket/mkconstants.rb29
1 files changed, 24 insertions, 5 deletions
diff --git a/ext/socket/mkconstants.rb b/ext/socket/mkconstants.rb
index 64743d7..e11c9c3 100644
--- a/ext/socket/mkconstants.rb
+++ b/ext/socket/mkconstants.rb
@@ -64,6 +64,17 @@ def each_name(pat)
}
end
+def each_names_with_len(pat)
+ h = {}
+ DEFS.each {|name, default_value|
+ next if pat !~ name
+ (h[name.length] ||= []) << name
+ }
+ h.keys.sort.each {|len|
+ yield h[len], len
+ }
+end
+
result << ERB.new(<<'EOS', nil, '%').result(binding)
static void
init_constants(VALUE mConst)
@@ -89,13 +100,21 @@ init_constants(VALUE mConst)
static int
family_to_int(char *str, int len)
{
-% each_name(/\A[AP]F_/) {|name|
+ switch (len) {
+% each_names_with_len(/\A[AP]F_/) {|names, len|
+ case <%=len%>:
+% names.each {|name|
#ifdef <%=name%>
-% size = name.bytesize
- if (len == <%=size%> && memcmp(str, <%=c_str name%>, <%=size%>) == 0) return <%=name%>;
+% size = name.bytesize
+ if (memcmp(str, <%=c_str name%>, <%=size%>) == 0) return <%=name%>;
#endif
-% }
- return -1;
+% }
+ return -1;
+
+% }
+ default:
+ return -1;
+ }
}
EOS