Giter Site home page Giter Site logo

aperture147 / stringformatter Goto Github PK

View Code? Open in Web Editor NEW

This project forked from wissance/stringformatter

0.0 0.0 0.0 513 KB

A set of tools to manipulate text, i.e. making text from templates in C#/Python way is faster than fmt.Sprintf

Home Page: https://wissance.github.io/stringFormatter/

License: Apache License 2.0

Go 100.00%

stringformatter's Introduction

StringFormatter

A set of a high performance string tools that helps to build strings from templates and process text that faster than 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:

  • {n} , n here is a number to notes order of argument list to use i.e. {0}, {1}
  • {name} to notes arguments by name i.e. {name}, {last_name}, {address} and so on ...

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 := 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]interface{}{"user":"vpupkin", "app":"mn_console"})

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

another example is:

    strFormatResult = FormatComplex("Current app settings are: ipAddr: {ipaddr}, port: {port}, use ssl: {ssl}.", 
                                    map[string]interface{}{"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 Benchmarks of the Format and FormatComplex functions

benchmark could be running using following commands from command line:

  • to see Format result - go test -bench=Format -benchmem -cpu 1
  • to see fmt result - go test -bench=Fmt -benchmem -cpu 1

2. Text utilities

2.1 Map to string utility

Map to string function allow to convert map to string using one of predefined line format:

  • key => value
  • key : value
  • value

For example see code from test (text_utils_test.go):

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

	str := MapToString(&options, KeyValueWithSemicolonSepFormat, ", ")
	assert.True(t, len(str) > 0)
	assert.Equal(t, "connectTimeout : 1000, useSsl : true, login : sa, password : sa", str)

2.2 Benchmarks of the MapToStr function

  • to see MapToStr result - go test -bench=MapToStr -benchmem -cpu 1

MapToStr benchmarks

3. Contributors

stringformatter's People

Contributors

2767mr avatar evillord666 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.