Skip to content

Strings methods: lstrip(), rstrip(), strip() #1001

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 8 commits into from
Aug 22, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions integration_tests/test_str_attributes.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,5 +15,16 @@ def lower():
assert s.lower() == "aaaaaabbbbbbbb!@12223bn"
assert "DDd12Vv" .lower() == "ddd12vv"

def strip():
s: str
s = " AASAsaSas "
assert s.rstrip() == " AASAsaSas"
assert s.lstrip() == "AASAsaSas "
assert s.strip() == "AASAsaSas"
assert " AASAsaSas " .rstrip() == " AASAsaSas"
assert " AASAsaSas " .lstrip() == "AASAsaSas "
assert " AASAsaSas " .strip() == "AASAsaSas"

capitalize()
lower()
strip()
91 changes: 91 additions & 0 deletions src/lpython/semantics/python_ast_to_asr.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4268,6 +4268,48 @@ class BodyVisitor : public CommonVisitor<BodyVisitor> {
args.push_back(al, arg);
tmp = make_call_helper(al, fn_div, current_scope, args, "_lpython_str_lower", x.base.base.loc);
return;
} else if (std::string(at->m_attr) == std::string("rstrip")) {
if(args.size() != 0) {
throw SemanticError("str.srtrip() takes no arguments",
x.base.base.loc);
}
ASR::symbol_t *fn_div = resolve_intrinsic_function(x.base.base.loc, "_lpython_str_rstrip");
Vec<ASR::call_arg_t> args;
args.reserve(al, 1);
ASR::call_arg_t arg;
arg.loc = x.base.base.loc;
arg.m_value = se;
args.push_back(al, arg);
tmp = make_call_helper(al, fn_div, current_scope, args, "_lpython_str_rstrip", x.base.base.loc);
return;
} else if (std::string(at->m_attr) == std::string("lstrip")) {
if(args.size() != 0) {
throw SemanticError("str.lrtrip() takes no arguments",
x.base.base.loc);
}
ASR::symbol_t *fn_div = resolve_intrinsic_function(x.base.base.loc, "_lpython_str_lstrip");
Vec<ASR::call_arg_t> args;
args.reserve(al, 1);
ASR::call_arg_t arg;
arg.loc = x.base.base.loc;
arg.m_value = se;
args.push_back(al, arg);
tmp = make_call_helper(al, fn_div, current_scope, args, "_lpython_str_lstrip", x.base.base.loc);
return;
} else if (std::string(at->m_attr) == std::string("strip")) {
if(args.size() != 0) {
throw SemanticError("str.srtrip() takes no arguments",
x.base.base.loc);
}
ASR::symbol_t *fn_div = resolve_intrinsic_function(x.base.base.loc, "_lpython_str_strip");
Vec<ASR::call_arg_t> args;
args.reserve(al, 1);
ASR::call_arg_t arg;
arg.loc = x.base.base.loc;
arg.m_value = se;
args.push_back(al, arg);
tmp = make_call_helper(al, fn_div, current_scope, args, "_lpython_str_strip", x.base.base.loc);
return;
}
}
handle_attribute(se, at->m_attr, x.base.base.loc, eles);
Expand Down Expand Up @@ -4332,6 +4374,55 @@ class BodyVisitor : public CommonVisitor<BodyVisitor> {
1, 1, nullptr, nullptr , 0));
tmp = ASR::make_StringConstant_t(al, x.base.base.loc, s2c(al, res), str_type);
return;
} else if (std::string(at->m_attr) == std::string("rstrip")) {
if(args.size() != 0) {
throw SemanticError("str.rstrip() takes no arguments",
x.base.base.loc);
}
AST::ConstantStr_t *n = AST::down_cast<AST::ConstantStr_t>(at->m_value);
std::string res = n->m_value;
int ind = res.size()-1;
while (ind >= 0 && res[ind] == ' '){
ind--;
}
res = std::string(res.begin(), res.begin() + ind +1);
ASR::ttype_t *str_type = ASRUtils::TYPE(ASR::make_Character_t(al, x.base.base.loc,
1, 1, nullptr, nullptr , 0));
tmp = ASR::make_StringConstant_t(al, x.base.base.loc, s2c(al, res), str_type);
return;
} else if (std::string(at->m_attr) == std::string("lstrip")) {
if(args.size() != 0) {
throw SemanticError("str.lstrip() takes no arguments",
x.base.base.loc);
}
AST::ConstantStr_t *n = AST::down_cast<AST::ConstantStr_t>(at->m_value);
std::string res = n->m_value;
int ind = 0;
while (ind < res.size() && res[ind] == ' ') {
ind++;
}
res = std::string(res.begin() + ind, res.end());
ASR::ttype_t *str_type = ASRUtils::TYPE(ASR::make_Character_t(al, x.base.base.loc,
1, 1, nullptr, nullptr , 0));
tmp = ASR::make_StringConstant_t(al, x.base.base.loc, s2c(al, res), str_type);
return;
} else if (std::string(at->m_attr) == std::string("strip")) {
if(args.size() != 0) {
throw SemanticError("str.strip() takes no arguments",
x.base.base.loc);
}
AST::ConstantStr_t *n = AST::down_cast<AST::ConstantStr_t>(at->m_value);
std::string res = n->m_value;
int l = 0 ,r = res.size() - 1;
while (l < res.size() && r >= 0 && (res[l] == ' ' || res[r] == ' ')) {
l += res[l] == ' ';
r -= res[r] == ' ';
}
res = std::string(res.begin() + l, res.begin() + r + 1);
ASR::ttype_t *str_type = ASRUtils::TYPE(ASR::make_Character_t(al, x.base.base.loc,
1, 1, nullptr, nullptr , 0));
tmp = ASR::make_StringConstant_t(al, x.base.base.loc, s2c(al, res), str_type);
return;
} else {
throw SemanticError("'str' object has no attribute '" + std::string(at->m_attr) + "'",
x.base.base.loc);
Expand Down
58 changes: 56 additions & 2 deletions src/lpython/semantics/python_comptime_eval.h
Original file line number Diff line number Diff line change
Expand Up @@ -63,8 +63,10 @@ struct PythonIntrinsicProcedures {
{"max" , {m_builtin , &eval_max}},
{"min" , {m_builtin , &eval_min}},
{"_lpython_str_capitalize", {m_builtin, &eval__lpython_str_capitalize}},
{"_lpython_str_lower", {m_builtin, &eval__lpython_str_lower}}

{"_lpython_str_lower", {m_builtin, &eval__lpython_str_lower}},
{"_lpython_str_rstrip", {m_builtin, &eval__lpython_str_rstrip}},
{"_lpython_str_lstrip", {m_builtin, &eval__lpython_str_lstrip}},
{"_lpython_str_strip", {m_builtin, &eval__lpython_str_strip}}
};
}

