[Date Prev][Date Next][Thread Prev][Thread Next]
[Search]
[Date Index]
[Thread Index]
[FWP] Simplification #2
# Problem - pad out a string
# Here is the original snippet of code:
$nSize = length($str);
if ($nSize > 0 )
{
while ((($nSize + 1) % 4) != 0)
{
$nSize++;
}
$nSize++;
$nValue = $nOffset;
$nOffset += $nSize;
}
# and the corresponding part where they pad out the string:
# Pad out the string with NULLs out to the nearest 4 byte mark.
while (((length($str) + 1) % 4) != 0)
{
$str .= "\0";
$nSize++;
}
# you can change the loop to:
while ((++$nSize % 4) != 0) {
# the test does it all
}
# Or stop all this nonsense and just write:
$nSize += 4 - ($nSize % 4);
# Since what we are doing is computing:
# the length of the string + one null terminator +
# some null bytes to pad to a modulo four boundary.
# Note that our problem was simplified because we are always going
# to add at least one byte.
# I like this sort of "condensing" exercise. The best thing here is that
# the condensation doesn't simply obfuscate; it actually simplifies
-- --
|\ _,,,---,,_ Vicki Brown <vlb@cfcl.com>
ZZZzz /,`.-'`' -. ;-;;,_ Journeyman Sourceror: Scripts & Philtres
|,4- ) )-,_. ,\ ( `'-' P.O. Box 1269 San Bruno CA 94066
'---''(_/--' `-'\_) http://www.cfcl.com/~vlb http://www.macperl.com
==== Want to unsubscribe from Fun With Perl?
==== Well, if you insist... Send mail with body "unsubscribe" to
==== fwp-request@technofile.org