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

Adds Char#swapcase and String#swapcase instance methods. #2805

Closed
wants to merge 1 commit into from
Closed

Adds Char#swapcase and String#swapcase instance methods. #2805

wants to merge 1 commit into from

Conversation

senorprogrammer
Copy link

@senorprogrammer senorprogrammer commented Jun 11, 2016

To compliment #upcase and #downcase.

Examples:

"a".swapcase # => "A"
"Cats".swapcase # => "cATS"

To compliment #upcase and #downcase.

Examples:

    "a".swapcase # => "A"
    "Cats".swapcase # => "cATS"
@oprypin
Copy link
Member

oprypin commented Jun 11, 2016

What are the usecases?

Also, I'd call it swap_case. It's not the same kind of name as upcase.

@jhass
Copy link
Member

jhass commented Jun 11, 2016

Mmh, I haven't used swapcase in Ruby yet and never missed in Crystal, so I'm curious about its usecases :)

@oprypin
Copy link
Member

oprypin commented Jun 11, 2016

Then again, even Python has a swapcase method (named like this too), so it's OK

@senorprogrammer
Copy link
Author

senorprogrammer commented Jun 11, 2016

I was just porting some Ruby code to Crystal that used swapcase to randomize character cases in randomly-generated strings. Thought it would be nice to have it for compatibility.

The name is for Ruby equivalence (http://ruby-doc.org/core-2.2.0/String.html#method-i-swapcase).

@jhass
Copy link
Member

jhass commented Jun 11, 2016

Can you link/show the Ruby code?

@senorprogrammer
Copy link
Author

senorprogrammer commented Jun 11, 2016

Original gist: https://gist.github.com/lpar/1031933

@jhass
Copy link
Member

jhass commented Jun 11, 2016

Well, there's no need for swapcase in that example. The source text is guaranteed to be lowercase and the comment right above it even says that the intent is to upcase a letter, so using upcase is just fine here.

@senorprogrammer
Copy link
Author

senorprogrammer commented Jun 11, 2016

Yes, obviously the linked code isn't great code, that's not really the point. There are so many different ways to write that, we could go on about it all day.

In porting the concept over I just happened to notice that a simple Ruby function wasn't available in Crystal and took the few minutes to add it. People are coming to Crystal from Ruby and I thought it would be nice to have that additional singular point of compatibility.

Feel free to accept or reject, it's your language, I don't feel strongly enough either way to continue this.

@asterite
Copy link
Member

I also can't find a strong use case for this. Searching in Google and GitHub I found two use cases:

  1. Matching a password as is and with swapcase, in case the user had caps lock on (well, let's say a "use case")
  2. Use in competitions that ask a user to implement a swapcase function. In Ruby and Python one uses swapcase and that's it, problem solved.

Given that the implementation is trivial (at least seeing the string as ASCII) and that it's unlikely to change in the future, I wouldn't mind including it in the standard library. At least it doesn't bother.

@asterite
Copy link
Member

A note: the linked code mutates a String, so that will anyway fail to compile and you'll have to do it in a different way... though maybe it would be nice to have a String method to replace a char or a string at a given position/range with another char/string/object. Ruby uses []= for that, maybe we can add a replace_at(index, value) and replace_at(range, value), or maybe just reuse the sub name?

@jhass
Copy link
Member

jhass commented Jun 11, 2016

sub(Int, Char) and sub(Range, String) should be okay.

@kostya
Copy link
Contributor

kostya commented Nov 21, 2016

👍

@Sija
Copy link
Contributor

Sija commented Nov 22, 2016

Since #3560 is merged this PR should take care of passing Unicode::CaseOptions.

@asterite
Copy link
Member

I don't think there's a strong use case for this, so I'm closing this.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

6 participants