Skip to content

Commit a41041e

Browse files
committed
Resolve comments
1 parent b7b6243 commit a41041e

File tree

2 files changed

+23
-18
lines changed

2 files changed

+23
-18
lines changed

integration_tests/test_str_attributes.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -283,6 +283,8 @@ def is_title():
283283
assert "hELlo wOrlD".istitle() == False
284284
assert " Hello".istitle() == True
285285
assert " ".istitle() == False
286+
assert "&&".istitle() == False
287+
286288

287289
s = "Hello World"
288290
assert s.istitle() == True

src/lpython/semantics/python_ast_to_asr.cpp

Lines changed: 21 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -6949,7 +6949,7 @@ class BodyVisitor : public CommonVisitor<BodyVisitor> {
69496949
* islower() method is limited to English Alphabets currently
69506950
* TODO: We can support other characters from Unicode Library
69516951
*/
6952-
std::vector<std::string> validation_methods{"lower", "upper", "decimal", "ascii", "space", "alpha", "title"}; // Database of validation methods supported
6952+
std::vector<std::string> validation_methods{"lower", "upper", "decimal", "ascii", "space","alpha","title"}; // Database of validation methods supported
69536953
std::string method_name = attr_name.substr(2);
69546954
if(std::find(validation_methods.begin(),validation_methods.end(), method_name) == validation_methods.end()) {
69556955
throw SemanticError("String method not implemented: " + attr_name, loc);
@@ -7015,40 +7015,43 @@ class BodyVisitor : public CommonVisitor<BodyVisitor> {
70157015
return;
70167016
} else if (attr_name == "istitle") {
70177017
/*
7018-
* Specification: Return True if the string is in title case, where the first
7019-
* letter of each word is capitalized and the rest are lowercase. Return False
7018+
* Specification:
7019+
* Return True if the string is in title case, where the first letter
7020+
* of each word is capitalized and the rest are lowercase. Return False
70207021
* if the string is empty or does not meet the title case criteria.
7022+
* *title case criteria *
7023+
* we are threating every non-alpha charater from 0-255 ascii code as
7024+
* seperator and we will return false if all chars are whitespace only
7025+
* but we are skipping leading whitespace and will perform istitle operation
7026+
* on remaing char
70217027
*/
7022-
bool is_title = true;
7023-
int length = s_var.length();
7024-
if (length == 0) {
7025-
is_title = false;
7026-
}
7027-
7028-
bool word_start = true;
7029-
bool only_whitespace = true;
7028+
bool is_title = true; // Flag to track if it's a title
7029+
bool word_start = true; // Flag to track the start of a word
7030+
bool only_whitespace = true; // Flag to track if the input contains only whitespace
70307031

70317032
for (auto &ch : s_var) {
7032-
if ((ch == ' ' || ch == '\t' || ch == '\n') && word_start) {
7033-
continue;
7034-
} else if (std::isalpha(ch) && (std::isupper(ch))) {
7033+
if ((ch == ' ' || ch == '\t' || ch == '\n' || ch == '\r') && word_start) {
7034+
continue; // Skip leading whitespace
7035+
} else if ((ch >= 'A' && ch <= 'Z')) {
70357036
only_whitespace = false;
70367037
if (word_start) {
70377038
word_start = false;
70387039
} else {
7039-
is_title=false;
7040+
is_title = false; // This is not a title if an uppercase letter follows a non-uppercase one
70407041
}
7041-
} else if (std::isalpha(ch) && (std::islower(ch))) {
7042+
} else if ((ch >= 'a' && ch <= 'z')) {
70427043
only_whitespace = false;
70437044
if (word_start) {
7044-
is_title=false;
7045+
is_title = false; // This is not a title if a lowercase letter follows whitespace
70457046
}
70467047
word_start = false;
70477048
} else {
70487049
word_start = true;
70497050
}
70507051
}
7051-
is_title = !only_whitespace && is_title;
7052+
7053+
is_title = !only_whitespace && is_title; // It's a title if it's not only whitespace and all conditions are met
7054+
70527055
tmp = ASR::make_LogicalConstant_t(al, loc, is_title,
70537056
ASRUtils::TYPE(ASR::make_Logical_t(al, loc, 4)));
70547057
return;

0 commit comments

Comments
 (0)