Skip to content
Closed
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
12 changes: 12 additions & 0 deletions integration_tests/test_str_attributes.py
Original file line number Diff line number Diff line change
Expand Up @@ -497,6 +497,17 @@ def expandtabs():
assert s.expandtabs(0) == "testtest"
assert s.expandtabs(-5) == "testtest"

def zfill():
s1: str = "Teststr"
s2: str = "+12T8tr"
s3: str = "-!@)21"
assert s1.zfill(4) == "Teststr"
assert s1.zfill(-3) == "Teststr"
assert s1.zfill(10) == "000Teststr"

assert s2.zfill(9) == "+0012T8tr"
assert s3.zfill(11) == "-00000!@)21"

def check():
capitalize()
lower()
Expand All @@ -519,6 +530,7 @@ def check():
is_numeric()
center()
expandtabs()
zfill()


check()
19 changes: 19 additions & 0 deletions src/lpython/semantics/python_ast_to_asr.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7598,6 +7598,25 @@ class BodyVisitor : public CommonVisitor<BodyVisitor> {
value.m_value = args[0].m_value;
fn_args.push_back(al, value);
}
} else if (attr_name == "zfill") {
if (args.size() != 1) {
throw SemanticError("str.zfill() takes one argument", loc);
}
ASR::expr_t *arg_sub = args[0].m_value;
ASR::ttype_t *arg_sub_type = ASRUtils::expr_type(arg_sub);
if (!ASRUtils::is_integer(*arg_sub_type)) {
throw SemanticError("str.zfill() argument must be integer", loc);
}
fn_call_name = "_lpython_str_zfill";
ASR::call_arg_t str;
str.loc = loc;
str.m_value = s_var;

ASR::call_arg_t width;
width.loc = loc;
width.m_value = args[0].m_value;
fn_args.push_back(al, str);
fn_args.push_back(al, width);
} else if(attr_name.size() > 2 && attr_name[0] == 'i' && attr_name[1] == 's') {
/*
String Validation Methods i.e all "is" based functions are handled here
Expand Down
37 changes: 19 additions & 18 deletions src/lpython/semantics/python_comptime_eval.h
Original file line number Diff line number Diff line change
Expand Up @@ -77,32 +77,33 @@ struct PythonIntrinsicProcedures {
// The following functions for string methods are not used
// for evaluation.
{"_lpython_str_capitalize", {m_builtin, &not_implemented}},
{"_lpython_str_center", {m_builtin, &not_implemented}},
{"_lpython_str_count", {m_builtin, &not_implemented}},
{"_lpython_str_lower", {m_builtin, &not_implemented}},
{"_lpython_str_upper", {m_builtin, &not_implemented}},
{"_lpython_str_join", {m_builtin, &not_implemented}},
{"_lpython_str_endswith", {m_builtin, &not_implemented}},
{"_lpython_str_expandtabs", {m_builtin, &not_implemented}},
{"_lpython_str_find", {m_builtin, &not_implemented}},
{"_lpython_str_isalpha", {m_builtin, &not_implemented}},
{"_lpython_str_isalnum", {m_builtin, &not_implemented}},
{"_lpython_str_isalpha", {m_builtin, &not_implemented}},
{"_lpython_str_isascii", {m_builtin, &not_implemented}},
{"_lpython_str_isdecimal", {m_builtin, &not_implemented}},
{"_lpython_str_islower", {m_builtin, &not_implemented}},
{"_lpython_str_isnumeric", {m_builtin, &not_implemented}},
{"_lpython_str_title", {m_builtin, &not_implemented}},
{"_lpython_str_isspace", {m_builtin, &not_implemented}},
{"_lpython_str_istitle", {m_builtin, &not_implemented}},
{"_lpython_str_rstrip", {m_builtin, &not_implemented}},
{"_lpython_str_isupper", {m_builtin, &not_implemented}},
{"_lpython_str_join", {m_builtin, &not_implemented}},
{"_lpython_str_lower", {m_builtin, &not_implemented}},
{"_lpython_str_lstrip", {m_builtin, &not_implemented}},
{"_lpython_str_strip", {m_builtin, &not_implemented}},
{"_lpython_str_split", {m_builtin, &not_implemented}},
{"_lpython_str_partition", {m_builtin, &not_implemented}},
{"_lpython_str_replace", {m_builtin, &not_implemented}},
{"_lpython_str_swapcase", {m_builtin, &not_implemented}},
{"_lpython_str_rstrip", {m_builtin, &not_implemented}},
{"_lpython_str_split", {m_builtin, &not_implemented}},
{"_lpython_str_startswith", {m_builtin, &not_implemented}},
{"_lpython_str_endswith", {m_builtin, &not_implemented}},
{"_lpython_str_partition", {m_builtin, &not_implemented}},
{"_lpython_str_islower", {m_builtin, &not_implemented}},
{"_lpython_str_isupper", {m_builtin, &not_implemented}},
{"_lpython_str_isdecimal", {m_builtin, &not_implemented}},
{"_lpython_str_isascii", {m_builtin, &not_implemented}},
{"_lpython_str_isspace", {m_builtin, &not_implemented}},
{"_lpython_str_center", {m_builtin, &not_implemented}},
{"_lpython_str_expandtabs", {m_builtin, &not_implemented}}
{"_lpython_str_strip", {m_builtin, &not_implemented}},
{"_lpython_str_swapcase", {m_builtin, &not_implemented}},
{"_lpython_str_title", {m_builtin, &not_implemented}},
{"_lpython_str_upper", {m_builtin, &not_implemented}},
{"_lpython_str_zfill", {m_builtin, &not_implemented}}
};
}

Expand Down
18 changes: 16 additions & 2 deletions src/runtime/lpython_builtin.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
from lpython import (i8, i16, i32, i64, f32, f64, c32, c64, overload, u8,
u16, u32, u64)
from lpython import (c32, c64, f32, f64, i8, i16, i32, i64, overload, u8, u16,
u32, u64)

#from sys import exit

#: abs() as a generic procedure.
Expand Down Expand Up @@ -1135,6 +1136,19 @@ def _lpython_str_expandtabs(s: str, tabsize: i32) -> str:
def _lpython_str_expandtabs(s: str) -> str:
return _lpython_str_expandtabs(s, 8)

def _lpython_str_zfill(s: str, width: i32) -> str:
if (width <= len(s) ):
return s
ret: str
ret = "0"*(width-len(s))

if (s[0] == '+' or s[0] == '-'):
ret = s[0] + ret + s[1:]
else:
ret = ret + s

return ret

def list(s: str) -> list[str]:
l: list[str] = []
i: i32
Expand Down
2 changes: 1 addition & 1 deletion tests/reference/asr-array_01_decl-39cf894.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
"outfile": null,
"outfile_hash": null,
"stdout": "asr-array_01_decl-39cf894.stdout",
"stdout_hash": "3a65f3ea0a230ad60dcabd62518f2ee3d52a8aa788fc1f7d3835ad72",
"stdout_hash": "b4be50651f899e04a031b4dfd46d34206ad554a8dbfd7699a1076f03",
"stderr": null,
"stderr_hash": null,
"returncode": 0
Expand Down
Loading
Loading