New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Clarification for the return type of pos() in doc #15174
Comments
From jason.gibson@gmail.comCreated by jason.gibson@gmail.comThe documentation for the core pos() function does not say whether or PAGER=cat perldoc -f pos | grep Return The offset is in characters, and the text could be improved by saying that. perl -E 'binmode STDOUT, "encoding(UTF-8)"; $_ = "\N{U+10000}abc"; Thank you. Perl Info
|
From @jkeenanOn Thu Feb 11 10:27:03 2016, jason.gibson@gmail.com wrote:
Please review the patch attached. Thank you very much. -- |
From @jkeenan127518-0001-Document-that-return-value-of-pos-by-default-express.patchFrom ae005ed6395e0d26c42dacffe246ee5ebf5f1cb8 Mon Sep 17 00:00:00 2001
From: James E Keenan <jkeenan@cpan.org>
Date: Fri, 12 Feb 2016 20:36:25 -0500
Subject: [PATCH] Document that return value of pos(), by default, expresses
characters.
When 'use bytes' is in effect, the return value expresses bytes.
Revise documentation and add some tests exemplifying this.
On recommendation of Jason Gibson.
For: RT # 127518
---
pod/perlfunc.pod | 10 ++++++----
t/op/pos.t | 31 ++++++++++++++++++++++++++++++-
2 files changed, 36 insertions(+), 5 deletions(-)
diff --git a/pod/perlfunc.pod b/pod/perlfunc.pod
index 87c61f4..f283277 100644
--- a/pod/perlfunc.pod
+++ b/pod/perlfunc.pod
@@ -5221,10 +5221,12 @@ X<pos> X<match, position>
=for Pod::Functions find or set the offset for the last/next m//g search
Returns the offset of where the last C<m//g> search left off for the
-variable in question (C<$_> is used when the variable is not
-specified). Note that 0 is a valid match offset. C<undef> indicates
-that the search position is reset (usually due to match failure, but
-can also be because no match has yet been run on the scalar).
+variable in question (C<$_> is used when the variable is not specified).
+This offset is in characters unless the (no-longer-recommended) C<use
+bytes> pragma is in effect, in which case the offset is in bytes. Note
+that 0 is a valid match offset. C<undef> indicates that the search
+position is reset (usually due to match failure, but can also be because
+no match has yet been run on the scalar).
C<pos> directly accesses the location used by the regexp engine to
store the offset, so assigning to C<pos> will change that offset, and
diff --git a/t/op/pos.t b/t/op/pos.t
index 04b5272..c52f471 100644
--- a/t/op/pos.t
+++ b/t/op/pos.t
@@ -6,7 +6,7 @@ BEGIN {
require './test.pl';
}
-plan tests => 29;
+plan tests => 33;
$x='banana';
$x=~/.a/g;
@@ -131,3 +131,32 @@ for my $one(pos $x) {
'no assertion failure when getting pos clobbers ref with undef';
}
}
+
+{
+ # RT # 127518
+ my $x = "\N{U+10000}abc";
+ my %expected = (
+ chars => { length => 4, pos => 2 },
+ bytes => { length => 7, pos => 5 },
+ );
+ my %observed;
+ $observed{chars}{length} = length($x);
+ $x =~ m/a/g;
+ $observed{chars}{pos} = pos($x);
+
+ {
+ use bytes;
+ $observed{bytes}{length} = length($x);
+ $observed{bytes}{pos} = pos($x);
+ }
+
+ is( $observed{chars}{length}, $expected{chars}{length},
+ "Got expected length in chars");
+ is( $observed{chars}{pos}, $expected{chars}{pos},
+ "Got expected pos in chars");
+ is( $observed{bytes}{length}, $expected{bytes}{length},
+ "Got expected length in bytes");
+ is( $observed{bytes}{pos}, $expected{bytes}{pos},
+ "Got expected pos in bytes");
+}
+
--
1.9.1
|
The RT System itself - Status changed from 'new' to 'open' |
From jason.gibson@gmail.comLooks good. Thank you.
|
From @jkeenanOn Fri Feb 12 17:47:43 2016, jkeenan wrote:
While the OP liked the patch proposed, we didn't hear from any other committers. Could we hear from committers so that we know whether to apply this (in 5.25.1)? Thank you very much. -- |
From @iabynOn Wed, Apr 20, 2016 at 06:53:02PM -0700, James E Keenan via RT wrote:
Looks good to me. -- |
From @khwilliamsonThanks, applied (after rebasing) as |
@khwilliamson - Status changed from 'open' to 'pending release' |
From @khwilliamsonThank you for filing this report. You have helped make Perl better. With the release today of Perl 5.26.0, this and 210 other issues have been Perl 5.26.0 may be downloaded via: If you find that the problem persists, feel free to reopen this ticket. |
@khwilliamson - Status changed from 'pending release' to 'resolved' |
Migrated from rt.perl.org#127518 (status was 'resolved')
Searchable as RT127518$
The text was updated successfully, but these errors were encountered: