pehape

package module
v0.0.0-...-8e24813 Latest Latest
Warning

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

Go to latest
Published: Oct 13, 2023 License: GPL-3.0 Imports: 16 Imported by: 0

README

Import

import "github.com/teknologi-umum/pehape/go"

Usage?

Levenshtein

str1 := "kitten"
str2 := "sitting"
fmt.Println(pehape.Levenshtein(str1, str2))
//result : 3
  • with custom cost
str1 := "kitten"
str2 := "sitting"
insertionCost := 1
replacementCost := 2
deletionCost := 3
fmt.Println(pehape.Levenshtein(str1, str2, insertionCost, replacementCost, deletionCost))
//result : 5

Add Slashes

  • sample string
pehape.AddSlashes(`What does "yolo" mean?`)
//result : What does \"yolo\" mean?

Implode

  • sample string
var array = []string{"Hello", "world"}
fmt.Println(pehape.Implode(array, " "))
//result : Hello world
  • sample number
var array = []int{1, 2, 3, 4, 5}
fmt.Println(pehape.Implode(array, " "))
//result : 1 2 3 4 5

Explode

  • With No Limit
var str string = "Hello pehape world"
fmt.Println(pehape.Explode(" ", str))

//result : [Hello pehape world]
  • With Limit
 var str string = "Hello pehape world"
 fmt.Println(pehape.Explode(" ", str, 2))
 //result : [Hello pehape]

 fmt.Println(pehape.Explode(" ", str, -2))
 //result : [Hello]

Ucwords

var bar = "HELLO WORLD!"
fmt.Println(pehape.Ucwords(bar))
//result : "Hello World!"

Bin2Hex

var str = "Hello World!!"
fmt.Println(pehape.Bin2Hex(str))
//result : "48656c6c6f20576f726c642121"

Hex2Bin

var str = "48656c6c6f20576f726c642121"
fmt.Println(pehaphe.Hex2Bin(str))
//result : "Hello World!!"

LTrim

  • with no chars specified
var str = "	\n\x0BHello World"
res, err := pehape.LTrim(str)
if err != nil {
  panic(err)
}
fmt.Println(res)
//result : "Hello World"
  • with specified chars
var str = "123456\t    \n\x0BHello World"
res, err := pehape.LTrim(str, "123456", " ", "\t\n\x0B")
if err != nil {
  panic(err)
}
fmt.Println(res)
//result: "Hello World"
  • with specified char range
var str = "abcdefghijklmnopqrstuvwxyzHELLO WORLD!"
res, err := pehape.LTrim(str, "a..z")
if err != nil {
  panic(err)
}
fmt.Println(res)
//result: "HELLO WOLRD!"

RTrim

  • with no chars specified
var str = "Hello World	\n\x0B"
res, err := pehape.RTrim(str)
if err != nil {
  panic(err)
}
fmt.Println(res)
//result : "Hello World"
  • with specified chars
var str = "Hello World123456\t    \n\x0B"
res, err := pehape.RTrim(str, "123456", " ", "\t\n\x0B")
if err != nil {
  panic(err)
}
fmt.Println(res)
//result : "Hello World"
  • with specified char range
var str = "HELLO WORLD!abcdefghijklmnopqrstuvwxyz"
res, err := pehape.RTrim(str, "a..z")
if err != nil {
  panic(err)
}
fmt.Println(res)
//result : "HELLO WORLD!"

Strrev

var foo = "Hello World!"
fmt.Println(pehape.Strrev(foo))
//result : "!dlroW olleH"

var bar = "kasur rusak"
fmt.Println(pehape.Strrev(bar))
//result : "kasur rusak"

Chr

var charFromNumber = pehape.Chr(65)
fmt.Println(charFromNumber)
//result : "A"

var charFromOctal = pehape.Chr(053)
fmt.Println(charFromOctal)
//result: "+"

var charFromHex = pehape.Chr(0x52)
fmt.Println(charFromHex)
//result: "R"

var symbol = pehape.Chr(240) + pehape.Chr(159) + pehape.Chr(144) + pehape.Chr(152)
fmt.Println(symbol)
//result : "🐘"

StrStartsWith

var result = pehape.StrStartsWith("Hello World", "Hell")
fmt.Println(result)
//result : true

var result2 = pehape.StrStartsWith("Hello World", "World")
fmt.Println(result2)
//result : false

Strlen

var length = pehape.Strlen("🤗🥰")
fmt.Println(length)
// result: 8

StrPad

input := "Alien";
fmt.Println(StrPad(input, 10));                     // produces "Alien     "
fmt.Println(StrPad(input, 10, "-=", STR_PAD_LEFT)); // produces "-=-=-Alien"
fmt.Println(StrPad(input, 10, "_", STR_PAD_BOTH));  // produces "__Alien___"
fmt.Println(StrPad(input,  6, "___"));              // produces "Alien_"
fmt.Println(StrPad(input,  3, "*"));                // produces "Alien"

StrShuffle

shuffle := StrShuffle("abcdef")

//This will print something like: "bfdaec"
fmt.Println(shuffle)

