Skip to content

Commit

Permalink
add NSString#encode which returns a properly encoded RubyString, and …
Browse files Browse the repository at this point in the history
…NSString#encode! which raises an exception
  • Loading branch information
Laurent Sansonetti committed Jun 6, 2011
1 parent 3e200e8 commit d178850
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 10 deletions.
8 changes: 0 additions & 8 deletions NSString.m
Original file line number Diff line number Diff line change
Expand Up @@ -180,14 +180,6 @@
return range.location == NSNotFound ? Qfalse : Qtrue;
}

static id
rstr_only(id rcv, SEL sel)
{
rb_raise(rb_eArgError, "method `%s' does not work on NSStrings",
sel_getName(sel));
return rcv; // never reached
}

static VALUE
nsstr_to_rstr(id nsstr)
{
Expand Down
8 changes: 8 additions & 0 deletions encoding.h
Original file line number Diff line number Diff line change
Expand Up @@ -383,6 +383,14 @@ VALUE rstr_new_path(const char *path);
const char *nsstr_cstr(VALUE str);
long nsstr_clen(VALUE str);

static inline id
rstr_only(id rcv, SEL sel)
{
rb_raise(rb_eArgError, "method `%s' does not work on NSStrings",
sel_getName(sel));
return rcv; // never reached
}

#if defined(__cplusplus)
} // extern "C"
#endif
Expand Down
7 changes: 5 additions & 2 deletions transcode.c
Original file line number Diff line number Diff line change
Expand Up @@ -313,7 +313,7 @@ rstr_encode(VALUE str, SEL sel, int argc, VALUE *argv)
}
}

rb_str_t *self = RSTR(str);
rb_str_t *self = str_need_string(str);
rb_str_t *replacement_str = NULL;
rb_encoding_t *src_encoding, *dst_encoding;
transcode_behavior_t behavior_for_invalid = TRANSCODE_BEHAVIOR_RAISE_EXCEPTION;
Expand Down Expand Up @@ -381,8 +381,11 @@ rstr_encode_bang(VALUE str, SEL sel, int argc, VALUE *argv)
void
Init_Transcode(void)
{
rb_objc_define_method(rb_cRubyString, "encode", rstr_encode, -1);
// #encode works on both NSStrings and RubyStrings, #encode! only works
// on RubyStrings.
rb_objc_define_method(rb_cNSString, "encode", rstr_encode, -1);
rb_objc_define_method(rb_cRubyString, "encode!", rstr_encode_bang, -1);
rb_objc_define_method(rb_cNSString, "encode!", rstr_only, -1);

rb_cEncodingConverter = rb_define_class_under(rb_cEncoding, "Converter", rb_cObject);
rb_objc_define_method(*(VALUE *)rb_cEncodingConverter, "alloc", rb_econv_alloc, 0);
Expand Down

0 comments on commit d178850

Please sign in to comment.