summaryrefslogtreecommitdiff
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
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
-rw-r--r--ChangeLog4
-rw-r--r--ext/socket/mkconstants.rb29
2 files changed, 28 insertions, 5 deletions
diff --git a/ChangeLog b/ChangeLog
index 89b7b381c5..e711f7c639 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,7 @@
+Thu Jan 1 17:01:50 2009 Tanaka Akira <akr@fsij.org>
+
+ * ext/socket/mkconstants.rb: refine family_to_int.
+
Thu Jan 1 16:48:07 2009 Tanaka Akira <akr@fsij.org>
* ext/socket/mkconstants.rb: generate family_to_int().
diff --git a/ext/socket/mkconstants.rb b/ext/socket/mkconstants.rb
index 64743d723b..e11c9c344a 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