@@ -6872,13 +6872,13 @@ class BodyVisitor : public CommonVisitor<BodyVisitor> {
68726872 }
68736873 } else if (attr_name == " find" ) {
68746874 if (args.size () != 1 ) {
6875- throw SemanticError (" str.find() takes one arguments " ,
6875+ throw SemanticError (" str.find() takes one argument " ,
68766876 loc);
68776877 }
68786878 ASR::expr_t *arg = args[0 ].m_value ;
68796879 ASR::ttype_t *type = ASRUtils::expr_type (arg);
68806880 if (!ASRUtils::is_character (*type)) {
6881- throw SemanticError (" str.find() takes one arguments of type: str" ,
6881+ throw SemanticError (" str.find() takes one argument of type: str" ,
68826882 arg->base .loc );
68836883 }
68846884 if (ASRUtils::expr_value (arg) != nullptr ) {
@@ -6905,6 +6905,41 @@ class BodyVisitor : public CommonVisitor<BodyVisitor> {
69056905 tmp = make_call_helper (al, fn_div, current_scope, args, " _lpython_str_find" , loc);
69066906 }
69076907 return ;
6908+ } else if (attr_name == " count" ) {
6909+ if (args.size () != 1 ) {
6910+ throw SemanticError (" str.count() takes one argument" ,
6911+ loc);
6912+ }
6913+ ASR::expr_t *arg = args[0 ].m_value ;
6914+ ASR::ttype_t *type = ASRUtils::expr_type (arg);
6915+ if (!ASRUtils::is_character (*type)) {
6916+ throw SemanticError (" str.count() takes one argument of type: str" ,
6917+ arg->base .loc );
6918+ }
6919+ if (ASRUtils::expr_value (arg) != nullptr ) {
6920+ ASR::StringConstant_t* sub_str_con = ASR::down_cast<ASR::StringConstant_t>(arg);
6921+ std::string sub = sub_str_con->m_s ;
6922+ int res = ASRUtils::KMP_string_match_count (s_var, sub);
6923+ tmp = ASR::make_IntegerConstant_t (al, loc, res,
6924+ ASRUtils::TYPE (ASR::make_Integer_t (al,loc, 4 )));
6925+ } else {
6926+ ASR::symbol_t *fn_div = resolve_intrinsic_function (loc, " _lpython_str_count" );
6927+ Vec<ASR::call_arg_t > args;
6928+ args.reserve (al, 1 );
6929+ ASR::call_arg_t str_arg;
6930+ str_arg.loc = loc;
6931+ ASR::ttype_t *str_type = ASRUtils::TYPE (ASR::make_Character_t (al, loc,
6932+ 1 , s_var.size (), nullptr ));
6933+ str_arg.m_value = ASRUtils::EXPR (
6934+ ASR::make_StringConstant_t (al, loc, s2c (al, s_var), str_type));
6935+ ASR::call_arg_t sub_arg;
6936+ sub_arg.loc = loc;
6937+ sub_arg.m_value = arg;
6938+ args.push_back (al, str_arg);
6939+ args.push_back (al, sub_arg);
6940+ tmp = make_call_helper (al, fn_div, current_scope, args, " _lpython_str_count" , loc);
6941+ }
6942+ return ;
69086943 } else if (attr_name == " rstrip" ) {
69096944 if (args.size () != 0 ) {
69106945 throw SemanticError (" str.rstrip() takes no arguments" ,
0 commit comments