| Filename | /Users/timbo/perl5/perlbrew/perls/perl-5.18.2/lib/site_perl/5.18.2/IO/String.pm |
| Statements | Executed 39 statements in 2.23ms |
| Calls | P | F | Exclusive Time |
Inclusive Time |
Subroutine |
|---|---|---|---|---|---|
| 1 | 1 | 1 | 889µs | 914µs | IO::String::BEGIN@13 |
| 1 | 1 | 1 | 17µs | 36µs | IO::String::BEGIN@9 |
| 1 | 1 | 1 | 10µs | 70µs | IO::String::BEGIN@10 |
| 0 | 0 | 0 | 0s | 0s | IO::String::DESTROY |
| 0 | 0 | 0 | 0s | 0s | IO::String::FILENO |
| 0 | 0 | 0 | 0s | 0s | IO::String::READLINE |
| 0 | 0 | 0 | 0s | 0s | IO::String::TIEHANDLE |
| 0 | 0 | 0 | 0s | 0s | IO::String::__ANON__[:387] |
| 0 | 0 | 0 | 0s | 0s | IO::String::_init_seek_constants |
| 0 | 0 | 0 | 0s | 0s | IO::String::binmode |
| 0 | 0 | 0 | 0s | 0s | IO::String::blocking |
| 0 | 0 | 0 | 0s | 0s | IO::String::close |
| 0 | 0 | 0 | 0s | 0s | IO::String::dump |
| 0 | 0 | 0 | 0s | 0s | IO::String::eof |
| 0 | 0 | 0 | 0s | 0s | IO::String::getc |
| 0 | 0 | 0 | 0s | 0s | IO::String::getline |
| 0 | 0 | 0 | 0s | 0s | IO::String::getlines |
| 0 | 0 | 0 | 0s | 0s | IO::String::getpos |
| 0 | 0 | 0 | 0s | 0s | IO::String::input_line_number |
| 0 | 0 | 0 | 0s | 0s | IO::String::new |
| 0 | 0 | 0 | 0s | 0s | IO::String::open |
| 0 | 0 | 0 | 0s | 0s | IO::String::opened |
| 0 | 0 | 0 | 0s | 0s | IO::String::pad |
| 0 | 0 | 0 | 0s | 0s | IO::String::pos |
| 0 | 0 | 0 | 0s | 0s | IO::String::print |
| 0 | 0 | 0 | 0s | 0s | IO::String::printf |
| 0 | 0 | 0 | 0s | 0s | IO::String::read |
| 0 | 0 | 0 | 0s | 0s | IO::String::seek |
| 0 | 0 | 0 | 0s | 0s | IO::String::stat |
| 0 | 0 | 0 | 0s | 0s | IO::String::string_ref |
| 0 | 0 | 0 | 0s | 0s | IO::String::truncate |
| 0 | 0 | 0 | 0s | 0s | IO::String::ungetc |
| 0 | 0 | 0 | 0s | 0s | IO::String::write |
| Line | State ments |
Time on line |
Calls | Time in subs |
Code |
|---|---|---|---|---|---|
| 1 | package IO::String; | ||||
| 2 | |||||
| 3 | # Copyright 1998-2005 Gisle Aas. | ||||
| 4 | # | ||||
| 5 | # This library is free software; you can redistribute it and/or | ||||
| 6 | # modify it under the same terms as Perl itself. | ||||
| 7 | |||||
| 8 | 1 | 11µs | require 5.005_03; | ||
| 9 | 2 | 35µs | 2 | 55µs | # spent 36µs (17+19) within IO::String::BEGIN@9 which was called:
# once (17µs+19µs) by PPI::Token::Data::BEGIN@31 at line 9 # spent 36µs making 1 call to IO::String::BEGIN@9
# spent 19µs making 1 call to strict::import |
| 10 | 2 | 48µs | 2 | 130µs | # spent 70µs (10+60) within IO::String::BEGIN@10 which was called:
# once (10µs+60µs) by PPI::Token::Data::BEGIN@31 at line 10 # spent 70µs making 1 call to IO::String::BEGIN@10
# spent 60µs making 1 call to vars::import |
| 11 | 1 | 600ns | $VERSION = "1.08"; # $Date: 2005/12/05 12:00:47 $ | ||
| 12 | |||||
| 13 | 2 | 2.12ms | 1 | 914µs | # spent 914µs (889+25) within IO::String::BEGIN@13 which was called:
# once (889µs+25µs) by PPI::Token::Data::BEGIN@31 at line 13 # spent 914µs making 1 call to IO::String::BEGIN@13 |
| 14 | |||||
| 15 | sub new | ||||
| 16 | { | ||||
| 17 | my $class = shift; | ||||
| 18 | my $self = bless Symbol::gensym(), ref($class) || $class; | ||||
| 19 | tie *$self, $self; | ||||
| 20 | $self->open(@_); | ||||
| 21 | return $self; | ||||
| 22 | } | ||||
| 23 | |||||
| 24 | sub open | ||||
| 25 | { | ||||
| 26 | my $self = shift; | ||||
| 27 | return $self->new(@_) unless ref($self); | ||||
| 28 | |||||
| 29 | if (@_) { | ||||
| 30 | my $bufref = ref($_[0]) ? $_[0] : \$_[0]; | ||||
| 31 | $$bufref = "" unless defined $$bufref; | ||||
| 32 | *$self->{buf} = $bufref; | ||||
| 33 | } | ||||
| 34 | else { | ||||
| 35 | my $buf = ""; | ||||
| 36 | *$self->{buf} = \$buf; | ||||
| 37 | } | ||||
| 38 | *$self->{pos} = 0; | ||||
| 39 | *$self->{lno} = 0; | ||||
| 40 | return $self; | ||||
| 41 | } | ||||
| 42 | |||||
| 43 | sub pad | ||||
| 44 | { | ||||
| 45 | my $self = shift; | ||||
| 46 | my $old = *$self->{pad}; | ||||
| 47 | *$self->{pad} = substr($_[0], 0, 1) if @_; | ||||
| 48 | return "\0" unless defined($old) && length($old); | ||||
| 49 | return $old; | ||||
| 50 | } | ||||
| 51 | |||||
| 52 | sub dump | ||||
| 53 | { | ||||
| 54 | require Data::Dumper; | ||||
| 55 | my $self = shift; | ||||
| 56 | print Data::Dumper->Dump([$self], ['*self']); | ||||
| 57 | print Data::Dumper->Dump([*$self{HASH}], ['$self{HASH}']); | ||||
| 58 | return; | ||||
| 59 | } | ||||
| 60 | |||||
| 61 | sub TIEHANDLE | ||||
| 62 | { | ||||
| 63 | print "TIEHANDLE @_\n" if $DEBUG; | ||||
| 64 | return $_[0] if ref($_[0]); | ||||
| 65 | my $class = shift; | ||||
| 66 | my $self = bless Symbol::gensym(), $class; | ||||
| 67 | $self->open(@_); | ||||
| 68 | return $self; | ||||
| 69 | } | ||||
| 70 | |||||
| 71 | sub DESTROY | ||||
| 72 | { | ||||
| 73 | print "DESTROY @_\n" if $DEBUG; | ||||
| 74 | } | ||||
| 75 | |||||
| 76 | sub close | ||||
| 77 | { | ||||
| 78 | my $self = shift; | ||||
| 79 | delete *$self->{buf}; | ||||
| 80 | delete *$self->{pos}; | ||||
| 81 | delete *$self->{lno}; | ||||
| 82 | undef *$self if $] eq "5.008"; # workaround for some bug | ||||
| 83 | return 1; | ||||
| 84 | } | ||||
| 85 | |||||
| 86 | sub opened | ||||
| 87 | { | ||||
| 88 | my $self = shift; | ||||
| 89 | return defined *$self->{buf}; | ||||
| 90 | } | ||||
| 91 | |||||
| 92 | sub binmode | ||||
| 93 | { | ||||
| 94 | my $self = shift; | ||||
| 95 | return 1 unless @_; | ||||
| 96 | # XXX don't know much about layers yet :-( | ||||
| 97 | return 0; | ||||
| 98 | } | ||||
| 99 | |||||
| 100 | sub getc | ||||
| 101 | { | ||||
| 102 | my $self = shift; | ||||
| 103 | my $buf; | ||||
| 104 | return $buf if $self->read($buf, 1); | ||||
| 105 | return undef; | ||||
| 106 | } | ||||
| 107 | |||||
| 108 | sub ungetc | ||||
| 109 | { | ||||
| 110 | my $self = shift; | ||||
| 111 | $self->setpos($self->getpos() - 1); | ||||
| 112 | return 1; | ||||
| 113 | } | ||||
| 114 | |||||
| 115 | sub eof | ||||
| 116 | { | ||||
| 117 | my $self = shift; | ||||
| 118 | return length(${*$self->{buf}}) <= *$self->{pos}; | ||||
| 119 | } | ||||
| 120 | |||||
| 121 | sub print | ||||
| 122 | { | ||||
| 123 | my $self = shift; | ||||
| 124 | if (defined $\) { | ||||
| 125 | if (defined $,) { | ||||
| 126 | $self->write(join($,, @_).$\); | ||||
| 127 | } | ||||
| 128 | else { | ||||
| 129 | $self->write(join("",@_).$\); | ||||
| 130 | } | ||||
| 131 | } | ||||
| 132 | else { | ||||
| 133 | if (defined $,) { | ||||
| 134 | $self->write(join($,, @_)); | ||||
| 135 | } | ||||
| 136 | else { | ||||
| 137 | $self->write(join("",@_)); | ||||
| 138 | } | ||||
| 139 | } | ||||
| 140 | return 1; | ||||
| 141 | } | ||||
| 142 | 1 | 2µs | *printflush = \*print; | ||
| 143 | |||||
| 144 | sub printf | ||||
| 145 | { | ||||
| 146 | my $self = shift; | ||||
| 147 | print "PRINTF(@_)\n" if $DEBUG; | ||||
| 148 | my $fmt = shift; | ||||
| 149 | $self->write(sprintf($fmt, @_)); | ||||
| 150 | return 1; | ||||
| 151 | } | ||||
| 152 | |||||
| 153 | |||||
| 154 | 1 | 200ns | my($SEEK_SET, $SEEK_CUR, $SEEK_END); | ||
| 155 | |||||
| 156 | sub _init_seek_constants | ||||
| 157 | { | ||||
| 158 | if ($IO_CONSTANTS) { | ||||
| 159 | require IO::Handle; | ||||
| 160 | $SEEK_SET = &IO::Handle::SEEK_SET; | ||||
| 161 | $SEEK_CUR = &IO::Handle::SEEK_CUR; | ||||
| 162 | $SEEK_END = &IO::Handle::SEEK_END; | ||||
| 163 | } | ||||
| 164 | else { | ||||
| 165 | $SEEK_SET = 0; | ||||
| 166 | $SEEK_CUR = 1; | ||||
| 167 | $SEEK_END = 2; | ||||
| 168 | } | ||||
| 169 | } | ||||
| 170 | |||||
| 171 | |||||
| 172 | sub seek | ||||
| 173 | { | ||||
| 174 | my($self,$off,$whence) = @_; | ||||
| 175 | my $buf = *$self->{buf} || return 0; | ||||
| 176 | my $len = length($$buf); | ||||
| 177 | my $pos = *$self->{pos}; | ||||
| 178 | |||||
| 179 | _init_seek_constants() unless defined $SEEK_SET; | ||||
| 180 | |||||
| 181 | if ($whence == $SEEK_SET) { $pos = $off } | ||||
| 182 | elsif ($whence == $SEEK_CUR) { $pos += $off } | ||||
| 183 | elsif ($whence == $SEEK_END) { $pos = $len + $off } | ||||
| 184 | else { die "Bad whence ($whence)" } | ||||
| 185 | print "SEEK(POS=$pos,OFF=$off,LEN=$len)\n" if $DEBUG; | ||||
| 186 | |||||
| 187 | $pos = 0 if $pos < 0; | ||||
| 188 | $self->truncate($pos) if $pos > $len; # extend file | ||||
| 189 | *$self->{pos} = $pos; | ||||
| 190 | return 1; | ||||
| 191 | } | ||||
| 192 | |||||
| 193 | sub pos | ||||
| 194 | { | ||||
| 195 | my $self = shift; | ||||
| 196 | my $old = *$self->{pos}; | ||||
| 197 | if (@_) { | ||||
| 198 | my $pos = shift || 0; | ||||
| 199 | my $buf = *$self->{buf}; | ||||
| 200 | my $len = $buf ? length($$buf) : 0; | ||||
| 201 | $pos = $len if $pos > $len; | ||||
| 202 | *$self->{pos} = $pos; | ||||
| 203 | } | ||||
| 204 | return $old; | ||||
| 205 | } | ||||
| 206 | |||||
| 207 | sub getpos { shift->pos; } | ||||
| 208 | |||||
| 209 | 1 | 900ns | *sysseek = \&seek; | ||
| 210 | 1 | 300ns | *setpos = \&pos; | ||
| 211 | 1 | 300ns | *tell = \&getpos; | ||
| 212 | |||||
| - - | |||||
| 215 | sub getline | ||||
| 216 | { | ||||
| 217 | my $self = shift; | ||||
| 218 | my $buf = *$self->{buf} || return; | ||||
| 219 | my $len = length($$buf); | ||||
| 220 | my $pos = *$self->{pos}; | ||||
| 221 | return if $pos >= $len; | ||||
| 222 | |||||
| 223 | unless (defined $/) { # slurp | ||||
| 224 | *$self->{pos} = $len; | ||||
| 225 | return substr($$buf, $pos); | ||||
| 226 | } | ||||
| 227 | |||||
| 228 | unless (length $/) { # paragraph mode | ||||
| 229 | # XXX slow&lazy implementation using getc() | ||||
| 230 | my $para = ""; | ||||
| 231 | my $eol = 0; | ||||
| 232 | my $c; | ||||
| 233 | while (defined($c = $self->getc)) { | ||||
| 234 | if ($c eq "\n") { | ||||
| 235 | $eol++; | ||||
| 236 | next if $eol > 2; | ||||
| 237 | } | ||||
| 238 | elsif ($eol > 1) { | ||||
| 239 | $self->ungetc($c); | ||||
| 240 | last; | ||||
| 241 | } | ||||
| 242 | else { | ||||
| 243 | $eol = 0; | ||||
| 244 | } | ||||
| 245 | $para .= $c; | ||||
| 246 | } | ||||
| 247 | return $para; # XXX wantarray | ||||
| 248 | } | ||||
| 249 | |||||
| 250 | my $idx = index($$buf,$/,$pos); | ||||
| 251 | if ($idx < 0) { | ||||
| 252 | # return rest of it | ||||
| 253 | *$self->{pos} = $len; | ||||
| 254 | $. = ++ *$self->{lno}; | ||||
| 255 | return substr($$buf, $pos); | ||||
| 256 | } | ||||
| 257 | $len = $idx - $pos + length($/); | ||||
| 258 | *$self->{pos} += $len; | ||||
| 259 | $. = ++ *$self->{lno}; | ||||
| 260 | return substr($$buf, $pos, $len); | ||||
| 261 | } | ||||
| 262 | |||||
| 263 | sub getlines | ||||
| 264 | { | ||||
| 265 | die "getlines() called in scalar context\n" unless wantarray; | ||||
| 266 | my $self = shift; | ||||
| 267 | my($line, @lines); | ||||
| 268 | push(@lines, $line) while defined($line = $self->getline); | ||||
| 269 | return @lines; | ||||
| 270 | } | ||||
| 271 | |||||
| 272 | sub READLINE | ||||
| 273 | { | ||||
| 274 | goto &getlines if wantarray; | ||||
| 275 | goto &getline; | ||||
| 276 | } | ||||
| 277 | |||||
| 278 | sub input_line_number | ||||
| 279 | { | ||||
| 280 | my $self = shift; | ||||
| 281 | my $old = *$self->{lno}; | ||||
| 282 | *$self->{lno} = shift if @_; | ||||
| 283 | return $old; | ||||
| 284 | } | ||||
| 285 | |||||
| 286 | sub truncate | ||||
| 287 | { | ||||
| 288 | my $self = shift; | ||||
| 289 | my $len = shift || 0; | ||||
| 290 | my $buf = *$self->{buf}; | ||||
| 291 | if (length($$buf) >= $len) { | ||||
| 292 | substr($$buf, $len) = ''; | ||||
| 293 | *$self->{pos} = $len if $len < *$self->{pos}; | ||||
| 294 | } | ||||
| 295 | else { | ||||
| 296 | $$buf .= ($self->pad x ($len - length($$buf))); | ||||
| 297 | } | ||||
| 298 | return 1; | ||||
| 299 | } | ||||
| 300 | |||||
| 301 | sub read | ||||
| 302 | { | ||||
| 303 | my $self = shift; | ||||
| 304 | my $buf = *$self->{buf}; | ||||
| 305 | return undef unless $buf; | ||||
| 306 | |||||
| 307 | my $pos = *$self->{pos}; | ||||
| 308 | my $rem = length($$buf) - $pos; | ||||
| 309 | my $len = $_[1]; | ||||
| 310 | $len = $rem if $len > $rem; | ||||
| 311 | return undef if $len < 0; | ||||
| 312 | if (@_ > 2) { # read offset | ||||
| 313 | substr($_[0],$_[2]) = substr($$buf, $pos, $len); | ||||
| 314 | } | ||||
| 315 | else { | ||||
| 316 | $_[0] = substr($$buf, $pos, $len); | ||||
| 317 | } | ||||
| 318 | *$self->{pos} += $len; | ||||
| 319 | return $len; | ||||
| 320 | } | ||||
| 321 | |||||
| 322 | sub write | ||||
| 323 | { | ||||
| 324 | my $self = shift; | ||||
| 325 | my $buf = *$self->{buf}; | ||||
| 326 | return unless $buf; | ||||
| 327 | |||||
| 328 | my $pos = *$self->{pos}; | ||||
| 329 | my $slen = length($_[0]); | ||||
| 330 | my $len = $slen; | ||||
| 331 | my $off = 0; | ||||
| 332 | if (@_ > 1) { | ||||
| 333 | $len = $_[1] if $_[1] < $len; | ||||
| 334 | if (@_ > 2) { | ||||
| 335 | $off = $_[2] || 0; | ||||
| 336 | die "Offset outside string" if $off > $slen; | ||||
| 337 | if ($off < 0) { | ||||
| 338 | $off += $slen; | ||||
| 339 | die "Offset outside string" if $off < 0; | ||||
| 340 | } | ||||
| 341 | my $rem = $slen - $off; | ||||
| 342 | $len = $rem if $rem < $len; | ||||
| 343 | } | ||||
| 344 | } | ||||
| 345 | substr($$buf, $pos, $len) = substr($_[0], $off, $len); | ||||
| 346 | *$self->{pos} += $len; | ||||
| 347 | return $len; | ||||
| 348 | } | ||||
| 349 | |||||
| 350 | 1 | 300ns | *sysread = \&read; | ||
| 351 | 1 | 200ns | *syswrite = \&write; | ||
| 352 | |||||
| 353 | sub stat | ||||
| 354 | { | ||||
| 355 | my $self = shift; | ||||
| 356 | return unless $self->opened; | ||||
| 357 | return 1 unless wantarray; | ||||
| 358 | my $len = length ${*$self->{buf}}; | ||||
| 359 | |||||
| 360 | return ( | ||||
| 361 | undef, undef, # dev, ino | ||||
| 362 | 0666, # filemode | ||||
| 363 | 1, # links | ||||
| 364 | $>, # user id | ||||
| 365 | $), # group id | ||||
| 366 | undef, # device id | ||||
| 367 | $len, # size | ||||
| 368 | undef, # atime | ||||
| 369 | undef, # mtime | ||||
| 370 | undef, # ctime | ||||
| 371 | 512, # blksize | ||||
| 372 | int(($len+511)/512) # blocks | ||||
| 373 | ); | ||||
| 374 | } | ||||
| 375 | |||||
| 376 | sub FILENO { | ||||
| 377 | return undef; # XXX perlfunc says this means the file is closed | ||||
| 378 | } | ||||
| 379 | |||||
| 380 | sub blocking { | ||||
| 381 | my $self = shift; | ||||
| 382 | my $old = *$self->{blocking} || 0; | ||||
| 383 | *$self->{blocking} = shift if @_; | ||||
| 384 | return $old; | ||||
| 385 | } | ||||
| 386 | |||||
| 387 | 1 | 2µs | my $notmuch = sub { return }; | ||
| 388 | |||||
| 389 | 1 | 300ns | *fileno = $notmuch; | ||
| 390 | 1 | 100ns | *error = $notmuch; | ||
| 391 | 1 | 100ns | *clearerr = $notmuch; | ||
| 392 | 1 | 100ns | *sync = $notmuch; | ||
| 393 | 1 | 100ns | *flush = $notmuch; | ||
| 394 | 1 | 100ns | *setbuf = $notmuch; | ||
| 395 | 1 | 100ns | *setvbuf = $notmuch; | ||
| 396 | |||||
| 397 | 1 | 100ns | *untaint = $notmuch; | ||
| 398 | 1 | 100ns | *autoflush = $notmuch; | ||
| 399 | 1 | 100ns | *fcntl = $notmuch; | ||
| 400 | 1 | 100ns | *ioctl = $notmuch; | ||
| 401 | |||||
| 402 | 1 | 300ns | *GETC = \&getc; | ||
| 403 | 1 | 200ns | *PRINT = \&print; | ||
| 404 | 1 | 200ns | *PRINTF = \&printf; | ||
| 405 | 1 | 200ns | *READ = \&read; | ||
| 406 | 1 | 200ns | *WRITE = \&write; | ||
| 407 | 1 | 200ns | *SEEK = \&seek; | ||
| 408 | 1 | 200ns | *TELL = \&getpos; | ||
| 409 | 1 | 200ns | *EOF = \&eof; | ||
| 410 | 1 | 200ns | *CLOSE = \&close; | ||
| 411 | 1 | 200ns | *BINMODE = \&binmode; | ||
| 412 | |||||
| 413 | |||||
| 414 | sub string_ref | ||||
| 415 | { | ||||
| 416 | my $self = shift; | ||||
| 417 | return *$self->{buf}; | ||||
| 418 | } | ||||
| 419 | 1 | 200ns | *sref = \&string_ref; | ||
| 420 | |||||
| 421 | 1 | 11µs | 1; | ||
| 422 | |||||
| 423 | __END__ |