Wissance / stringFormatter

A set of tools to manipulate text, i.e. making text from templates in C#/Python way is faster than fmt.Sprintf
https://wissance.github.io/stringFormatter/
Apache License 2.0
50 stars 3 forks source link
golang golang-library golang-text string string-formatter string-template template-processor text-templating-engine

StringFormatter

A set of a high performance string tools that helps to build strings from templates and process text faster than with fmt!!!.

GitHub go.mod Go version (subdirectory of monorepo) GitHub code size in bytes GitHub issues GitHub Release Date GitHub release (latest by date)

String Formatter: a convenient string formatting tool

1. Features

  1. Text formatting with template using traditional for C#, Python programmers style - {0}, {name} that faster then fmt does: String Formatter: a convenient string formatting tool
  2. Additional text utilities:
    • convert map to string using one of predefined formats (see text_utils.go)

1. Text formatting from templates

1.1 Description

This is a GO module for template text formatting in syntax like in C# or/and Python using:

1.2 Examples

1.2.1 Format by arg order

i.e. you have following template: "Hello {0}, we are greeting you here: {1}!"

if you call Format with args "manager" and "salesApp" :

formattedStr := stringFormatter.Format("Hello {0}, we are greeting you here: {1}!", "manager", "salesApp")

you get string "Hello manager, we are greeting you here: salesApp!"

1.2.2 Format by arg key

i.e. you have following template: "Hello {user} what are you doing here {app} ?"

if 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"})

you get string "Hello vpupkin what are you doing here mn_console ?"

another example is:

strFormatResult = stringFormatter.FormatComplex(
    "Current app settings are: ipAddr: {ipaddr}, port: {port}, use ssl: {ssl}.", 
    map[string]any{"ipaddr":"127.0.0.1", "port":5432, "ssl":false},
)

a result will be: `"Current app settings are: ipAddr: 127.0.0.1, port: 5432, use ssl: false."``

1.2.3 Advanced arguments formatting

For more convenient lines formatting we should choose how arguments are representing in output text, stringFormatter supports following format options:

  1. Bin number formatting
    • {0:B}, 15 outputs -> 1111
    • {0:B8}, 15 outputs -> 00001111
  2. Hex number formatting
    • {0:X}, 250 outputs -> fa
    • {0:X4}, 250 outputs -> 00fa
  3. Oct number formatting
    • {0:o}, 11 outputs -> 14
  4. Float point number formatting
    • {0:E2}, 191.0478 outputs -> 1.91e+02
    • {0:F}, 10.4567890 outputs -> 10.456789
    • {0:F4}, 10.4567890 outputs -> 10.4568
    • {0:F8}, 10.4567890 outputs -> 10.45678900
  5. Percentage output
    • {0:P100}, 12 outputs -> 12%
1.2.4 Benchmarks of the Format and FormatComplex functions

benchmark could be running using following commands from command line:

2. Text utilities

2.1 Map to string utility

MapToString function allows to convert map with primitive key to string using format, including key and value, e.g.:

For example:

options := map[string]any{
    "connectTimeout": 1000,
    "useSsl":         true,
    "login":          "sa",
    "password":       "sa",
}

str := stringFormatter.MapToString(&options, "{key} : {value}", ", ")
// NOTE: order of key-value pairs is not guranteed though
// str will be something like:
"connectTimeout : 1000, useSsl : true, login : sa, password : sa"

2.2 Benchmarks of the MapToStr function

MapToStr benchmarks

3. Contributors