{"id":23122857,"url":"https://github.com/wissance/stringformatter","last_synced_at":"2025-04-10T00:19:29.776Z","repository":{"id":43103105,"uuid":"290164466","full_name":"Wissance/stringFormatter","owner":"Wissance","description":"A set of tools to manipulate text, i.e. making text from templates in C#/Python way is faster than fmt.Sprintf","archived":false,"fork":false,"pushed_at":"2025-03-21T21:20:56.000Z","size":767,"stargazers_count":53,"open_issues_count":10,"forks_count":3,"subscribers_count":5,"default_branch":"master","last_synced_at":"2025-04-02T22:06:37.649Z","etag":null,"topics":["golang","golang-library","golang-text","string","string-formatter","string-template","template-processor","text-templating-engine"],"latest_commit_sha":null,"homepage":"https://wissance.github.io/stringFormatter/","language":"Go","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"apache-2.0","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/Wissance.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":"CODE_OF_CONDUCT.md","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":"2020-08-25T08:53:26.000Z","updated_at":"2025-03-21T21:20:53.000Z","dependencies_parsed_at":"2024-01-26T07:27:52.832Z","dependency_job_id":"a72df84d-4e61-46d3-b3cf-3e365d6738a8","html_url":"https://github.com/Wissance/stringFormatter","commit_stats":null,"previous_names":[],"tags_count":18,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Wissance%2FstringFormatter","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Wissance%2FstringFormatter/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Wissance%2FstringFormatter/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Wissance%2FstringFormatter/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/Wissance","download_url":"https://codeload.github.com/Wissance/stringFormatter/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248131546,"owners_count":21052873,"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":["golang","golang-library","golang-text","string","string-formatter","string-template","template-processor","text-templating-engine"],"created_at":"2024-12-17T07:31:04.984Z","updated_at":"2025-04-10T00:19:29.729Z","avatar_url":"https://github.com/Wissance.png","language":"Go","readme":"# StringFormatter\n\nA set of a ***high performance string tools*** that helps to build strings from templates and process text faster than with `fmt`!!!.\nSlice printing is **50% faster with 8 items** slice and **250% with 20 items** slice \n\n![GitHub go.mod Go version (subdirectory of monorepo)](https://img.shields.io/github/go-mod/go-version/wissance/stringFormatter?style=plastic) \n![GitHub code size in bytes](https://img.shields.io/github/languages/code-size/wissance/stringFormatter?style=plastic) \n![GitHub issues](https://img.shields.io/github/issues/wissance/stringFormatter?style=plastic)\n![GitHub Release Date](https://img.shields.io/github/release-date/wissance/stringFormatter) \n![GitHub release (latest by date)](https://img.shields.io/github/downloads/wissance/stringFormatter/v1.4.1/total?style=plastic)\n\n![String Formatter: a convenient string formatting tool](img/sf_cover.png)\n\n## 1. Features\n\n1. Text formatting with template using traditional for `C#, Python programmers style` - `{0}`, `{name}` that faster then `fmt` does:\n![String Formatter: a convenient string formatting tool](img/benchmarks_adv.png)\n2. Additional text utilities:\n   - convert ***map to string*** using one of predefined formats (see `text_utils.go`)\n\n### 1. Text formatting from templates\n\n#### 1.1 Description\n\nThis is a GO module for ***template text formatting in syntax like in C# or/and Python*** using:\n- `{n}` , n here is a number to notes order of argument list to use i.e. `{0}`, `{1}`\n- `{name}` to notes arguments by name i.e. `{name}`, `{last_name}`, `{address}` and so on ...\n\n#### 1.2 Examples\n\n##### 1.2.1 Format by arg order\n\ni.e. you have following template:  `\"Hello {0}, we are greeting you here: {1}!\"`\n\nif you call Format with args \"manager\" and \"salesApp\" :\n\n```go\nformattedStr := stringFormatter.Format(\"Hello {0}, we are greeting you here: {1}!\", \"manager\", \"salesApp\")\n```\n\nyou get string `\"Hello manager, we are greeting you here: salesApp!\"`\n\n##### 1.2.2 Format by arg key\n\ni.e. you have following template: `\"Hello {user} what are you doing here {app} ?\"`\n\nif you call `FormatComplex` with args `\"vpupkin\"` and `\"mn_console\"` `FormatComplex(\"Hello {user} what are you doing here {app} ?\", map[string]any{\"user\":\"vpupkin\", \"app\":\"mn_console\"})`\n\nyou get string `\"Hello vpupkin what are you doing here mn_console ?\"`\n\nanother example is:\n\n```go\nstrFormatResult = stringFormatter.FormatComplex(\n\t\"Current app settings are: ipAddr: {ipaddr}, port: {port}, use ssl: {ssl}.\", \n\tmap[string]any{\"ipaddr\":\"127.0.0.1\", \"port\":5432, \"ssl\":false},\n)\n```\na result will be: `\"Current app settings are: ipAddr: 127.0.0.1, port: 5432, use ssl: false.\"``\n\n##### 1.2.3 Advanced arguments formatting\n\nFor more convenient lines formatting we should choose how arguments are representing in output text, \n`stringFormatter` supports following format options:\n1. Bin number formatting \n   - `{0:B}, 15 outputs -\u003e 1111`\n   - `{0:B8}, 15 outputs -\u003e 00001111`\n2. Hex number formatting\n   - `{0:X}, 250 outputs -\u003e fa`\n   - `{0:X4}, 250 outputs -\u003e 00fa`\n3. Oct number formatting\n   - `{0:o}, 11 outputs -\u003e 14`\n4. Float point number formatting\n   - `{0:E2}, 191.0478 outputs -\u003e 1.91e+02`\n   - `{0:F}, 10.4567890 outputs -\u003e 10.456789`\n   - `{0:F4}, 10.4567890 outputs -\u003e 10.4568`\n   - `{0:F8}, 10.4567890 outputs -\u003e 10.45678900`\n5. Percentage output\n   - `{0:P100}, 12 outputs -\u003e 12%`\n6. Lists\n   - `{0:L-}, [1,2,3] outputs -\u003e 1-2-3`\n   - `{0:L, }, [1,2,3] outputs -\u003e 1, 2, 3`\n\n##### 1.2.4 Benchmarks of the Format and FormatComplex functions\n\nbenchmark could be running using following commands from command line:\n* to see `Format` result - `go test -bench=Format -benchmem -cpu 1`\n* to see `fmt` result - `go test -bench=Fmt -benchmem -cpu 1`\n\n### 2. Text utilities\n\n#### 2.1 Map to string utility\n\n`MapToString` function allows to convert map with primitive key to string using format, including key and value, e.g.:\n* `{key} =\u003e {value}`\n* `{key} : {value}`\n* `{value}`\n\nFor example:\n```go\noptions := map[string]any{\n\t\"connectTimeout\": 1000,\n\t\"useSsl\":         true,\n\t\"login\":          \"sa\",\n\t\"password\":       \"sa\",\n}\n\nstr := stringFormatter.MapToString(\u0026options, \"{key} : {value}\", \", \")\n// NOTE: order of key-value pairs is not guranteed though\n// str will be something like:\n\"connectTimeout : 1000, useSsl : true, login : sa, password : sa\"\n```\n\n#### 2.2 Benchmarks of the MapToString function\n\n* to see `MapToStr` result - `go test -bench=MapToStr -benchmem -cpu 1`\n\n![MapToStr benchmarks](/img/map2str_benchmarks.png)\n\n#### 2.3 Slice to string utility\n\n`SliceToString` - function that converts slice with passed separation between items to string.\n```go\nslice := []any{100, \"200\", 300, \"400\", 500, 600, \"700\", 800, 1.09, \"hello\"}\nseparator := \",\"\nresult := stringFormatter.SliceToString(\u0026slice, \u0026separator)\n```\n\n`SliceSameTypeToString` - function that converts typed slice to line with separator\n```go\nseparator := \":\"\nnumericSlice := []int{100, 200, 400, 800}\nresult := stringFormatter.SliceSameTypeToString(\u0026numericSlice, \u0026separator)\n```\n\n#### 2.4 Benchmarks of the SliceToString function\n\n`sf` is rather fast then `fmt` 2.5 times (250%) faster on slice with 20 items, see benchmark:\n![SliceToStr benchmarks](/img/slice2str_benchmarks.png)\n\n### 3. Contributors\n\n\u003ca href=\"https://github.com/Wissance/stringFormatter/graphs/contributors\"\u003e\n  \u003cimg src=\"https://contrib.rocks/image?repo=Wissance/stringFormatter\" /\u003e\n\u003c/a\u003e\n","funding_links":[],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fwissance%2Fstringformatter","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fwissance%2Fstringformatter","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fwissance%2Fstringformatter/lists"}