mirror of
https://github.com/ruby/ruby.git
synced 2025-09-18 01:54:00 +02:00

(path_initialize): bypass to_path call for T_STRING. (path_freeze): implemented. * ext/pathname/lib/pathname.rb (Pathname#freeze): removed. git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@28683 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
65 lines
1.4 KiB
C
65 lines
1.4 KiB
C
#include "ruby.h"
|
|
|
|
static VALUE rb_cPathname;
|
|
static ID id_at_path, id_to_path;
|
|
|
|
static VALUE
|
|
get_strpath(VALUE obj)
|
|
{
|
|
VALUE strpath;
|
|
strpath = rb_ivar_get(obj, id_at_path);
|
|
if (TYPE(strpath) != T_STRING)
|
|
rb_raise(rb_eTypeError, "unexpected @path");
|
|
return strpath;
|
|
}
|
|
|
|
static void
|
|
set_strpath(VALUE obj, VALUE val)
|
|
{
|
|
rb_ivar_set(obj, id_at_path, val);
|
|
}
|
|
|
|
/*
|
|
* Create a Pathname object from the given String (or String-like object).
|
|
* If +path+ contains a NUL character (<tt>\0</tt>), an ArgumentError is raised.
|
|
*/
|
|
static VALUE
|
|
path_initialize(VALUE self, VALUE arg)
|
|
{
|
|
VALUE str;
|
|
if (TYPE(arg) == T_STRING) {
|
|
str = arg;
|
|
}
|
|
else {
|
|
str = rb_check_funcall(arg, id_to_path, 0, NULL);
|
|
if (str == Qundef)
|
|
str = arg;
|
|
StringValue(str);
|
|
}
|
|
if (memchr(RSTRING_PTR(str), '\0', RSTRING_LEN(str)))
|
|
rb_raise(rb_eArgError, "pathname contains null byte");
|
|
str = rb_obj_dup(str);
|
|
|
|
set_strpath(self, str);
|
|
OBJ_INFECT(self, str);
|
|
return self;
|
|
}
|
|
|
|
static VALUE
|
|
path_freeze(VALUE self)
|
|
{
|
|
rb_call_super(0, 0);
|
|
rb_str_freeze(get_strpath(self));
|
|
return self;
|
|
}
|
|
|
|
void
|
|
Init_pathname()
|
|
{
|
|
id_at_path = rb_intern("@path");
|
|
id_to_path = rb_intern("to_path");
|
|
|
|
rb_cPathname = rb_define_class("Pathname", rb_cObject);
|
|
rb_define_method(rb_cPathname, "initialize", path_initialize, 1);
|
|
rb_define_method(rb_cPathname, "freeze", path_freeze, 0);
|
|
}
|