diff options
Diffstat (limited to 'spec/ruby/optional/capi/ext/string_spec.c')
| -rw-r--r-- | spec/ruby/optional/capi/ext/string_spec.c | 659 |
1 files changed, 349 insertions, 310 deletions
diff --git a/spec/ruby/optional/capi/ext/string_spec.c b/spec/ruby/optional/capi/ext/string_spec.c index 929d69f9e5..8291b73ea4 100644 --- a/spec/ruby/optional/capi/ext/string_spec.c +++ b/spec/ruby/optional/capi/ext/string_spec.c @@ -1,45 +1,51 @@ #include "ruby.h" #include "rubyspec.h" +#include <fcntl.h> #include <string.h> #include <stdarg.h> +#include <errno.h> -#ifdef HAVE_RUBY_ENCODING_H #include "ruby/encoding.h" -#endif #ifdef __cplusplus extern "C" { #endif -#ifdef HAVE_RB_CSTR2INUM +#ifdef PTR2NUM +#elif SIZEOF_VOIDP <= SIZEOF_LONG +# define PTR2NUM(x) (LONG2NUM((long)(x))) +#elif SIZEOF_VOIDP <= SIZEOF_LONG_LONG +# define PTR2NUM(x) (LL2NUM((LONG_LONG)(x))) +#endif + +/* Make sure the RSTRING_PTR and the bytes are in native memory. + * On TruffleRuby RSTRING_PTR and the bytes remain in managed memory + * until they must be written to native memory. + * In some specs we want to test using the native memory. */ +#ifndef NATIVE_RSTRING_PTR +#define NATIVE_RSTRING_PTR(str) RSTRING_PTR(str) +#endif + VALUE string_spec_rb_cstr2inum(VALUE self, VALUE str, VALUE inum) { int num = FIX2INT(inum); return rb_cstr2inum(RSTRING_PTR(str), num); } -#endif -#ifdef HAVE_RB_CSTR_TO_INUM static VALUE string_spec_rb_cstr_to_inum(VALUE self, VALUE str, VALUE inum, VALUE badcheck) { int num = FIX2INT(inum); return rb_cstr_to_inum(RSTRING_PTR(str), num, RTEST(badcheck)); } -#endif -#ifdef HAVE_RB_STR2INUM VALUE string_spec_rb_str2inum(VALUE self, VALUE str, VALUE inum) { int num = FIX2INT(inum); return rb_str2inum(str, num); } -#endif -#ifdef HAVE_RB_STR_APPEND VALUE string_spec_rb_str_append(VALUE self, VALUE str, VALUE str2) { return rb_str_append(str, str2); } -#endif -#ifdef HAVE_RB_STR_SET_LEN VALUE string_spec_rb_str_set_len(VALUE self, VALUE str, VALUE len) { rb_str_set_len(str, NUM2LONG(len)); @@ -51,62 +57,84 @@ VALUE string_spec_rb_str_set_len_RSTRING_LEN(VALUE self, VALUE str, VALUE len) { return INT2FIX(RSTRING_LEN(str)); } -#endif -#ifdef HAVE_RB_STR_BUF_NEW VALUE string_spec_rb_str_buf_new(VALUE self, VALUE len, VALUE str) { VALUE buf; buf = rb_str_buf_new(NUM2LONG(len)); - if(RTEST(str)) { + if (RTEST(str)) { snprintf(RSTRING_PTR(buf), NUM2LONG(len), "%s", RSTRING_PTR(str)); } return buf; } -#endif -#ifdef HAVE_RB_STR_BUF_NEW2 +VALUE string_spec_rb_str_capacity(VALUE self, VALUE str) { + return SIZET2NUM(rb_str_capacity(str)); +} + VALUE string_spec_rb_str_buf_new2(VALUE self) { return rb_str_buf_new2("hello\0invisible"); } -#endif -#ifdef HAVE_RB_STR_BUF_CAT +VALUE string_spec_rb_str_tmp_new(VALUE self, VALUE len) { + VALUE str = rb_str_tmp_new(NUM2LONG(len)); + rb_obj_reveal(str, rb_cString); + return str; +} + +VALUE string_spec_rb_str_tmp_new_klass(VALUE self, VALUE len) { + return RBASIC_CLASS(rb_str_tmp_new(NUM2LONG(len))); +} + +VALUE string_spec_rb_str_buf_append(VALUE self, VALUE str, VALUE two) { + return rb_str_buf_append(str, two); +} + VALUE string_spec_rb_str_buf_cat(VALUE self, VALUE str) { const char *question_mark = "?"; rb_str_buf_cat(str, question_mark, strlen(question_mark)); return str; } -#endif -#ifdef HAVE_RB_STR_CAT +VALUE string_spec_rb_enc_str_buf_cat(VALUE self, VALUE str, VALUE other, VALUE encoding) { + char *cstr = StringValueCStr(other); + rb_encoding* enc = rb_to_encoding(encoding); + return rb_enc_str_buf_cat(str, cstr, strlen(cstr), enc); +} + VALUE string_spec_rb_str_cat(VALUE self, VALUE str) { return rb_str_cat(str, "?", 1); } -#endif -#ifdef HAVE_RB_STR_CAT2 VALUE string_spec_rb_str_cat2(VALUE self, VALUE str) { return rb_str_cat2(str, "?"); } -#endif -#ifdef HAVE_RB_STR_CMP +VALUE string_spec_rb_str_cat_cstr(VALUE self, VALUE str, VALUE other) { + return rb_str_cat_cstr(str, StringValueCStr(other)); +} + +VALUE string_spec_rb_str_cat_cstr_constant(VALUE self, VALUE str) { + return rb_str_cat_cstr(str, "?"); +} + VALUE string_spec_rb_str_cmp(VALUE self, VALUE str1, VALUE str2) { return INT2NUM(rb_str_cmp(str1, str2)); } -#endif -#ifdef HAVE_RB_STR_CONV_ENC +VALUE string_spec_rb_str_strlen(VALUE self, VALUE str) { + return LONG2NUM(rb_str_strlen(str)); +} + VALUE string_spec_rb_str_conv_enc(VALUE self, VALUE str, VALUE from, VALUE to) { rb_encoding* from_enc; rb_encoding* to_enc; from_enc = rb_to_encoding(from); - if(NIL_P(to)) { + if (NIL_P(to)) { to_enc = 0; } else { to_enc = rb_to_encoding(to); @@ -114,18 +142,15 @@ VALUE string_spec_rb_str_conv_enc(VALUE self, VALUE str, VALUE from, VALUE to) { return rb_str_conv_enc(str, from_enc, to_enc); } -#endif -#ifdef HAVE_RB_STR_CONV_ENC_OPTS VALUE string_spec_rb_str_conv_enc_opts(VALUE self, VALUE str, VALUE from, VALUE to, - VALUE ecflags, VALUE ecopts) -{ + VALUE ecflags, VALUE ecopts) { rb_encoding* from_enc; rb_encoding* to_enc; from_enc = rb_to_encoding(from); - if(NIL_P(to)) { + if (NIL_P(to)) { to_enc = 0; } else { to_enc = rb_to_encoding(to); @@ -133,159 +158,120 @@ VALUE string_spec_rb_str_conv_enc_opts(VALUE self, VALUE str, VALUE from, VALUE return rb_str_conv_enc_opts(str, from_enc, to_enc, FIX2INT(ecflags), ecopts); } -#endif -#ifdef HAVE_RB_STR_EXPORT +VALUE string_spec_rb_str_drop_bytes(VALUE self, VALUE str, VALUE len) { + return rb_str_drop_bytes(str, NUM2LONG(len)); +} + VALUE string_spec_rb_str_export(VALUE self, VALUE str) { return rb_str_export(str); } -#endif -#ifdef HAVE_RB_STR_EXPORT_LOCALE VALUE string_spec_rb_str_export_locale(VALUE self, VALUE str) { return rb_str_export_locale(str); } -#endif -#ifdef HAVE_RB_STR_DUP VALUE string_spec_rb_str_dup(VALUE self, VALUE str) { return rb_str_dup(str); } -#endif -#ifdef HAVE_RB_STR_FREEZE VALUE string_spec_rb_str_freeze(VALUE self, VALUE str) { return rb_str_freeze(str); } -#endif -#ifdef HAVE_RB_STR_INSPECT VALUE string_spec_rb_str_inspect(VALUE self, VALUE str) { return rb_str_inspect(str); } -#endif -#ifdef HAVE_RB_STR_INTERN VALUE string_spec_rb_str_intern(VALUE self, VALUE str) { return rb_str_intern(str); } -#endif -#ifdef HAVE_RB_STR_LENGTH VALUE string_spec_rb_str_length(VALUE self, VALUE str) { return rb_str_length(str); } -#endif -#ifdef HAVE_RB_STR_NEW VALUE string_spec_rb_str_new(VALUE self, VALUE str, VALUE len) { return rb_str_new(RSTRING_PTR(str), FIX2INT(len)); } +VALUE string_spec_rb_str_new_native(VALUE self, VALUE str, VALUE len) { + return rb_str_new(NATIVE_RSTRING_PTR(str), FIX2INT(len)); +} + VALUE string_spec_rb_str_new_offset(VALUE self, VALUE str, VALUE offset, VALUE len) { return rb_str_new(RSTRING_PTR(str) + FIX2INT(offset), FIX2INT(len)); } -#endif -#ifdef HAVE_RB_STR_NEW2 VALUE string_spec_rb_str_new2(VALUE self, VALUE str) { - if(NIL_P(str)) { + if (NIL_P(str)) { return rb_str_new2(""); } else { return rb_str_new2(RSTRING_PTR(str)); } } -#endif -#ifdef HAVE_RB_STR_ENCODE VALUE string_spec_rb_str_encode(VALUE self, VALUE str, VALUE enc, VALUE flags, VALUE opts) { return rb_str_encode(str, enc, FIX2INT(flags), opts); } -#endif -#ifdef HAVE_RB_STR_NEW_CSTR +VALUE string_spec_rb_str_export_to_enc(VALUE self, VALUE str, VALUE enc) { + return rb_str_export_to_enc(str, rb_to_encoding(enc)); +} + VALUE string_spec_rb_str_new_cstr(VALUE self, VALUE str) { - if(NIL_P(str)) { + if (NIL_P(str)) { return rb_str_new_cstr(""); } else { return rb_str_new_cstr(RSTRING_PTR(str)); } } -#endif -#ifdef HAVE_RB_EXTERNAL_STR_NEW VALUE string_spec_rb_external_str_new(VALUE self, VALUE str) { return rb_external_str_new(RSTRING_PTR(str), RSTRING_LEN(str)); } -#endif -#ifdef HAVE_RB_EXTERNAL_STR_NEW_CSTR VALUE string_spec_rb_external_str_new_cstr(VALUE self, VALUE str) { return rb_external_str_new_cstr(RSTRING_PTR(str)); } -#endif -#ifdef HAVE_RB_EXTERNAL_STR_NEW_WITH_ENC VALUE string_spec_rb_external_str_new_with_enc(VALUE self, VALUE str, VALUE len, VALUE encoding) { return rb_external_str_new_with_enc(RSTRING_PTR(str), FIX2LONG(len), rb_to_encoding(encoding)); } -#endif -#ifdef HAVE_RB_LOCALE_STR_NEW VALUE string_spec_rb_locale_str_new(VALUE self, VALUE str, VALUE len) { return rb_locale_str_new(RSTRING_PTR(str), FIX2INT(len)); } -#endif -#ifdef HAVE_RB_LOCALE_STR_NEW_CSTR VALUE string_spec_rb_locale_str_new_cstr(VALUE self, VALUE str) { return rb_locale_str_new_cstr(RSTRING_PTR(str)); } -#endif -#ifdef HAVE_RB_STR_NEW3 VALUE string_spec_rb_str_new3(VALUE self, VALUE str) { return rb_str_new3(str); } -#endif -#ifdef HAVE_RB_STR_NEW4 VALUE string_spec_rb_str_new4(VALUE self, VALUE str) { return rb_str_new4(str); } -#endif -#ifdef HAVE_RB_STR_NEW5 VALUE string_spec_rb_str_new5(VALUE self, VALUE str, VALUE ptr, VALUE len) { return rb_str_new5(str, RSTRING_PTR(ptr), FIX2INT(len)); } -#endif - -#ifdef HAVE_RB_TAINTED_STR_NEW -VALUE string_spec_rb_tainted_str_new(VALUE self, VALUE str, VALUE len) { - return rb_tainted_str_new(RSTRING_PTR(str), FIX2INT(len)); -} -#endif - -#ifdef HAVE_RB_TAINTED_STR_NEW2 -VALUE string_spec_rb_tainted_str_new2(VALUE self, VALUE str) { - return rb_tainted_str_new2(RSTRING_PTR(str)); -} -#endif -#ifdef HAVE_RB_STR_PLUS VALUE string_spec_rb_str_plus(VALUE self, VALUE str1, VALUE str2) { return rb_str_plus(str1, str2); } -#endif -#ifdef HAVE_RB_STR_TIMES VALUE string_spec_rb_str_times(VALUE self, VALUE str, VALUE times) { return rb_str_times(str, times); } -#endif -#ifdef HAVE_RB_STR_RESIZE +VALUE string_spec_rb_str_modify_expand(VALUE self, VALUE str, VALUE size) { + rb_str_modify_expand(str, FIX2LONG(size)); + return str; +} + VALUE string_spec_rb_str_resize(VALUE self, VALUE str, VALUE size) { return rb_str_resize(str, FIX2INT(size)); } @@ -294,45 +280,66 @@ VALUE string_spec_rb_str_resize_RSTRING_LEN(VALUE self, VALUE str, VALUE size) { VALUE modified = rb_str_resize(str, FIX2INT(size)); return INT2FIX(RSTRING_LEN(modified)); } -#endif -#ifdef HAVE_RB_STR_SPLIT +VALUE string_spec_rb_str_resize_copy(VALUE self, VALUE str) { + rb_str_modify_expand(str, 5); + char *buffer = RSTRING_PTR(str); + buffer[1] = 'e'; + buffer[2] = 's'; + buffer[3] = 't'; + rb_str_resize(str, 4); + return str; +} + VALUE string_spec_rb_str_split(VALUE self, VALUE str) { return rb_str_split(str, ","); } -#endif -#ifdef HAVE_RB_STR_SUBSEQ VALUE string_spec_rb_str_subseq(VALUE self, VALUE str, VALUE beg, VALUE len) { return rb_str_subseq(str, FIX2INT(beg), FIX2INT(len)); } -#endif -#ifdef HAVE_RB_STR_SUBSTR VALUE string_spec_rb_str_substr(VALUE self, VALUE str, VALUE beg, VALUE len) { return rb_str_substr(str, FIX2INT(beg), FIX2INT(len)); } -#endif -#ifdef HAVE_RB_STR_TO_STR +VALUE string_spec_rb_str_subpos(VALUE self, VALUE str, VALUE beg) { + char* original = RSTRING_PTR(str); + char* end = RSTRING_END(str); + long len = rb_str_strlen(str); + char *p = rb_str_subpos(str, FIX2LONG(beg), &len); + if (p == NULL) { + return Qnil; + } + + if (p >= original && p <= end) { + return rb_ary_new_from_args(2, LONG2FIX(p - RSTRING_PTR(str)), LONG2FIX(len)); + } else { + rb_raise(rb_eRuntimeError, "the returned pointer is not inside the original string buffer"); + } +} + +VALUE string_spec_rb_str_sublen(VALUE self, VALUE str, VALUE pos) { + return LONG2FIX(rb_str_sublen(str, FIX2LONG(pos))); +} + VALUE string_spec_rb_str_to_str(VALUE self, VALUE arg) { return rb_str_to_str(arg); } -#endif -#ifdef HAVE_RSTRING_LEN VALUE string_spec_RSTRING_LEN(VALUE self, VALUE str) { return INT2FIX(RSTRING_LEN(str)); } -#endif -#ifdef HAVE_RSTRING_LENINT VALUE string_spec_RSTRING_LENINT(VALUE self, VALUE str) { return INT2FIX(RSTRING_LENINT(str)); } -#endif -#ifdef HAVE_RSTRING_PTR +VALUE string_spec_RSTRING_PTR(VALUE self, VALUE str) { + char* ptr = RSTRING_PTR(str); + return PTR2NUM(ptr); +} + VALUE string_spec_RSTRING_PTR_iterate(VALUE self, VALUE str) { int i; char* ptr; @@ -344,6 +351,25 @@ VALUE string_spec_RSTRING_PTR_iterate(VALUE self, VALUE str) { return Qnil; } +VALUE string_spec_RSTRING_PTR_iterate_uint32(VALUE self, VALUE str) { + uint32_t* ptr; + long i, l = RSTRING_LEN(str) / sizeof(uint32_t); + + ptr = (uint32_t *)RSTRING_PTR(str); + for(i = 0; i < l; i++) { + rb_yield(UINT2NUM(ptr[i])); + } + return Qnil; +} + +VALUE string_spec_RSTRING_PTR_short_memcpy(VALUE self, VALUE str) { + /* Short memcpy operations may be optimised by the compiler to a single write. */ + if (RSTRING_LEN(str) >= 8) { + memcpy(RSTRING_PTR(str), "Infinity", 8); + } + return str; +} + VALUE string_spec_RSTRING_PTR_assign(VALUE self, VALUE str, VALUE chr) { int i; char c; @@ -358,65 +384,135 @@ VALUE string_spec_RSTRING_PTR_assign(VALUE self, VALUE str, VALUE chr) { return Qnil; } +VALUE string_spec_RSTRING_PTR_set(VALUE self, VALUE str, VALUE i, VALUE chr) { + RSTRING_PTR(str)[FIX2INT(i)] = (char) FIX2INT(chr); + return str; +} + VALUE string_spec_RSTRING_PTR_after_funcall(VALUE self, VALUE str, VALUE cb) { /* Silence gcc 4.3.2 warning about computed value not used */ - if(RSTRING_PTR(str)) { /* force it out */ + if (RSTRING_PTR(str)) { /* force it out */ rb_funcall(cb, rb_intern("call"), 1, str); } return rb_str_new2(RSTRING_PTR(str)); } -#endif -#ifdef HAVE_STRINGVALUE +VALUE string_spec_RSTRING_PTR_after_yield(VALUE self, VALUE str) { + char* ptr = NATIVE_RSTRING_PTR(str); + long len = RSTRING_LEN(str); + VALUE from_rstring_ptr; + + ptr[0] = '1'; + rb_yield(str); + ptr[2] = '2'; + + from_rstring_ptr = rb_str_new(ptr, len); + return from_rstring_ptr; +} + +VALUE string_spec_RSTRING_PTR_read(VALUE self, VALUE str, VALUE path) { + char *cpath = StringValueCStr(path); + int fd = open(cpath, O_RDONLY); + VALUE capacities = rb_ary_new(); + if (fd < 0) { + rb_syserr_fail(errno, "open"); + } + + rb_str_modify_expand(str, 30); + rb_ary_push(capacities, SIZET2NUM(rb_str_capacity(str))); + char *buffer = RSTRING_PTR(str); + if (read(fd, buffer, 30) < 0) { + rb_syserr_fail(errno, "read"); + } + rb_str_set_len(str, 30); + + rb_str_modify_expand(str, 53); + rb_ary_push(capacities, SIZET2NUM(rb_str_capacity(str))); + char *buffer2 = RSTRING_PTR(str); + if (read(fd, buffer2 + 30, 53 - 30) < 0) { + rb_syserr_fail(errno, "read"); + } + + rb_str_set_len(str, 53); + close(fd); + return capacities; +} + +VALUE string_spec_RSTRING_PTR_null_terminate(VALUE self, VALUE str, VALUE min_length) { + char* ptr = RSTRING_PTR(str); + char* end = ptr + RSTRING_LEN(str); + return rb_str_new(end, FIX2LONG(min_length)); +} + VALUE string_spec_StringValue(VALUE self, VALUE str) { return StringValue(str); } -#endif -#ifdef HAVE_SAFE_STRING_VALUE static VALUE string_spec_SafeStringValue(VALUE self, VALUE str) { SafeStringValue(str); return str; } -#endif -#ifdef HAVE_RB_STR_HASH static VALUE string_spec_rb_str_hash(VALUE self, VALUE str) { st_index_t val = rb_str_hash(str); -#if SIZEOF_LONG == SIZEOF_VOIDP || SIZEOF_LONG_LONG == SIZEOF_VOIDP - return LONG2FIX((long)val); -#else -# error unsupported platform -#endif + return ST2FIX(val); } -#endif -#ifdef HAVE_RB_STR_UPDATE static VALUE string_spec_rb_str_update(VALUE self, VALUE str, VALUE beg, VALUE end, VALUE replacement) { rb_str_update(str, FIX2LONG(beg), FIX2LONG(end), replacement); return str; } -#endif -#ifdef HAVE_RB_STR_FREE static VALUE string_spec_rb_str_free(VALUE self, VALUE str) { rb_str_free(str); return Qnil; } -#endif -#ifdef HAVE_RB_SPRINTF static VALUE string_spec_rb_sprintf1(VALUE self, VALUE str, VALUE repl) { return rb_sprintf(RSTRING_PTR(str), RSTRING_PTR(repl)); } + static VALUE string_spec_rb_sprintf2(VALUE self, VALUE str, VALUE repl1, VALUE repl2) { return rb_sprintf(RSTRING_PTR(str), RSTRING_PTR(repl1), RSTRING_PTR(repl2)); } -#endif -#ifdef HAVE_RB_VSPRINTF +static VALUE string_spec_rb_sprintf3(VALUE self, VALUE str) { + return rb_sprintf("Result: %" PRIsVALUE ".", str); +} + +static VALUE string_spec_rb_sprintf4(VALUE self, VALUE str) { + return rb_sprintf("Result: %+" PRIsVALUE ".", str); +} + +static VALUE string_spec_rb_sprintf5(VALUE self, VALUE width, VALUE precision, VALUE str) { + return rb_sprintf("Result: %*.*s.", FIX2INT(width), FIX2INT(precision), RSTRING_PTR(str)); +} + +static VALUE string_spec_rb_sprintf6(VALUE self, VALUE width, VALUE precision, VALUE str) { + return rb_sprintf("Result: %*.*" PRIsVALUE ".", FIX2INT(width), FIX2INT(precision), str); +} + +static VALUE string_spec_rb_sprintf7(VALUE self, VALUE str, VALUE obj) { + VALUE results = rb_ary_new(); + rb_ary_push(results, rb_sprintf(RSTRING_PTR(str), obj)); + char cstr[256]; + int len = snprintf(cstr, 256, RSTRING_PTR(str), obj); + rb_ary_push(results, rb_str_new(cstr, len)); + return results; +} + +static VALUE string_spec_rb_sprintf8(VALUE self, VALUE str, VALUE num) { + VALUE results = rb_ary_new(); + rb_ary_push(results, rb_sprintf(RSTRING_PTR(str), FIX2LONG(num))); + char cstr[256]; + int len = snprintf(cstr, 256, RSTRING_PTR(str), FIX2LONG(num)); + rb_ary_push(results, rb_str_new(cstr, len)); + return results; +} + +PRINTF_ARGS(static VALUE string_spec_rb_vsprintf_worker(char* fmt, ...), 1, 2); static VALUE string_spec_rb_vsprintf_worker(char* fmt, ...) { va_list varargs; VALUE str; @@ -432,265 +528,208 @@ static VALUE string_spec_rb_vsprintf(VALUE self, VALUE fmt, VALUE str, VALUE i, return string_spec_rb_vsprintf_worker(RSTRING_PTR(fmt), RSTRING_PTR(str), FIX2INT(i), RFLOAT_VALUE(f)); } -#endif -#ifdef HAVE_RB_STR_EQUAL VALUE string_spec_rb_str_equal(VALUE self, VALUE str1, VALUE str2) { return rb_str_equal(str1, str2); } -#endif -#ifdef HAVE_RB_USASCII_STR_NEW static VALUE string_spec_rb_usascii_str_new(VALUE self, VALUE str, VALUE len) { return rb_usascii_str_new(RSTRING_PTR(str), NUM2INT(len)); } -#endif -#ifdef HAVE_RB_USASCII_STR_NEW_CSTR +static VALUE string_spec_rb_usascii_str_new_lit(VALUE self) { + return rb_usascii_str_new_lit("nokogiri"); +} + +static VALUE string_spec_rb_usascii_str_new_lit_non_ascii(VALUE self) { + return rb_usascii_str_new_lit("r\xc3\xa9sum\xc3\xa9"); +} + static VALUE string_spec_rb_usascii_str_new_cstr(VALUE self, VALUE str) { return rb_usascii_str_new_cstr(RSTRING_PTR(str)); } -#endif -#ifdef HAVE_RB_STRING static VALUE string_spec_rb_String(VALUE self, VALUE val) { return rb_String(val); } -#endif -void Init_string_spec(void) { - VALUE cls; - cls = rb_define_class("CApiStringSpecs", rb_cObject); +static VALUE string_spec_rb_string_value_cstr(VALUE self, VALUE str) { + char *c_str = rb_string_value_cstr(&str); + return c_str ? Qtrue : Qfalse; +} -#ifdef HAVE_RB_CSTR2INUM - rb_define_method(cls, "rb_cstr2inum", string_spec_rb_cstr2inum, 2); -#endif +static VALUE string_spec_rb_str_modify(VALUE self, VALUE str) { + rb_str_modify(str); + return str; +} -#ifdef HAVE_RB_CSTR_TO_INUM - rb_define_method(cls, "rb_cstr_to_inum", string_spec_rb_cstr_to_inum, 3); -#endif +static VALUE string_spec_rb_utf8_str_new_static(VALUE self) { + const char* literal = "nokogiri"; + return rb_ary_new_from_args(2, + rb_utf8_str_new_static("nokogiri", 8), + PTR2NUM(literal)); +} -#ifdef HAVE_RB_STR2INUM - rb_define_method(cls, "rb_str2inum", string_spec_rb_str2inum, 2); -#endif +static VALUE string_spec_rb_utf8_str_new(VALUE self) { + return rb_utf8_str_new("nokogiri", 8); +} -#ifdef HAVE_RB_STR_APPEND - rb_define_method(cls, "rb_str_append", string_spec_rb_str_append, 2); -#endif +static VALUE string_spec_rb_utf8_str_new_cstr(VALUE self) { + return rb_utf8_str_new_cstr("nokogiri"); +} -#ifdef HAVE_RB_STR_BUF_NEW - rb_define_method(cls, "rb_str_buf_new", string_spec_rb_str_buf_new, 2); -#endif +PRINTF_ARGS(static VALUE call_rb_str_vcatf(VALUE mesg, const char *fmt, ...), 2, 3); +static VALUE call_rb_str_vcatf(VALUE mesg, const char *fmt, ...) { + va_list ap; + va_start(ap, fmt); + VALUE result = rb_str_vcatf(mesg, fmt, ap); + va_end(ap); + return result; +} -#ifdef HAVE_RB_STR_BUF_NEW2 - rb_define_method(cls, "rb_str_buf_new2", string_spec_rb_str_buf_new2, 0); -#endif +static VALUE string_spec_rb_str_vcatf(VALUE self, VALUE mesg) { + return call_rb_str_vcatf(mesg, "fmt %d %d number", 42, 7); +} -#ifdef HAVE_RB_STR_BUF_CAT - rb_define_method(cls, "rb_str_buf_cat", string_spec_rb_str_buf_cat, 1); -#endif +static VALUE string_spec_rb_str_catf(VALUE self, VALUE mesg) { + return rb_str_catf(mesg, "fmt %d %d number", 41, 6); +} -#ifdef HAVE_RB_STR_CAT - rb_define_method(cls, "rb_str_cat", string_spec_rb_str_cat, 1); -#endif +static VALUE string_spec_rb_str_locktmp(VALUE self, VALUE str) { + return rb_str_locktmp(str); +} -#ifdef HAVE_RB_STR_CAT2 - rb_define_method(cls, "rb_str_cat2", string_spec_rb_str_cat2, 1); -#endif +static VALUE string_spec_rb_str_unlocktmp(VALUE self, VALUE str) { + return rb_str_unlocktmp(str); +} -#ifdef HAVE_RB_STR_CMP - rb_define_method(cls, "rb_str_cmp", string_spec_rb_str_cmp, 2); -#endif +static VALUE string_spec_rb_enc_interned_str_cstr(VALUE self, VALUE str, VALUE enc) { + rb_encoding *e = NIL_P(enc) ? 0 : rb_to_encoding(enc); + return rb_enc_interned_str_cstr(RSTRING_PTR(str), e); +} -#ifdef HAVE_RB_STR_CONV_ENC - rb_define_method(cls, "rb_str_conv_enc", string_spec_rb_str_conv_enc, 3); -#endif +static VALUE string_spec_rb_enc_interned_str(VALUE self, VALUE str, VALUE len, VALUE enc) { + rb_encoding *e = NIL_P(enc) ? 0 : rb_to_encoding(enc); + return rb_enc_interned_str(RSTRING_PTR(str), FIX2LONG(len), e); +} -#ifdef HAVE_RB_STR_CONV_ENC_OPTS - rb_define_method(cls, "rb_str_conv_enc_opts", string_spec_rb_str_conv_enc_opts, 5); -#endif +static VALUE string_spec_rb_str_to_interned_str(VALUE self, VALUE str) { + return rb_str_to_interned_str(str); +} -#ifdef HAVE_RB_STR_EXPORT - rb_define_method(cls, "rb_str_export", string_spec_rb_str_export, 1); -#endif +static VALUE string_spec_rb_interned_str(VALUE self, VALUE str, VALUE len) { + return rb_interned_str(RSTRING_PTR(str), FIX2LONG(len)); +} -#ifdef HAVE_RB_STR_EXPORT_LOCALE - rb_define_method(cls, "rb_str_export_locale", string_spec_rb_str_export_locale, 1); -#endif +static VALUE string_spec_rb_interned_str_cstr(VALUE self, VALUE str) { + return rb_interned_str_cstr(RSTRING_PTR(str)); +} -#ifdef HAVE_RB_STR_DUP +void Init_string_spec(void) { + VALUE cls = rb_define_class("CApiStringSpecs", rb_cObject); + rb_define_method(cls, "rb_cstr2inum", string_spec_rb_cstr2inum, 2); + rb_define_method(cls, "rb_cstr_to_inum", string_spec_rb_cstr_to_inum, 3); + rb_define_method(cls, "rb_str2inum", string_spec_rb_str2inum, 2); + rb_define_method(cls, "rb_str_append", string_spec_rb_str_append, 2); + rb_define_method(cls, "rb_str_buf_new", string_spec_rb_str_buf_new, 2); + rb_define_method(cls, "rb_str_capacity", string_spec_rb_str_capacity, 1); + rb_define_method(cls, "rb_str_buf_new2", string_spec_rb_str_buf_new2, 0); + rb_define_method(cls, "rb_str_tmp_new", string_spec_rb_str_tmp_new, 1); + rb_define_method(cls, "rb_str_tmp_new_klass", string_spec_rb_str_tmp_new_klass, 1); + rb_define_method(cls, "rb_str_buf_append", string_spec_rb_str_buf_append, 2); + rb_define_method(cls, "rb_str_buf_cat", string_spec_rb_str_buf_cat, 1); + rb_define_method(cls, "rb_enc_str_buf_cat", string_spec_rb_enc_str_buf_cat, 3); + rb_define_method(cls, "rb_str_cat", string_spec_rb_str_cat, 1); + rb_define_method(cls, "rb_str_cat2", string_spec_rb_str_cat2, 1); + rb_define_method(cls, "rb_str_cat_cstr", string_spec_rb_str_cat_cstr, 2); + rb_define_method(cls, "rb_str_cat_cstr_constant", string_spec_rb_str_cat_cstr_constant, 1); + rb_define_method(cls, "rb_str_cmp", string_spec_rb_str_cmp, 2); + rb_define_method(cls, "rb_str_strlen", string_spec_rb_str_strlen, 1); + rb_define_method(cls, "rb_str_conv_enc", string_spec_rb_str_conv_enc, 3); + rb_define_method(cls, "rb_str_conv_enc_opts", string_spec_rb_str_conv_enc_opts, 5); + rb_define_method(cls, "rb_str_drop_bytes", string_spec_rb_str_drop_bytes, 2); + rb_define_method(cls, "rb_str_export", string_spec_rb_str_export, 1); + rb_define_method(cls, "rb_str_export_locale", string_spec_rb_str_export_locale, 1); rb_define_method(cls, "rb_str_dup", string_spec_rb_str_dup, 1); -#endif - -#ifdef HAVE_RB_STR_FREEZE rb_define_method(cls, "rb_str_freeze", string_spec_rb_str_freeze, 1); -#endif - -#ifdef HAVE_RB_STR_INSPECT rb_define_method(cls, "rb_str_inspect", string_spec_rb_str_inspect, 1); -#endif - -#ifdef HAVE_RB_STR_INTERN rb_define_method(cls, "rb_str_intern", string_spec_rb_str_intern, 1); -#endif - -#ifdef HAVE_RB_STR_LENGTH rb_define_method(cls, "rb_str_length", string_spec_rb_str_length, 1); -#endif - -#ifdef HAVE_RB_STR_NEW rb_define_method(cls, "rb_str_new", string_spec_rb_str_new, 2); + rb_define_method(cls, "rb_str_new_native", string_spec_rb_str_new_native, 2); rb_define_method(cls, "rb_str_new_offset", string_spec_rb_str_new_offset, 3); -#endif - -#ifdef HAVE_RB_STR_NEW2 rb_define_method(cls, "rb_str_new2", string_spec_rb_str_new2, 1); -#endif - -#ifdef HAVE_RB_STR_ENCODE rb_define_method(cls, "rb_str_encode", string_spec_rb_str_encode, 4); -#endif - -#ifdef HAVE_RB_STR_NEW_CSTR + rb_define_method(cls, "rb_str_export_to_enc", string_spec_rb_str_export_to_enc, 2); rb_define_method(cls, "rb_str_new_cstr", string_spec_rb_str_new_cstr, 1); -#endif - -#ifdef HAVE_RB_EXTERNAL_STR_NEW rb_define_method(cls, "rb_external_str_new", string_spec_rb_external_str_new, 1); -#endif - -#ifdef HAVE_RB_EXTERNAL_STR_NEW_CSTR - rb_define_method(cls, "rb_external_str_new_cstr", - string_spec_rb_external_str_new_cstr, 1); -#endif - -#ifdef HAVE_RB_EXTERNAL_STR_NEW_WITH_ENC + rb_define_method(cls, "rb_external_str_new_cstr", string_spec_rb_external_str_new_cstr, 1); rb_define_method(cls, "rb_external_str_new_with_enc", string_spec_rb_external_str_new_with_enc, 3); -#endif - -#ifdef HAVE_RB_LOCALE_STR_NEW rb_define_method(cls, "rb_locale_str_new", string_spec_rb_locale_str_new, 2); -#endif - -#ifdef HAVE_RB_LOCALE_STR_NEW_CSTR rb_define_method(cls, "rb_locale_str_new_cstr", string_spec_rb_locale_str_new_cstr, 1); -#endif - -#ifdef HAVE_RB_STR_NEW3 rb_define_method(cls, "rb_str_new3", string_spec_rb_str_new3, 1); -#endif - -#ifdef HAVE_RB_STR_NEW4 rb_define_method(cls, "rb_str_new4", string_spec_rb_str_new4, 1); -#endif - -#ifdef HAVE_RB_STR_NEW5 rb_define_method(cls, "rb_str_new5", string_spec_rb_str_new5, 3); -#endif - -#ifdef HAVE_RB_TAINTED_STR_NEW - rb_define_method(cls, "rb_tainted_str_new", string_spec_rb_tainted_str_new, 2); -#endif - -#ifdef HAVE_RB_TAINTED_STR_NEW2 - rb_define_method(cls, "rb_tainted_str_new2", string_spec_rb_tainted_str_new2, 1); -#endif - -#ifdef HAVE_RB_STR_PLUS rb_define_method(cls, "rb_str_plus", string_spec_rb_str_plus, 2); -#endif - -#ifdef HAVE_RB_STR_TIMES rb_define_method(cls, "rb_str_times", string_spec_rb_str_times, 2); -#endif - -#ifdef HAVE_RB_STR_RESIZE + rb_define_method(cls, "rb_str_modify_expand", string_spec_rb_str_modify_expand, 2); rb_define_method(cls, "rb_str_resize", string_spec_rb_str_resize, 2); - rb_define_method(cls, "rb_str_resize_RSTRING_LEN", - string_spec_rb_str_resize_RSTRING_LEN, 2); -#endif - -#ifdef HAVE_RB_STR_SET_LEN + rb_define_method(cls, "rb_str_resize_RSTRING_LEN", string_spec_rb_str_resize_RSTRING_LEN, 2); + rb_define_method(cls, "rb_str_resize_copy", string_spec_rb_str_resize_copy, 1); rb_define_method(cls, "rb_str_set_len", string_spec_rb_str_set_len, 2); - rb_define_method(cls, "rb_str_set_len_RSTRING_LEN", - string_spec_rb_str_set_len_RSTRING_LEN, 2); -#endif - -#ifdef HAVE_RB_STR_SPLIT + rb_define_method(cls, "rb_str_set_len_RSTRING_LEN", string_spec_rb_str_set_len_RSTRING_LEN, 2); rb_define_method(cls, "rb_str_split", string_spec_rb_str_split, 1); -#endif - -#ifdef HAVE_RB_STR_SUBSEQ rb_define_method(cls, "rb_str_subseq", string_spec_rb_str_subseq, 3); -#endif - -#ifdef HAVE_RB_STR_SUBSTR rb_define_method(cls, "rb_str_substr", string_spec_rb_str_substr, 3); -#endif - -#ifdef HAVE_RB_STR_TO_STR + rb_define_method(cls, "rb_str_subpos", string_spec_rb_str_subpos, 2); + rb_define_method(cls, "rb_str_sublen", string_spec_rb_str_sublen, 2); rb_define_method(cls, "rb_str_to_str", string_spec_rb_str_to_str, 1); -#endif - -#ifdef HAVE_RSTRING_LEN rb_define_method(cls, "RSTRING_LEN", string_spec_RSTRING_LEN, 1); -#endif - -#ifdef HAVE_RSTRING_LENINT rb_define_method(cls, "RSTRING_LENINT", string_spec_RSTRING_LENINT, 1); -#endif - -#ifdef HAVE_RSTRING_PTR + rb_define_method(cls, "RSTRING_PTR", string_spec_RSTRING_PTR, 1); rb_define_method(cls, "RSTRING_PTR_iterate", string_spec_RSTRING_PTR_iterate, 1); + rb_define_method(cls, "RSTRING_PTR_iterate_uint32", string_spec_RSTRING_PTR_iterate_uint32, 1); + rb_define_method(cls, "RSTRING_PTR_short_memcpy", string_spec_RSTRING_PTR_short_memcpy, 1); rb_define_method(cls, "RSTRING_PTR_assign", string_spec_RSTRING_PTR_assign, 2); - rb_define_method(cls, "RSTRING_PTR_after_funcall", - string_spec_RSTRING_PTR_after_funcall, 2); -#endif - -#ifdef HAVE_STRINGVALUE + rb_define_method(cls, "RSTRING_PTR_set", string_spec_RSTRING_PTR_set, 3); + rb_define_method(cls, "RSTRING_PTR_after_funcall", string_spec_RSTRING_PTR_after_funcall, 2); + rb_define_method(cls, "RSTRING_PTR_after_yield", string_spec_RSTRING_PTR_after_yield, 1); + rb_define_method(cls, "RSTRING_PTR_read", string_spec_RSTRING_PTR_read, 2); + rb_define_method(cls, "RSTRING_PTR_null_terminate", string_spec_RSTRING_PTR_null_terminate, 2); rb_define_method(cls, "StringValue", string_spec_StringValue, 1); -#endif - -#ifdef HAVE_SAFE_STRING_VALUE rb_define_method(cls, "SafeStringValue", string_spec_SafeStringValue, 1); -#endif - -#ifdef HAVE_RB_STR_HASH rb_define_method(cls, "rb_str_hash", string_spec_rb_str_hash, 1); -#endif - -#ifdef HAVE_RB_STR_UPDATE rb_define_method(cls, "rb_str_update", string_spec_rb_str_update, 4); -#endif - -#ifdef HAVE_RB_STR_FREE rb_define_method(cls, "rb_str_free", string_spec_rb_str_free, 1); -#endif - -#ifdef HAVE_RB_SPRINTF rb_define_method(cls, "rb_sprintf1", string_spec_rb_sprintf1, 2); rb_define_method(cls, "rb_sprintf2", string_spec_rb_sprintf2, 3); -#endif - -#ifdef HAVE_RB_VSPRINTF + rb_define_method(cls, "rb_sprintf3", string_spec_rb_sprintf3, 1); + rb_define_method(cls, "rb_sprintf4", string_spec_rb_sprintf4, 1); + rb_define_method(cls, "rb_sprintf5", string_spec_rb_sprintf5, 3); + rb_define_method(cls, "rb_sprintf6", string_spec_rb_sprintf6, 3); + rb_define_method(cls, "rb_sprintf7", string_spec_rb_sprintf7, 2); + rb_define_method(cls, "rb_sprintf8", string_spec_rb_sprintf8, 2); rb_define_method(cls, "rb_vsprintf", string_spec_rb_vsprintf, 4); -#endif - -#ifdef HAVE_RB_STR_EQUAL rb_define_method(cls, "rb_str_equal", string_spec_rb_str_equal, 2); -#endif - -#ifdef HAVE_RB_USASCII_STR_NEW rb_define_method(cls, "rb_usascii_str_new", string_spec_rb_usascii_str_new, 2); -#endif - -#ifdef HAVE_RB_USASCII_STR_NEW_CSTR + rb_define_method(cls, "rb_usascii_str_new_lit", string_spec_rb_usascii_str_new_lit, 0); + rb_define_method(cls, "rb_usascii_str_new_lit_non_ascii", string_spec_rb_usascii_str_new_lit_non_ascii, 0); rb_define_method(cls, "rb_usascii_str_new_cstr", string_spec_rb_usascii_str_new_cstr, 1); -#endif - -#ifdef HAVE_RB_STRING rb_define_method(cls, "rb_String", string_spec_rb_String, 1); -#endif + rb_define_method(cls, "rb_string_value_cstr", string_spec_rb_string_value_cstr, 1); + rb_define_method(cls, "rb_str_modify", string_spec_rb_str_modify, 1); + rb_define_method(cls, "rb_utf8_str_new_static", string_spec_rb_utf8_str_new_static, 0); + rb_define_method(cls, "rb_utf8_str_new", string_spec_rb_utf8_str_new, 0); + rb_define_method(cls, "rb_utf8_str_new_cstr", string_spec_rb_utf8_str_new_cstr, 0); + rb_define_method(cls, "rb_str_vcatf", string_spec_rb_str_vcatf, 1); + rb_define_method(cls, "rb_str_catf", string_spec_rb_str_catf, 1); + rb_define_method(cls, "rb_str_locktmp", string_spec_rb_str_locktmp, 1); + rb_define_method(cls, "rb_str_unlocktmp", string_spec_rb_str_unlocktmp, 1); + rb_define_method(cls, "rb_enc_interned_str_cstr", string_spec_rb_enc_interned_str_cstr, 2); + rb_define_method(cls, "rb_enc_interned_str", string_spec_rb_enc_interned_str, 3); + rb_define_method(cls, "rb_str_to_interned_str", string_spec_rb_str_to_interned_str, 1); + rb_define_method(cls, "rb_interned_str", string_spec_rb_interned_str, 2); + rb_define_method(cls, "rb_interned_str_cstr", string_spec_rb_interned_str_cstr, 1); } #ifdef __cplusplus |
