Submission efe636cb...

ChallengeString search
Submitterricmoo.firefly.eth
Submitted at2018-23-31
Gas used1376364
contract IndexOf {
    function indexOf(string haystackString, string needleString) public pure returns(int) {
        bytes memory haystack = bytes(haystackString);
        bytes memory needle = bytes(needleString);

        uint haystackLength = haystack.length;
        uint needleLength = needle.length;

        if(needleLength > haystackLength) { return -1; }

        uint end = haystackLength - needleLength + 1;
        for (uint i = 0; i < end; i++) {
            uint found = 1;
            for (uint j = 0; j < needleLength; j++) {
                if (haystack[i + j] != needle[j]) {
                    found = 0;
                    break;
                }
            }

            if (found == 1) {
                return int(i);
            }
        }

        return -1;
    }
}