escaper

package
v0.0.10 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Oct 12, 2019 License: MIT Imports: 1 Imported by: 0

Documentation

Overview

Package escaper provides an easy API to escapae and unescape a string using a custom escape prefix. See the examples below for usage.

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Escaper

type Escaper struct {
	// contains filtered or unexported fields
}

Escaper provides an easy API to escape and unescape a string using a custom escape prefix.

func New

func New() *Escaper

New returns a new escaper with the default prefix "\" and no additional substrings. This default escaper will only escape the prefix itself, so "\" becomes "\\".

func (*Escaper) Clone

func (e *Escaper) Clone() *Escaper

Clone returns a new copy of this escaper.

func (*Escaper) Escape

func (e *Escaper) Escape(in string) string

Escape returns the escaped version of the given string.

Example (Equal)

This example shows that you can specify the substrings to escape.

in := "Hello=World"
e := New().Prefix("\\").Sub("=")
out := e.Escape(in)
fmt.Println(out)
Output:

Hello\=World
Example (Multiple)

This example shows you can escape multiple substrings.

in := "Hello Beautiful=World:Again"
e := New().Prefix("\\").Sub(" ", "=", ":")
out := e.Escape(in)
fmt.Println(out)
Output:

Hello\ Beautiful\=World\:Again
Example (Simple)

This examples shows an simple use case for Escape.

in := "Hello Beautiful\\World Again"
e := New().Prefix("\\")
out := e.Escape(in)
fmt.Println(out)
Output:

Hello Beautiful\\World Again

func (*Escaper) Prefix

func (e *Escaper) Prefix(prefix string) *Escaper

The escape prefix to use for escaping substrings. The escaper will also escape the prefix itself. Any string can be used.

Example (Asterix)

This example shows that any string can be used as an escape prefix.

in := "Hello*World"
e := New().Prefix("*") // set the prefix to a custom string (asterix)
out := e.Escape(in)    // escape escapes the prefix itself resulting in 2 asterixis
fmt.Println(out)
Output:

Hello**World
Example (Off)

This example shows that when no prefix is set, then Escape is a no-op.

in := "Hello*World"
e := New().Prefix("") // set the prefix to a blank string
out := e.Escape(in)   // escape has no effect since their is no prefix set
fmt.Println(out)
Output:

Hello*World

func (*Escaper) Sub

func (e *Escaper) Sub(substrings ...string) *Escaper

Sub adds the given substrings to the Escaper to be escaped and unescaped.

Example (Multiple)

This example shows that sub is a variadic function and accepts multiple substrings.

in := "Hello Beautiful=World:Again"
e := New().Prefix("\\").Sub(" ", "=", ":")
out := e.Escape(in)
fmt.Println(out)
Output:

Hello\ Beautiful\=World\:Again
Example (Simple)

This example shows that sub can be called to set substrings.

in := "Hello Beautiful=World:Again"
e := New().Prefix("\\").Sub("=").Sub(":")
out := e.Escape(in)
fmt.Println(out)
Output:

Hello Beautiful\=World\:Again

func (*Escaper) Unescape

func (e *Escaper) Unescape(in string) string

Unescape returns the unescaped version of the given string.

Example (Multiple)

This example shows you can unescape multiple substrings.

in := "Hello\\ Beautiful\\=World\\:Again"
e := New().Prefix("\\").Sub(" ", "=", ":")
out := e.Unescape(in)
fmt.Println(out)
Output:

Hello Beautiful=World:Again
Example (Newline)

This example shows you can unescape new line characters.

// You can escape new line characters
in := "Hello Beautiful\\\nWorld Again"
e := New().Prefix("\\").Sub("\n")
out := e.Unescape(in)
fmt.Println(out)
Output:

Hello Beautiful
World Again
Example (Simple)

This exmaple shows that you can unescape text.

in := "Hello Beautiful\\\\World Again"
e := New().Prefix("\\")
out := e.Unescape(in)
fmt.Println(out)
Output:

Hello Beautiful\World Again

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL