Skip to content

trimmingPrefixOrNil #814

New issue

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

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

Already on GitHub? # to your account

Open
dabrahams opened this issue May 7, 2025 · 1 comment
Open

trimmingPrefixOrNil #814

dabrahams opened this issue May 7, 2025 · 1 comment

Comments

@dabrahams
Copy link

Motivation

The current trimmingPrefx drops the information about whether the prefix was matched. I often do something like:

if x.starts(with: "foo") { doSomething(with: x.dropFirst("foo".count)) }

Proposed solution

if let y = x.trimmingPrefixOrNil("foo") { doSomething(with: y) }

Would be more expressive, less error prone, and more efficient. I don't love the name; maybe something better can be found. Frankly I think trimmingPrefix would have been a better name for the functionality I want. After all if "foo" can't be trimmed off the front of x, then x with the prefix "foo" trimmed is not a thing, and nil is an appropriate response.

Alternatives considered

No response

Additional information

No response

@AnthonyLatsis
Copy link

@hamishknight Requesting transfer to experimental string processing.

@hamishknight hamishknight transferred this issue from swiftlang/swift May 19, 2025
# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants