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

Fixes #2490: Adds unescaping \f and the correct implementation of isspace() string method. #2551

Closed
wants to merge 3 commits into from

Conversation

kmr-srbh
Copy link
Contributor

@kmr-srbh kmr-srbh commented Feb 21, 2024

Fix:

#2490 gave wrong output due to the incorrect handling of the escape sequence '\f'. Escaping the sequence was handled, but unescaping was missed. This led to the string " \t\n\v\f\r" being transformed to " \t\n\v\\f\r" in the AST. Note the extra "\" after "v".
Screenshot from 2024-02-21 17-30-11

Hence, the answer in #2490 was thus 'not actually' wrong as non-whitespace and lowercase characters did exist. Fixes #2490

Improvement

Though the error in the above case was due to incorrect string handling, the isspace() method was actually not implemented correctly. The definition of a 'whitespace' character is actually broad. The Python interpreter checks for all of them in it's implementation of the method. I incorporated checking for those characters.

A huge shout-out to @advikkabra for the clever testing. 🎉

Note

The Black formatter has automatically formatted the Python file I worked on. Please note that only change I made was the implementation of the isspace() method.

Last but not the least, I thoroughly enjoyed working on the issue. 😃

@kmr-srbh kmr-srbh changed the title (fixes #2490): Adds unescaping \f and adds the correct implementation of the isspace() string method. Fixes #2490: Adds unescaping \f and adds the correct implementation of the isspace() string method. Feb 21, 2024
@kmr-srbh kmr-srbh changed the title Fixes #2490: Adds unescaping \f and adds the correct implementation of the isspace() string method. Fixes #2490: Adds unescaping \f and the correct implementation of isspace() string method. Feb 21, 2024
@kmr-srbh kmr-srbh closed this Feb 27, 2024
@kmr-srbh kmr-srbh deleted the fix-string-methods branch February 27, 2024 18:40
@kmr-srbh kmr-srbh restored the fix-string-methods branch February 27, 2024 18:45
@kmr-srbh kmr-srbh reopened this Feb 27, 2024
@Thirumalai-Shaktivel
Copy link
Collaborator

What is the status of this PR?

@kmr-srbh
Copy link
Contributor Author

kmr-srbh commented Mar 7, 2024

What is the status of this PR?

It is ready for review @Thirumalai-Shaktivel.

Copy link
Collaborator

@Thirumalai-Shaktivel Thirumalai-Shaktivel left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also, add a test

@@ -974,16 +1103,18 @@ def _lpython_str_isupper(s: str) -> bool:
return False
return is_cased_present


Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Remove all the formatting changes and you can submit a new PR for it.

# If none of the characters match, we have atleast one character which is not
# a whitespace, so we return `False`.
if (
ch_ord != 9
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for handling all the cases, but it doesn't seems to be more readable.
By readable what I meant is, I have to manually check what 9 is, and confirm that \t is handled.

Was there any issue with previous changes?
Also, instead of this long condition check, why not keep the strings in list and have a single condition check?

x += "\f";
idx++;
}
else if (s[idx] == '\\' && s[idx + 1] == '\\') {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please remove the formatting changes.

@Thirumalai-Shaktivel
Copy link
Collaborator

Please mark this PR ready for review once it is ready.

@Thirumalai-Shaktivel Thirumalai-Shaktivel marked this pull request as draft March 7, 2024 06:26
@kmr-srbh kmr-srbh closed this Mar 7, 2024
@kmr-srbh kmr-srbh deleted the fix-string-methods branch March 7, 2024 16:10
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Unexpected output for string method on whitespace characters
2 participants