strarr

package module
v1.0.0 Latest Latest
Warning

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

Go to latest
Published: Jun 10, 2014 License: MIT Imports: 3 Imported by: 2

README

Go-StrArr GoDoc

Various operations for string slices for Go

Go-StrArr is a collection of functions that operate on string slices. Most are functions from package strings that have been adapted to work with string slices, and some were ported from PHP array_* functions.

Features

  • Using a thin layer of idiomatic Go; correctness over performance.
  • Provide most basic array-ish operations: index, trim, filter, map
  • Some PHP favorites like: pop, push, shift, unshift, shuffle, etc...
  • Non-destructive returns (won't alter original slice), except for explicit tasks.
  • The perfect companion to work with net/http headers!

Installation

Using "go get":

go get github.com/codehack/go-strarr

Then import:

import ("github.com/codehack/go-strarr")

Example

package main

import(
   "github.com/codehack/go-strarr"
   "fmt"
)

// This example shows basic usage of various functions by manipulating
// the array 'arr'.
func main() {
   arr := []string{"Go", "nuts", "for", "Go"}

   foo := strarr.Repeat("Go",3)
   fmt.Println(foo)

   fmt.Println(strarr.Count(arr, "Go"))

   fmt.Println(strarr.Index(arr, "Go"))
   fmt.Println(strarr.LastIndex(arr, "Go"))

   if strarr.Contains(arr, "nuts") {
      arr = strarr.Replace(arr, []string{"Insanely"})
   }
   fmt.Println(arr)

   str := strarr.Shift(&arr)
   fmt.Println(str)
   fmt.Println(arr)

   strarr.Unshift(&arr, "Really")
   fmt.Println(arr)

   fmt.Println(strarr.ToUpper(arr))
   fmt.Println(strarr.ToLower(arr))
   fmt.Println(strarr.ToTitle(arr))

   fmt.Println(strarr.Trim(arr,"Really"))
   fmt.Println(strarr.Filter(arr,"Go"))

   fmt.Println(strarr.Diff(arr,foo))
   fmt.Println(strarr.Intersect(arr,foo))

   fmt.Println(strarr.Rand(arr,2))

   fmt.Println(strarr.Reverse(arr))
}

See the testing source strarr_test.go for more examples.

Documentation

The full code documentation is located at GoDoc:

http://godoc.org/github.com/codehack/go-strarr

Go-StrArr is Copyright (c) 2014 Codehack. Published under MIT License

Go nuts for Go!

Documentation

Overview

Various operations for string slices for Go

Package strarr is a collection of functions to manipulate string arrays/slices. Some functions were adapted from the strings package to work with string slices, other were ported from PHP 'array_*' function equivalents.

Example (Basic)

This example shows basic usage of various functions by manipulating the array 'arr'.

arr := []string{"Go", "nuts", "for", "Go"}

foo := strarr.Repeat("Go", 3)
fmt.Println(foo)

fmt.Println(strarr.Count(arr, "Go"))

fmt.Println(strarr.Index(arr, "Go"))
fmt.Println(strarr.LastIndex(arr, "Go"))

if strarr.Contains(arr, "nuts") {
	arr = strarr.Replace(arr, []string{"Insanely"})
}
fmt.Println(arr)

str := strarr.Shift(&arr)
fmt.Println(str)
fmt.Println(arr)

strarr.Unshift(&arr, "Really")
fmt.Println(arr)

fmt.Println(strarr.ToUpper(arr))
fmt.Println(strarr.ToLower(arr))
fmt.Println(strarr.ToTitle(arr))

fmt.Println(strarr.Trim(arr, "Really"))
fmt.Println(strarr.Filter(arr, "Go"))

fmt.Println(strarr.Diff(arr, foo))
fmt.Println(strarr.Intersect(arr, foo))

// this will return 2 random elements from arr
// but it will fail testing, so...
// fmt.Println(strarr.Rand(arr,2))

fmt.Println(strarr.Reverse(arr))
Output:

[Go Go Go]
2
0
3
[Insanely nuts for Go]
Insanely
[nuts for Go]
[Really nuts for Go]
[REALLY NUTS FOR GO]
[really nuts for go]
[Really Nuts For Go]
[nuts for Go]
[Go]
[Really nuts for]
[Go]
[Go for nuts Really]

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func Contains

func Contains(a []string, s string) bool

Contains returns true if 's' is in 'a', false otherwise

func ContainsPrefix

func ContainsPrefix(a []string, prefix string) bool

ContainsPrefix returns true if any entry in 'a' has prefix 'prefix', false otherwise

func ContainsSuffix

func ContainsSuffix(a []string, suffix string) bool

ContainsSuffix returns true if any entry in 'a' has suffix 'suffix', false otherwise

func Count

func Count(a []string, s string) int

Count returns the number of occurrances of 's' in 'a'

func Diff

func Diff(a, b []string) []string

Diff returns a slice with all the entries of 'a' that are not found in 'b'

func Fill

func Fill(n int, s string) []string

Fill is an alias of Repeat (for PHP converts)

func Filter

func Filter(a []string, s string) []string

Filter returns a slice with all the entries of 'a' that match string 's'

func FilterFunc

func FilterFunc(a []string, s string, f func(string, string) bool) []string

FilterFunc returns a slice with all the entries of 'a' that match string 's' using a callback function 'f' Callback is f(value, key string) where value is in 'a' and is checked for key. If true, value will be filtered.

func FilterPrefix

func FilterPrefix(a []string, prefix string) []string

FilterPrefix returns a slice with all the entries of 'a' that have prefix 'prefix'

func FilterSuffix

func FilterSuffix(a []string, suffix string) []string

FilterSuffix returns a slice with all the entries of 'a' that have suffix 'suffix'

func Index

func Index(a []string, s string) int

Index returns the index of the first instance of 's' in 'a', or -1 if not found

func IndexPrefix

func IndexPrefix(a []string, prefix string) int

IndexPrefix returns the index of the first entry in 'a' with prefix 'prefix', or -1 if not found

func IndexSuffix

func IndexSuffix(a []string, suffix string) int

IndexSuffix returns the index of the first entry in 'a' with suffix 'suffix', or -1 if not found

func Intersect

func Intersect(a, b []string) []string

Intersect returns a slice with all the entries of 'a' that are found in 'b'

func LastIndex

func LastIndex(a []string, s string) int

LastIndex returns the index of the last instance of 's' in 'a', or -1 if not found

func LastIndexPrefix

func LastIndexPrefix(a []string, prefix string) int

LastIndexPrefix returns the index of the last entry in 'a' with prefix 'prefix', or -1 if not found

func LastIndexSuffix

func LastIndexSuffix(a []string, suffix string) int

LastIndexSuffix returns the index of the last entry in 'a' with suffix 'suffix', or -1 if not found

func LastSearch

func LastSearch(a []string, s string) int

LastSearch returns the index of the last entry containing the substring 's' in 'a', or -1 if not found

func Map

func Map(mapping func(string) string, a []string) []string

Map returns a slice of 'a' with the function 'mapping' applied to each element

func Pop

func Pop(a *[]string) string

Pop removes the last element in '*a' and returns it, shortening the array by one. If '*a' is empty returns empty string "". Note that this function will change the array pointed by 'a'.

func Push

func Push(a *[]string, s ...string) int

Push appends one or more elements to '*a' and returns the number of entries. Note that this function will change the array pointed by 'a'.

func Rand

func Rand(a []string, n int) []string

Rand returns a slice with 'n' number of random entries of 'a'

func Repeat

func Repeat(s string, n int) []string

Repeat returns a slice consisting of 'n' copies of 's'

func Replace

func Replace(a, b []string) []string

Replace returns a slice with the values of 'a' replaced with the index-matching values of 'b'. If 'b' has more entries than 'a' they will be appended.

func Reverse

func Reverse(a []string) []string

Reverse returns a slice of 'a' in reverse index order.

func Search(a []string, s string) int

Search returns the index of the first entry containing the substring 's' in 'a', or -1 if not found

func Shift

func Shift(a *[]string) string

Shift shifts the first element of '*a' and returns it, shortening the array by one. If '*a' is empty returns empty string "". Note that this function will change the array pointed by 'a'.

func Shuffle

func Shuffle(a []string) []string

Shuffle returns a slice with randomized order of elements in 'a'.

func ToLower

func ToLower(a []string) []string

ToLower returns a slice with all entries of 'a' changed to lower case

func ToTitle

func ToTitle(a []string) []string

ToTitle returns a slice with all entries of 'a' changed to title case

func ToUpper

func ToUpper(a []string) []string

ToUpper returns a slice with all entries of 'a' changed to upper case

func Trim

func Trim(a []string, s string) []string

Trim returns a slice with all the entries of 'a' that don't match string 's'

func TrimFunc

func TrimFunc(a []string, s string, f func(string, string) bool) []string

TrimFunc returns a slice with all the entries of 'a' that don't match string 's' using a callback function 'f' Callback is f(value, key string) where value is in 'a' and is checked for key. If true, value will be trimmed.

func TrimPrefix

func TrimPrefix(a []string, prefix string) []string

TrimPrefix returns a slice with all the entries of 'a' that don't have prefix 'prefix'

func TrimSuffix

func TrimSuffix(a []string, suffix string) []string

TrimSuffix returns a slice with all the entries of 'a' that don't have suffix 'suffix'

func Unshift

func Unshift(a *[]string, s ...string) int

Unshift prepends one or more elements to '*a' and returns the number of entries. Note that this function will change the array pointed by 'a'

Types

This section is empty.

Jump to

Keyboard shortcuts

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