split
除非您开始交换,否则应该非常快。我能看到加速它的唯一方法是编写一个 XS 函数来查找 LF 而不是使用正则表达式。
顺便说一句,您可以通过切换到
while ($$file_ref =~ /\G([^\n]*\n|[^\n]+)/g) {
my $line = $1;
# process line
}
说XS功能。如果您不想大吃大喝,请移动语句newSVpvn_flags
后面的行。if
SV* next_line(SV* buf_sv) {
STRLEN buf_len;
const char* buf = SvPV_force(buf_sv, buf_len);
char* next_line_ptr;
char* buf_end;
SV* rv;
if (!buf_len)
return &PL_sv_undef;
next_line_ptr = buf;
buf_end = buf + buf_len;
while (next_line_ptr != buf_end && *next_line_ptr != '\n')
++next_line_ptr;
rv = newSVpvn_flags(buf, next_line_ptr-buf, SvUTF8(buf_sv) ? SVf_UTF8 : 0);
if (next_line_ptr != buf_end)
++next_line_ptr;
sv_chop(buf_sv, next_line_ptr);
return rv; /* Typemap will mortalize */
}
测试方法:
use strict;
use warnings;
use Inline C => <<'__EOC__';
SV* next_line(SV* buf_sv) {
...
}
__EOC__
my $s = <<'__EOI__';
foo
bar
baz
__EOI__
while (defined($_ = next_line($s))) {
print "<$_>\n";
}