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

strings: Docs & Examples unclear about difference between ToTitle and ToUpper #37334

Open
dionysius opened this issue Feb 20, 2020 · 5 comments · May be fixed by #37949
Open

strings: Docs & Examples unclear about difference between ToTitle and ToUpper #37334

dionysius opened this issue Feb 20, 2020 · 5 comments · May be fixed by #37949
Assignees
Milestone

Comments

@dionysius
Copy link

@dionysius dionysius commented Feb 20, 2020

What version of Go are you using (go version)?

The current documentation on https://golang.org/pkg/strings/

Does this issue reproduce with the latest release?

Yes, using the examples inside the strings package documentation

What operating system and processor architecture are you using (go env)?

None providable

What did you do?

Copied strings.ToTitle example to strings.ToUpper and changed ToTitle() to ToUpper():

package main

import (
	"fmt"
	"strings"
)

func main() {
	fmt.Println(strings.ToUpper("her royal highness"))
	fmt.Println(strings.ToUpper("loud noises"))
	fmt.Println(strings.ToUpper("хлеб"))
}

What did you expect to see?

A difference in output.

What did you see instead?

Example ToTitle (unmodified) outputs:

HER ROYAL HIGHNESS
LOUD NOISES
ХЛЕБ

Example ToUpper (modified as described above) outputs:

HER ROYAL HIGHNESS
LOUD NOISES
ХЛЕБ

No difference. So for me it is unclear which function is now the correct one to use for an imaginary case.

@dionysius
Copy link
Author

@dionysius dionysius commented Feb 20, 2020

Or in other words:

package main

import (
	"fmt"
	"strings"
)

func main() {
	a := strings.ToTitle("her royal highness, loud noises, хлеб")
	b := strings.ToUpper("her royal highness, loud noises, хлеб")
	fmt.Println(a == b)
}

// outputs:
// true

Document an example that can help the user decide which function is the right one to use for his case. Something that helps him understand what the difference is.

@toothrot toothrot changed the title Docs & Examples unclear about difference between strings.ToTitle and strings.ToUpper strings: Docs & Examples unclear about difference between ToTitle and ToUpper Feb 20, 2020
@toothrot
Copy link
Contributor

@toothrot toothrot commented Feb 20, 2020

Title case and Upper case of a character have distinct definitions in unicode to allow for specific characters that have distinctive differences: https://unicode.org/faq/casemap_charprop.html#4

dz, in latin, is one for example: http://www.fileformat.info/info/unicode/char/01f3/index.htm

It's hard to find a good example to add that doesn't add confusion to people who aren't already familiar with this feature of unicode.

package main

import (
	"fmt"
	"strings"
)

func main() {
	fmt.Println(strings.ToUpper("dzz")) // DZZ
	fmt.Println(strings.ToTitle("dzz")) // DzZ
}

What would you suggest?

/cc @griesemer

@toothrot toothrot added this to the Backlog milestone Feb 20, 2020
@griesemer
Copy link
Contributor

@griesemer griesemer commented Feb 20, 2020

Assigned to @mpvl who is our local Unicode expert.

@dionysius
Copy link
Author

@dionysius dionysius commented Feb 20, 2020

It's hard to find a good example to add that doesn't add confusion to people who aren't already familiar with this feature of unicode.

True! After thinking about it for a short while I know what the difference means now as well. I thought, why not help and raise an small issue to make the difference a little more clear in the docs to be - in the future - quicker to decide what to use for a specific case.

I think just add a little ToTitle specific string for the ToTitle example and we're good to go.

If you want, I'm fine also with a decision against it.

hugolgst added a commit to hugolgst/go that referenced this issue Mar 19, 2020
Fixes golang#37334
@gopherbot
Copy link

@gopherbot gopherbot commented Mar 19, 2020

Change https://golang.org/cl/224097 mentions this issue: strings: change examples for ToTitle and Title, fixes #37334

@dionysius dionysius closed this Jun 16, 2020
@dionysius dionysius reopened this Jun 16, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Linked pull requests

Successfully merging a pull request may close this issue.

5 participants
You can’t perform that action at this time.