Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 03a9cf1

Browse files
authored
Merge pull request #2342 from SudhanshuJoshi09/str-count
Implemented Str.Count() method
2 parents a872c0b + ee5eeb0 commit 03a9cf1

File tree

67 files changed

+1313
-1260
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

67 files changed

+1313
-1260
lines changed

integration_tests/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -519,6 +519,7 @@ RUN(NAME test_str_01 LABELS cpython llvm c)
519519
RUN(NAME test_str_02 LABELS cpython llvm c)
520520
RUN(NAME test_str_03 LABELS cpython llvm c)
521521
RUN(NAME test_str_04 LABELS cpython llvm c wasm)
522+
RUN(NAME test_str_05 LABELS cpython llvm c)
522523
RUN(NAME test_list_01 LABELS cpython llvm c)
523524
RUN(NAME test_list_02 LABELS cpython llvm c)
524525
RUN(NAME test_list_03 LABELS cpython llvm c NOFAST)

integration_tests/test_str_05.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
def main0():
2+
x: str
3+
x = "Hello, World"
4+
y: str
5+
y = "o"
6+
assert x.count(y) == 2
7+
y = ""
8+
assert x.count(y) == len(x) + 1
9+
y = "Hello,"
10+
assert x.count(y) == 1
11+
12+
main0()

src/lpython/semantics/python_ast_to_asr.cpp

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6791,6 +6791,28 @@ class BodyVisitor : public CommonVisitor<BodyVisitor> {
67916791
[&](const std::string &msg, const Location &loc) {
67926792
throw SemanticError(msg, loc); });
67936793
return;
6794+
} else if(attr_name == "count") {
6795+
if(args.size() != 1) {
6796+
throw SemanticError("str.count() takes one argument for now.", loc);
6797+
}
6798+
ASR::expr_t *arg_value = args[0].m_value;
6799+
ASR::ttype_t *arg_value_type = ASRUtils::expr_type(arg_value);
6800+
if (!ASRUtils::is_character(*arg_value_type)) {
6801+
throw SemanticError("str.count() takes one argument of type: str", loc);
6802+
}
6803+
6804+
fn_call_name = "_lpython_str_count";
6805+
ASR::call_arg_t str;
6806+
str.loc = loc;
6807+
str.m_value = s_var;
6808+
6809+
ASR::call_arg_t value;
6810+
value.loc = loc;
6811+
value.m_value = args[0].m_value;
6812+
6813+
// Push string and substring argument on top of Vector (or Function Arguments Stack basically)
6814+
fn_args.push_back(al, str);
6815+
fn_args.push_back(al, value);
67946816
} else if(attr_name.size() > 2 && attr_name[0] == 'i' && attr_name[1] == 's') {
67956817
/*
67966818
String Validation Methods i.e all "is" based functions are handled here

src/lpython/semantics/python_comptime_eval.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,7 @@ struct PythonIntrinsicProcedures {
7777
// The following functions for string methods are not used
7878
// for evaluation.
7979
{"_lpython_str_capitalize", {m_builtin, &not_implemented}},
80+
{"_lpython_str_count", {m_builtin, &not_implemented}},
8081
{"_lpython_str_lower", {m_builtin, &not_implemented}},
8182
{"_lpython_str_upper", {m_builtin, &not_implemented}},
8283
{"_lpython_str_find", {m_builtin, &not_implemented}},

src/runtime/lpython_builtin.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -722,6 +722,23 @@ def _lpython_str_capitalize(x: str) -> str:
722722
res = chr(val) + res[1:]
723723
return res
724724

725+
726+
@overload
727+
def _lpython_str_count(x: str, y: str) -> i32:
728+
if(len(y) == 0): return len(x) + 1
729+
730+
count: i32 = 0
731+
curr_char: str
732+
i: i32
733+
734+
for i in range(len(x)):
735+
curr_char = x[i]
736+
if curr_char == y[0]:
737+
count += i32(x[i:i+len(y)] == y)
738+
739+
return count
740+
741+
725742
@overload
726743
def _lpython_str_lower(x: str) -> str:
727744
res: str

tests/reference/asr-array_01_decl-39cf894.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
"outfile": null,
77
"outfile_hash": null,
88
"stdout": "asr-array_01_decl-39cf894.stdout",
9-
"stdout_hash": "b0dc16e057dc08b7ec8adac23b2d98fa29d536fca17934c2689425d8",
9+
"stdout_hash": "960bc68922ceaabf957cbdc2cfc76c1f4a4758856072d6be3ae9ed18",
1010
"stderr": null,
1111
"stderr_hash": null,
1212
"returncode": 0

0 commit comments

Comments
 (0)