{"id":22542027,"url":"https://github.com/t34-dev/go-text-replacer","last_synced_at":"2026-01-23T20:41:27.268Z","repository":{"id":249319392,"uuid":"831173873","full_name":"t34-dev/go-text-replacer","owner":"t34-dev","description":"Package for nested text (byte/rune) replacement in a single pass with Unicode support","archived":false,"fork":false,"pushed_at":"2024-09-02T18:15:54.000Z","size":25,"stargazers_count":3,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"main","last_synced_at":"2026-01-12T02:53:14.663Z","etag":null,"topics":["go","golang","replacer","runes","string-manipulation","text-replacer","text-substitution","unicode"],"latest_commit_sha":null,"homepage":"","language":"Go","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"isc","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/t34-dev.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":"2024-07-19T20:56:55.000Z","updated_at":"2024-09-13T06:47:56.000Z","dependencies_parsed_at":"2024-07-22T08:33:33.611Z","dependency_job_id":"f1748bdc-ed13-4ecb-bf3f-f702e0fef196","html_url":"https://github.com/t34-dev/go-text-replacer","commit_stats":null,"previous_names":["well-do-it-too/nestedreplacer","t34-dev/go-text-replacer","t34-developer/go-text-replacer"],"tags_count":13,"template":false,"template_full_name":null,"purl":"pkg:github/t34-dev/go-text-replacer","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/t34-dev%2Fgo-text-replacer","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/t34-dev%2Fgo-text-replacer/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/t34-dev%2Fgo-text-replacer/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/t34-dev%2Fgo-text-replacer/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/t34-dev","download_url":"https://codeload.github.com/t34-dev/go-text-replacer/tar.gz/refs/heads/main","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/t34-dev%2Fgo-text-replacer/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":286080680,"owners_count":28699717,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2026-01-23T17:25:48.045Z","status":"ssl_error","status_checked_at":"2026-01-23T17:25:47.153Z","response_time":59,"last_error":"SSL_read: unexpected eof while reading","robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":false,"can_crawl_api":true,"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":["go","golang","replacer","runes","string-manipulation","text-replacer","text-substitution","unicode"],"created_at":"2024-12-07T13:08:20.043Z","updated_at":"2026-01-23T20:41:27.240Z","avatar_url":"https://github.com/t34-dev.png","language":"Go","funding_links":[],"categories":[],"sub_categories":[],"readme":"[![ISC License](http://img.shields.io/badge/license-ISC-blue.svg)](http://copyfree.org)\n[![Coverage Status](https://coveralls.io/repos/github/t34-dev/go-text-replacer/badge.svg?branch=main\u0026ver=1723898449)](https://coveralls.io/github/t34-dev/go-text-replacer?branch=main\u0026ver=1723898449)\n![Go Version](https://img.shields.io/badge/Go-1.22-blue?logo=go\u0026ver=1723898449)\n![GitHub release (latest by date)](https://img.shields.io/github/v/release/t34-dev/go-text-replacer?ver=1723898449)\n![GitHub tag (latest by date)](https://img.shields.io/github/v/tag/t34-dev/go-text-replacer?sort=semver\u0026style=flat\u0026logo=git\u0026logoColor=white\u0026label=Latest%20Version\u0026color=blue\u0026ver=1723898449)\n\n# Go-Text-Replacer\n\nGo-Text-Replacer is a Go package that provides functionality for performing nested replacements in text content. It allows you to replace multiple sections of text while maintaining the original structure and handling overlapping replacements.\n\n## Features\n\n- Replace multiple sections of text in a single operation\n- Handle overlapping replacements\n- Support for both byte slices and strings\n- Find all occurrences of a given text\n- Find first or last occurrence of a given text\n- Convert between rune and byte positions\n- Create replacement blocks from strings or byte slices\n\n## Installation\n\nTo install Go-Text-Replacer, use `go get`:\n\n```\ngo get github.com/t34-dev/go-text-replacer\n```\n\n## Usage\n\nHere's a simple example of how to use Go-Text-Replacer with strings:\n\n```go\npackage main\n\nimport (\n\t\"fmt\"\n\ttextreplacer \"github.com/t34-dev/go-text-replacer\"\n\t\"log\"\n)\n\nfunc main() {\n\tcontent := `\nChapter 1: Introduction to Programming (编程简介)\n\n    1.1 What is Programming (什么是编程)\n        Programming is the art of creating instructions for computers.\n        It includes many aspects such as:\n            - Algorithmic thinking (算法思维)\n            - Block structures\n            - Programming language syntax\n`\n\n\treplacer := textreplacer.NewFromString(content)\n\n\tblocks := []textreplacer.Block{\n\t\treplacer.CreateBlockFromString(\"Introduction\", \"Getting Started with\"),\n\t\treplacer.CreateBlockFromString(\"Programming\", \"Coding\"),\n\t\treplacer.CreateBlockFromString(\"(算法思维)\", \"(Algorithmic Thinking)\"),\n\t}\n\n\tresult, err := replacer.Enter(blocks)\n\tif err != nil {\n\t\tlog.Fatalln(err)\n\t}\n\n\t// output:\n\t//Chapter 1: Getting Started with to Coding (编程简介)\n\t//\n\t//1.1 What is Programming (什么是编程)\n\t//Programming is the art of creating instructions for computers.\n\t//\tIt includes many aspects such as:\n\t//- Algorithmic thinking (Algorithmic Thinking)\n\t//- Block structures\n\t//- Programming language syntax\n\t\n\tfmt.Println(string(result))\n}\n\n```\n\nAnd here's an example using byte slices:\n\n```go\npackage main\n\nimport (\n\t\"fmt\"\n\ttextreplacer \"github.com/t34-dev/go-text-replacer\"\n\t\"log\"\n)\n\nvar content = `\nChapter 1: Introduction to Programming (编程简介)\n\n    1.1 What is Programming (什么是编程)\n        Programming is the art of creating instructions for computers.\n        It includes many aspects such as:\n            - Algorithmic thinking (算法思维)\n            - Block structures\n            - Programming language syntax\n`\n\nfunc main() {\n\t// analyze the file\n\treplacer := textreplacer.New([]byte(content))\n\n\t// if we want to find the byte range for the searched word\n\tposition := replacer.FindFirstPosition([]byte(\"Introduction\"), 0)\n\t// output: 'Introduction' found at positions 12 to 24\n\tif position != nil {\n\t\tfmt.Printf(\"'Introduction' found at positions %d to %d\\n\", position.Start, position.End)\n\t} else {\n\t\tlog.Fatalln(\"'Introduction' not found\")\n\t}\n\n\tblocks := []textreplacer.Block{\n\t\t// if we know exactly the byte numbers to replace\n\t\t// output: 'Introduction' found at positions 12 to 24\n\t\t{\n\t\t\tStart: 12,\n\t\t\tEnd:   24,\n\t\t\tTxt:   []byte(\"What_stuck a bit\"),\n\t\t},\n\t\t// if we don't know the byte numbers\n\t\treplacer.CreateBlock([]byte(\"to\"), []byte(\"is your\")),\n\t\treplacer.CreateBlockFromString(\"Programming\", \"[用户名]\"),\n\t\treplacer.CreateBlockFromString(\"(算法思维)\", \"\\n            - 另一个要点\"),\n\t}\n\t// perform replacement\n\tresult, err := replacer.Enter(blocks)\n\n\tif err != nil {\n\t\tlog.Fatalln(err)\n\t}\n\n\treplacer = textreplacer.New(result)\n\tresult, err = replacer.Enter([]textreplacer.Block{\n\t\treplacer.CreateBlock([]byte(\"Block\"), []byte(\"END\")),\n\t})\n\n\tif err != nil {\n\t\tlog.Fatalln(err)\n\t}\n\n\t// output:\n\t// 1.1 What is Programming (什么是编程)\n\t// Programming is the art of creating instructions for computers.\n\t// It includes many aspects such as:\n\t// - Algorithmic thinking\n\t// - 另一个要点\n\t// - END structures\n\t// - Programming language syntax\n\tfmt.Println(string(result))\n}\n```\n\n## API Reference\n\n### Types\n\n- `Block`: Represents a replacement block with Start, End, and Txt fields.\n- `Position`: Represents the position of found text with Start and End fields.\n\n### Functions\n\n- `New(content []byte) *textreplacer`: Creates a new instance of textreplacer with the given content.\n- `NewFromString(content string) *textreplacer`: Creates a new instance of textreplacer from a string.\n\n### Methods\n\n- `Enter(blocks []Block) ([]byte, error)`: Applies replacement blocks to the original content.\n- `FindAllPositions(text []byte) []Position`: Finds all positions of the given text in the content.\n- `FindFirstPosition(text []byte, startIndex int) *Position`: Finds the first position of the given text in the content, starting from the specified index.\n- `FindLastPosition(text []byte, startIndex int) *Position`: Finds the last position of the given text in the content, starting from the specified index from the end.\n- `CreateBlock(find, txt []byte) Block`: Creates a block using the given find and replacement text.\n- `RuneToBytePosition(runeStart, runeEnd int) (byteStart, byteEnd int)`: Converts a position in runes to a position in bytes.\n- `ByteToRunePosition(byteStart, byteEnd int) (runeStart, runeEnd int)`: Converts a position in bytes to a position in runes.\n- `CreateBlockFromString(findRunes, txtRunes string) Block`: Creates a block using rune positions.\n\n## License\n\nThis project is licensed under the ISC License. See the [LICENSE](LICENSE) file for details.\n\n## Contributing\n\nContributions are welcome! Please feel free to submit a Pull Request.\n\n---\n\nDeveloped with ❤️ by [T34](https://github.com/t34-dev)\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ft34-dev%2Fgo-text-replacer","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Ft34-dev%2Fgo-text-replacer","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ft34-dev%2Fgo-text-replacer/lists"}