{"id":21586065,"url":"https://github.com/magiclen/node-stringbuilder","last_synced_at":"2025-04-10T20:20:21.334Z","repository":{"id":65424749,"uuid":"94566134","full_name":"magiclen/node-stringbuilder","owner":"magiclen","description":"An easy and fast in-memory string builder for Node.js.","archived":false,"fork":false,"pushed_at":"2023-10-23T20:23:57.000Z","size":70,"stargazers_count":11,"open_issues_count":5,"forks_count":5,"subscribers_count":2,"default_branch":"master","last_synced_at":"2025-03-24T17:55:20.608Z","etag":null,"topics":["c","nodejs","string-buffer","string-builder"],"latest_commit_sha":null,"homepage":"","language":"C","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/magiclen.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2017-06-16T17:19:49.000Z","updated_at":"2024-03-29T01:33:22.000Z","dependencies_parsed_at":"2024-06-18T21:14:21.962Z","dependency_job_id":null,"html_url":"https://github.com/magiclen/node-stringbuilder","commit_stats":{"total_commits":19,"total_committers":1,"mean_commits":19.0,"dds":0.0,"last_synced_commit":"e8de8e92337ac252c00e1143b602b8ac1ac31fbb"},"previous_names":[],"tags_count":16,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/magiclen%2Fnode-stringbuilder","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/magiclen%2Fnode-stringbuilder/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/magiclen%2Fnode-stringbuilder/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/magiclen%2Fnode-stringbuilder/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/magiclen","download_url":"https://codeload.github.com/magiclen/node-stringbuilder/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248252669,"owners_count":21072699,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["c","nodejs","string-buffer","string-builder"],"created_at":"2024-11-24T15:12:32.133Z","updated_at":"2025-04-10T20:20:21.317Z","avatar_url":"https://github.com/magiclen.png","language":"C","funding_links":[],"categories":[],"sub_categories":[],"readme":"StringBuilder for Node.js\n=========\n\n[![CI](https://github.com/magiclen/node-stringbuilder/actions/workflows/ci.yml/badge.svg)](https://github.com/magiclen/node-stringbuilder/actions/workflows/ci.yml)\n\nAn easy and fast in-memory string builder for Node.js.\n\n## Code Example\n\n```javascript\nconst StringBuilder = require(\"node-stringbuilder\");\nconst sb = new StringBuilder(\"Hi\");\nsb.appendLine(\",\").append(\"This is a simple example demonstrating how to use this module.\");\nconsole.log(sb.toString()); // Hi,\n// This is a simple example demonstrating how to use this module.\nsb.insert(\"Text can be added into any position of this builder.\");\nsb.replace(53, 118, \"Or replace the existing text.\");\nconsole.log(sb.toString()); // Text can be added into any position of this builder.HOr replace the existing text.\nsb.deleteCharAt(52).insert(52, \" \");\nconsole.log(sb.toString()); // Text can be added into any position of this builder. Or replace the existing text.\nsb.toLowerCase().replaceAll(\"text\", \"string\");\nconsole.log(sb.toString()); // string can be added into any position of this builder. or replace the existing string.\nconsole.log(sb.clone().reverse().toString()); // .gnirts gnitsixe eht ecalper ro .redliub siht fo noitisop yna otni dedda eb nac gnirts\nconsole.log(sb.toString(0, 19)); // string can be added\nconsole.log(sb.length()); // 86\nconsole.log(sb.count()); // 15\nconsole.log(sb.indexOf(\"is\")); // Uint32Array [ 43, 72 ]\nconsole.log(sb.indexOfSkip(\"is\")); // Uint32Array [ 43, 72 ]\nconsole.log(sb.lastIndexOf(\"is\")); // Uint32Array [ 72, 43 ]\nconsole.log(sb.indexOfRegExp(/is/g)); // { index: [ 43, 72 ], lastIndex: [ 45, 74 ] }\nconsole.log(sb.repeat().indexOf(\"is\")); // Uint32Array [ 43, 72, 129, 158 ]\nsb.substring(11, 37);\nconsole.log(sb.toString()); // be added into any position\nconsole.log(sb.equalsIgnoreCase(\"be Added into Any position\")); // true\nconsole.log(sb.toBuffer()); // UTF-8 encoded\n```\n\n## Features\n\n  * Implemented with N-API.\n  * Operating strings in a scalable buffer.\n  * Multiple types of data are allowed to input.\n    * Strings\n    * Buffers(UTF-8 encoded)\n    * Instances of this `StringBuilder` module\n    * ReadStream(to read file)\n    * Numbers, booleans, other objects\n  * Fast string search algorithm([Boyer-Moore-MagicLen](https://magiclen.org/boyer-moore-magiclen/))\n  * Clonable\n\n## Usage\n\n### Initiaiizing\n\nImport this module by using `require` function.\n\n```javascript\nconst StringBuilder = require('node-stringbuilder');\n```\n\nUse `new` operator or `from` function to create a `StringBuilder` instance.\n\n```javascript\nconst sb1 = new StringBuilder();\n// or \nconst sb2 = StringBuilder.from();\n```\n\nWhen creating an instance of `StringBuilder`, you can initialize the text and capacity.\n\n```javascript\nconst sb = StringBuilder.from(\"First\", 4096);\n```\n\nBy default, a block of buffer space used by `StringBuilder` is 128 characters. The space of the buffer can be expanded or shrinked by blocks.\n\n```javascript\n// To expand\nconst newCapacity = 65536;\nsb.expandCapacity(newCapacity);\n// To shrink\nsb.shrinkCapacity();\n```\n\nIf some text are added into `StringBuilder`, `StringBuilder` will check its space. And if the space is too small, it will re-alloc a bigger one automatically. This re-allocation has overheads, if it does this frequently, your program may be slowed down. Therefore, if you can predict the length of your text, please set the capacity when creating a `StringBuilder` instance.\n\n### Append\n\nConcat text.\n\n```javascript\nsb.append(\"string\").append(123).append(false).append(fs.createReadStream(path));\n```\n\nAdd a new line after append.\n\n```javascript\nsb.appendLine(\"string\");\n```\n\nAppend text repeatedly.\n\n```javascript\nsb.appendRepeat(\"string\", 3);\n```\n\nAppend a file asynchronizely.\n\n```javascript\nawait sb.appendReadStream(fs.createReadStream(path));\n```\n\n### Insert\n\nInsert text to any position.\n\n```javascript\nsb.insert(\"string\"); // To the head.\nsb.insert(5, \"string\");\n```\n\n### Replace\n\nReplace text to the position in a range of index.\n\n```javascript\nsb.replace(4, 15, \"string\");\n```\n\nReplace existing substrings to another.\n\n```javascript\nsb.replacePattern(\"old\", \"new\");\nsb.replacePattern(\n    \"old\",\n    \"new\",\n    offset,\n    limit\n);\n```\n\nReplace all existing substrings to another.\n\n```javascript\nsb.replaceAll(\"old\", \"new\");\n```\n\n### Delete\n\nDelete text from a range of index.\n\n```javascript\nsb.delete(4, 15);\n```\n\nDelete a character at a index.\n\n```javascript\nsb.deleteCharAt(4);\n```\n\nClear all text, but preserve the capacity.\n\n```javascript\nsb.clear();\n```\n\n### Substring\n\nReserve text in a range of index.\n\n```javascript\nsb.substring(1, 5); // input the start and end index\n```\n\n```javascript\nsb.substr(1, 5); // input the start index and length\n```\n\n### Reverse\n\nReverse text.\n\n```javascript\nsb.reverse();\n```\n\n### Upper/Lower Case\n\nConvert text to upper or lower case.\n\n```javascript\nsb.upperCase();\nsb.lowerCase();\n```\n\n### Trim\n\nRemove any leading and trailing whitespace.\n\n```javascript\nsb.trim();\n```\n\n### Repeat\n\nRepeat current text for specific count.\n\n```javascript\nsb.repeat(1);\n```\n\n### Expand Capacity\n\nExpand the capacity of this `StringBuilder`.\n\n```javascript\nsb.expandCapacity(4096).append(\"string\");\n```\n\nExpand and get the updated capacity,\n\n```javascript\nconst capacity = sb.expandCapacity(4096, true);\n```\n\n### Shrink Capacity\n\nShrink the capacity of this `StringBuilder`.\n\n```javascript\nsb.shrinkCapacity().clone().append(\"string\");\n```\n\nShrink and get the updated capacity,\n\n```javascript\nconst capacity = sb.shrinkCapacity(true);\n```\n\n### Get Current Text Length\n\nTo get the length of this `StringBuilder`,\n\n```javascript\nconst length = sb.length();\n```\n\n### Get Current Capacity\n\nTo get the length of this `StringBuilder`,\n\n```javascript\nconst capacity = sb.capacity();\n```\n\n### Count the words\n\nTo count the words,\n\n```javascript\nconst words = sb.count();\n```\n\n### Build String\n\nBuild a string of a specific range of index.\n\n```javascript\nconst str = sb.toString(4, 10);\n```\n\nBuild a UTF-8 buffer of a specific range of index.\n\n```javascript\nconst buffer = sb.toBuffer(4, 10);\n```\n\nTo get the full text,\n\n```javascript\nconst text = sb.toString();\nconst buffer = sb.toBuffer();\n```\n\nTo get one character at a specific index,\n\n```javascript\nconst c = sb.charAt(4);\n```\n\n### Search String\n\nSearch substrings from the head,\n\n```javascript\nconst indexArray = sb.indexOf(\"string\");\nconst indexArray2 = sb.indexOf(\"string\", offset, limit);\n```\n\nSearch substrings from the head by using RegExp,\n\n```javascript\nvar indexArray = sb.indexOf(/string/g);\n```\n\nSearch substrings from the end,\n\n```javascript\nconst indexArray = sb.lastIndexOf(\"string\");\n```\n\n### Equals\n\nDetermine whether the two strings are the same.\n\n```javascript\nconst equal = sb.equals(\"string\");\n```\n\nTo ignore the case of letters,\n\n```javascript\nconst equal = sb.equalsIgnoreCase(\"string\");\n```\n\nDetermine whether it starts or ends with a specific pattern.\n\n```javascript\nconst start = sb.startsWith(\"string\");\nconst end = sb.endsWith(\"string\");\n```\n\nRegExp is not supported in `startsWith` and `endsWith` methods.\n\n### Clone\n\nClone this `StringBuilder`.\n\n```javascript\nconst newSB = sb.clone();\n```\n\n## Tests\n\nTo run the test suite, first install the dependencies, then run `npm test`:\n\n```bash\nnpm install\nnpm test\n```\n\n## Benchmark\n\nTo run the benchmark suite, first install the dependencies, then run `npm run benchmark`:\n\n```bash\nnpm install\nnpm run benchmark\n```\n\nHere is my result,\n\n```bash\nAppend\n  - 43 milliseconds\n  ✓ Natively append text 1000000 times (43ms)\n  - 567 milliseconds\n  ✓ Use StringBuilder to append text 1000000 times (567ms)\n  - 1278 milliseconds\n  ✓ Use StringBuilder to insert text 1000000 times at the end (1287ms)\n  - 17 milliseconds\n  ✓ Use StringBuilder to append text 1000000 times repeatly\n\nInsert\n  - 92 milliseconds\n  ✓ Natively insert text 10000 times (92ms)\n  - 10 milliseconds\n  ✓ Use StringBuilder to insert text 10000 times\n\nDelete\n  - 1427 milliseconds\n  ✓ Natively delete text 5000 times (1429ms)\n  - 87 milliseconds\n  ✓ Use StringBuilder to delete text 5000 times (88ms)\n\nReplace\n  - 1511 milliseconds\n  ✓ Natively replace text 5000 times (1513ms)\n  - 85 milliseconds\n  ✓ Use StringBuilder to replace text 5000 times (86ms)\n\nReplace Pattern\n  - 37 milliseconds\n  ✓ Natively replace text with the same length by using a RegExp pattern\n  - 20 milliseconds\n  ✓ Use StringBuilder to replace text with the same length by using a pattern\n  - 35 milliseconds\n  ✓ Natively replace text by using a RegExp pattern\n  - 29 milliseconds\n  ✓ Use StringBuilder to replace text by using a pattern\n\nEquals\n  - 2 milliseconds\n  ✓ Natively check the equal 50000 times\n  - 13 milliseconds\n  ✓ Use StringBuilder to check the equal 50000 times\n\nEqualsIgnoreCase\n  - 21 milliseconds\n  ✓ Natively check the equal 50000 times\n  - 19 milliseconds\n  ✓ Use StringBuilder to check the equal 50000 times\n\nIndexOf\n  - 65 milliseconds\n  ✓ Natively search text (65ms)\n  - 2 milliseconds\n  ✓ Use StringBuilder to search text\n\nReverse\n  - 516 milliseconds\n  ✓ Natively reverse text (516ms)\n  - 14 milliseconds\n  ✓ Use StringBuilder to reverse text\n```\n\nAccording to the result of benchmark, if you just want to append a few different strings, please append them by using native operator `+` instead of this module.\n\n## License\n\n[MIT](LICENSE)","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmagiclen%2Fnode-stringbuilder","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fmagiclen%2Fnode-stringbuilder","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmagiclen%2Fnode-stringbuilder/lists"}