Commit f13f2e7b authored by Kelvin Fichter's avatar Kelvin Fichter Committed by GitHub

cleanup: Bytes32Utils (#293)

parent 34ba270d
...@@ -10,61 +10,86 @@ library Lib_Bytes32Utils { ...@@ -10,61 +10,86 @@ library Lib_Bytes32Utils {
* Internal Functions * * Internal Functions *
**********************/ **********************/
/**
* Converts a bytes32 value to a boolean. Anything non-zero will be converted to "true."
* @param _in Input bytes32 value.
* @return Bytes32 as a boolean.
*/
function toBool( function toBool(
bytes32 _in bytes32 _in
) )
internal internal
pure pure
returns ( returns (
bool _out bool
) )
{ {
return _in != 0; return _in != 0;
} }
/**
* Converts a boolean to a bytes32 value.
* @param _in Input boolean value.
* @return Boolean as a bytes32.
*/
function fromBool( function fromBool(
bool _in bool _in
) )
internal internal
pure pure
returns ( returns (
bytes32 _out bytes32
) )
{ {
return bytes32(uint256(_in ? 1 : 0)); return bytes32(uint256(_in ? 1 : 0));
} }
/**
* Converts a bytes32 value to an address. Takes the *last* 20 bytes.
* @param _in Input bytes32 value.
* @return Bytes32 as an address.
*/
function toAddress( function toAddress(
bytes32 _in bytes32 _in
) )
internal internal
pure pure
returns ( returns (
address _out address
) )
{ {
return address(uint160(uint256(_in))); return address(uint160(uint256(_in)));
} }
/**
* Converts an address to a bytes32.
* @param _in Input address value.
* @return Address as a bytes32.
*/
function fromAddress( function fromAddress(
address _in address _in
) )
internal internal
pure pure
returns ( returns (
bytes32 _out bytes32
) )
{ {
return bytes32(uint256(_in)); return bytes32(uint256(_in));
} }
/**
* Removes the leading zeros from a bytes32 value and returns a new (smaller) bytes value.
* @param _in Input bytes32 value.
* @return Bytes32 without any leading zeros.
*/
function removeLeadingZeros( function removeLeadingZeros(
bytes32 _in bytes32 _in
) )
internal internal
pure pure
returns ( returns (
bytes memory _out bytes memory
) )
{ {
bytes memory out; bytes memory out;
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment