Skip to content

Commit

Permalink
Make startswith and endswith work - Issue skulpt#35
Browse files Browse the repository at this point in the history
This is Issue 119 in Google Code
  • Loading branch information
csev committed Jan 23, 2013
1 parent 2dffb94 commit 7b5246f
Show file tree
Hide file tree
Showing 5 changed files with 111 additions and 0 deletions.
9 changes: 9 additions & 0 deletions src/str.js
Original file line number Diff line number Diff line change
Expand Up @@ -383,6 +383,15 @@ Sk.builtin.str.prototype['rindex'] = new Sk.builtin.func(function(self, tgt, sta
return self.v.lastIndexOf(tgt.v,start);
});

Sk.builtin.str.prototype['startswith'] = new Sk.builtin.func(function(self, tgt) {
return 0 == self.v.indexOf(tgt.v);
});

// http://stackoverflow.com/questions/280634/endswith-in-javascript
Sk.builtin.str.prototype['endswith'] = new Sk.builtin.func(function(self, tgt) {
return self.v.indexOf(tgt.v, self.v.length - tgt.v.length) !== -1;
});

Sk.builtin.str.prototype['replace'] = new Sk.builtin.func(function(self, oldS, newS, count)
{
if (oldS.constructor !== Sk.builtin.str || newS.constructor !== Sk.builtin.str)
Expand Down
21 changes: 21 additions & 0 deletions test/run/t333.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
x = "Please make startswith and endswith work"
if x.startswith("Please") :
print "Starts with Please"
else:
print "Not good"

if x.endswith("work") :
print "Ends with work"
else:
print "Not good"

if x.startswith("please") :
print "Not good"
else:
print "Does not start with please"

if x.endswith("please") :
print "Not good"
else:
print "Does not end with please"

4 changes: 4 additions & 0 deletions test/run/t333.py.real
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
Starts with Please
Ends with work
Does not start with please
Does not end with please
18 changes: 18 additions & 0 deletions test/run/t333.py.symtab
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
Sym_type: module
Sym_name: top
Sym_lineno: 0
Sym_nested: False
Sym_haschildren: False
-- Identifiers --
name: x
is_referenced: True
is_imported: False
is_parameter: False
is_global: False
is_declared_global: False
is_local: True
is_free: False
is_assigned: True
is_namespace: False
namespaces: [
]
59 changes: 59 additions & 0 deletions test/run/t333.trans
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
Module(body=[Assign(targets=[Name(id='x',
ctx=Store())],
value=Str(s='Please make startswith and endswith work')),
If(test=Call(func=Attribute(value=Name(id='x',
ctx=Load()),
attr='startswith',
ctx=Load()),
args=[Str(s='Please')],
keywords=[],
starargs=None,
kwargs=None),
body=[Print(dest=None,
values=[Str(s='Starts with Please')],
nl=True)],
orelse=[Print(dest=None,
values=[Str(s='Not good')],
nl=True)]),
If(test=Call(func=Attribute(value=Name(id='x',
ctx=Load()),
attr='endswith',
ctx=Load()),
args=[Str(s='work')],
keywords=[],
starargs=None,
kwargs=None),
body=[Print(dest=None,
values=[Str(s='Ends with work')],
nl=True)],
orelse=[Print(dest=None,
values=[Str(s='Not good')],
nl=True)]),
If(test=Call(func=Attribute(value=Name(id='x',
ctx=Load()),
attr='startswith',
ctx=Load()),
args=[Str(s='please')],
keywords=[],
starargs=None,
kwargs=None),
body=[Print(dest=None,
values=[Str(s='Not good')],
nl=True)],
orelse=[Print(dest=None,
values=[Str(s='Does not start with please')],
nl=True)]),
If(test=Call(func=Attribute(value=Name(id='x',
ctx=Load()),
attr='endswith',
ctx=Load()),
args=[Str(s='please')],
keywords=[],
starargs=None,
kwargs=None),
body=[Print(dest=None,
values=[Str(s='Not good')],
nl=True)],
orelse=[Print(dest=None,
values=[Str(s='Does not end with please')],
nl=True)])])

0 comments on commit 7b5246f

Please sign in to comment.