@@ -66,6 +66,7 @@ struct PythonIntrinsicProcedures {
66
66
{" _lpython_str_lower" , {m_builtin, &eval__lpython_str_lower}},
67
67
{" _lpython_str_rstrip" , {m_builtin, &eval__lpython_str_rstrip}},
68
68
{" _lpython_str_lstrip" , {m_builtin, &eval__lpython_str_lstrip}},
69
+ {" _lpython_str_strip" , {m_builtin, &eval__lpython_str_strip}}
69
70
};
70
71
}
71
72
@@ -724,6 +725,25 @@ struct PythonIntrinsicProcedures {
724
725
return ASR::down_cast<ASR::expr_t >(ASR::make_StringConstant_t (al, loc, s2c (al, res), res_type));
725
726
}
726
727
728
+ static ASR::expr_t *eval__lpython_str_strip (Allocator &al, const Location &loc, Vec<ASR::expr_t *> &args) {
729
+ LFORTRAN_ASSERT (ASRUtils::all_args_evaluated (args));
730
+ if (args.size () != 0 ) {
731
+ throw SemanticError (" str.strip() takes no arguments" , loc);
732
+ }
733
+ ASR::expr_t *arg = args[0 ];
734
+ ASR::ttype_t *arg_type = ASRUtils::expr_type (arg);
735
+ std::string res = ASR::down_cast<ASR::StringConstant_t>(arg)->m_s ;
736
+ int l = 0 ,r = res.size () - 1 ;
737
+ while (l < res.size () && r >= 0 && (res[l] == ' ' || res[r] == ' ' )) {
738
+ l += res[l] == ' ' ;
739
+ r -= res[r] == ' ' ;
740
+ }
741
+ res = std::string (res.begin () + l, res.begin () + r + 1 );
742
+ ASR::ttype_t *type = ASRUtils::TYPE (ASR::make_Character_t (al, loc,
743
+ 1 , 1 , nullptr , nullptr , 0 ));
744
+ ASR::ttype_t *res_type = ASRUtils::TYPE (ASR::make_StringConstant_t (al, loc, s2c (al, " " ), type));
745
+ return ASR::down_cast<ASR::expr_t >(ASR::make_StringConstant_t (al, loc, s2c (al, res), res_type));
746
+ }
727
747
}; // ComptimeEval
728
748
729
749
} // namespace LFortran
0 commit comments