Skip to content

Instantly share code, notes, and snippets.

@pipermerriam
Created October 26, 2016 19:32
Show Gist options
  • Save pipermerriam/3b32915b0eb0446d81de7aaabdce1512 to your computer and use it in GitHub Desktop.
Save pipermerriam/3b32915b0eb0446d81de7aaabdce1512 to your computer and use it in GitHub Desktop.
pragma solidity ^0.4.0;
library StringLib {
function concat(string storage _head, string _tail) returns (bool) {
bytes head = bytes(_head);
bytes memory tail = bytes(_tail);
for (uint i = 0; i < tail.length; i++) {
head.push(tail[i]);
}
_head = string(head);
return true;
}
function concatByte(string storage value, bytes1 b) returns (bool) {
bytes memory _b = new bytes(1);
_b[0] = b;
return concat(value, string(_b));
}
function concatUInt(string storage value, uint n) returns (bool) {
if (n == 0) {
return concatByte(value, byte(48));
}
uint exp;
if (n >= 10 ** 77) {
exp = 77;
} else {
while (n >= 10 ** (exp + 1)) {
exp += 1;
}
}
for (uint i = 0; i <= exp; i++) {
concatByte(value, byte(n / 10 ** (exp - i) + 48));
n %= 10 ** (exp - i);
}
return true;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment