@@ -6577,26 +6577,6 @@ class BodyVisitor : public CommonVisitor<BodyVisitor> {
65776577 arg.loc = loc;
65786578 arg.m_value = s_var;
65796579 fn_args.push_back (al, arg);
6580- } else if (attr_name == " isalpha" ) {
6581- if (args.size () != 0 ) {
6582- throw SemanticError (" str.isalpha() takes no arguments" ,
6583- loc);
6584- }
6585- fn_call_name = " _lpython_str_isalpha" ;
6586- ASR::call_arg_t arg;
6587- arg.loc = loc;
6588- arg.m_value = s_var;
6589- fn_args.push_back (al, arg);
6590- } else if (attr_name == " istitle" ) {
6591- if (args.size () != 0 ) {
6592- throw SemanticError (" str.istitle() takes no arguments" ,
6593- loc);
6594- }
6595- fn_call_name = " _lpython_str_istitle" ;
6596- ASR::call_arg_t arg;
6597- arg.loc = loc;
6598- arg.m_value = s_var;
6599- fn_args.push_back (al, arg);
66006580 } else if (attr_name == " title" ) {
66016581 if (args.size () != 0 ) {
66026582 throw SemanticError (" str.title() takes no arguments" ,
@@ -6813,7 +6793,7 @@ class BodyVisitor : public CommonVisitor<BodyVisitor> {
68136793 /*
68146794 String Validation Methods i.e all "is" based functions are handled here
68156795 */
6816- std::vector<std::string> validation_methods{" lower" , " upper" , " decimal" , " ascii" , " space" }; // Database of validation methods supported
6796+ std::vector<std::string> validation_methods{" lower" , " upper" , " decimal" , " ascii" , " space" , " alpha " , " title " }; // Database of validation methods supported
68176797 std::string method_name = attr_name.substr (2 );
68186798
68196799 if (std::find (validation_methods.begin (),validation_methods.end (), method_name) == validation_methods.end ()) {
@@ -7116,7 +7096,7 @@ class BodyVisitor : public CommonVisitor<BodyVisitor> {
71167096 * islower() method is limited to English Alphabets currently
71177097 * TODO: We can support other characters from Unicode Library
71187098 */
7119- std::vector<std::string> validation_methods{" lower" , " upper" , " decimal" , " ascii" , " space" }; // Database of validation methods supported
7099+ std::vector<std::string> validation_methods{" lower" , " upper" , " decimal" , " ascii" , " space" , " alpha " , " title " }; // Database of validation methods supported
71207100 std::string method_name = attr_name.substr (2 );
71217101 if (std::find (validation_methods.begin (),validation_methods.end (), method_name) == validation_methods.end ()) {
71227102 throw SemanticError (" String method not implemented: " + attr_name, loc);
@@ -7214,6 +7194,57 @@ we will have to use something else.
72147194 tmp = ASR::make_LogicalConstant_t (al, loc, is_space,
72157195 ASRUtils::TYPE (ASR::make_Logical_t (al, loc, 4 )));
72167196 return ;
7197+ } else if (attr_name == " isalpha" ) {
7198+ /*
7199+ * Specification -
7200+ Return True if all characters in the string are alphabets,
7201+ and there is at least one character in the string.
7202+ */
7203+ bool is_alpha = (s_var.size () != 0 );
7204+ for (auto &i : s_var) {
7205+ if (!((i >= ' A' && i <= ' Z' ) || (i >= ' a' && i <= ' z' ))) {
7206+ is_alpha = false ;
7207+ break ;
7208+ }
7209+ }
7210+ tmp = ASR::make_LogicalConstant_t (al, loc, is_alpha,
7211+ ASRUtils::TYPE (ASR::make_Logical_t (al, loc, 4 )));
7212+ return ;
7213+ } else if (attr_name == " istitle" ) {
7214+ /*
7215+ * Specification -
7216+ Returns True if all words in the string are in title case,
7217+ and there is at least one character in the string.
7218+ */
7219+ bool is_title = (s_var.size () != 0 );
7220+
7221+ bool in_word = false ; // Represents if we are in a word or not
7222+ bool is_alpha_present = false ;
7223+ for (auto &i : s_var) {
7224+ if (i >= ' A' && i <= ' Z' ) {
7225+ is_alpha_present = true ;
7226+ if (in_word) {
7227+ // We have come across an uppercase character in the middle of a word
7228+ is_title = false ;
7229+ break ;
7230+ } else {
7231+ in_word = true ;
7232+ }
7233+ } else if (i >= ' a' && i <= ' z' ) {
7234+ is_alpha_present = true ;
7235+ if (!in_word) {
7236+ // We have come across a lowercase character at the start of a word
7237+ is_title = false ;
7238+ break ;
7239+ }
7240+ } else {
7241+ in_word = false ;
7242+ }
7243+ }
7244+ is_title = is_title && is_alpha_present;
7245+ tmp = ASR::make_LogicalConstant_t (al, loc, is_title,
7246+ ASRUtils::TYPE (ASR::make_Logical_t (al, loc, 4 )));
7247+ return ;
72177248 } else {
72187249 throw SemanticError (" 'str' object has no attribute '" + attr_name + " '" , loc);
72197250 }
0 commit comments