Skip to content

Instantly share code, notes, and snippets.

@makoto
Last active April 25, 2018 10:45
Show Gist options
  • Save makoto/74481c051c4874be2ebdd82053fef961 to your computer and use it in GitHub Desktop.
Save makoto/74481c051c4874be2ebdd82053fef961 to your computer and use it in GitHub Desktop.
import "./BytesUtils.sol";
import "./RRUtils.sol";
contract Test {
using BytesUtils for *;
using RRUtils for *;
bytes public foo;
event Logger(string name);
event LoggerBytes(bytes name);
event LoggerInt(uint label);
// 0x0378797a00 = 'xyz.'
// 0x066274686c61620378797a00 = 'bthlab.xyz.'
// 0x066574686c61620378797a00 = 'ethlab.xyz.'
// a: 'bthlab.xyz.' b: 'xyz.',
// [6, 98, 116, 104, 108, 97, 98, 3, 120, 121, 122, 0], [3, 120, 121, 122, 0]
// a: 'bthlab.xyz.' b: 'ethlab.xyz.'
// [6, 98, 116, 104, 108, 97, 98, 3, 120, 121, 122, 0], [6, 101, 116, 104, 108, 97, 98, 3, 120, 121, 122, 0]
// [6, 101, 116, 104, 108, 97, 98, 3, 120, 121, 122, 0], [6, 98, 116, 104, 108, 97, 98, 3, 120, 121, 122, 0]
// [6, 101, 116, 104, 108, 97, 98, 3, 120, 121, 122, 0], [6, 101, 116, 104, 108, 97, 98, 3, 120, 121, 122, 0]
function compare(bytes a, bytes b) public returns (int) {
// when both are '.'
if (keccak256(a) == keccak256('') && keccak256(b) == keccak256('')){
return 0;
}
BytesUtils.Slice memory aSlice = a.toSlice();
BytesUtils.Slice memory bSlice = b.toSlice();
// getting head
BytesUtils.Slice memory aHead;
BytesUtils.Slice memory bHead;
BytesUtils.Slice memory aTail;
BytesUtils.Slice memory bTail;
(aHead, aTail) = headAndTail(aSlice);
(bHead, bTail) = headAndTail(bSlice);
//uint result = compareTail(aTail, bTail);
uint result = 0;
if(result == 0){
return aHead.compare(bHead);
}
}
function headAndTail(BytesUtils.Slice aSlice) internal returns(BytesUtils.Slice head, BytesUtils.Slice tail){
uint aHeadLength = aSlice.uint8At(0);
BytesUtils.Slice memory aHead;
aHead.copyFrom(aSlice).s(1,aHeadLength);
// getting tail
BytesUtils.Slice memory aTail;
aTail.copyFrom(aSlice).s(1 + aHeadLength, aSlice.len);
return (aHead, aTail);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment