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

Add String#swapcase and Char#swapcase #10897

Open
Daniel-Worrall opened this issue Jul 6, 2021 · 2 comments
Open

Add String#swapcase and Char#swapcase #10897

Daniel-Worrall opened this issue Jul 6, 2021 · 2 comments

Comments

@Daniel-Worrall
Copy link
Contributor

While I personally do not have a realworld problem for this request, it's a common coding challenge method, and Ruby has String#swapcase

"abc".swapcase # => "ABC"
'a'.swapcase # => 'A'
"aBC".swapcase # => "Abc"

Does not break backwards compatibility, and improves compatibility when translating from Ruby.

Would be nice to make a search if #swapcase is used in any real world Ruby projects.

There have been varying opinions about it in the past about both its usefulness and whether the library should implement "nice to have" trivial methods like this.

Related Issues #2230 #2805 #2889 #7142 #8554

@HertzDevil
Copy link
Contributor

One actual use case of swapcase I found is supporting base 62 in BigInt#to_s, which is actually supported by GMP (plus bases 37..61 which Crystal doesn't support at all):

For base in the range 2..36, digits and lower-case letters are used; for -2..-36, digits and upper-case letters are used; for 37..62, digits, upper-case letters, and lower-case letters (in that significance order) are used.

The problem is, the current to_s for primitive integers uses lowercase letters for the digits 10..35, and uppercase letters for 36..61, so swapcase would precisely fix the string returned by GMP with one call. Unfortunately it is also inappropriate here because it is way faster to modify the C string in-place and Int#to_s needs to be as fast as possible.

@HertzDevil
Copy link
Contributor

A Unicode-correct implementation requires us to invent mappings from e.g. U+01F2 LATIN CAPITAL LETTER D WITH SMALL LETTER Z Dz to the 2-character string dZ.

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

No branches or pull requests

3 participants