summaryrefslogtreecommitdiff
path: root/mg.c
diff options
context:
space:
mode:
authorLarry Wall <larry@wall.org>1998-07-24 05:44:33 +0000
committerLarry Wall <larry@wall.org>1998-07-24 05:44:33 +0000
commita0ed51b321531af4b47cce24205ab9656f043f0f (patch)
tree610356407b37a4041ea8bcaf44571579b2da5613 /mg.c
parent9332a1c1d80ded85a2b1f32b1c8968a35e3b0fbb (diff)
downloadperl-a0ed51b321531af4b47cce24205ab9656f043f0f.tar.gz
Here are the long-expected Unicode/UTF-8 modifications.
p4raw-id: //depot/utfperl@1651
Diffstat (limited to 'mg.c')
-rw-r--r--mg.c22
1 files changed, 21 insertions, 1 deletions
diff --git a/mg.c b/mg.c
index 6068d9bdec..3efa41764b 100644
--- a/mg.c
+++ b/mg.c
@@ -1131,7 +1131,10 @@ magic_getpos(SV *sv, MAGIC *mg)
mg = mg_find(lsv, 'g');
if (mg && mg->mg_len >= 0) {
dTHR;
- sv_setiv(sv, mg->mg_len + PL_curcop->cop_arybase);
+ I32 i = mg->mg_len;
+ if (IN_UTF8)
+ sv_pos_b2u(lsv, &i);
+ sv_setiv(sv, i + PL_curcop->cop_arybase);
return 0;
}
}
@@ -1145,6 +1148,7 @@ magic_setpos(SV *sv, MAGIC *mg)
SV* lsv = LvTARG(sv);
SSize_t pos;
STRLEN len;
+ STRLEN ulen;
mg = 0;
@@ -1163,6 +1167,15 @@ magic_setpos(SV *sv, MAGIC *mg)
len = SvPOK(lsv) ? SvCUR(lsv) : sv_len(lsv);
WITH_THR(pos = SvIV(sv) - PL_curcop->cop_arybase);
+
+ if (IN_UTF8) {
+ ulen = sv_len_utf8(lsv);
+ if (ulen)
+ len = ulen;
+ else
+ ulen = 0;
+ }
+
if (pos < 0) {
pos += len;
if (pos < 0)
@@ -1170,6 +1183,13 @@ magic_setpos(SV *sv, MAGIC *mg)
}
else if (pos > len)
pos = len;
+
+ if (ulen) {
+ I32 p = pos;
+ sv_pos_u2b(lsv, &p, 0);
+ pos = p;
+ }
+
mg->mg_len = pos;
mg->mg_flags &= ~MGf_MINMATCH;