Thanks to visit codestin.com
Credit goes to github.com

Skip to content

use istitle , isalpha on string literal #2380

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

Draft
wants to merge 162 commits into
base: main
Choose a base branch
from

Conversation

Agent-Hellboy
Copy link
Contributor

fixes #2376

@@ -6681,7 +6681,7 @@ class BodyVisitor : public CommonVisitor<BodyVisitor> {
/*
String Validation Methods i.e all "is" based functions are handled here
*/
std::vector<std::string> validation_methods{"lower", "upper", "decimal", "ascii", "space"}; // Database of validation methods supported
std::vector<std::string> validation_methods{"lower", "upper", "decimal", "ascii", "space","alpha","title"}; // Database of validation methods supported
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
std::vector<std::string> validation_methods{"lower", "upper", "decimal", "ascii", "space","alpha","title"}; // Database of validation methods supported
std::vector<std::string> validation_methods{"lower", "upper", "decimal", "ascii", "space", "alpha", "title"}; // Database of validation methods supported

@@ -6949,7 +6949,7 @@ class BodyVisitor : public CommonVisitor<BodyVisitor> {
* islower() method is limited to English Alphabets currently
* TODO: We can support other characters from Unicode Library
*/
std::vector<std::string> validation_methods{"lower", "upper", "decimal", "ascii", "space"}; // Database of validation methods supported
std::vector<std::string> validation_methods{"lower", "upper", "decimal", "ascii", "space","alpha","title"}; // Database of validation methods supported
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
std::vector<std::string> validation_methods{"lower", "upper", "decimal", "ascii", "space","alpha","title"}; // Database of validation methods supported
std::vector<std::string> validation_methods{"lower", "upper", "decimal", "ascii", "space", "alpha", "title"}; // Database of validation methods supported

@@ -6998,6 +6998,60 @@ class BodyVisitor : public CommonVisitor<BodyVisitor> {
tmp = ASR::make_LogicalConstant_t(al, loc, is_lower,
ASRUtils::TYPE(ASR::make_Logical_t(al, loc, 4)));
return;
} else if(attr_name == "isalpha") {
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
} else if(attr_name == "isalpha") {
} else if (attr_name == "isalpha") {

* Specification:
* Return True if all characters in the string are alphabetic (letters), and False otherwise.
*/
bool is_alpha = true;
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
bool is_alpha = true;
bool is_alpha = true;

tmp = ASR::make_LogicalConstant_t(al, loc, is_alpha,
ASRUtils::TYPE(ASR::make_Logical_t(al, loc, 4)));
return;
} else if(attr_name == "istitle") {
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
} else if(attr_name == "istitle") {
} else if (attr_name == "istitle") {

bool is_title = true;
int length = s_var.length();
if (length == 0) {
is_title =false;
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
is_title =false;
is_title = false;

Comment on lines 7031 to 7050
for (auto &ch : s_var) {
if ((ch == ' ' || ch == '\t' || ch == '\n') && word_start) {
continue;
} else if (std::isalpha(ch) && (std::isupper(ch))) {
only_whitespace = false;
if (word_start) {
word_start = false;
} else {
is_title=false;
}
} else if (std::isalpha(ch) && (std::islower(ch))) {
only_whitespace = false;
if (word_start) {
is_title=false;
}
word_start = false;
} else {
word_start = true;
}
}
Copy link
Collaborator

Choose a reason for hiding this comment

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

I am slightly concerned about the logic we use here. I wonder if it could be simplified.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Sure, it can be , I will try tomorrow morning.

Copy link
Contributor Author

@Agent-Hellboy Agent-Hellboy Oct 15, 2023

Choose a reason for hiding this comment

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

Hi @Shaikh-Ubaid,

actually we handling 3 flags in here that's why we have complex branching

if ((ch == ' ' || ch == '\t' || ch == '\n') && word_start) {
                        continue; 

we need the only_whitespace flag to tack if all character inside the string is whitespace only so that we can mark the below example as false

(lp) ➜  lpython git:(redo_ismethods) python3                        
Python 3.10.2 | packaged by conda-forge | (main, Mar  8 2022, 15:52:47) [Clang 11.1.0 ] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> "   ".istitle()
False
>>> "   H".istitle()
True
>>> "   \n".istitle()
False
>>> "   \t".istitle()
False
>>> "   \r".istitle()
False

we need word_start to mark it true if a non-alpha char is found so that we can check if the next word is the title case

(lp) ➜  lpython git:(redo_ismethods) ✗ python3
Python 3.10.2 | packaged by conda-forge | (main, Mar  8 2022, 15:52:47) [Clang 11.1.0 ] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> "Hello\rRock".istitle()
True
>>> "Hello\nRock".istitle()
True
>>> "Hello'Rock".istitle()
True

and we need is_ttile to store the response

I think we can somehow merge the 2 branches into one but for simplicity, we should keep these separate
I have added comments to the code

Comment on lines +7018 to +7020
* Specification: Return True if the string is in title case, where the first
* letter of each word is capitalized and the rest are lowercase. Return False
* if the string is empty or does not meet the title case criteria.
Copy link
Collaborator

Choose a reason for hiding this comment

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

I would mention here which specific characters the istitile() ignores/skips. I would also share two examples here about some (small) sentences (couple of words with spaces and special character) and what istitle() would return for them.

... does not meet the title case criteria.

Or I would share about the title case critera (possibly using example(s).

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Added few lines

@ubaidsk
Copy link
Collaborator

ubaidsk commented Oct 14, 2023

This looks good to me @Agent-Hellboy! Thank you so much for the contributions! Can you add some tests for the istitile() and isalpha() on compile time strings? (I think the tests refactored in this PR are for runtime strings.)

@Agent-Hellboy
Copy link
Contributor Author

Can you add some tests for the istitile() and isalpha() on compile time strings? (I think the tests refactored in this PR are for runtime strings.)

Hi @Shaikh-Ubaid, I didn't understand much, so the test added in test_str_attribute is for execution happening at asr, I guess that's compile time, for runtime where do I need to add those tests?

@Thirumalai-Shaktivel
Copy link
Collaborator

@Agent-Hellboy What is the status of this PR?
Resolve the conflicts and reviews. After that mark it as Ready for review.

We can add both compile time and run time in the same test.
Example:

"abc".istitle() # compile time
s: str = "abc"
s.istitle() # runtime

I think @Shaikh-Ubaid suggested to keep the test removed from test_str_01.py

@Thirumalai-Shaktivel Thirumalai-Shaktivel marked this pull request as draft March 5, 2024 12:15
@Agent-Hellboy
Copy link
Contributor Author

@Agent-Hellboy What is the status of this PR? Resolve the conflicts and reviews. After that mark it as Ready for review.

We can add both compile time and run time in the same test. Example:

"abc".istitle() # compile time
s: str = "abc"
s.istitle() # runtime

I think @Shaikh-Ubaid suggested to keep the test removed from test_str_01.py

sure, let me do it.

certik and others added 29 commits March 5, 2024 21:42
See this issue for possibly removing Const, which would avoid this
error:

lfortran/lfortran#3374
* Add semantic error for float declaration & added tests for the same.
Replaced the mistakenly added `gcc` with `g++`.
1. Updated the installation instructions referring to the documentation.
2. Reviewed the complete README and fixed sentences and grammar wherever required.
…les. (lcompilers#2540)

* Added `flex` and `zstd-static=1.5.5` packages.

* Added `flex` and `zstd-static=1.5.5` packages.

* Incorporated suggestions from lcompilers#2537

Moved global package installations to conda environment.

* Updated README.md to reflect changes suggested in lcompilers#2537

* Remove `flex`

* Remove `flex`
@Agent-Hellboy
Copy link
Contributor Author

Agent-Hellboy commented Mar 5, 2024

Hi @Thirumalai-Shaktivel
Actually I made a fresh clone and fetched the PR and have made some changes, now I am stuck in a git issue.

git pull myclone main
From https://github.com/Agent-Hellboy/lpython
 * branch                main       -> FETCH_HEAD
 * [new branch]          main       -> myclone/main
Current branch redo_ismethods is up to date.

as you can see redo_ismethods is in sync still i am getting the below error

git push myclone redo_ismethods
Username for 'https://github.com': Agent-Hellboy
Password for 'https://[email protected]':
To https://github.com/Agent-Hellboy/lpython.git
 ! [rejected]            redo_ismethods -> redo_ismethods (non-fast-forward)
error: failed to push some refs to 'https://github.com/Agent-Hellboy/lpython.git'
hint: Updates were rejected because the tip of your current branch is behind
hint: its remote counterpart. If you want to integrate the remote changes,
hint: use 'git pull' before pushing again.
hint: See the 'Note about fast-forwards' in 'git push --help' for details.

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.

Handle isalpha and istitle like isupper, islower and several is* checks