{"action":"created","issue":{"url":"https://api.github.com/repos/nodejs/node/issues/6191","repository_url":"https://api.github.com/repos/nodejs/node","labels_url":"https://api.github.com/repos/nodejs/node/issues/6191/labels{/name}","comments_url":"https://api.github.com/repos/nodejs/node/issues/6191/comments","events_url":"https://api.github.com/repos/nodejs/node/issues/6191/events","html_url":"https://github.com/nodejs/node/pull/6191","id":148216522,"number":6191,"title":"buffer: adds buffer.prototype.split / join","user":{"login":"eljefedelrodeodeljefe","id":3899684,"avatar_url":"https://avatars.githubusercontent.com/u/3899684?v=3","gravatar_id":"","url":"https://api.github.com/users/eljefedelrodeodeljefe","html_url":"https://github.com/eljefedelrodeodeljefe","followers_url":"https://api.github.com/users/eljefedelrodeodeljefe/followers","following_url":"https://api.github.com/users/eljefedelrodeodeljefe/following{/other_user}","gists_url":"https://api.github.com/users/eljefedelrodeodeljefe/gists{/gist_id}","starred_url":"https://api.github.com/users/eljefedelrodeodeljefe/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/eljefedelrodeodeljefe/subscriptions","organizations_url":"https://api.github.com/users/eljefedelrodeodeljefe/orgs","repos_url":"https://api.github.com/users/eljefedelrodeodeljefe/repos","events_url":"https://api.github.com/users/eljefedelrodeodeljefe/events{/privacy}","received_events_url":"https://api.github.com/users/eljefedelrodeodeljefe/received_events","type":"User","site_admin":false},"labels":[{"url":"https://api.github.com/repos/nodejs/node/labels/buffer","name":"buffer","color":"f7c6c7"}],"state":"open","locked":false,"assignee":null,"milestone":null,"comments":9,"created_at":"2016-04-14T00:15:12Z","updated_at":"2016-04-15T20:08:02Z","closed_at":null,"pull_request":{"url":"https://api.github.com/repos/nodejs/node/pulls/6191","html_url":"https://github.com/nodejs/node/pull/6191","diff_url":"https://github.com/nodejs/node/pull/6191.diff","patch_url":"https://github.com/nodejs/node/pull/6191.patch"},"body":"<!--\r\nThank you for submitting a pull request to Node.js. Before you submit, please\r\nreview below requirements and walk through the checklist. You can 'tick'\r\na box by using the letter \"x\": [x].\r\n\r\nRun the test suite by invoking: `make -j4 lint test` on linux or\r\n`vcbuild test nosign` on Windows.\r\n\r\nIf this aims to fix a regression or you’re adding a feature, make sure you also\r\nwrite a test. Finally – if possible – a benchmark that quantifies your changes.\r\n\r\nFinally, read through our contributors guide and make adjustments as necessary:\r\nhttps://github.com/nodejs/node/blob/master/CONTRIBUTING.md\r\n-->\r\n\r\n##### Checklist\r\n\r\n<!-- remove lines that do not apply to you -->\r\n\r\n- [x] tests and code linting passes\r\n- [x] a test and/or benchmark is included\r\n- [x] documentation is changed or added\r\n- [x] the commit message follows commit guidelines\r\n\r\n\r\n##### Affected core subsystem(s)\r\n\r\n<!-- provide affected core subsystem(s) (like doc, cluster, crypto, etc) -->\r\n`buffer`\r\n\r\n##### Description of change\r\n\r\n<!-- provide a description of the change below this comment -->\r\n\r\n> **WIP**\r\n* benchmarks\r\n* one less copy (had problems persisting the input buffer in `.split()`)\r\n* general `c++` related review induced changes, style etc.\r\n\r\n**Description/ Motivation**\r\nThis PR introduces two additions to the `buffer` API, `.split()` and `.join()`. Motivation is too handle buffers directly (say in streams) and having higher efficiency to what would be possible in userland js. This continues the discussion from https://github.com/nodejs/node/issues/5683 whereas as a `.replace()` API seemed too high level, but can be easily implemented with the methods here. \r\n\r\n**Comparison**\r\nIn string scenarios the methods are 1.5x to 3x times slower, whereas a similar JS implementation would be 10x to 20x slower. Detailed benchmarks are yet to be written and also the currently implementation makes at least one copy too much (WIP).\r\n\r\n**Usage**\r\nThe potential usage is bot only string manipulation, but having a general byte manipulation interface with efficient copying. \r\n\r\n **For docs:**\r\n\r\n##### Class Method: Buffer.split([separator])\r\n\r\n* `separator` {Buffer} A separator that gets matched against the input buffer\r\n* Return: {Array} Array of Buffers\r\n\r\n`Buffer.split()` behaves similar to `String.prototype.split`, as such it matches\r\nan input against a source buffer, splits the source buffer, removes the input\r\nand returns the number of resulting buffers in an array.\r\n\r\n```js\r\nconst buf = new Buffer('Hello World');\r\nconst res = buf.split(new Buffer(' '))\r\n\r\nconsole.log(res);\r\n// [ <Buffer 48 65 6c 6c 6f>, <Buffer 57 6f 72 6c 64> ]\r\n```\r\n\r\nIt is also possible to split with an empty buffer, which results in an array\r\nwith the single bytes of buffer contents wrapped in new buffers.\r\n\r\n```js\r\nconst buf = new Buffer('Hello');\r\nconst res = buf.split(new Buffer(''))\r\n\r\nconsole.log(res);\r\n// [ <Buffer 48>, <Buffer 65>, <Buffer 6c>, <Buffer 6c>, <Buffer 6f> ]\r\n```\r\n\r\n##### Class Method: Buffer.join(array[, insert])\r\n\r\n* `array` {Array} Array of buffers that will be joined together\r\n* `insert` {Buffer} Buffer that gets inserted between the subjects\r\n* Return: {Buffer}\r\n\r\n`Buffer.join()` behaves similar to `Array.prototype.join`, as such it matches\r\ntakes an array of buffers and generate new buffer with the concatenated buffers.\r\nOptionally it takes a an `insert` as argument that will be inserted between the\r\nbuffers.\r\n\r\n```js\r\nconst res = Buffer.join([\r\n  new Buffer('Hello'),\r\n  new Buffer('World'),\r\n], new Buffer('\\n'));\r\n\r\nconsole.log(res);\r\n// <Buffer 48 65 6c 6c 6f 0a 57 6f 72 6c 64>\r\nconsole.log(res.toString());\r\n// Hello\r\n// World\r\n```\r\n\r\n_Note: `Array.prototype.join` would fill in commas if no insert will be\r\nprovided. `Buffer.join()` though will simply concat against the next buffer._\r\n\r\n```js\r\nconst res = Buffer.join([\r\n  new Buffer('Hello'),\r\n  new Buffer('World'),\r\n]);\r\n\r\nconsole.log(res.toString());\r\n// HelloWorld\r\n```\r\n\r\n"},"comment":{"url":"https://api.github.com/repos/nodejs/node/issues/comments/210620902","html_url":"https://github.com/nodejs/node/pull/6191#issuecomment-210620902","issue_url":"https://api.github.com/repos/nodejs/node/issues/6191","id":210620902,"user":{"login":"trevnorris","id":289174,"avatar_url":"https://avatars.githubusercontent.com/u/289174?v=3","gravatar_id":"","url":"https://api.github.com/users/trevnorris","html_url":"https://github.com/trevnorris","followers_url":"https://api.github.com/users/trevnorris/followers","following_url":"https://api.github.com/users/trevnorris/following{/other_user}","gists_url":"https://api.github.com/users/trevnorris/gists{/gist_id}","starred_url":"https://api.github.com/users/trevnorris/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/trevnorris/subscriptions","organizations_url":"https://api.github.com/users/trevnorris/orgs","repos_url":"https://api.github.com/users/trevnorris/repos","events_url":"https://api.github.com/users/trevnorris/events{/privacy}","received_events_url":"https://api.github.com/users/trevnorris/received_events","type":"User","site_admin":false},"created_at":"2016-04-15T20:08:02Z","updated_at":"2016-04-15T20:08:02Z","body":"@jasnell My bad. Read \"can be fairly\" as \"can't be fairly\"."}}