Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Fixed XS Stash UTF8 bug

git-svn-id: svn://svn.tt2.org/tt/Template2/trunk@1240 d5a88997-0a34-4036-9ed2-92fb5d660d91
  • Loading branch information...
commit 45c1dfecf9d32bb00352f2dde0c66120dde52370 1 parent 4d7c262
@abw authored
Showing with 39 additions and 8 deletions.
  1. +27 −8 t/stash-xs-unicode.t
  2. +12 −0 xs/Stash.xs
View
35 t/stash-xs-unicode.t
@@ -21,31 +21,42 @@ use utf8;
use Template;
use Template::Test;
+BEGIN {
+ unless ($] > 5.007) {
+ skip_all("perl < 5.8 can't do unicode well enough\n");
+ }
+}
+
eval {
require Template::Stash::XS;
};
if ($@) {
warn $@;
- skip_all('cannot load Template::Stash::XS');
}
binmode STDOUT, ':utf8';
+$Template::Config::STASH = 'Template::Stash::XS';
+
# XXX: uncomment this to make Template work properly
#$Template::Config::STASH = 'Template::Stash';
+my $config = {
+ ENCODING => 'utf8',
+};
+
my $data = {
ascii => 'key',
utf8 => 'ключ',
hash => {
- key => 'value',
+ key => 'value',
ключ => 'значение'
},
str => 'щука'
};
-test_expect(\*DATA, undef, $data);
+test_expect(\*DATA, $config, $data);
__DATA__
-- test --
@@ -62,14 +73,22 @@ str.length = [% str.length %]
-- expect --
str.length = 4
--- stop --
-This test fails. A trivial attempt at fixing the XS Stash didn't work. Needs a proper look.
+#-- stop --
+#This test fails. A trivial attempt at fixing the XS Stash didn't work. Needs a proper look.
-- test --
--- name UTF8 key --
+-- name UTF8 key fetch --
utf8 = [% utf8 %]
-hash.$utf8 = [% hash.$utf8 %]
+hash.$utf8 = hash.[% utf8 %] = [% hash.$utf8 %]
-- expect --
utf8 = ключ
-hash.$utf8 = значение
+hash.$utf8 = hash.ключ = значение
+-- test --
+-- name UTF8 key assign --
+[% value = hash.$utf8; hash.$value = utf8 -%]
+value = [% value %]
+hash.$value = hash.[% value %] = [% hash.$value %]
+-- expect --
+value = значение
+hash.$value = hash.значение = ключ
View
12 xs/Stash.xs
@@ -142,6 +142,14 @@ static TT_RET tt_fetch_item(pTHX_ SV *root, SV *key_sv, AV *args, SV **result) {
char *key = SvPV(key_sv, key_len);
SV **value = NULL;
+#ifndef WIN32
+ debug("fetch item: %s\n", key);
+#endif
+
+ /* negative key_len is used to indicate UTF8 string */
+ if (SvUTF8(key_sv))
+ key_len = -key_len;
+
if (!SvROK(root))
return TT_RET_UNDEF;
@@ -478,6 +486,10 @@ static SV *assign(pTHX_ SV *root, SV *key_sv, AV *args, SV *value, int flags) {
debug("assign(%s)\n", key2);
#endif
+ /* negative key_len is used to indicate UTF8 string */
+ if (SvUTF8(key_sv))
+ key_len = -key_len;
+
if (!root || !SvOK(key_sv) || key_sv == &PL_sv_undef || looks_private(aTHX_ key)) {
/* ignore _private or .private members */
return &PL_sv_undef;
Please sign in to comment.
Something went wrong with that request. Please try again.