Skip to content

Instantly share code, notes, and snippets.

@cldg
Created July 31, 2018 20:57
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save cldg/d73800e839ce8c4ecda0d938ea725ac2 to your computer and use it in GitHub Desktop.
Save cldg/d73800e839ce8c4ecda0d938ea725ac2 to your computer and use it in GitHub Desktop.
Created using remix-ide: Realtime Ethereum Contract Compiler and Runtime. Load this file by pasting this gists URL or ID at https://remix.ethereum.org/#version=soljson-v0.4.24+commit.e67f0147.js&optimize=false&gist=
pragma solidity ^0.4.24;
contract Concatenation {
struct Element{
string first;
string second;
string separator;
bytes concatenatedValue;
uint concatenatedValueIndex;
}
Element element;
function concatenate(string _first, string _second, string _separator) public payable returns (string){
element = Element(_first, _second, _separator, "0X12", 0);
createConcatenatedValue(element);
executeConcatenation();
return getConcatanatedValue();
}
function createConcatenatedValue(Element _element) private{
string memory value = new string(bytes(_element.first).length + bytes(_element.second).length + bytes(_element.separator).length);
element.concatenatedValue = bytes(value);
}
function add(string _value) private {
bytes memory value = bytes(_value);
for(uint index = 0; index < value.length; index++){
element.concatenatedValue[element.concatenatedValueIndex++] = value[index];
}
}
function executeConcatenation() private {
add(element.first);
add(element.separator);
add(element.second);
}
function getConcatanatedValue() private view returns(string){
return string(element.concatenatedValue);
}
}
pragma solidity ^0.4.24;
contract Concatenation {
function concatenate(string _first, string _second, string _separator) public view returns (string) {}
}
contract StringUtil {
address concatenationContractAddress = 0x0dcd2f752394c41875e259e00bb44fd505297caf;
Concatenation concatenation = Concatenation(concatenationContractAddress);
function concatenate(string _first, string _second, string _separator) public view returns (string){
return concatenation.concatenate(_first, _second, _separator);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment