text

package module
v0.0.0-...-1a71e68 Latest Latest
Warning

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

Go to latest
Published: Jun 2, 2016 License: LGPL-3.0 Imports: 4 Imported by: 1

README

Build Status GoDoc Built with Spacemacs

Golibri/text

This package is a thin wrapper over the built-in string type and provides several methods for one would expect from strings. Transformations are chainable, convenience checks work consistently and unicode characters are respected. The methods use Go's stdlib functions wherever possible, wrapping them in a programmer-friendly way. Inspired by Ruby's String class

Installation

go get -u github.com/golibri/text

Usage examples

  1. Create a text object, typically from a string:
txt := text.New(" hello world! ") // or: text.FromString("...")
// also possible: text.FromBytes(...)
  1. Check some properties of the text:
txt.IsASCIIOnly() // true
txt.IsEmpty() // false
txt.StartsWith(" hello") // true
  1. Get some metrics from the text:
txt.Length() // 13
txt.Count("l") // 3
  1. Transform the text with chainable methods:
result := txt.
  ReplaceString("world", "golang").
  Capitalize().
  Strip() // text.Text("Hello Golang")
  1. Export the text back to a usual string:
result.ToString() // "Hello Golang"

Caveats

This package aims to provide a convenient programming interface and is not optimized for the best possible performance, there are memory allocations, especially for the transformation methods.

For a good overview about all the available methods, checkout the godoc documentation!

License

LGPLv3. (You can use it in commercial projects as you like, but improvements/bugfixes must flow back to this lib.)

Documentation

Overview

Package text wraps the default string type and adds useful methods, OOP style

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Text

type Text string

Text is the same as a string, comparison operators etc apply as usual

func FromBytes

func FromBytes(b []byte) Text

FromBytes does the casting for you

func FromChars

func FromChars(c []string) Text

FromChars takes an []string slice of (one or more) characters

func FromString

func FromString(s string) Text

FromString is the same as New()

func New

func New(s string) Text

New is the canonical constructor, same as FromString()

func (Text) ByteSize

func (t Text) ByteSize() int

ByteSize returns the length of bytes of the given string

func (Text) Capitalize

func (t Text) Capitalize() Text

Capitalize uppercases the first letter of every word

func (Text) Count

func (t Text) Count(phrase string) int

Count determines how often a given phrase is contained in the string

func (Text) DeletePattern

func (t Text) DeletePattern(s string) Text

DeletePattern Removes every occurence of the given regex pattern from the text

func (Text) DeleteString

func (t Text) DeleteString(s string) Text

DeleteString Removes every occurence of the given string from the text

func (Text) DoesContainString

func (t Text) DoesContainString(s string) bool

DoesContainString checks whether a given string is contained in the text.

func (Text) DoesMatchPattern

func (t Text) DoesMatchPattern(s string) bool

DoesMatchPattern tests a gioven regex string against the text

func (Text) Downcase

func (t Text) Downcase() Text

Downcase lowercases all characters in the string

func (Text) EachByte

func (t Text) EachByte(f func(byte))

EachByte applies the given function on every byte of the string

func (Text) EachChar

func (t Text) EachChar(f func(string))

EachChar applies the given function on every char of the string

func (Text) EachLine

func (t Text) EachLine(f func(string))

EachLine applies the given function on every Line of the string

func (Text) EndsWith

func (t Text) EndsWith(pattern string) bool

EndsWith is similar to StartsWith, but checks the last characters

func (Text) EscapeHTML

func (t Text) EscapeHTML() Text

EscapeHTML translates `<` to `&lt;`, and so on

func (Text) First

func (t Text) First(i int) Text

First truncates the text to the first `i` characters

func (Text) Index

func (t Text) Index(seq string) int

Index finds the first occurence of the given string in the text, or -1

func (Text) IsASCIIOnly

func (t Text) IsASCIIOnly() bool

IsASCIIOnly checks if the string contain unicode characters

func (Text) IsEmpty

func (t Text) IsEmpty() bool