Expand Down Expand Up @@ -689,7 +691,59 @@ struct PythonIntrinsicProcedures {
return ASR::down_cast<ASR::expr_t>(ASR::make_StringConstant_t(al, loc, s2c(al, val), res_type));
}

static ASR::expr_t *eval__lpython_str_rstrip(Allocator &al, const Location &loc, Vec<ASR::expr_t *> &args) {
LFORTRAN_ASSERT(ASRUtils::all_args_evaluated(args));
if (args.size() != 0) {
throw SemanticError("str.rstrip() takes no arguments", loc);
}
ASR::expr_t *arg = args[0];
ASR::ttype_t *arg_type = ASRUtils::expr_type(arg);
std::string res = ASR::down_cast<ASR::StringConstant_t>(arg)->m_s;
int ind = res.size()-1;
while (ind >= 0 && res[ind--] == ' ');
res = std::string(res.begin(), res.begin() + ind +1);
ASR::ttype_t *type = ASRUtils::TYPE(ASR::make_Character_t(al, loc,
1, 1, nullptr, nullptr, 0));
ASR::ttype_t *res_type = ASRUtils::TYPE(ASR::make_StringConstant_t(al, loc, s2c(al, ""), type));
return ASR::down_cast<ASR::expr_t>(ASR::make_StringConstant_t(al, loc, s2c(al, res), res_type));
}

