Core Function BinaryReplace
From Sputnik Wiki
(Difference between revisions)
(→Example) |
|||
Line 1: | Line 1: | ||
<pre> | <pre> | ||
− | BinaryReplace( <binary-array>, <needle>, <replacement> ) | + | BinaryReplace( <binary-array>, <needle>, <replacement>, <maxReplacements> ) |
</pre> | </pre> | ||
Line 24: | Line 24: | ||
(It will be cast as binary if it isn't already) | (It will be cast as binary if it isn't already) | ||
+ | |||
+ | ==== maxReplacements ==== | ||
+ | |||
+ | Optional; The max allowed replacements to do | ||
=== Return Value === | === Return Value === | ||
Line 95: | Line 99: | ||
// Print changed | // Print changed | ||
say $subject; // Hello Spuntik ok Spuntik hehe Spuntik!!! | say $subject; // Hello Spuntik ok Spuntik hehe Spuntik!!! | ||
+ | </syntaxhighlight> | ||
+ | |||
+ | Only allow one replacement | ||
+ | |||
+ | <syntaxhighlight lang="sputnik"> | ||
+ | // Assign a value to do the replace on | ||
+ | $subject = Pack("A*", "Hello world ok world hehe world!!!"); | ||
+ | // Assign a value to find | ||
+ | $find = Pack("A*", "world"); | ||
+ | // Assign a value to replace the found with | ||
+ | $replace = Pack("A*", "Spuntik"); | ||
+ | |||
+ | // Print unchanged | ||
+ | say $subject; // Hello world ok world hehe world!!! | ||
+ | |||
+ | // Do the replacement | ||
+ | BinaryReplace($subject, $find, $replace, 1); | ||
+ | |||
+ | // Print changed | ||
+ | say $subject; // Hello Spuntik ok world hehe world!!! | ||
+ | </syntaxhighlight> | ||
+ | |||
+ | Only allow two replacements | ||
+ | |||
+ | <syntaxhighlight lang="sputnik"> | ||
+ | // Assign a value to do the replace on | ||
+ | $subject = Pack("A*", "Hello world ok world hehe world!!!"); | ||
+ | // Assign a value to find | ||
+ | $find = Pack("A*", "world"); | ||
+ | // Assign a value to replace the found with | ||
+ | $replace = Pack("A*", "Spuntik"); | ||
+ | |||
+ | // Print unchanged | ||
+ | say $subject; // Hello world ok world hehe world!!! | ||
+ | |||
+ | // Do the replacement | ||
+ | BinaryReplace($subject, $find, $replace, 1); | ||
+ | |||
+ | // Print changed | ||
+ | say $subject; // Hello Spuntik ok Spuntik hehe world!!! | ||
</syntaxhighlight> | </syntaxhighlight> | ||
[[Category:Core Function]] | [[Category:Core Function]] |
Revision as of 06:57, 11 August 2014
BinaryReplace( <binary-array>, <needle>, <replacement>, <maxReplacements> )
Contents |
Description
Search for a byte or string of bytes in a binary variable and replace it.
Parameters
binary-array
The binary variable to do the replacement on.
needle
The needle to find.
(It will be cast as binary if it isn't already)
replacement
The replacement to use.
(It will be cast as binary if it isn't already)
maxReplacements
Optional; The max allowed replacements to do
Return Value
Success: Returns true.
Failure: Returns false.
Remarks
None.
Example
Simple replacement
// Assign a value to do the replace on $subject = Pack("A*", "Hello world!!!"); // Assign a value to find $find = Pack("A*", "world"); // Assign a value to replace the found with $replace = Pack("A*", "cat"); // Print unchanged say $subject; // Hello world!!! // Do the replacement BinaryReplace($subject, $find, $replace); // Print changed say $subject; // Hello cat!!!
This time the replacement is bigger than what is being replaced
// Assign a value to do the replace on $subject = Pack("A*", "Hello world!!!"); // Assign a value to find $find = Pack("A*", "world"); // Assign a value to replace the found with $replace = Pack("A*", "Spuntik"); // Print unchanged say $subject; // Hello world!!! // Do the replacement BinaryReplace($subject, $find, $replace); // Print changed say $subject; // Hello Spuntik!!!
Of course it will replace all it finds
// Assign a value to do the replace on $subject = Pack("A*", "Hello world ok world hehe world!!!"); // Assign a value to find $find = Pack("A*", "world"); // Assign a value to replace the found with $replace = Pack("A*", "Spuntik"); // Print unchanged say $subject; // Hello world ok world hehe world!!! // Do the replacement BinaryReplace($subject, $find, $replace); // Print changed say $subject; // Hello Spuntik ok Spuntik hehe Spuntik!!!
Only allow one replacement
// Assign a value to do the replace on $subject = Pack("A*", "Hello world ok world hehe world!!!"); // Assign a value to find $find = Pack("A*", "world"); // Assign a value to replace the found with $replace = Pack("A*", "Spuntik"); // Print unchanged say $subject; // Hello world ok world hehe world!!! // Do the replacement BinaryReplace($subject, $find, $replace, 1); // Print changed say $subject; // Hello Spuntik ok world hehe world!!!
Only allow two replacements
// Assign a value to do the replace on $subject = Pack("A*", "Hello world ok world hehe world!!!"); // Assign a value to find $find = Pack("A*", "world"); // Assign a value to replace the found with $replace = Pack("A*", "Spuntik"); // Print unchanged say $subject; // Hello world ok world hehe world!!! // Do the replacement BinaryReplace($subject, $find, $replace, 1); // Print changed say $subject; // Hello Spuntik ok Spuntik hehe world!!!