Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Strings a structures with methods #1401

Open
serge-hulne opened this issue Jul 30, 2023 · 0 comments
Open

Strings a structures with methods #1401

serge-hulne opened this issue Jul 30, 2023 · 0 comments

Comments

@serge-hulne
Copy link

serge-hulne commented Jul 30, 2023

Proposal

in order to be able to use strings as structures (which is more familiar for Python users), as in:

func main() {
	// Strings
	var s String = "Hello world"
	println s.ToUpper()
	for item <- s.Split(" ") {
		println item
	}
}

Background

I propose the code hereunder:

Workarounds

package main

import (
	"strings"
)

type String string

// Functions that return a new String value

func (s String) ToLower() String {
	return String(strings.ToLower(string(s)))
}

func (s String) ToUpper() String {
	return String(strings.ToUpper(string(s)))
}

func (s String) Title() String {
	return String(strings.Title(string(s)))
}

func (s String) ToTitle() String {
	return String(strings.ToTitle(string(s)))
}

func (s String) TrimSpace() String {
	return String(strings.TrimSpace(string(s)))
}

func (s String) Trim(cutset String) String {
	return String(strings.Trim(string(s), string(cutset)))
}

func (s String) TrimLeft(cutset String) String {
	return String(strings.TrimLeft(string(s), string(cutset)))
}

func (s String) TrimRight(cutset String) String {
	return String(strings.TrimRight(string(s), string(cutset)))
}

// Functions that return a new String value with custom replacements

func (s String) Replace(old, new String, n int) String {
	return String(strings.Replace(string(s), string(old), string(new), n))
}

func (s String) ReplaceAll(old, new String) String {
	return String(strings.ReplaceAll(string(s), string(old), string(new)))
}

func (s String) Map(mapping func(r rune) rune) String {
	rs := []rune(string(s))
	for i, r := range rs {
		rs[i] = mapping(r)
	}
	return String(string(rs))
}

// Functions that return slices of substrings

func (s String) Fields() []String {
	fields := strings.Fields(string(s))
	res := make([]String, len(fields))
	for i, field := range fields {
		res[i] = String(field)
	}
	return res
}

func (s String) Split(sep String) []String {
	parts := strings.Split(string(s), string(sep))
	res := make([]String, len(parts))
	for i, part := range parts {
		res[i] = String(part)
	}
	return res
}

func (s String) SplitAfter(sep String) []String {
	parts := strings.SplitAfter(string(s), string(sep))
	res := make([]String, len(parts))
	for i, part := range parts {
		res[i] = String(part)
	}
	return res
}

func (s String) SplitN(sep String, n int) []String {
	parts := strings.SplitN(string(s), string(sep), n)
	res := make([]String, len(parts))
	for i, part := range parts {
		res[i] = String(part)
	}
	return res
}

// Functions that test the prefix/suffix

func (s String) HasPrefix(prefix String) bool {
	return strings.HasPrefix(string(s), string(prefix))
}

func (s String) HasSuffix(suffix String) bool {
	return strings.HasSuffix(string(s), string(suffix))
}

// Functions that test the inclusion of substrings

func (s String) Contains(subs String) bool {
	return strings.Contains(string(s), string(subs))
}

func (s String) ContainsAny(chars String) bool {
	return strings.ContainsAny(string(s), string(chars))
}

func (s String) Index(subs String) int {
	return strings.Index(string(s), string(subs))
}

func (s String) LastIndex(subs String) int {
	return strings.LastIndex(string(s), string(subs))
}

func (s String) IndexAny(chars String) int {
	return strings.IndexAny(string(s), string(chars))
}

func (s String) LastIndexAny(chars String) int {
	return strings.LastIndexAny(string(s), string(chars))
}

// Functions that count occurrences

func (s String) Count(subs String) int {
	return strings.Count(string(s), string(subs))
}

// Function that compare strings

func (s String) EqualFold(t String) bool {
	return strings.EqualFold(string(s), string(t))
}

// Function that compare strings with custom comparison

func (s String) Compare(t String) int {
	return strings.Compare(string(s), string(t))
}

/*
func main() {
	var s String = "   Hello, 世界!  "
	fmt.Println("Original String:", s)
	fmt.Println("ToLower():", s.ToLower())
	fmt.Println("ToUpper():", s.ToUpper())
	fmt.Println("Title():", s.Title())
	fmt.Println("ToTitle():", s.ToTitle())
	fmt.Println("TrimSpace():", s.TrimSpace())
	fmt.Println("Trim(\"He !\"):", s.Trim("He !"))
	fmt.Println("TrimLeft(\" H!\"):", s.TrimLeft(" H!"))
	fmt.Println("TrimRight(\"H !\"):", s.TrimRight("H !"))
	fmt.Println("Replace(\"世\", \"\uFFFd\", -1):", s.Replace("世", "\uFFFd", -1))
	fmt.Println("ReplaceAll(\"l\", \"L\"):", s.ReplaceAll("l", "L"))
	fmt.Println("Map(unicode.ToUpper):", s.Map(unicode.ToUpper))
	fmt.Println("Fields():", s.Fields())
	fmt.Println("Split(\", \"):", s.Split(", "))
	fmt.Println("SplitAfter(\", \"):", s.SplitAfter(", "))
	fmt.Println("SplitN(\", \", 2):", s.SplitN(", ", 2))
	fmt.Println("HasPrefix(\"   He\"):", s.HasPrefix("   He"))
	fmt.Println("HasSuffix(\"界!  \"):", s.HasSuffix("界!  "))
	fmt.Println("Contains(\"l\"):", s.Contains("l"))
	fmt.Println("ContainsAny(\"!世\"):", s.ContainsAny("!世"))
	fmt.Println("Index(\", 世界\"):", s.Index(", 世界"))
	fmt.Println("LastIndex(\"界\"):", s.LastIndex("界"))
	fmt.Println("IndexAny(\" 世\"):", s.IndexAny(" 世"))
	fmt.Println("LastIndexAny(\" 世\"):", s.LastIndexAny(" 世"))
	fmt.Println("Count(\"l\"):", s.Count("l"))
	fmt.Println("EqualFold(\"hello, 世界!\"):", s.EqualFold("hello, 世界!"))
	fmt.Println("Compare(\"Hello, 世界!\"):", s.Compare("Hello, 世界!"))
}
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants