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

proposal: string: add HasAnyOfPrefixes, HasAnyOfSuffixes #66191

Open
testwill opened this issue Mar 8, 2024 · 5 comments
Open

proposal: string: add HasAnyOfPrefixes, HasAnyOfSuffixes #66191

testwill opened this issue Mar 8, 2024 · 5 comments
Labels
Milestone

Comments

@testwill
Copy link
Contributor

testwill commented Mar 8, 2024

Proposal Details

// HasAnyOfSuffixes returns an indication if the given string has any of the given suffixes.
func HasAnyOfSuffixes(input string, suffixes ...string) bool {
	for _, suffix := range suffixes {
		if HasSuffix(input, suffix) {
			return true
		}
	}

	return false
}

// HasAnyOfPrefixes returns an indication if the given string has any of the given prefixes.
func HasAnyOfPrefixes(input string, prefixes ...string) bool {
	for _, prefix := range prefixes {
		if HasPrefix(input, prefix) {
			return true
		}
	}

	return false
}
@gopherbot gopherbot added this to the Proposal milestone Mar 8, 2024
@seankhliao
Copy link
Member

is this a common enough operation to be in the standard library?

@testwill
Copy link
Contributor Author

testwill commented Mar 8, 2024

not very often

@ianlancetaylor
Copy link
Contributor

@testwill
Copy link
Contributor Author

testwill commented Mar 9, 2024

https://go.dev/doc/faq#x_in_std
thanks

@apparentlymart
Copy link

I could imagine having this in the standard library if it were capable of matching all of the prefixes together in a single pass, but that sort of thing tends to require precomputing a data structure such as a state machine, and the standard library already has regexp for doing that.

It might be interesting to extend that package to have a different frontend that accepts a set of static strings instead of a regexp pattern and compiles it in the same way as a foo|bar|baz-style pattern without having to build the regexp pattern string first, but in most cases where I've needed something like that it's not been a big deal to construct a regexp string and parse it during init, since I'm typically more concerned about the matching performance at runtime than the one-time init cost. 🤔

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
Status: Incoming
Development

No branches or pull requests

5 participants