Ucfirst

var converted = pehape.Ucfirst("hello world")
fmt.Println(converted)
//result : "Hello world"

StrContains

var isContains = pehape.StrContains("Hello World!", "World")
fmt.Println(isContains)
//result : true

var isContains2 = pehape.StrContains("Hello World!", "world")
fmt.Println(isContains2)
//restult: false

ChunkSplit

chunk, err := ChunkSplit("hello", 2, "oke")

//This will print: "<nil> heokellokeooke"
fmt.Println(err, chunk)

Documentation

Index

Constants

View Source
const (
	STR_PAD_RIGHT int = iota
	STR_PAD_LEFT
	STR_PAD_BOTH
)

Variables

View Source
var (
	ErrLengthNotValid            = errors.New("length is not valid")
	ErrLengthMustGreaterThanZero = errors.New("length must be greater than zero")

	ErrSeparatorNotValid = errors.New("separator is not valid")
)
View Source
var (
	ErrTooManyArgs           = errors.New("too many args")
	ErrPadTypeInvalid        = errors.New("invalid pad type")
	ErrPadInvalid            = errors.New("invalid pad")
	ErrPadMustNotEmptyString = errors.New("pad must be a non-empty string")
)
View Source
var (
	ErrStrchrInvalidParameterType = errors.New("invalid parameter type")
	ErrStrchrStringNotFound       = errors.New("string not found")
)
View Source
var (
	ErrStrposInvalidOffset  = errors.New("offset is greather than length of string")
	ErrStrposStringNotFound = errors.New("string not found")
)
View Source
var (
	ErrStrrposInvalidOffset  = errors.New("offset is greather than length of string")
	ErrStrrposStringNotFound = errors.New("string is not found")
)
View Source
var (
	ErrLTrimInvalidRange = errors.New("invalid character range")
)
View Source
var (
	ErrRTrimInvalidRange = errors.New("invalid character range")
)
View Source
var (
	ErrStrReplaceInvalidParameter = errors.New("must be given parameter correctly")
)
View Source
var (
	ErrTrimInvalidRange = errors.New("invalid character range")
)

Functions

func AddSlashes

func AddSlashes(str string) string

AddSlashes returns a string with backslashes added before characters that need to be escaped. These characters are: \\ " and \'

func Bin2Hex

func Bin2Hex(str string) (encoded string)

The bin2hex() function converts a string of ASCII characters to hexadecimal values. Parameter: - str => string to be converted Return: - encoded => converted string

func Chr

func Chr(codepoint int) string

Chr is a function that returns a character from a number. Parameter : - codepoint => it can be in decimal, octal, or hex values. Return : - single-byte string

func ChunkSplit

func ChunkSplit(str string, args ...any) (result string, err error)

ChunkSplit - Split a string into smaller chunks php doc: https://www.php.net/manual/en/function.chunk-split

func Explode

func Explode(separator string, str string, limit ...int) ([]string, error)

The Explode function breaks a string into an array. Parameter : - separator => Specifies where to break the string. - str => The string to split. - limit => Specifies the number of array elements to return. If the separator is empty then the function returns an error with the error message `Separator cannot be empty`. If the limit is greater than the length of an element after a split, then return all elements. If the limit is less than the minus length of an element after a split, then return all elements. if the limit is empty or null then return all elements. If the limit is greater than 0, then an array/slice with a maximum of limit elements is returned. If the limit is less than 0, then an array/slice with a maximum of limit elements is returned. If the limit is 0, return the result as an array/slice with a single element. Return : - The array/slice with all elements.

func Hex2Bin

func Hex2Bin(encoded string) (str string, err error)

The hex2bin() function converts a string of hexadecimal values to ASCII characters. Parameter -encoded => hexadecimal value to be converted Return - str => string of ASCII characters - error => error if encoded string contain non hexadecimal character

func Implode

func Implode[T any](array []T, sep ...string) string

The Implode function joins array/slice element with a string. Parameter : - array => The array/slice to join to a string. - separator => Specifies what to put between the array elements. Return : - The string with all array/slice elements joined.

func Lcfirst

func Lcfirst(str string) string

The Lcfirst() function converts the first character of a string to lowercase. Parameter - str => string to be converted Return - res => converted string

func Levenshtein

func Levenshtein(str1 string, str2 string, cost ...int) int

Levenshtein calculates the distance between two strings. This algorithm allow insertions, deletions and substitutions to change one string to the second Compatible with non-ASCII characters

func Ltrim

func Ltrim(str string, chars ...string) (string, error)

The Ltrim() function removes whitespace or other predefined characters from the left side of a string. Parameters

  • str => specifies the string to check
  • chars (optional) => Specifies which characters to remove from the string. if omitted, ordinary whitespace, tab, new line, vertical tab, and carriage return are removed

Return - res => modified string - err => error if given range is invalid

func Md5

func Md5(str string, raw ...bool) (res string)

The Md5() function calculates the MD5 hash of a string. Parameter

  • str => string to be calculated
  • raw => (optional). Specifies hex or binary output format: TRUE: Raw 16 character binary format FALSE: Default. 32 character hex number

