about summary refs log tree commit homepage
path: root/ext/unicorn_http/ext_help.h
diff options
context:
space:
mode:
authorEric Wong <normalperson@yhbt.net>2009-08-02 16:16:28 -0700
committerEric Wong <normalperson@yhbt.net>2009-08-09 01:29:40 -0700
commit60b4397f51894d5e679a6eed73a8cde957f03c4a (patch)
tree9da48622b713dd0f3913f83a83a4c0e420f936ff /ext/unicorn_http/ext_help.h
parent074dc90aaefc17dd5a60c3f67187305924849b05 (diff)
downloadunicorn-60b4397f51894d5e679a6eed73a8cde957f03c4a.tar.gz
The macro version lets us painlessly compare Ruby strings
against constant C strings.  It wraps a C version of the
function which is necessary to avoid double evaluation while
preventing the user from having to type sizeof or strlen.
Diffstat (limited to 'ext/unicorn_http/ext_help.h')
-rw-r--r--ext/unicorn_http/ext_help.h8
1 files changed, 8 insertions, 0 deletions
diff --git a/ext/unicorn_http/ext_help.h b/ext/unicorn_http/ext_help.h
index fec4f41..19f08c9 100644
--- a/ext/unicorn_http/ext_help.h
+++ b/ext/unicorn_http/ext_help.h
@@ -18,4 +18,12 @@ static void rb_18_str_set_len(VALUE str, long len)
 #  define rb_str_set_len(str,len) rb_18_str_set_len(str,len)
 #endif
 
+static inline int str_cstr_eq(VALUE val, const char *ptr, size_t len)
+{
+  return (RSTRING_LEN(val) == len && !memcmp(ptr, RSTRING_PTR(val), len));
+}
+
+#define STR_CSTR_EQ(val, const_str) \
+  str_cstr_eq(val, const_str, sizeof(const_str) - 1)
+
 #endif