strings.um

fn contains

returns true, if string is contained in another string

fn contains*(inp, check: str): bool {

fn join

joins an array of string into one and adds a joiner between them

fn join*(inp: []str, joiner: str): str {

fn has_prefix

Returns true if s starts with p

fn has_prefix*(s, p: str): bool {

fn split

Splits the string by the patterns in ps

fn split*(s: str, ps: ..str): []str {

fn replace

replaces an pattern with a string

fn replace*(inp, pattern, replacer: str): str {

fn toupper

returns the same string, but characters are upper case

fn toupper*(inp: str): str {

fn tolower

returns the same string, but all characters are lower case

fn tolower*(inp: str): str {

fn repeat

repeats string count number of times

fn repeat*(inp: str, count: int): str {

fn trimspaces

trims whitespaces from the start and end of a string

fn trimspaces*(inp: str): str {

fn trimprefix

trims a prefix of a string

fn trimprefix*(inp: str, prefix: str): str {

fn trimsuffix

trims a suffix of a string

fn trimsuffix*(inp: str, suffix: str): str {


//~~fn contains
// returns true, if string is contained in another string
fn contains*(inp, check: str): bool {
//~~
	for i:=0; i < len(inp); i++ {
		for j:=0; i+j < len(inp) && inp[i+j] == check[j] && j < len(check); j++ {
			if j == len(check) - 1 {
				return true
			}
		}
	}

	return false
}

//~~fn join
// joins an array of string into one and adds a joiner between them
fn join*(inp: []str, joiner: str): str {
//~~
	s := ""

	for i:=0; i < len(inp); i++ {
		s += inp[i]
		if i != len(inp) - 1 {
			s += joiner
		}
	}

	return s
}

fn _has_prefix(s, p: str, i, ls, lp: int): bool {
	if ls - i < lp { return false }

	for j:=0; j < lp; j++ {
		if s[i] != p[j] { return false }
		i++
	}

	return true
}

//~~fn has_prefix
// Returns true if `s` starts with `p`
fn has_prefix*(s, p: str): bool {
//~~
	return _has_prefix(s, p, 0, len(s), len(p))
}

//~~fn split
// Splits the string by the patterns in `ps`
fn split*(s: str, ps: ..str): []str {
//~~
	o := []str{}
	e := 0
	ls := len(s)

	for i:=0; i < ls; i++ {
		for _, p in ps {
			lp := len(p)
			if _has_prefix(s, p, i, ls, lp) {
				o = append(o, slice(s, e, i))
				i += lp - 1
				e = i + 1
				break
			}            
		}
	}
	o = append(o, slice(s, e))
	return o
}

//~~fn replace
// replaces an pattern with a string
fn replace*(inp, pattern, replacer: str): str {
//~~
	return join(split(inp, pattern), replacer)
}

fn chartoupper(inp: char): char {
	if int(inp) >= int('a') && int(inp) <= int('z') {
		return char(int(inp) - 32) 
	}

	return inp
}

//~~fn toupper
// returns the same string, but characters are upper case
fn toupper*(inp: str): str {
//~~
	l := len(inp)
	b := make([]char, l)

	for i:=0; i < l; i++ {
		b[i] = chartoupper(inp[i])
	}

	return str(b)
}

fn chartolower(inp: char): char {
	if int(inp) >= 63 && int(inp) <= 90 {
		return char(int(inp) + 32) 
	}

	return inp
}

//~~fn tolower
// returns the same string, but all characters are lower case
fn tolower*(inp: str): str {
//~~
	l := len(inp)
	b := make([]char, l)

	for i:=0; i < l; i++ {
		b[i] = chartolower(inp[i])
	}

	return str(b)
}

//~~fn repeat
// repeats string count number of times
fn repeat*(inp: str, count: int): str {
//~~
	s := ""
	for i:=0; i < count; i++ { s += inp }
	return s
}

//~~fn trimspaces
// trims whitespaces from the start and end of a string
fn trimspaces*(inp: str): str {
//~~
	if len(inp) == 0 {
		return ""
	}

	var start, end: int

	for start < len(inp) && inp[start] == ' ' {
		start++
	}

	if start >= len(inp) {
		return ""
	}

	end = len(inp)-1
	for end >= 0 && inp[end] == ' ' {
		end--
	}

	if end > len(inp) {
		end = len(inp)
	}

	return slice(inp, start, end+1)
}

//~~fn trimprefix
// trims a prefix of a string
fn trimprefix*(inp: str, prefix: str): str {
//~~
	lenght := 0

	for lenght < len(prefix) && lenght < len(inp) && inp[lenght] == prefix[lenght] {
		lenght++
	}

	if lenght != len(prefix) {
		return inp
	}

	return slice(inp, lenght, len(inp))
}

//~~fn trimsuffix
// trims a suffix of a string
fn trimsuffix*(inp: str, suffix: str): str {
//~~
	lenght := len(inp) - 1

	for i:=len(suffix)-1; i >= 0 && lenght >= 0 && inp[lenght] == suffix[i]; i-- {
		lenght--
	}

	if len(inp) - 1 - lenght != len(suffix) {
		return inp
	}

	return slice(inp, 0, lenght+1)
}