Support binary data and unicode in symbol names
Leon Timmermans [Tue, 15 Mar 2016 23:01:53 +0000 (00:01 +0100)]
Name.xs

diff --git a/Name.xs b/Name.xs
index 312485f..71f516c 100644 (file)
--- a/Name.xs
+++ b/Name.xs
@@ -7,6 +7,8 @@
 #include "EXTERN.h"
 #include "perl.h"
 #include "XSUB.h"
+#define NEED_sv_2pv_flags
+#define NEED_gv_fetchpvn_flags
 #include "ppport.h"
 
 static MGVTBL subname_vtbl;
@@ -29,14 +31,17 @@ PROTOTYPES: DISABLE
 
 void
 subname(name, sub)
-       char *name
+       SV *name
        SV *sub
     PREINIT:
        CV *cv = NULL;
        GV *gv;
        HV *stash = CopSTASH(PL_curcop);
-       char *s, *end = NULL;
+       const char *s, *end = NULL;
        MAGIC *mg;
+       STRLEN namelen;
+       int utf8flag = SvUTF8(name);
+       const char* nameptr = SvPV(name, namelen);
     PPCODE:
        if (!SvROK(sub) && SvGMAGICAL(sub))
                mg_get(sub);
@@ -49,25 +54,25 @@ subname(name, sub)
        else if (PL_op->op_private & HINT_STRICT_REFS)
                croak("Can't use string (\"%.32s\") as %s ref while \"strict refs\" in use",
                      SvPV_nolen(sub), "a subroutine");
-       else if ((gv = gv_fetchpv(SvPV_nolen(sub), FALSE, SVt_PVCV)))
+       else if ((gv = gv_fetchsv(sub, FALSE, SVt_PVCV)))
                cv = GvCVu(gv);
        if (!cv)
                croak("Undefined subroutine %s", SvPV_nolen(sub));
        if (SvTYPE(cv) != SVt_PVCV && SvTYPE(cv) != SVt_PVFM)
                croak("Not a subroutine reference");
-       for (s = name; *s++; ) {
+
+       for (s = nameptr; s <= nameptr + namelen; s++) {
                if (*s == ':' && s[-1] == ':')
                        end = ++s;
                else if (*s && s[-1] == '\'')
                        end = s;
        }
        s--;
-        if (end) {
-               char *namepv = savepvn(name, end - name);
-               stash = GvHV(gv_fetchpv(namepv, TRUE, SVt_PVHV));
-               Safefree(namepv);
-                name = end;
-        }
+       if (end) {
+               stash = GvHV(gv_fetchpvn_flags(nameptr, end - nameptr, GV_ADD | utf8flag, SVt_PVHV));
+               nameptr = end;
+               namelen -= end - nameptr;
+       }
 
        /* under debugger, provide information about sub location */
        if (PL_DBsub && CvGV(cv)) {
@@ -80,7 +85,7 @@ subname(name, sub)
                char* old_pkg = HvNAME( GvSTASH(CvGV(cv)) );
 
                int old_len = strlen(old_name) + strlen(old_pkg);
-               int new_len = strlen(name) + strlen(new_pkg);
+               int new_len = namelen + strlen(new_pkg);
 
                char* full_name;
                Newxz(full_name, (old_len > new_len ? old_len : new_len) + 3, char);
@@ -94,7 +99,7 @@ subname(name, sub)
                if (old_data) {
                        strcpy(full_name, new_pkg);
                        strcat(full_name, "::");
-                       strcat(full_name, name);
+                       strcat(full_name, nameptr);
 
                        SvREFCNT_inc(*old_data);
                        if (!hv_store(hv, full_name, strlen(full_name), *old_data, 0))
@@ -104,7 +109,7 @@ subname(name, sub)
        }
 
        gv = (GV *) newSV(0);
-       gv_init(gv, stash, name, s - name, TRUE);
+       gv_init_pvn(gv, stash, nameptr, s - nameptr, GV_ADDMULTI | utf8flag);
 
        mg = SvMAGIC(cv);
        while (mg && mg->mg_virtual != &subname_vtbl)