Text::LineNumber - Convert between offsets and line numbers.
This module creates a conversion object for the given text. The object features two lookup methods that convert forward or backward.
my $text = "foo\nHello World\r\nbar\rbaz";
my $tln = Text::LineNumber->new($text);
my $world_lnr = $tln->off2lnr(10); # = 2
my @world = $tln->off2lnr(10); # = (2, 7)
my $l3o = $tln->lnr2off(3); # = 17
my $line3 = substr $text, $l3o, $tln->lnr2off(4)-$l3o; # = "bar\r"
All three line ending styles (Unix, Mac, Windows) are recognized as line breaks. The offset of the first character in the text is 0. the number of the first line is 1. The column of the first character in a line is 1.
New reads the entire text and creates an object containing sufficient metadata. Later changes of $text have no effect on the methods of this object.
Off2lnr converts a byte offset to a line number. If called in an array context it returns line number and column number. A binary search is used for the line that contains the given offset.
Lnr2off converts a line number to a byte offset. The offset of the first character of a line is returned. the first character is the one immediatly following the previous line ending.
Returns 0 when called with 0 or negative parameters. Returns the offset of the last line when called with too high a line number.
Juergen Weigert, <jw at suse.de>
<jw at suse.de>
- The implementation is quite trivial and uses a straight forward binary search.
- Learning how to use this module may be more effort than writing something similar yourself. Using this module still saves you some headache about off-by-one errors.
Please report any bugs or feature requests to bug-text-linenumber at rt.cpan.org, or through the web interface at http://rt.cpan.org/NoAuth/ReportBug.html?Queue=Text-LineNumber. I will be notified, and then you'll automatically be notified of progress on your bug as I make changes.
bug-text-linenumber at rt.cpan.org
You can find documentation for this module with the perldoc command.
You can also look for information at:
RT: CPAN's request tracker
AnnoCPAN: Annotated CPAN documentation
Copyright 2008 Juergen Weigert, all rights reserved.
This program is free software; you can redistribute it and/or modify it under the same terms as Perl itself.
To install Text::LineNumber, copy and paste the appropriate command in to your terminal.
perl -MCPAN -e shell
For more information on module installation, please visit the detailed CPAN module installation guide.