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

Closed
dionysius opened this issue Feb 20, 2020 · 8 comments
Closed
Labels
Documentation FrozenDueToAge NeedsInvestigation Someone must examine and confirm this is a valid issue and not a duplicate of an existing one.
Milestone

Comments

@dionysius
Copy link

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 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

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 the NeedsInvestigation Someone must examine and confirm this is a valid issue and not a duplicate of an existing one. label Feb 20, 2020
@toothrot toothrot added this to the Backlog milestone Feb 20, 2020
@griesemer
Copy link
Contributor

Assigned to @mpvl who is our local Unicode expert.

@dionysius
Copy link
Author

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.

@gopherbot
Copy link

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

@dionysius dionysius reopened this Jun 16, 2020
@fenollp
Copy link

fenollp commented Apr 3, 2022

Quoting https://unicode.org/faq/casemap_charprop.html#4 (@toothrot's link):

Titlecase is also used in forming a sentence by capitalizing the first word, and for forming proper names.
The titlecase mapping in the Unicode Standard is the mapping applied to the initial character in a word.

(emphasis mine)

To recap: strings.Title() (which mapped on words) is deprecated and replaced with strings.ToTitle() which maps on letters.
Why was it named ToTitle when it seems to not even match the Unicode description?

@ianlancetaylor
Copy link
Contributor

strings.Title is not being replaced by strings.ToTitle. Those two functions do different things.

@dionysius
Copy link
Author

dionysius commented Apr 4, 2022

Hi all, I didn't plan to leave this issue open. Don't know why I reopened it. I suggest for further discussion to open a new one and link to this if needed.

@rsc rsc unassigned mpvl Jun 23, 2022
@golang golang locked and limited conversation to collaborators Jun 23, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Documentation FrozenDueToAge NeedsInvestigation Someone must examine and confirm this is a valid issue and not a duplicate of an existing one.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

7 participants