|
| 1 | +// Jest Snapshot v1, https://goo.gl/fbAQLP |
| 2 | + |
| 3 | +exports[`IndexOf.sol 1`] = ` |
| 4 | +contract IndexOf { |
| 5 | +
|
| 6 | + address creator; |
| 7 | +
|
| 8 | + function IndexOf() |
| 9 | + { |
| 10 | + creator = msg.sender; |
| 11 | + } |
| 12 | + |
| 13 | + int whatwastheval = -10; // -2 = not yet tested, as separate from -1, tested but error |
| 14 | + |
| 15 | + function indexOf(string _a, string _b) returns (int) // _a = string to search, _b = string we want to find |
| 16 | + { |
| 17 | + bytes memory a = bytes(_a); |
| 18 | + bytes memory b = bytes(_b); |
| 19 | + |
| 20 | + if(a.length < 1 || b.length < 1 || (b.length > a.length)) |
| 21 | + { |
| 22 | + whatwastheval = -1; |
| 23 | + return -1; |
| 24 | + } |
| 25 | + else if(a.length > (2**128 -1)) // since we have to be able to return -1 (if the char isn't found or input error), this function must return an "int" type with a max length of (2^128 - 1) |
| 26 | + { |
| 27 | + whatwastheval = -1; |
| 28 | + return -1; |
| 29 | + } |
| 30 | + else |
| 31 | + { |
| 32 | + uint subindex = 0; |
| 33 | + for (uint i = 0; i < a.length; i ++) |
| 34 | + { |
| 35 | + if (a[i] == b[0]) // found the first char of b |
| 36 | + { |
| 37 | + subindex = 1; |
| 38 | + while(subindex < b.length && (i + subindex) < a.length && a[i + subindex] == b[subindex]) // search until the chars don't match or until we reach the end of a or b |
| 39 | + { |
| 40 | + subindex++; |
| 41 | + } |
| 42 | + if(subindex == b.length) |
| 43 | + { |
| 44 | + whatwastheval = int(i); |
| 45 | + return int(i); |
| 46 | + } |
| 47 | + } |
| 48 | + } |
| 49 | + whatwastheval = -1; |
| 50 | + return -1; |
| 51 | + } |
| 52 | + } |
| 53 | + |
| 54 | + function whatWasTheVal() constant returns (int) |
| 55 | + { |
| 56 | + return whatwastheval; |
| 57 | + } |
| 58 | + |
| 59 | + /********** |
| 60 | + Standard kill() function to recover funds |
| 61 | + **********/ |
| 62 | + |
| 63 | + function kill() |
| 64 | + { |
| 65 | + if (msg.sender == creator) |
| 66 | + { |
| 67 | + suicide(creator); // kills this contract and sends remaining funds back to creator |
| 68 | + } |
| 69 | + } |
| 70 | +} |
| 71 | +~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
| 72 | +contract IndexOf { |
| 73 | + address creator; |
| 74 | +
|
| 75 | + function IndexOf() { |
| 76 | + creator = msg.sender; |
| 77 | + } |
| 78 | +
|
| 79 | + int whatwastheval = -10; // -2 = not yet tested, as separate from -1, tested but error |
| 80 | +
|
| 81 | + function indexOf(string _a, string _b) returns(int) { // _a = string to search, _b = string we want to find |
| 82 | + bytes a = bytes(_a); |
| 83 | + bytes b = bytes(_b); |
| 84 | +
|
| 85 | + if (a.length < 1 || b.length < 1 || (b.length > a.length)) { |
| 86 | + whatwastheval = -1; |
| 87 | + return -1; |
| 88 | + } else if (a.length > (2 ** 128 - 1)) { // since we have to be able to return -1 (if the char isn't found or input error), this function must return an "int" type with a max length of (2^128 - 1) |
| 89 | + whatwastheval = -1; |
| 90 | + return -1; |
| 91 | + } else { |
| 92 | + uint subindex = 0; |
| 93 | + for (uint i = 0; i < a.length; i++) { |
| 94 | + if (a[i] == b[0]) { // found the first char of b |
| 95 | + subindex = 1; |
| 96 | + while (subindex < b.length && (i + subindex) < a.length && a[i + subindex] == b[subindex]) { // search until the chars don't match or until we reach the end of a or b |
| 97 | + subindex++; |
| 98 | + } |
| 99 | + if (subindex == b.length) { |
| 100 | + whatwastheval = int(i); |
| 101 | + return int(i); |
| 102 | + } |
| 103 | + } |
| 104 | + } |
| 105 | + whatwastheval = -1; |
| 106 | + return -1; |
| 107 | + } |
| 108 | + } |
| 109 | +
|
| 110 | + function whatWasTheVal() constant returns(int) { |
| 111 | + return whatwastheval; |
| 112 | + } |
| 113 | +
|
| 114 | + /********** |
| 115 | + Standard kill() function to recover funds |
| 116 | + **********/ |
| 117 | +
|
| 118 | + function kill() { |
| 119 | + if (msg.sender == creator) { |
| 120 | + suicide(creator); // kills this contract and sends remaining funds back to creator |
| 121 | + } |
| 122 | + } |
| 123 | +} |
| 124 | +
|
| 125 | +`; |
0 commit comments