Hello Swift Community,
I would like to propose a new feature to the Swift standard library that simplifies substring searching in Swift. The goal is to make it easy to search for multiple substrings within a given text, providing a more concise and expressive way to represent complex search patterns.
Motivation
In many scenarios, developers need to search for multiple substrings within a larger string. Swift's built-in contains
method is great for checking if a single substring exists within a string, but it can become cumbersome and unwieldy when searching for multiple substrings.
Consider the following example:
let text = "The quick brown fox jumps over the lazy dog."
let containsQuickOrJumps = text.contains("quick") || text.contains("jumps")
let containsFoxAndDog = text.contains("fox") && text.contains("dog")
This approach can quickly become complex and difficult to maintain when checking for a large number of substrings. As a result, it is essential to find a more efficient and expressive way to represent complex search patterns in Swift.
Proposed Solution
The proposed solution is to introduce custom infix operators and predicates to the Swift standard library, which can be used to create complex and flexible search patterns in a more readable manner.
Here's an example of how this new feature can be used to search for substrings in a given text:
let text = "The quick brown fox jumps over the lazy dog."
// Check if text contains "quick" OR "jumps"
let containsQuickOrJumps = text.contains("quick" || "jumps")
// Check if text contains "fox" AND "dog"
let containsFoxAndDog = text.contains("fox" && "dog")
// Check if text contains "fox" AND ("jumps" OR "swift")
let containsFoxAndJumpsOrSwift = text.contains("fox" && ("jumps" || "swift"))
By introducing custom infix operators and predicates for substring searching, the search patterns become more readable, expressive, and easier to understand. This new feature simplifies the process of searching for multiple substrings in Swift, making the code more maintainable and less prone to bugs.
Request for Feedback
I would love to hear your thoughts and feedback on this proposal. If you have any suggestions or concerns, please feel free to share them in this thread. I'm particularly interested in hearing about:
- The overall utility and usefulness of this feature.
- Potential issues or concerns related to source compatibility, ABI stability, or API resilience.
- Alternative approaches or improvements to the proposed solution.
Thank you for your time and consideration!
Link to the full proposal: SE-NNNN
Link to the repository with Implementation.