[svn:parrot] r37937 - in trunk: src/pmc t/pmc
pmichaud at svn.parrot.org
pmichaud at svn.parrot.org
Tue Apr 7 02:29:26 UTC 2009
Author: pmichaud
Date: Tue Apr 7 02:29:25 2009
New Revision: 37937
URL: https://trac.parrot.org/parrot/changeset/37937
Log:
[codestring]: Add 'lineof' method for finding line numbers.
Modified:
trunk/src/pmc/codestring.pmc
trunk/t/pmc/codestring.t
Modified: trunk/src/pmc/codestring.pmc
==============================================================================
--- trunk/src/pmc/codestring.pmc Tue Apr 7 02:13:03 2009 (r37936)
+++ trunk/src/pmc/codestring.pmc Tue Apr 7 02:29:25 2009 (r37937)
@@ -127,6 +127,33 @@
}
+/*
+
+=item lineof(INTVAL pos)
+
+Return the line number of the line at offset C<pos>. This code
+assumes that the first line is line number zero.
+
+*/
+
+ METHOD lineof(INTVAL pos) {
+ STRING *str = SELF.get_string();
+ INTVAL line = 0;
+ INTVAL ipos = 0;
+ INTVAL jpos;
+
+ jpos = Parrot_str_find_cclass(INTERP, enum_cclass_newline, str, ipos, pos);
+ while (jpos < pos) {
+ line++;
+ ipos = jpos + 1;
+ /* treat \r\n as a single line separator */
+ ipos += (string_ord(INTERP, str, jpos) == 13
+ && string_ord(INTERP, str, jpos+1) == 10);
+ jpos = Parrot_str_find_cclass(INTERP, enum_cclass_newline, str, ipos, pos);
+ }
+ RETURN(INTVAL line);
+}
+
/*
Modified: trunk/t/pmc/codestring.t
==============================================================================
--- trunk/t/pmc/codestring.t Tue Apr 7 02:13:03 2009 (r37936)
+++ trunk/t/pmc/codestring.t Tue Apr 7 02:29:25 2009 (r37937)
@@ -19,7 +19,7 @@
.sub main :main
.include 'test_more.pir'
- plan(24)
+ plan(38)
create_codestring()
calls_to_unique()
@@ -32,6 +32,7 @@
namespace_keys()
first_char_repl_regression()
ord_from_name()
+ lineof_tests()
.end
.sub create_codestring
@@ -189,6 +190,40 @@
is($I0, -1, '<no such symbol>')
.end
+.sub 'lineof_tests'
+ $P0 = new 'CodeString'
+ $P0 = "0123\n5678\r0123\r\n678\n"
+ $I0 = $P0.'lineof'(0)
+ is($I0, 0, "lineof - beginning of string")
+ $I0 = $P0.'lineof'(1)
+ is($I0, 0, "lineof - char on first line")
+ $I0 = $P0.'lineof'(4)
+ is($I0, 0, "lineof - immediately before nl")
+ $I0 = $P0.'lineof'(5)
+ is($I0, 1, "lineof - immediately after nl")
+ $I0 = $P0.'lineof'(8)
+ is($I0, 1, "lineof - char before cr")
+ $I0 = $P0.'lineof'(9)
+ is($I0, 1, "lineof - immediately before cr")
+ $I0 = $P0.'lineof'(10)
+ is($I0, 2, "lineof - immediately after cr")
+ $I0 = $P0.'lineof'(11)
+ is($I0, 2, "lineof - char after cr")
+ $I0 = $P0.'lineof'(13)
+ is($I0, 2, "lineof - char before crnl")
+ $I0 = $P0.'lineof'(14)
+ is($I0, 2, "lineof - immediately before crnl")
+ $I0 = $P0.'lineof'(15)
+ is($I0, 3, "lineof - middle of crnl")
+ $I0 = $P0.'lineof'(16)
+ is($I0, 3, "lineof - immediately after crnl")
+ $I0 = $P0.'lineof'(19)
+ is($I0, 3, "lineof - immediately before final nl")
+ $I0 = $P0.'lineof'(20)
+ is($I0, 4, "lineof - immediately after final nl")
+.end
+
+
# Local Variables:
# mode: pir
# fill-column: 100
More information about the parrot-commits
mailing list