Beefy Boxes and Bandwidth Generously Provided by pair Networks
P is for Practical
 
PerlMonks  

Re^2: Pattern matching in binary mode (I/O)

by kschwab (Vicar)
on Oct 20, 2021 at 21:34 UTC ( #11137826=note: print w/replies, xml ) Need Help??


in reply to Re: Pattern matching in binary mode (I/O)
in thread Pattern matching in binary mode

Appreciated this comment, as it's one of the few useful things Google returned when searching for "perl sliding window string replace". I used it to make something similar that uses substr() instead of a regex, and bumps the buffer up if the search string is larger than the window size.

# note: only lightly tested, ymmv sub sliding_replace { my($srcfile,$dstfile,$search,$replace)=@_; if (! -e $file) { die("File [$file] does not exist\n"); } open(my $src,'<:raw',$srcfile); open(my $dst,'>:raw',$dstfile); my $winsize=4096; my $buf= ''; while(1) { my $bytecount=$src->sysread($buf, $winsize*2, length($buf)); while (1) { my $index=index($buf,$search); if ($index > 0) { substr($buf,$index,length($search),$replace); my $len=$index+length($replace); $dst->print(substr($buf,0,$len,'')); } else { $dst->print(substr($buf,0,$winsize,'')); last; } } last if $bytecount == 0; } # print any leftovers $dst->print($buf); $src->close(); $dst->close(); }

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://11137826]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others making s'mores by the fire in the courtyard of the Monastery: (1)
As of 2022-01-18 02:57 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    In 2022, my preferred method to securely store passwords is:












    Results (52 votes). Check out past polls.

    Notices?