From f9329339985bbe276e4b0f6602431237cca69e4a Mon Sep 17 00:00:00 2001 From: Ricardo Guilherme Schmidt <3esmit@gmail.com> Date: Thu, 6 Apr 2017 19:21:06 +0000 Subject: [PATCH] removed unecessary MLOADs --- contracts/lib/JSONLib.sol | 21 +++++++++----- contracts/lib/StringLib.sol | 58 +++++++++++++++++++++++-------------- 2 files changed, 49 insertions(+), 30 deletions(-) diff --git a/contracts/lib/JSONLib.sol b/contracts/lib/JSONLib.sol index d795afb..40f1d5d 100644 --- a/contracts/lib/JSONLib.sol +++ b/contracts/lib/JSONLib.sol @@ -5,7 +5,8 @@ library JSONLib { function getNextString(bytes _str, uint8 _pos) internal constant returns (string,uint8) { uint8 start = 0; uint8 end = 0; - for (;_str.length > _pos; _pos++) { + uint strl =_str.length; + for (;strl > _pos; _pos++) { if (_str[_pos] == '"'){ //Found quotation mark if(_str[_pos-1] != '\\'){ //is not escaped end = start == 0 ? 0: _pos; @@ -26,12 +27,14 @@ library JSONLib { function getNextUInt(bytes _str, uint8 _pos) internal constant returns (uint,uint8) { uint val = 0; - for (; _str.length > _pos; _pos++) { - if (_str[_pos] == ','){ //Find ends + uint strl =_str.length; + for (; strl > _pos; _pos++) { + byte bp = _str[_pos]; + if (bp == ','){ //Find ends _pos++; break; - }else if ((_str[_pos] >= 48)&&(_str[_pos] <= 57)){ //only ASCII numbers + }else if ((bp >= 48)&&(bp <= 57)){ //only ASCII numbers val *= 10; - val += uint(_str[_pos]) - 48; + val += uint(bp) - 48; } } return (val,_pos); @@ -39,8 +42,10 @@ library JSONLib { function getNextAddr(bytes _str, uint8 _pos) internal constant returns (address, uint8){ uint160 iaddr = 0; - for(;_str.length > _pos; _pos++){ - if (_str[_pos] == '0'){ + uint strl =_str.length; + for(;strl > _pos; _pos++){ + byte bp = _str[_pos]; + if (bp == '0'){ if (_str[_pos+1] == 'x'){ for (_pos=_pos+2; _pos<2+2*20; _pos+=2){ iaddr *= 256; @@ -49,7 +54,7 @@ library JSONLib { _pos++; break; } - }else if (_str[_pos] == ','){ + }else if (bp == ','){ _pos++; break; } diff --git a/contracts/lib/StringLib.sol b/contracts/lib/StringLib.sol index cf28d95..72cca22 100644 --- a/contracts/lib/StringLib.sol +++ b/contracts/lib/StringLib.sol @@ -49,15 +49,18 @@ library StringLib { bytes memory a = bytes(self); bytes memory b = bytes(_b); uint minLength = a.length; - if (b.length < minLength) minLength = b.length; - for (uint i = 0; i < minLength; i ++) - if (a[i] < b[i]) + uint bLength = b.length; + if (bLength < minLength) minLength = bLength; + for (uint i = 0; i < minLength; i++) + byte ai = a[i]; + byte bi = b[i]; + if (ai < bi) return -1; - else if (a[i] > b[i]) + else if (ai > bi) return 1; - if (a.length < b.length) + if (minLength < bLength) return -1; - else if (a.length > b.length) + else if (minLength > bLength) return 1; else return 0; @@ -66,23 +69,27 @@ library StringLib { function indexOf(string _haystack, string _needle) constant returns (int) { bytes memory h = bytes(_haystack); bytes memory n = bytes(_needle); - if(h.length < 1 || n.length < 1 || (n.length > h.length)) + uint hlength = h.length; + uint nlength = n.length; + byte n0 = n[0]; + if(hlength < 1 || nlength < 1 || (nlength > hlength)) return -1; - else if(h.length > (2**128 -1)) + else if(hlength > (2**128 -1)) return -1; else { uint subindex = 0; - for (uint i = 0; i < h.length; i ++) + for (uint i = 0; i < hlength; i ++) { - if (h[i] == n[0]) + if (h[i] == n0) { subindex = 1; - while(subindex < n.length && (i + subindex) < h.length && h[i + subindex] == n[subindex]) + byte nsubindex = n[subindex]; + while(subindex < nlength && (i + subindex) < hlength && h[i + subindex] == nsubindex) { subindex++; } - if(subindex == n.length) + if(subindex == nlength) return int(i); } } @@ -100,15 +107,17 @@ library StringLib { bytes memory bresult = bytes(self); uint mint = 0; bool decimals = false; - for (uint i=0; i= 48)&&(bresult[i] <= 57)){ + uint bresultlength = bresult.length; + byte bresulti = bresult[i]; + for (uint i=0; i< bresultlength; i++){ + if ((bresulti >= 48)&&(bresulti <= 57)){ if (decimals){ if (_b == 0) break; else _b--; } mint *= 10; - mint += uint(bresult[i]) - 48; - } else if (bresult[i] == 46) decimals = true; + mint += uint(bresulti) - 48; + } else if (bresulti == 46) decimals = true; } if (_b > 0) mint *= 10**_b; return mint; @@ -121,14 +130,19 @@ library StringLib { bytes memory _bc = bytes(_c); bytes memory _bd = bytes(_d); bytes memory _be = bytes(_e); - string memory abcde = new string(_ba.length + _bb.length + _bc.length + _bd.length + _be.length); + uint _balength = _ba.length; + uint _bblength = _bb.length; + uint _bclength = _bc.length; + uint _bdlength = _bd.length; + uint _belength = _be.length; + string memory abcde = new string(_balength + _bblength + _bclength + _bdlength + _belength); bytes memory babcde = bytes(abcde); uint k = 0; - for (uint i = 0; i < _ba.length; i++) babcde[k++] = _ba[i]; - for (i = 0; i < _bb.length; i++) babcde[k++] = _bb[i]; - for (i = 0; i < _bc.length; i++) babcde[k++] = _bc[i]; - for (i = 0; i < _bd.length; i++) babcde[k++] = _bd[i]; - for (i = 0; i < _be.length; i++) babcde[k++] = _be[i]; + for (uint i = 0; i < _balength; i++) babcde[k++] = _ba[i]; + for (i = 0; i < _bblength; i++) babcde[k++] = _bb[i]; + for (i = 0; i < _bclength; i++) babcde[k++] = _bc[i]; + for (i = 0; i < _bdlength; i++) babcde[k++] = _bd[i]; + for (i = 0; i < _belength; i++) babcde[k++] = _be[i]; return string(babcde); }