@@ -6949,7 +6949,7 @@ class BodyVisitor : public CommonVisitor<BodyVisitor> {
6949
6949
* islower() method is limited to English Alphabets currently
6950
6950
* TODO: We can support other characters from Unicode Library
6951
6951
*/
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
6953
6953
std::string method_name = attr_name.substr (2 );
6954
6954
if (std::find (validation_methods.begin (),validation_methods.end (), method_name) == validation_methods.end ()) {
6955
6955
throw SemanticError (" String method not implemented: " + attr_name, loc);
@@ -7015,40 +7015,43 @@ class BodyVisitor : public CommonVisitor<BodyVisitor> {
7015
7015
return ;
7016
7016
} else if (attr_name == " istitle" ) {
7017
7017
/*
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
7020
7021
* 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
7021
7027
*/
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
7030
7031
7031
7032
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 ' )) {
7035
7036
only_whitespace = false ;
7036
7037
if (word_start) {
7037
7038
word_start = false ;
7038
7039
} else {
7039
- is_title= false ;
7040
+ is_title = false ; // This is not a title if an uppercase letter follows a non-uppercase one
7040
7041
}
7041
- } else if (std::isalpha (ch) && ( std::islower (ch) )) {
7042
+ } else if ((ch >= ' a ' && ch <= ' z ' )) {
7042
7043
only_whitespace = false ;
7043
7044
if (word_start) {
7044
- is_title= false ;
7045
+ is_title = false ; // This is not a title if a lowercase letter follows whitespace
7045
7046
}
7046
7047
word_start = false ;
7047
7048
} else {
7048
7049
word_start = true ;
7049
7050
}
7050
7051
}
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
+
7052
7055
tmp = ASR::make_LogicalConstant_t (al, loc, is_title,
7053
7056
ASRUtils::TYPE (ASR::make_Logical_t (al, loc, 4 )));
7054
7057
return ;
0 commit comments