static ASR::expr_t *eval__lpython_str_lstrip(Allocator &al, const Location &loc, Vec<ASR::expr_t *> &args) {
LFORTRAN_ASSERT(ASRUtils::all_args_evaluated(args));
if (args.size() != 0) {
throw SemanticError("str.lstrip() takes no arguments", loc);
}
ASR::expr_t *arg = args[0];
ASR::ttype_t *arg_type = ASRUtils::expr_type(arg);
std::string res = ASR::down_cast<ASR::StringConstant_t>(arg)->m_s;
int ind = 0;
while (ind < res.size() && res[ind++] == ' ');
res = std::string(res.begin() + ind, res.end());
ASR::ttype_t *type = ASRUtils::TYPE(ASR::make_Character_t(al, loc,
1, 1, nullptr, nullptr, 0));
ASR::ttype_t *res_type = ASRUtils::TYPE(ASR::make_StringConstant_t(al, loc, s2c(al, ""), type));
return ASR::down_cast<ASR::expr_t>(ASR::make_StringConstant_t(al, loc, s2c(al, res), res_type));
}

static ASR::expr_t *eval__lpython_str_strip(Allocator &al, const Location &loc, Vec<ASR::expr_t *> &args) {
LFORTRAN_ASSERT(ASRUtils::all_args_evaluated(args));
if (args.size() != 0) {
throw SemanticError("str.strip() takes no arguments", loc);
}
ASR::expr_t *arg = args[0];
ASR::ttype_t *arg_type = ASRUtils::expr_type(arg);
std::string res = ASR::down_cast<ASR::StringConstant_t>(arg)->m_s;
int l = 0 ,r = res.size() - 1;
while (l < res.size() && r >= 0 && (res[l] == ' ' || res[r] == ' ')) {
l += res[l] == ' ';
r -= res[r] == ' ';
}
res = std::string(res.begin() + l, res.begin() + r + 1);
ASR::ttype_t *type = ASRUtils::TYPE(ASR::make_Character_t(al, loc,
1, 1, nullptr, nullptr, 0));
ASR::ttype_t *res_type = ASRUtils::TYPE(ASR::make_StringConstant_t(al, loc, s2c(al, ""), type));
return ASR::down_cast<ASR::expr_t>(ASR::make_StringConstant_t(al, loc, s2c(al, res), res_type));
}
}; // ComptimeEval

} // namespace LFortran
Expand Down
25 changes: 24 additions & 1 deletion src/runtime/lpython_builtin.py
Original file line number Diff line number Diff line change
Expand Up @@ -626,7 +626,7 @@ def _lpython_str_capitalize(x: str) -> str:
return x

@overload
def _lpython_str_lower(x: str) ->str:
def _lpython_str_lower(x: str) -> str:
res: str
res = ""
i:str
Expand All @@ -636,3 +636,26 @@ def _lpython_str_lower(x: str) ->str:
else:
res += i
return res

@overload
def _lpython_str_rstrip(x: str) -> str:
ind: i32
ind = len(x) - 1
while ind >= 0 and x[ind] == ' ':
ind -= 1
return x[0: ind + 1]

@overload
def _lpython_str_lstrip(x: str) -> str:
ind :i32
ind = 0
while ind < len(x) and x[ind] == ' ':
ind += 1
return x[ind :len(x)]

@overload
def _lpython_str_strip(x: str) -> str:
res :str
res = _lpython_str_lstrip(x)
res = _lpython_str_rstrip(res)
return res
2 changes: 1 addition & 1 deletion tests/reference/asr-complex1-f26c460.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
"outfile": null,
"outfile_hash": null,
"stdout": "asr-complex1-f26c460.stdout",
"stdout_hash": "0fb9cae39aefa01c8744f5657e256f05bb9efa74fefed59000535758",
"stdout_hash": "339be2545e1bb94b7c1e71af21f5261ffce5abd8d202a8767f1a47f4",
"stderr": null,
"stderr_hash": null,
"returncode": 0
Expand Down
Loading