Skip to content

Commit

Permalink
Fix strlen.
Browse files Browse the repository at this point in the history
  • Loading branch information
aardvark179 committed Mar 20, 2017
1 parent 578dc1f commit cea9205
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 4 deletions.
2 changes: 0 additions & 2 deletions spec/tags/optional/capi/string_tags.txt
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
fails:C-API String function rb_str_buf_new returns a string whose bytes can be accessed by RSTRING_PTR
fails:C-API String function rb_str_buf_new returns a string that can be modified by rb_str_set_len
fails:C-API String function rb_str_new2 returns a new string object calling strlen on the passed C string
fails:C-API String function rb_str_new2 encodes the string with ASCII_8BIT
fails:C-API String function rb_str_new_cstr returns a new string object calling strlen on the passed C string
fails:C-API String function rb_str_new_cstr encodes the string with ASCII_8BIT
fails:C-API String function rb_str_subseq returns a byte-indexed substring
fails:C-API String function StringValue does not call #to_s on non-String objects
Expand Down
4 changes: 3 additions & 1 deletion truffleruby/src/main/c/cext/ruby.c
Original file line number Diff line number Diff line change
Expand Up @@ -876,7 +876,9 @@ VALUE rb_str_new(const char *string, long length) {

VALUE rb_str_new_cstr(const char *string) {
if (truffle_is_truffle_object((VALUE) string)) {
return (VALUE) truffle_invoke(RUBY_CEXT, "to_ruby_string", string);
VALUE ruby_string = (VALUE) truffle_invoke(RUBY_CEXT, "to_ruby_string", string);
int len = strlen(string);
return (VALUE) truffle_invoke(ruby_string, "[]", 0, len);
} else {
return (VALUE) truffle_invoke(RUBY_CEXT, "rb_str_new_cstr", truffle_read_string(string));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,14 @@ protected Object access(StringCharPointerAdapter object) {
public static abstract class CharPointerGetSizeNode extends Node {

protected Object access(StringCharPointerAdapter stringCharPointerAdapter) {
return rope(stringCharPointerAdapter.getString()).byteLength();
byte[] bytes = rope(stringCharPointerAdapter.getString()).getBytes();
int i = 0;
for (;i < bytes.length; i++) {
if (bytes[i] == 0) {
break;
}
}
return i;
}

}
Expand Down

0 comments on commit cea9205

Please sign in to comment.