Return - res => calculated MD5 hash

func NumberFormat

func NumberFormat(num float64, options ...string) (string, error)

NumberFormat is for formats a number with grouped thousands and optionally decimal digits.

num : The number being formatted. option 1 : Sets the number of decimal digits. If 0, the decimal_separator is omitted from the return value. Default = 0 option 2 : Sets the separator for the decimal point. Default = "." option 3 : Sets the thousands separator. Default = "," The behaviour of this function is like https://www.php.net/manual/en/function.number-format.php

func Rtrim

func Rtrim(str string, chars ...string) (string, error)

The RTrim() function removes whitespace or other predefined characters from the right side of a string. Parameters

  • str => specifies the string to check
  • chars (optional) => Specifies which characters to remove from the string. if omitted, ordinary whitespace, tab, new line, vertical tab, and carriage return are removed

Return - res => modified string - err => error if given range is invalid

func StrContains

func StrContains(haystack, needle string) bool

The StrContains() function checks if a string contains a specified string. Parameters - haystack => the string to search in. - needle => the string to search for. Returns - bool => true if the string contains the search value, false otherwise.

func StrPad

func StrPad(s string, length int, args ...any) (string, error)

StrPad Pad a string to a certain length with another string This function returns the string string padded on the left, the right, or both sides to the specified padding length. If the optional argument pad_string is not supplied, the string is padded with spaces, otherwise it is padded with characters from pad_string up to the limit. Optional params: - pad_string => string or int or float64 - pad_type => STR_PAD_LEFT, STR_PAD_RIGHT, STR_PAD_BOTH php doc: https://www.php.net/manual/en/function.str-pad.php

func StrReplace

func StrReplace(find, replace, str interface{}) (interface{}, int, error)

The StrReplace() function replaces some characters with some other characters in a string. but it has a bit different than PHP does: 1. it not allowed if parameters has nested slice. 2. parameters can only be string or []string, so if you must convert it first into string like PHP does. 3. you can get a count of the number of replacements in return value. Parameters - find => value to find. - replace => value to replace the value in 'find'. - str => value to be searched. Returns - interface{} => string or an slice with replaced values - int => count of the number of replacements - error => errors if parameter is invalid

func StrShuffle

func StrShuffle(str string) string

StrShuffle Randomly shuffles a string php doc: https://www.php.net/manual/en/function.str-shuffle

func StrSplit

func StrSplit(s string, length ...int) ([]string, error)

StrSplit is to convert a string to an array If the optional length parameter is specified, the returned array will be broken down into chunks with each being length in length, except the final chunk which may be shorter if the string does not divide evenly. The default length is 1, meaning every chunk will be one byte in size. If length is less than 1, a nil array and error errMust2Params will be thrown. The behaviour of this function is like https://www.php.net/manual/en/function.str-split.php

func StrStartsWith

func StrStartsWith(haystack, needle string) bool

func Strchr

func Strchr(str string, search interface{}, beforeSearch ...bool) (string, error)

The Strchr() function searches for the first occurrence of a string inside another string. Parameter

  • str => string to search
  • search => Specifies the string to search for. If this parameter is a number, it will search for the character matching the ASCII value of the number
  • beforeSearch => Optional. A boolean value whose default is "false". If set to "true", it returns the part of the string before the first occurrence of the search parameter.

Returns - string => Returns the rest of the string (from the matching point) - error => either string not found, or the parameter search is invalid

func Strlen

func Strlen(str string) int

Then Strlen() function calculates the length of the given string like php does. Parameter

  • str => string to calculate the length

Returns

  • int => the length of the string

func Strpos

func Strpos(str, find string, offset ...int) (int, error)

The Strpos() function finds the position of the first occurrence of a string inside another string. Parameters - str => Specifies the string to search - find => String to be find - offset => (optional). Specifies where to begin the search. If start is a negative number, it counts from the end of the string. Returns - int => Returns the position of the first occurrence of a string - error => errors if the given offsed is invalid or if string not found

func Strrev

func Strrev(s string) string

Strrev is a function that reverses a string. Parameter : - s => The string to reverse Return : - The reversed string

func Strrpos

func Strrpos(haystack, needle string, offset ...int) (int, error)

The Strrpos() function finds the position of the last occurrence of a string inside another string. Parameters

  • haystack => The string to search in.
  • needle => Specifies the string to find
  • offset => If zero or positive, the search is performed left to right skipping the first offset bytes of the haystack. If negative, the search is performed right to left skipping the last offset bytes of the haystack and searching for the first occurrence of needle.

func Trim

func Trim(str string, chars ...string) (string, error)

func Ucfirst

func Ucfirst(str string) string

The Ucfirst() function converts the first character of a string to uppercase. Parameter - str => string to be converted Return - res => converted string

func Ucwords

func Ucwords(str string) string

Ucwords function converts the first character of each word in a string to Uppercase. Parameter : - str => The string to convert. Return : - The string with the first character of each word is converted to Uppercase.

Types

This section is empty.

Jump to

Keyboard shortcuts

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