From 60b4397f51894d5e679a6eed73a8cde957f03c4a Mon Sep 17 00:00:00 2001 From: Eric Wong Date: Sun, 2 Aug 2009 16:16:28 -0700 Subject: http: generic C string vs VALUEs comparison function 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. --- ext/unicorn_http/ext_help.h | 8 ++++++++ 1 file changed, 8 insertions(+) (limited to 'ext/unicorn_http/ext_help.h') 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 -- cgit v1.2.3-24-ge0c7