lucy-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From nwelln...@apache.org
Subject [1/3] lucy-clownfish git commit: Simplify S_cfish_hash_to_perl_hash
Date Wed, 15 Apr 2015 14:05:48 GMT
Repository: lucy-clownfish
Updated Branches:
  refs/heads/CLOWNFISH-7-string-only-hash-keys [created] f030723c6


Simplify S_cfish_hash_to_perl_hash

hv_store accepts UTF-8 keys with a negative `klen` argument. This is
undocumented in older Perl versions but works since 5.8.0.


Project: http://git-wip-us.apache.org/repos/asf/lucy-clownfish/repo
Commit: http://git-wip-us.apache.org/repos/asf/lucy-clownfish/commit/f030723c
Tree: http://git-wip-us.apache.org/repos/asf/lucy-clownfish/tree/f030723c
Diff: http://git-wip-us.apache.org/repos/asf/lucy-clownfish/diff/f030723c

Branch: refs/heads/CLOWNFISH-7-string-only-hash-keys
Commit: f030723c68b50e7d36a410e808c21de444b77309
Parents: 07bc632
Author: Nick Wellnhofer <wellnhofer@aevum.de>
Authored: Tue Apr 14 14:45:59 2015 +0200
Committer: Nick Wellnhofer <wellnhofer@aevum.de>
Committed: Wed Apr 15 15:35:26 2015 +0200

----------------------------------------------------------------------
 runtime/perl/xs/XSBind.c | 19 ++++++-------------
 1 file changed, 6 insertions(+), 13 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/lucy-clownfish/blob/f030723c/runtime/perl/xs/XSBind.c
----------------------------------------------------------------------
diff --git a/runtime/perl/xs/XSBind.c b/runtime/perl/xs/XSBind.c
index 737cd9e..9c499e2 100644
--- a/runtime/perl/xs/XSBind.c
+++ b/runtime/perl/xs/XSBind.c
@@ -336,13 +336,8 @@ S_cfish_array_to_perl_array(pTHX_ cfish_VArray *varray) {
 static SV*
 S_cfish_hash_to_perl_hash(pTHX_ cfish_Hash *hash) {
     HV *perl_hash = newHV();
-    SV *key_sv    = newSV(1);
     cfish_String *key;
-    cfish_Obj     *val;
-
-    // Prepare the SV key.
-    SvPOK_on(key_sv);
-    SvUTF8_on(key_sv);
+    cfish_Obj    *val;
 
     // Iterate over key-value pairs.
     CFISH_Hash_Iterate(hash);
@@ -350,14 +345,12 @@ S_cfish_hash_to_perl_hash(pTHX_ cfish_Hash *hash) {
         // Recurse for each value.
         SV *val_sv = XSBind_cfish_to_perl(aTHX_ val);
 
-        STRLEN key_size = CFISH_Str_Get_Size(key);
-        char *key_sv_ptr = SvGROW(key_sv, key_size + 1);
-        memcpy(key_sv_ptr, CFISH_Str_Get_Ptr8(key), key_size);
-        SvCUR_set(key_sv, key_size);
-        *SvEND(key_sv) = '\0';
-        (void)hv_store_ent(perl_hash, key_sv, val_sv, 0);
+        const char *key_ptr  = CFISH_Str_Get_Ptr8(key);
+        I32         key_size = CFISH_Str_Get_Size(key);
+        // Using a negative `klen` argument to signal UTF-8 is undocumented
+        // in older Perl versions but works since 5.8.0.
+        hv_store(perl_hash, key_ptr, -key_size, val_sv, 0);
     }
-    SvREFCNT_dec(key_sv);
 
     return newRV_noinc((SV*)perl_hash);
 }


Mime
View raw message