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: regexp: add functions MustMatch, MustMatchReader, and MustMatchString #58399

Closed
mohammadv184 opened this issue Feb 8, 2023 · 7 comments

Comments

@mohammadv184
Copy link
Contributor

mohammadv184 commented Feb 8, 2023

Proposal: Add functions MustMatch, MustMatchReader, and MustMatchString

Description

The proposed functions MustMatch, MustMatchReader, and MustMatchString would provide a convenient way to check if a regular expression matches a byte slice, reader, or string, respectively, without having to manually compile the regular expression first. This would improve the readability and simplify the code in certain cases.

Syntax

func MustMatch(pattern string, b []byte) bool
func MustMatchReader(pattern string, r io.RuneReader) bool
func MustMatchString(pattern, s string) bool

Arguments:

  • pattern: A string representing the regular expression pattern.
  • b: A byte slice to be matched against the pattern (for MustMatch).
  • r: A RuneReader to be matched against the pattern (for MustMatchReader).
  • s: A string to be matched against the pattern (for MustMatchString).

Return value:

  • A boolean value indicating whether the byte slice, reader, or string matches the pattern, respectively.

Example usage:

// MustMatch
if regexp.MustMatch("pattern", []byte("subject")) {
    fmt.Println("Match found!")
} else {
    fmt.Println("No match found.")
}

// MustMatchReader
r := strings.NewReader("subject")
if regexp.MustMatchReader("pattern", r) {
    fmt.Println("Match found!")
} else {
    fmt.Println("No match found.")
}

// MustMatchString
if regexp.MustMatchString("pattern", "subject") {
    fmt.Println("Match found!")
} else {
    fmt.Println("No match found.")
}

Benefits:

  • Provides a convenient and readable way to perform regular expression matching against various input types.
  • Avoids the need for manual error handling when compiling the regular expression.
  • Simplifies code in cases where only a single match is needed.

Note:

The implementations would internally use regexp.Compile and the Match, MatchReader, or MatchString method, respectively, but hide the details from the user. In case of any error compiling the regular expression, the functions would panic, following the same behaviour as regexp.MustCompile.

@gopherbot gopherbot added this to the Proposal milestone Feb 8, 2023
@mvdan
Copy link
Member

mvdan commented Feb 8, 2023

How is regexp.MustMatch("pattern", []byte("subject")) different from regexp.MustCompile("pattern").Match([]byte("subject")), beyond saving you a few characters?

@mohammadv184
Copy link
Contributor Author

The purpose of the MustMatch function is to provide a more concise and readable way to perform the common task of matching a regular expression against a byte slice, reader, or string. The difference between MustMatch and MustCompile().Match is that MustMatch combines both the compilation and matching steps into a single function call, making the code more straightforward and easier to read. This can be especially useful in cases where the pattern is only used once or in cases where it's important to clearly convey the intention of the code.

@mvdan
Copy link
Member

mvdan commented Feb 8, 2023

So, just to be clear, the only difference is that your code becomes slightly shorter? You still end up with the same error checking and number of lines, as far as I can tell. Adding new API to just save typing a few characters seems like a bad idea in general to me, unless those savings are very common, like adding any as an alias for interface{}.

@mohammadv184
Copy link
Contributor Author

mohammadv184 commented Feb 8, 2023

Yes, you are correct in that the main difference between MustMatch and MustCompile("pattern").Match([]byte("subject")) is the conciseness of the code. While the error handling is the same in both cases, the MustMatch function provides a more readable and concise way to perform regular expression matching.

However, it's important to note that conciseness and readability are still valuable attributes in programming. By making the code easier to follow and understand, MustMatch can improve the overall maintainability and quality of the codebase. Additionally, the ability to write code in a more concise and readable way can increase developer productivity and reduce the likelihood of errors.

In summary, the MustMatch function may be a useful addition to the Go programming language even if it only offers a small reduction in code length, as it can improve the overall readability and maintainability of the code.

@dominikh
Copy link
Member

dominikh commented Feb 8, 2023

The addition of regexp.MustMatch may not necessarily improve code readability, as the use of regexp.MustCompile(...).Match already provides a clear and concise way of matching a regular expression and handling errors. The use of regexp.MustMatch could add additional complexity to the code, making it harder to understand and maintain. In general, it's best to keep the code simple and stick with established patterns, rather than adding new functions that might not have a clear advantage over existing ones.

@mohammadv184
Copy link
Contributor Author

I understand your concern that this new function may not necessarily improve code readability and may even add additional complexity to the code.

I would like to point out that the primary benefit of the regexp.MustMatch function is to provide a more concise and readable way to match a regular expression, as compared to using regexp.MustCompile(...).Match. In some cases, the use of regexp.MustCompile(...).Match can result in longer and less readable code, especially when used multiple times within a single program.

In terms of established patterns, regexp.MustMatch would simply provide an alternative approach that developers can choose to use based on their own preferences and specific use cases. It is important to note that it would not replace the existing regexp.MustCompile(...).Match function, but rather coexist with it as a choice for developers.

I hope this clarifies the potential benefits of adding regexp.MustMatch to the Go standard library.

@seankhliao
Copy link
Member

I believe we can decline this as another variant of #47593 (comment)

these shorter functions are simple wrappers for any package that needs it frequently enough to write.

@seankhliao seankhliao closed this as not planned Won't fix, can't repro, duplicate, stale Feb 8, 2023
@golang golang locked and limited conversation to collaborators Feb 8, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

5 participants