Skip to content

Instantly share code, notes, and snippets.

@webcaetano
Created August 6, 2018 14:15
Show Gist options
  • Save webcaetano/48f178fde7d8082eb323f68ae18faf9a to your computer and use it in GitHub Desktop.
Save webcaetano/48f178fde7d8082eb323f68ae18faf9a to your computer and use it in GitHub Desktop.

Revisions

  1. webcaetano created this gist Aug 6, 2018.
    61 changes: 61 additions & 0 deletions Utils.sol
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,61 @@
    pragma solidity ^0.4.22;


    contract Utils {
    function Utils() public {
    }

    // function compareStrings (string a, string b) view returns (bool){
    // return keccak256(a) == keccak256(b);
    // }

    // // verifies that an amount is greater than zero
    // modifier greaterThanZero(uint256 _amount) {
    // require(_amount > 0);
    // _;
    // }

    // validates an address - currently only checks that it isn't null
    modifier validAddress(address _address) {
    require(_address != address(0));
    _;
    }

    // verifies that the address is different than this contract address
    modifier notThis(address _address) {
    require(_address != address(this));
    _;
    }

    function strlen(string s) internal pure returns (uint) {
    // Starting here means the LSB will be the byte we care about
    uint ptr;
    uint end;
    assembly {
    ptr := add(s, 1)
    end := add(mload(s), ptr)
    }

    for (uint len = 0; ptr < end; len++) {
    uint8 b;
    assembly { b := and(mload(ptr), 0xFF) }
    if (b < 0x80) {
    ptr += 1;
    } else if(b < 0xE0) {
    ptr += 2;
    } else if(b < 0xF0) {
    ptr += 3;
    } else if(b < 0xF8) {
    ptr += 4;
    } else if(b < 0xFC) {
    ptr += 5;
    } else {
    ptr += 6;
    }
    }

    return len;
    }


    }
    27 changes: 27 additions & 0 deletions bundinha.sol
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,27 @@
    pragma solidity ^0.4.22;

    import './Utils.sol';

    contract bundinha is Utils {
    uint N;
    string bundinha;


    function setN(uint x) public {
    N = x;
    }

    function getN() constant public returns (uint) {
    return N;
    }

    function setBundinha(string x) public {
    require(strlen(x) <= 32);
    bundinha = x;
    }

    function getBundinha() constant public returns (string){
    return bundinha;
    }

    }