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

[DOCUMENTATION] Clarify ambiguous String Documentation #100849

Open
wants to merge 3 commits into
base: master
Choose a base branch
from

Conversation

ColinSORourke
Copy link

The documentation for the native String Class contains an ambiguity in the description of the CompareTo functions (casecmp_to(), nocasecmp_to(), naturalcasecmpto(), and naturalnocasecmp_to()

The description currently reads:

Performs a case-sensitive comparison to another string. Returns -1 if less than, 1 if greater than, or 0 if equal. "Less than" and "greater than" are determined by the Unicode code points of each string, which roughly matches the alphabetical order.

With different string lengths, returns 1 if this string is longer than the to string, or -1 if shorter. Note that the length of empty strings is always 0.

When looking at an example such as
"aab".nocasecmp_to("aaaa")
The most direct interpretation of the current wording suggests the nocasecmp_to() would return -1 because this string is shorter than the to string. The actual behavior of the function is that it returns 1 because the character comparison reaches 'b' v. 'a' and determines 'b' and the first string are greater than the second string (matching the common definition of Lexicographical Order). The length comparison only occurs conditionally, if the comparison had not yet already resolved.

I suggest updating this wording in the relevant to locations to

If the character comparison reaches the end of one string, but the other contains more characters, then it will use length as the deciding factor: 1 will be returned if this string is longer than the to string, or -1 if shorter.

Which I believe more accurately communicates the behavior of the function. Thanks!

Colin O'Rourke added 2 commits December 27, 2024 03:57
Clarifying Ambiguous wording in the description of multiple String Compare To functions.
Clarifying Ambiguous wording in the description of multiple String Compare To functions.
@ColinSORourke ColinSORourke requested a review from a team as a code owner December 27, 2024 12:53
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.

2 participants