Try this:
var %x = 1
while %x <= $bvar(&buffer,0) && $bfind(&buffer,%x,10) {
bcopy &temp $calc($bvar(&temp,0) + 1) &buffer %x $calc($v1 - %x)
bset &temp $calc($bvar(&temp,0) + 1) 13 10
%x = $v1 + 1
}
if $bvar(&buffer,0) >= %x { bcopy &temp $calc($bvar(&temp,0) + 1) &buffer %x -1 }
bcopy -c &buffer 1 &temp 1 -1
Normally, the
%x <= $bvar(&buffer,0) condition in the while loop shouldn't be necessary, but there's a bug in $bfind(); if you're searching a binvar for a character and that character is the last byte in that binvar and you search past that byte (ie with index $calc($bvar(&binvar,0) + 1) ), it claims to have found it, while this is certainly not true (how can you find something when you search
past its position?).
Personally, I would probably follow a different approach though:
btrunc temp.tmp 0
bwrite temp.tmp 1 -1 &buffer
filter -ffc temp.tmp temp.tmp
bread temp.tmp 0 $file(temp.tmp) &buffer
.remove temp.tmp
Apart from it being simpler in understanding/writing, it is also faster, since all the work is done internally. The fact that you're writing to a file may slow it down a bit, but, in the end, it has proven faster than the first method for me. Btw, keep in mind that /filter adds a CRLF at the end of the file, if it isn't there already, so you may want to trim that trailing CRLF, depending on what you need the binvar for.