IsEmpty checks whether the given string contains zero characters

func (Text) IsEqual

func (t Text) IsEqual(s string) bool

IsEqual compares a text directly with a given string

func (Text) Last

func (t Text) Last(i int) Text

Last truncates the text to the last `i` characters

func (Text) LeftJust

func (t Text) LeftJust(length int) Text

LeftJust increases the length of the string to `i` characters, preps whitespace to left

func (Text) LeftJustString

func (t Text) LeftJustString(length int, seq string) Text

LeftJustString increases the length of the string to `i` characters, preps `seq` to left

func (Text) LeftPad

func (t Text) LeftPad(i int) Text

LeftPad prepends `i` whitespaces to the string

func (Text) LeftPadString

func (t Text) LeftPadString(i int, seq string) Text

LeftPadString prepends `i` times the given sequence to a string

func (Text) Length

func (t Text) Length() int

Length returns the number of characters of the string

func (Text) MapBytes

func (t Text) MapBytes(f func(byte) byte) Text

MapBytes applies a given function on every byte directly

func (Text) MapChars

func (t Text) MapChars(f func(string) string) Text

MapChars applies a given function on every char directly

func (Text) MapLines

func (t Text) MapLines(f func(string) string) Text

MapLines applies a given function on every line directly

func (Text) ReplacePattern

func (t Text) ReplacePattern(pattern string, new string) Text

ReplacePattern accepts a regex pattern as a string, same as ReplaceString()

func (Text) ReplaceString

func (t Text) ReplaceString(old string, new string) Text

ReplaceString replaces all occurences of `old` with `new`

func (Text) Reverse

func (t Text) Reverse() Text

Reverse does exactly this, char by char

func (Text) RightJust

func (t Text) RightJust(length int) Text

RightJust increases the length of the string to `i` characters, appends whitespace to left

func (Text) RightJustString

func (t Text) RightJustString(length int, seq string) Text

RightJustString increases the length of the string to `i` characters, app `seq` to left

func (Text) RightPad

func (t Text) RightPad(i int) Text

RightPad appends `i` whitespaces to the string

func (Text) RightPadString

func (t Text) RightPadString(i int, seq string) Text

RightPadString appends `i` times the given sequence to a string

func (Text) Slice

func (t Text) Slice(from int, to int) Text

Slice cuts out a substring from char `from` to (incl) char `to`

func (Text) SplitPattern

func (t Text) SplitPattern(pattern string) []string

SplitPattern is the same as SplitString, but uses regex pattern as a string

func (Text) SplitString

func (t Text) SplitString(pattern string) []string

SplitString splits the string on every occurence of a given pattern

func (Text) StartsWith

func (t Text) StartsWith(pattern string) bool

StartsWith checks whether the strings first characters match the given pattern

func (Text) Strip

func (t Text) Strip() Text

Strip removes all whitespace at the beginning and end of the string

func (Text) StripLeft

func (t Text) StripLeft() Text

StripLeft removes all whitespace at the beginning of the string

func (Text) StripRight

func (t Text) StripRight() Text

StripRight removes all whitespace at the end of the string

func (Text) ToBytes

func (t Text) ToBytes() []byte

ToBytes returns the string as a byte slice

func (Text) ToChars

func (t Text) ToChars() []string

ToChars returns a slice of characters of the underlying string

func (Text) ToFloat

func (t Text) ToFloat() float64

ToFloat casts the text to a float64 value if possible

func (Text) ToInt

func (t Text) ToInt() int

ToInt casts the text to an int value if possible

func (Text) ToLines

func (t Text) ToLines() []string

ToLines returns a string slice with all the different lines

func (Text) ToString

func (t Text) ToString() string

ToString returns a plain string type

func (Text) UnescapeHTML

func (t Text) UnescapeHTML() Text

UnescapeHTML translates `&lt;` to `<`, and so on

func (Text) Upcase

func (t Text) Upcase() Text

Upcase replaces every character in the string with its uppercase variant

Jump to

Keyboard shortcuts

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