From: gfx Date: Tue, 23 Feb 2010 01:54:17 +0000 (+0900) Subject: Move is_valid_class_name into XS X-Git-Tag: 0.50_04~21 X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=commitdiff_plain;h=0ffc4183de68b15deeec5d662d9cc1d125dabf26;p=gitmo%2FMouse.git Move is_valid_class_name into XS --- diff --git a/lib/Mouse/PurePerl.pm b/lib/Mouse/PurePerl.pm index 6ab689b..fa6a1d7 100644 --- a/lib/Mouse/PurePerl.pm +++ b/lib/Mouse/PurePerl.pm @@ -11,6 +11,19 @@ use warnings FATAL => 'redefine'; # to avoid to load Mouse::PurePerl use B (); + +# taken from Class/MOP.pm +sub is_valid_class_name { + my $class = shift; + + return 0 if ref($class); + return 0 unless defined($class); + + return 1 if $class =~ /\A \w+ (?: :: \w+ )* \z/xms; + + return 0; +} + sub is_class_loaded { my $class = shift; diff --git a/lib/Mouse/Util.pm b/lib/Mouse/Util.pm index f6be4ea..a819618 100644 --- a/lib/Mouse/Util.pm +++ b/lib/Mouse/Util.pm @@ -197,17 +197,7 @@ BEGIN { sub get_code_info; sub get_code_package; -# taken from Class/MOP.pm -sub is_valid_class_name { - my $class = shift; - - return 0 if ref($class); - return 0 unless defined($class); - - return 1 if $class =~ /\A \w+ (?: :: \w+ )* \z/xms; - - return 0; -} +sub is_valid_class_name; # taken from Class/MOP.pm sub load_first_existing_class { diff --git a/t/001_mouse/020-load-class.t b/t/001_mouse/020-load-class.t index 311acd8..b8f7c99 100644 --- a/t/001_mouse/020-load-class.t +++ b/t/001_mouse/020-load-class.t @@ -1,20 +1,28 @@ #!/usr/bin/env perl use strict; use warnings; -use Test::More tests => 11; +use Test::More; use Test::Exception; require Mouse; use lib 't/lib'; -ok(!Mouse::is_class_loaded(undef), "is_class_loaded with undef returns false"); -ok(!Mouse::is_class_loaded(''), "can't load the empty class"); -ok(!Mouse::is_class_loaded(\"foo"), "can't load a class name reference??"); +lives_and { + ok(!Mouse::is_class_loaded(undef), "is_class_loaded with undef returns false"); + ok(!Mouse::is_class_loaded(''), "can't load the empty class"); + ok(!Mouse::is_class_loaded(\"foo"), "can't load a class name reference"); + + ok(Mouse::is_class_loaded("Mouse"), "Mouse is loaded"); + ok(Mouse::is_class_loaded("Test::More"), "Test::More is loaded"); +}; throws_ok { Mouse::load_class(undef) } qr/Invalid class name \(undef\)/; throws_ok { Mouse::load_class('') } qr/Invalid class name \(\)/; throws_ok { Mouse::load_class(\"foo") } qr/Invalid class name \(SCALAR\(\w+\)\)/; +throws_ok { Mouse::load_class("Foo!") } qr/Invalid class name/; +throws_ok { Mouse::load_class("Foo::Bar42!") } qr/Invalid class name/; + ok(Mouse::load_class('Unsweetened')); can_ok('Unsweetened' => 'unsweetened'); @@ -33,3 +41,4 @@ throws_ok { Mouse::load_class('SyntaxError'); } qr/Missing right curly/; +done_testing; diff --git a/xs-src/MouseUtil.xs b/xs-src/MouseUtil.xs index 006eab2..8704d89 100644 --- a/xs-src/MouseUtil.xs +++ b/xs-src/MouseUtil.xs @@ -285,6 +285,29 @@ CODE: } bool +is_valid_class_name(SV* sv) +CODE: +{ + SvGETMAGIC(sv); + if(SvPOKp(sv) && SvCUR(sv) > 0){ + UV i; + RETVAL = TRUE; + for(i = 0; i < SvCUR(sv); i++){ + char const c = SvPVX(sv)[i]; + if(!(isALNUM(c) || c == ':')){ + RETVAL = FALSE; + break; + } + } + } + else{ + RETVAL = SvNIOKp(sv) ? TRUE : FALSE; + } +} +OUTPUT: + RETVAL + +bool is_class_loaded(SV* sv) void