{"action":"created","comment":{"url":"https://api.github.com/repos/ethereum/dapp-bin/pulls/comments/61724408","id":61724408,"diff_hunk":"@@ -0,0 +1,171 @@\n+/// Used to create slices from 'bytes'.\n+contract ByteSlice {\n+\n+    struct Slice {\n+        uint _unsafe_memPtr;   // Memory address of the first byte.\n+        uint _unsafe_len;      // Length.\n+    }\n+\n+    /// Create a slice from bytes.\n+    function fromBytes(bytes memory bts) internal constant returns (Slice memory slice) {\n+        uint memPtr;\n+        assembly {\n+            memPtr := add(bts, 0x20)\n+        }\n+        slice = Slice(memPtr, bts.length);\n+    }\n+\n+    /// Length of the slice.\n+    function len(Slice memory slice) internal constant returns (uint) {\n+        return slice._unsafe_len;\n+    }\n+\n+    /// returns the byte at 'index'.\n+    /// Index must be less then 'len(slice)', or the function will throw.\n+    function at(Slice memory slice, uint index) internal constant returns (byte b) {\n+        if (index >= slice._unsafe_len)\n+            throw;\n+        uint start = slice._unsafe_memPtr;\n+        uint bb;\n+        assembly {\n+            bb := byte(0, mload(add(start, index)))\n+        }\n+        b = byte(bb);\n+    }\n+\n+    /// returns the byte at index 'index', where 'index' is allowed to be negative.\n+    /// The index must be less then 'len(slice)', and greater then '-len(slice)'\n+    function at(Slice memory slice, int index) internal constant returns (byte b) {\n+        if (index >= 0)\n+            return at(slice, uint(index));\n+        uint iAbs = uint(-index);\n+        if (iAbs > slice._unsafe_len)\n+            throw;\n+        return at(slice, slice._unsafe_len - iAbs);\n+    }\n+\n+    /// creates a 'bytes' memory-variable from a slice, copying the data.\n+    function toBytes(Slice memory slice) internal constant returns (bytes memory bts) {\n+        uint len = slice._unsafe_len;\n+        uint memPtr = slice._unsafe_memPtr;\n+        bts = new bytes(len);\n+        if (len == 0)\n+            return;\n+        // We can do word-by-word copying since 'bts' was the last thing to be\n+        // allocated. Just overwrite any excess bytes with zeroes at the end.\n+        assembly {\n+            {\n+                    let i := 0\n+                    let btsOffset := add(bts, 0x20)\n+                    let words := add(add(div(len, 32), gt(mod(len, 32), 0)), 1)","path":"androlo/bytes/ByteSlice.sol","position":60,"original_position":60,"commit_id":"9b57b2992cca18c3cc70c3543d34ab626a64213e","original_commit_id":"9b57b2992cca18c3cc70c3543d34ab626a64213e","user":{"login":"chriseth","id":9073706,"avatar_url":"https://avatars.githubusercontent.com/u/9073706?v=3","gravatar_id":"","url":"https://api.github.com/users/chriseth","html_url":"https://github.com/chriseth","followers_url":"https://api.github.com/users/chriseth/followers","following_url":"https://api.github.com/users/chriseth/following{/other_user}","gists_url":"https://api.github.com/users/chriseth/gists{/gist_id}","starred_url":"https://api.github.com/users/chriseth/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/chriseth/subscriptions","organizations_url":"https://api.github.com/users/chriseth/orgs","repos_url":"https://api.github.com/users/chriseth/repos","events_url":"https://api.github.com/users/chriseth/events{/privacy}","received_events_url":"https://api.github.com/users/chriseth/received_events","type":"User","site_admin":false},"body":"You might try `div(add(len, 31), 32)` here.","created_at":"2016-05-02T10:33:06Z","updated_at":"2016-05-02T10:33:06Z","html_url":"https://github.com/ethereum/dapp-bin/pull/59#discussion_r61724408","pull_request_url":"https://api.github.com/repos/ethereum/dapp-bin/pulls/59","_links":{"self":{"href":"https://api.github.com/repos/ethereum/dapp-bin/pulls/comments/61724408"},"html":{"href":"https://github.com/ethereum/dapp-bin/pull/59#discussion_r61724408"},"pull_request":{"href":"https://api.github.com/repos/ethereum/dapp-bin/pulls/59"}}},"pull_request":{"url":"https://api.github.com/repos/ethereum/dapp-bin/pulls/59","id":68508553,"html_url":"https://github.com/ethereum/dapp-bin/pull/59","diff_url":"https://github.com/ethereum/dapp-bin/pull/59.diff","patch_url":"https://github.com/ethereum/dapp-bin/pull/59.patch","issue_url":"https://api.github.com/repos/ethereum/dapp-bin/issues/59","number":59,"state":"open","locked":false,"title":"Basic (bytes) slices and some math functions.","user":{"login":"androlo","id":2809499,"avatar_url":"https://avatars.githubusercontent.com/u/2809499?v=3","gravatar_id":"","url":"https://api.github.com/users/androlo","html_url":"https://github.com/androlo","followers_url":"https://api.github.com/users/androlo/followers","following_url":"https://api.github.com/users/androlo/following{/other_user}","gists_url":"https://api.github.com/users/androlo/gists{/gist_id}","starred_url":"https://api.github.com/users/androlo/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/androlo/subscriptions","organizations_url":"https://api.github.com/users/androlo/orgs","repos_url":"https://api.github.com/users/androlo/repos","events_url":"https://api.github.com/users/androlo/events{/privacy}","received_events_url":"https://api.github.com/users/androlo/received_events","type":"User","site_admin":false},"body":"**ByteSlice**\r\n\r\nUsed to create slices from `bytes memory` variables. Slices point to the memory used by the input bytes, and the length. They can be sliced further, using several methods with rules that are common for most slice implementations. More thorough documentation can be found in the [storage repo](https://github.com/androlo/standard-contracts#byteslice), where docs and tests are kept.\r\n\r\nThey should be gas-efficient, but more can be done.\r\n\r\n**Integers**\r\n\r\nCurrently only has two functions, `invmod` and `expmod`. Docs [here](https://github.com/androlo/standard-contracts#integers).\r\n\r\n**Comments**\r\n\r\nGot more contracts as well, such as an RLP reader and a secp256k1 implementation, and while they seem to work well, I will not PR them until some testing and documentation issues has been cleared up.\r\n\r\nThe tests for these contracts are done using RPC calls with web3. For contracts like `Integers`, which is just an ordinary library with public functions, the tests are done using RPC calls with certain input, then making assertions on the output (using mocha as testing framework, same as web3).\r\n\r\nFor contracts that are \"internal by nature\", meaning they are meant to be extended by other contracts, and not called over RPC, I have used many different (more or less sophisticated) methods for testing. What I did now is what seems like the simplest possible solution: create a special test-contract that either exposes the internal functions, or if that isn't possible, just add functions that works on the internal types and functions then return a boolean - true if everything worked, otherwise false.\r\n\r\nFor automated builds I use local solc + my own little build tool, although the paths in the contracts are on the form 'github.com/...' so it is possible for others to import and just use whatever they see fit. This is not an issue with these two contracts though since they have no imports, but it will be for secp256k1 for example, which imports the integer contract. It calls the integer contract as a library btw, which makes the normal RPC tests a bit weird. The way I solve it normally is by running the tests through a custom test tool, but for web3 tests I just deploy the Integer contract first, copy the address that web3 returns and do some \"ghetto linking\" of the secp256k1 bytecode  (basically just replace any occurences of `__Integers_____ ...` with the address before deploying the contract).\r\n\r\nI'm caught up on the issues with the new RPC-based Solidity contract testing for core Solidity, and would like the contracts I add here to be compatible - should they (or anything else I make) be accepted into a standard library. That's why I'm moving towards RPC only, and no home made stuff.\r\n\r\nI don't expect this PR to be successful btw. What I want is feedback. When/if everything is fine I will add the final touches, complement with some more tests, docs, and examples, and make a new one. Then do the same for my other contracts.","created_at":"2016-05-01T21:48:09Z","updated_at":"2016-05-02T10:33:06Z","closed_at":null,"merged_at":null,"merge_commit_sha":"5c615c2e3f86979a284178f6fad2838fb55358a7","assignee":null,"milestone":null,"commits_url":"https://api.github.com/repos/ethereum/dapp-bin/pulls/59/commits","review_comments_url":"https://api.github.com/repos/ethereum/dapp-bin/pulls/59/comments","review_comment_url":"https://api.github.com/repos/ethereum/dapp-bin/pulls/comments{/number}","comments_url":"https://api.github.com/repos/ethereum/dapp-bin/issues/59/comments","statuses_url":"https://api.github.com/repos/ethereum/dapp-bin/statuses/9b57b2992cca18c3cc70c3543d34ab626a64213e","head":{"label":"androlo:master","ref":"master","sha":"9b57b2992cca18c3cc70c3543d34ab626a64213e","user":{"login":"androlo","id":2809499,"avatar_url":"https://avatars.githubusercontent.com/u/2809499?v=3","gravatar_id":"","url":"https://api.github.com/users/androlo","html_url":"https://github.com/androlo","followers_url":"https://api.github.com/users/androlo/followers","following_url":"https://api.github.com/users/androlo/following{/other_user}","gists_url":"https://api.github.com/users/androlo/gists{/gist_id}","starred_url":"https://api.github.com/users/androlo/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/androlo/subscriptions","organizations_url":"https://api.github.com/users/androlo/orgs","repos_url":"https://api.github.com/users/androlo/repos","events_url":"https://api.github.com/users/androlo/events{/privacy}","received_events_url":"https://api.github.com/users/androlo/received_events","type":"User","site_admin":false},"repo":{"id":57781723,"name":"dapp-bin","full_name":"androlo/dapp-bin","owner":{"login":"androlo","id":2809499,"avatar_url":"https://avatars.githubusercontent.com/u/2809499?v=3","gravatar_id":"","url":"https://api.github.com/users/androlo","html_url":"https://github.com/androlo","followers_url":"https://api.github.com/users/androlo/followers","following_url":"https://api.github.com/users/androlo/following{/other_user}","gists_url":"https://api.github.com/users/androlo/gists{/gist_id}","starred_url":"https://api.github.com/users/androlo/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/androlo/subscriptions","organizations_url":"https://api.github.com/users/androlo/orgs","repos_url":"https://api.github.com/users/androlo/repos","events_url":"https://api.github.com/users/androlo/events{/privacy}","received_events_url":"https://api.github.com/users/androlo/received_events","type":"User","site_admin":false},"private":false,"html_url":"https://github.com/androlo/dapp-bin","description":"A place for all the ÐApps to live","fork":true,"url":"https://api.github.com/repos/androlo/dapp-bin","forks_url":"https://api.github.com/repos/androlo/dapp-bin/forks","keys_url":"https://api.github.com/repos/androlo/dapp-bin/keys{/key_id}","collaborators_url":"https://api.github.com/repos/androlo/dapp-bin/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/androlo/dapp-bin/teams","hooks_url":"https://api.github.com/repos/androlo/dapp-bin/hooks","issue_events_url":"https://api.github.com/repos/androlo/dapp-bin/issues/events{/number}","events_url":"https://api.github.com/repos/androlo/dapp-bin/events","assignees_url":"https://api.github.com/repos/androlo/dapp-bin/assignees{/user}","branches_url":"https://api.github.com/repos/androlo/dapp-bin/branches{/branch}","tags_url":"https://api.github.com/repos/androlo/dapp-bin/tags","blobs_url":"https://api.github.com/repos/androlo/dapp-bin/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/androlo/dapp-bin/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/androlo/dapp-bin/git/refs{/sha}","trees_url":"https://api.github.com/repos/androlo/dapp-bin/git/trees{/sha}","statuses_url":"https://api.github.com/repos/androlo/dapp-bin/statuses/{sha}","languages_url":"https://api.github.com/repos/androlo/dapp-bin/languages","stargazers_url":"https://api.github.com/repos/androlo/dapp-bin/stargazers","contributors_url":"https://api.github.com/repos/androlo/dapp-bin/contributors","subscribers_url":"https://api.github.com/repos/androlo/dapp-bin/subscribers","subscription_url":"https://api.github.com/repos/androlo/dapp-bin/subscription","commits_url":"https://api.github.com/repos/androlo/dapp-bin/commits{/sha}","git_commits_url":"https://api.github.com/repos/androlo/dapp-bin/git/commits{/sha}","comments_url":"https://api.github.com/repos/androlo/dapp-bin/comments{/number}","issue_comment_url":"https://api.github.com/repos/androlo/dapp-bin/issues/comments{/number}","contents_url":"https://api.github.com/repos/androlo/dapp-bin/contents/{+path}","compare_url":"https://api.github.com/repos/androlo/dapp-bin/compare/{base}...{head}","merges_url":"https://api.github.com/repos/androlo/dapp-bin/merges","archive_url":"https://api.github.com/repos/androlo/dapp-bin/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/androlo/dapp-bin/downloads","issues_url":"https://api.github.com/repos/androlo/dapp-bin/issues{/number}","pulls_url":"https://api.github.com/repos/androlo/dapp-bin/pulls{/number}","milestones_url":"https://api.github.com/repos/androlo/dapp-bin/milestones{/number}","notifications_url":"https://api.github.com/repos/androlo/dapp-bin/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/androlo/dapp-bin/labels{/name}","releases_url":"https://api.github.com/repos/androlo/dapp-bin/releases{/id}","deployments_url":"https://api.github.com/repos/androlo/dapp-bin/deployments","created_at":"2016-05-01T19:52:59Z","updated_at":"2016-05-01T19:53:00Z","pushed_at":"2016-05-01T20:32:05Z","git_url":"git://github.com/androlo/dapp-bin.git","ssh_url":"git@github.com:androlo/dapp-bin.git","clone_url":"https://github.com/androlo/dapp-bin.git","svn_url":"https://github.com/androlo/dapp-bin","homepage":null,"size":791,"stargazers_count":0,"watchers_count":0,"language":"JavaScript","has_issues":false,"has_downloads":true,"has_wiki":true,"has_pages":false,"forks_count":0,"mirror_url":null,"open_issues_count":0,"forks":0,"open_issues":0,"watchers":0,"default_branch":"master"}},"base":{"label":"ethereum:master","ref":"master","sha":"0aeff249fc85a656bd768983477b3c5794f7f712","user":{"login":"ethereum","id":6250754,"avatar_url":"https://avatars.githubusercontent.com/u/6250754?v=3","gravatar_id":"","url":"https://api.github.com/users/ethereum","html_url":"https://github.com/ethereum","followers_url":"https://api.github.com/users/ethereum/followers","following_url":"https://api.github.com/users/ethereum/following{/other_user}","gists_url":"https://api.github.com/users/ethereum/gists{/gist_id}","starred_url":"https://api.github.com/users/ethereum/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/ethereum/subscriptions","organizations_url":"https://api.github.com/users/ethereum/orgs","repos_url":"https://api.github.com/users/ethereum/repos","events_url":"https://api.github.com/users/ethereum/events{/privacy}","received_events_url":"https://api.github.com/users/ethereum/received_events","type":"Organization","site_admin":false},"repo":{"id":22730041,"name":"dapp-bin","full_name":"ethereum/dapp-bin","owner":{"login":"ethereum","id":6250754,"avatar_url":"https://avatars.githubusercontent.com/u/6250754?v=3","gravatar_id":"","url":"https://api.github.com/users/ethereum","html_url":"https://github.com/ethereum","followers_url":"https://api.github.com/users/ethereum/followers","following_url":"https://api.github.com/users/ethereum/following{/other_user}","gists_url":"https://api.github.com/users/ethereum/gists{/gist_id}","starred_url":"https://api.github.com/users/ethereum/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/ethereum/subscriptions","organizations_url":"https://api.github.com/users/ethereum/orgs","repos_url":"https://api.github.com/users/ethereum/repos","events_url":"https://api.github.com/users/ethereum/events{/privacy}","received_events_url":"https://api.github.com/users/ethereum/received_events","type":"Organization","site_admin":false},"private":false,"html_url":"https://github.com/ethereum/dapp-bin","description":"A place for all the ÐApps to live","fork":false,"url":"https://api.github.com/repos/ethereum/dapp-bin","forks_url":"https://api.github.com/repos/ethereum/dapp-bin/forks","keys_url":"https://api.github.com/repos/ethereum/dapp-bin/keys{/key_id}","collaborators_url":"https://api.github.com/repos/ethereum/dapp-bin/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/ethereum/dapp-bin/teams","hooks_url":"https://api.github.com/repos/ethereum/dapp-bin/hooks","issue_events_url":"https://api.github.com/repos/ethereum/dapp-bin/issues/events{/number}","events_url":"https://api.github.com/repos/ethereum/dapp-bin/events","assignees_url":"https://api.github.com/repos/ethereum/dapp-bin/assignees{/user}","branches_url":"https://api.github.com/repos/ethereum/dapp-bin/branches{/branch}","tags_url":"https://api.github.com/repos/ethereum/dapp-bin/tags","blobs_url":"https://api.github.com/repos/ethereum/dapp-bin/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/ethereum/dapp-bin/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/ethereum/dapp-bin/git/refs{/sha}","trees_url":"https://api.github.com/repos/ethereum/dapp-bin/git/trees{/sha}","statuses_url":"https://api.github.com/repos/ethereum/dapp-bin/statuses/{sha}","languages_url":"https://api.github.com/repos/ethereum/dapp-bin/languages","stargazers_url":"https://api.github.com/repos/ethereum/dapp-bin/stargazers","contributors_url":"https://api.github.com/repos/ethereum/dapp-bin/contributors","subscribers_url":"https://api.github.com/repos/ethereum/dapp-bin/subscribers","subscription_url":"https://api.github.com/repos/ethereum/dapp-bin/subscription","commits_url":"https://api.github.com/repos/ethereum/dapp-bin/commits{/sha}","git_commits_url":"https://api.github.com/repos/ethereum/dapp-bin/git/commits{/sha}","comments_url":"https://api.github.com/repos/ethereum/dapp-bin/comments{/number}","issue_comment_url":"https://api.github.com/repos/ethereum/dapp-bin/issues/comments{/number}","contents_url":"https://api.github.com/repos/ethereum/dapp-bin/contents/{+path}","compare_url":"https://api.github.com/repos/ethereum/dapp-bin/compare/{base}...{head}","merges_url":"https://api.github.com/repos/ethereum/dapp-bin/merges","archive_url":"https://api.github.com/repos/ethereum/dapp-bin/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/ethereum/dapp-bin/downloads","issues_url":"https://api.github.com/repos/ethereum/dapp-bin/issues{/number}","pulls_url":"https://api.github.com/repos/ethereum/dapp-bin/pulls{/number}","milestones_url":"https://api.github.com/repos/ethereum/dapp-bin/milestones{/number}","notifications_url":"https://api.github.com/repos/ethereum/dapp-bin/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/ethereum/dapp-bin/labels{/name}","releases_url":"https://api.github.com/repos/ethereum/dapp-bin/releases{/id}","deployments_url":"https://api.github.com/repos/ethereum/dapp-bin/deployments","created_at":"2014-08-07T17:27:43Z","updated_at":"2016-04-28T10:32:50Z","pushed_at":"2016-05-01T21:48:11Z","git_url":"git://github.com/ethereum/dapp-bin.git","ssh_url":"git@github.com:ethereum/dapp-bin.git","clone_url":"https://github.com/ethereum/dapp-bin.git","svn_url":"https://github.com/ethereum/dapp-bin","homepage":null,"size":791,"stargazers_count":93,"watchers_count":93,"language":"JavaScript","has_issues":true,"has_downloads":true,"has_wiki":true,"has_pages":false,"forks_count":57,"mirror_url":null,"open_issues_count":5,"forks":57,"open_issues":5,"watchers":93,"default_branch":"master"}},"_links":{"self":{"href":"https://api.github.com/repos/ethereum/dapp-bin/pulls/59"},"html":{"href":"https://github.com/ethereum/dapp-bin/pull/59"},"issue":{"href":"https://api.github.com/repos/ethereum/dapp-bin/issues/59"},"comments":{"href":"https://api.github.com/repos/ethereum/dapp-bin/issues/59/comments"},"review_comments":{"href":"https://api.github.com/repos/ethereum/dapp-bin/pulls/59/comments"},"review_comment":{"href":"https://api.github.com/repos/ethereum/dapp-bin/pulls/comments{/number}"},"commits":{"href":"https://api.github.com/repos/ethereum/dapp-bin/pulls/59/commits"},"statuses":{"href":"https://api.github.com/repos/ethereum/dapp-bin/statuses/9b57b2992cca18c3cc70c3543d34ab626a64213e"}}}}