-
Notifications
You must be signed in to change notification settings - Fork 171
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
base: main
Are you sure you want to change the base?
Conversation
@@ -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 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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") { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
} 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; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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") { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
} 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; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
is_title =false; | |
is_title = false; |
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; | ||
} | ||
} |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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
* 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. |
There was a problem hiding this comment.
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).
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Added few lines
This looks good to me @Agent-Hellboy! Thank you so much for the contributions! Can you add some tests for the |
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? |
@Agent-Hellboy What is the status of this PR? We can add both compile time and run time in the same test. "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. |
As a workaround for lcompilers#2455.
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`
Hi @Thirumalai-Shaktivel
as you can see redo_ismethods is in sync still i am getting the below error
|
fixes #2376