0968080d4726acfdc1aeef4e073850b06980a754
[unicorn.git] / ext / unicorn_http / ext_help.h
blob0968080d4726acfdc1aeef4e073850b06980a754
1 #ifndef ext_help_h
2 #define ext_help_h
4 #ifndef RSTRING_PTR
5 #define RSTRING_PTR(s) (RSTRING(s)->ptr)
6 #endif /* !defined(RSTRING_PTR) */
7 #ifndef RSTRING_LEN
8 #define RSTRING_LEN(s) (RSTRING(s)->len)
9 #endif /* !defined(RSTRING_LEN) */
11 #ifndef HAVE_RB_STR_SET_LEN
12 # ifdef RUBINIUS
13 # error we should never get here with current Rubinius (1.x)
14 # endif
15 /* this is taken from Ruby 1.8.7, 1.8.6 may not have it */
16 static void rb_18_str_set_len(VALUE str, long len)
18 RSTRING(str)->len = len;
19 RSTRING(str)->ptr[len] = '\0';
21 # define rb_str_set_len(str,len) rb_18_str_set_len(str,len)
22 #endif /* !defined(HAVE_RB_STR_SET_LEN) */
24 /* not all Ruby implementations support frozen objects (Rubinius does not) */
25 #if defined(OBJ_FROZEN)
26 # define assert_frozen(f) assert(OBJ_FROZEN(f) && "unfrozen object")
27 #else
28 # define assert_frozen(f) do {} while (0)
29 #endif /* !defined(OBJ_FROZEN) */
31 #if !defined(OFFT2NUM)
32 # if SIZEOF_OFF_T == SIZEOF_LONG
33 # define OFFT2NUM(n) LONG2NUM(n)
34 # else
35 # define OFFT2NUM(n) LL2NUM(n)
36 # endif
37 #endif /* ! defined(OFFT2NUM) */
39 #if !defined(SIZET2NUM)
40 # if SIZEOF_SIZE_T == SIZEOF_LONG
41 # define SIZET2NUM(n) ULONG2NUM(n)
42 # else
43 # define SIZET2NUM(n) ULL2NUM(n)
44 # endif
45 #endif /* ! defined(SIZET2NUM) */
47 #if !defined(NUM2SIZET)
48 # if SIZEOF_SIZE_T == SIZEOF_LONG
49 # define NUM2SIZET(n) ((size_t)NUM2ULONG(n))
50 # else
51 # define NUM2SIZET(n) ((size_t)NUM2ULL(n))
52 # endif
53 #endif /* ! defined(NUM2SIZET) */
55 #ifndef HAVE_RB_STR_MODIFY
56 # define rb_str_modify(x) do {} while (0)
57 #endif /* ! defined(HAVE_RB_STR_MODIFY) */
59 static inline int str_cstr_eq(VALUE val, const char *ptr, long len)
61 return (RSTRING_LEN(val) == len && !memcmp(ptr, RSTRING_PTR(val), len));
64 #define STR_CSTR_EQ(val, const_str) \
65 str_cstr_eq(val, const_str, sizeof(const_str) - 1)
67 /* strcasecmp isn't locale independent */
68 static int str_cstr_case_eq(VALUE val, const char *ptr, long len)
70 if (RSTRING_LEN(val) == len) {
71 const char *v = RSTRING_PTR(val);
73 for (; len--; ++ptr, ++v) {
74 if ((*ptr == *v) || (*v >= 'A' && *v <= 'Z' && (*v | 0x20) == *ptr))
75 continue;
76 return 0;
78 return 1;
80 return 0;
83 #define STR_CSTR_CASE_EQ(val, const_str) \
84 str_cstr_case_eq(val, const_str, sizeof(const_str) - 1)
86 #endif /* ext_help_h */