@@ -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" ,
@@ -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" ," alpha" ," title" }; // 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);
@@ -7272,6 +7252,57 @@ we will have to use something else.
72727252 tmp = ASR::make_LogicalConstant_t (al, loc, is_space,
72737253 ASRUtils::TYPE (ASR::make_Logical_t (al, loc, 4 )));
72747254 return ;
7255+ } else if (attr_name == " isalpha" ) {
7256+ /*
7257+ * Specification -
7258+ Return True if all characters in the string are alphabets,
7259+ and there is at least one character in the string.
7260+ */
7261+ bool is_alpha = (s_var.size () != 0 );
7262+ for (auto &i : s_var) {
7263+ if (!((i >= ' A' && i <= ' Z' ) || (i >= ' a' && i <= ' z' ))) {
7264+ is_alpha = false ;
7265+ break ;
7266+ }
7267+ }
7268+ tmp = ASR::make_LogicalConstant_t (al, loc, is_alpha,
7269+ ASRUtils::TYPE (ASR::make_Logical_t (al, loc, 4 )));
7270+ return ;
7271+ } else if (attr_name == " istitle" ) {
7272+ /*
7273+ * Specification -
7274+ Returns True if all words in the string are in title case,
7275+ and there is at least one character in the string.
7276+ */
7277+ bool is_title = (s_var.size () != 0 );
7278+
7279+ bool in_word = false ; // Represents if we are in a word or not
7280+ bool is_alpha_present = false ;
7281+ for (auto &i : s_var) {
7282+ if (i >= ' A' && i <= ' Z' ) {
7283+ is_alpha_present = true ;
7284+ if (in_word) {
7285+ // We have come across an uppercase character in the middle of a word
7286+ is_title = false ;
7287+ break ;
7288+ } else {
7289+ in_word = true ;
7290+ }
7291+ } else if (i >= ' a' && i <= ' z' ) {
7292+ is_alpha_present = true ;
7293+ if (!in_word) {
7294+ // We have come across a lowercase character at the start of a word
7295+ is_title = false ;
7296+ break ;
7297+ }
7298+ } else {
7299+ in_word = false ;
7300+ }
7301+ }
7302+ is_title = is_title && is_alpha_present;
7303+ tmp = ASR::make_LogicalConstant_t (al, loc, is_title,
7304+ ASRUtils::TYPE (ASR::make_Logical_t (al, loc, 4 )));
7305+ return ;
72757306 } else {
72767307 throw SemanticError (" 'str' object has no attribute '" + attr_name + " '" , loc);
72777308 }
0 commit comments