summaryrefslogtreecommitdiff
path: root/spec/ruby/optional/capi/ext/string_spec.c
diff options
context:
space:
mode:
Diffstat (limited to 'spec/ruby/optional/capi/ext/string_spec.c')
-rw-r--r--spec/ruby/optional/capi/ext/string_spec.c150
1 files changed, 135 insertions, 15 deletions
diff --git a/spec/ruby/optional/capi/ext/string_spec.c b/spec/ruby/optional/capi/ext/string_spec.c
index dd7bc397cd..cec3f65f45 100644
--- a/spec/ruby/optional/capi/ext/string_spec.c
+++ b/spec/ruby/optional/capi/ext/string_spec.c
@@ -1,8 +1,10 @@
#include "ruby.h"
#include "rubyspec.h"
+#include <fcntl.h>
#include <string.h>
#include <stdarg.h>
+#include <errno.h>
#include "ruby/encoding.h"
@@ -49,18 +51,12 @@ VALUE string_spec_rb_str_set_len_RSTRING_LEN(VALUE self, VALUE str, VALUE len) {
return INT2FIX(RSTRING_LEN(str));
}
-VALUE rb_fstring(VALUE str); /* internal.h, used in ripper */
-
-VALUE string_spec_rb_str_fstring(VALUE self, VALUE str) {
- return rb_fstring(str);
-}
-
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));
}
@@ -85,6 +81,10 @@ 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));
@@ -123,7 +123,7 @@ VALUE string_spec_rb_str_conv_enc(VALUE self, VALUE str, VALUE from, VALUE to) {
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,14 +133,13 @@ VALUE string_spec_rb_str_conv_enc(VALUE self, VALUE str, VALUE from, VALUE to) {
}
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);
@@ -194,7 +193,7 @@ VALUE string_spec_rb_str_new_offset(VALUE self, VALUE str, VALUE offset, VALUE l
}
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));
@@ -210,7 +209,7 @@ VALUE string_spec_rb_str_export_to_enc(VALUE self, VALUE str, VALUE 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));
@@ -249,6 +248,7 @@ VALUE string_spec_rb_str_new5(VALUE self, VALUE str, VALUE ptr, VALUE len) {
return rb_str_new5(str, RSTRING_PTR(ptr), FIX2INT(len));
}
+#ifndef RUBY_VERSION_IS_3_2
VALUE string_spec_rb_tainted_str_new(VALUE self, VALUE str, VALUE len) {
return rb_tainted_str_new(RSTRING_PTR(str), FIX2INT(len));
}
@@ -256,6 +256,7 @@ VALUE string_spec_rb_tainted_str_new(VALUE self, VALUE str, VALUE len) {
VALUE string_spec_rb_tainted_str_new2(VALUE self, VALUE str) {
return rb_tainted_str_new2(RSTRING_PTR(str));
}
+#endif
VALUE string_spec_rb_str_plus(VALUE self, VALUE str1, VALUE str2) {
return rb_str_plus(str1, str2);
@@ -279,6 +280,16 @@ VALUE string_spec_rb_str_resize_RSTRING_LEN(VALUE self, VALUE str, VALUE size) {
return INT2FIX(RSTRING_LEN(modified));
}
+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, ",");
}
@@ -354,7 +365,7 @@ VALUE string_spec_RSTRING_PTR_set(VALUE self, VALUE str, VALUE i, VALUE chr) {
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);
}
@@ -374,6 +385,39 @@ VALUE string_spec_RSTRING_PTR_after_yield(VALUE self, VALUE str) {
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_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);
}
@@ -414,6 +458,33 @@ 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;
@@ -476,11 +547,44 @@ static VALUE string_spec_rb_utf8_str_new_cstr(VALUE self) {
return rb_utf8_str_new_cstr("nokogiri");
}
+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;
+}
+
+static VALUE string_spec_rb_str_vcatf(VALUE self, VALUE mesg) {
+ return call_rb_str_vcatf(mesg, "fmt %d %d number", 42, 7);
+}
+
+static VALUE string_spec_rb_str_catf(VALUE self, VALUE mesg) {
+ return rb_str_catf(mesg, "fmt %d %d number", 41, 6);
+}
+
+static VALUE string_spec_rb_str_locktmp(VALUE self, VALUE str) {
+ return rb_str_locktmp(str);
+}
+
+static VALUE string_spec_rb_str_unlocktmp(VALUE self, VALUE str) {
+ return rb_str_unlocktmp(str);
+}
+
+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);
+}
+
+static VALUE string_spec_rb_str_to_interned_str(VALUE self, VALUE str) {
+ return rb_str_to_interned_str(str);
+}
+
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_fstring", string_spec_rb_str_fstring, 1);
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);
@@ -488,6 +592,7 @@ void Init_string_spec(void) {
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);
@@ -520,13 +625,16 @@ void Init_string_spec(void) {
rb_define_method(cls, "rb_str_new3", string_spec_rb_str_new3, 1);
rb_define_method(cls, "rb_str_new4", string_spec_rb_str_new4, 1);
rb_define_method(cls, "rb_str_new5", string_spec_rb_str_new5, 3);
+#ifndef RUBY_VERSION_IS_3_2
rb_define_method(cls, "rb_tainted_str_new", string_spec_rb_tainted_str_new, 2);
rb_define_method(cls, "rb_tainted_str_new2", string_spec_rb_tainted_str_new2, 1);
+#endif
rb_define_method(cls, "rb_str_plus", string_spec_rb_str_plus, 2);
rb_define_method(cls, "rb_str_times", string_spec_rb_str_times, 2);
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);
+ 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);
rb_define_method(cls, "rb_str_split", string_spec_rb_str_split, 1);
@@ -542,6 +650,8 @@ void Init_string_spec(void) {
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);
rb_define_method(cls, "SafeStringValue", string_spec_SafeStringValue, 1);
rb_define_method(cls, "rb_str_hash", string_spec_rb_str_hash, 1);
@@ -551,6 +661,10 @@ void Init_string_spec(void) {
rb_define_method(cls, "rb_sprintf2", string_spec_rb_sprintf2, 3);
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);
rb_define_method(cls, "rb_str_equal", string_spec_rb_str_equal, 2);
rb_define_method(cls, "rb_usascii_str_new", string_spec_rb_usascii_str_new, 2);
@@ -563,6 +677,12 @@ void Init_string_spec(void) {
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_str_to_interned_str", string_spec_rb_str_to_interned_str, 1);
}
#ifdef __cplusplus