-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathStringUtils.sol
More file actions
46 lines (40 loc) · 1.07 KB
/
Copy pathStringUtils.sol
File metadata and controls
46 lines (40 loc) · 1.07 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
library StringUtils {
function substr(string text, uint start, uint end) private returns (string){
bytes memory d = bytes(text);
bytes memory b = new bytes(end-start);
for(uint i = start; i<end;i++){
b[i - start]=d[i];
}
return string(b);
}
function indexOf(string _haystack, string _needle) returns (int){
return indexOf(_haystack,_needle,0);
}
function indexOf(string _haystack, string _needle,uint _start) returns (int)
{
bytes memory h = bytes(_haystack);
bytes memory n = bytes(_needle);
if(h.length < 1 || n.length < 1 || (n.length > h.length))
return -1;
else if(h.length > (2**128 -1))
return -1;
else
{
uint subindex = 0;
for (uint i = _start; i < h.length; i ++)
{
if (h[i] == n[0])
{
subindex = 1;
while(subindex < n.length && (i + subindex) < h.length && h[i + subindex] == n[subindex])
{
subindex++;
}
if(subindex == n.length)
return int(i);
}
}
return -1;
}
}
}