Skip to content

Commit

Permalink
Merge pull request beeware#390 from avikantz/master
Browse files Browse the repository at this point in the history
Added implementation for string tests isdigit, isspace, and istitle. (Ticket beeware#36)
  • Loading branch information
eliasdorneles authored Mar 6, 2017
2 parents 01fe10d + b83fb27 commit 61bc3a9
Show file tree
Hide file tree
Showing 2 changed files with 74 additions and 23 deletions.
79 changes: 56 additions & 23 deletions python/common/org/python/types/Str.java
Original file line number Diff line number Diff line change
Expand Up @@ -748,7 +748,14 @@ public org.python.Object isdecimal() {
__doc__ = ""
)
public org.python.Object isdigit() {
throw new org.python.exceptions.NotImplementedError("isdigit() has not been implemented.");
if (!this.value.isEmpty()) {
for (char ch : this.value.toCharArray()) {
if (!(Character.isDigit(ch)))
return new org.python.types.Bool(false);
}
return new org.python.types.Bool(true);
}
return new org.python.types.Bool(false);
}

@org.python.Method(
Expand Down Expand Up @@ -786,14 +793,40 @@ public org.python.Object isprintable() {
__doc__ = ""
)
public org.python.Object isspace() {
throw new org.python.exceptions.NotImplementedError("isspace() has not been implemented.");
if (!this.value.isEmpty()) {
for (char ch : this.value.toCharArray()) {
if (" \t\n\r".indexOf(ch) == -1)
return new org.python.types.Bool(false);
}
return new org.python.types.Bool(true);
}
return new org.python.types.Bool(false);
}

@org.python.Method(
__doc__ = ""
)
public org.python.Object istitle() {
throw new org.python.exceptions.NotImplementedError("istitle() has not been implemented.");
if (!this.value.isEmpty()) {
Character first = Character.toUpperCase(this.value.charAt(0));
java.lang.String title = "";
title += Character.toString(first);
int c = 1;
char prev;
while (c < this.value.length()) {
prev = title.charAt(c - 1);
if (prev == ' ') {
if (!(Character.isUpperCase(this.value.charAt(c)))) {
return new org.python.types.Bool(false);
}

}
title += Character.toString(this.value.charAt(c));
c++;
}
return new org.python.types.Bool(true);
}
return new org.python.types.Bool(false);
}

@org.python.Method(
Expand Down Expand Up @@ -989,26 +1022,26 @@ public org.python.Object swapcase() {
__doc__ = "S.title() -> str\n\nReturn a titlecased version of S, i.e. words start with title case\ncharacters, all remaining cased characters have lower case."
)
public org.python.Object title() {
if (this.value.isEmpty()){
return new Str(this.value);
}
String title = "";
Character first = Character.toUpperCase(this.value.charAt(0));
title += Character.toString(first);
int c = 1;
char prev;
while(c < this.value.length()){
prev = title.charAt(c - 1);
if(prev == ' '){
title += Character.toString(Character.toUpperCase(this.value.charAt(c)));
} else if (Character.isUpperCase(this.value.charAt(c))){
title += Character.toString(Character.toLowerCase(this.value.charAt(c)));
} else {
title += Character.toString(this.value.charAt(c));
}
c ++;
}
return new Str(title);
if (this.value.isEmpty()){
return new Str(this.value);
}
String title = "";
Character first = Character.toUpperCase(this.value.charAt(0));
title += Character.toString(first);
int c = 1;
char prev;
while(c < this.value.length()){
prev = title.charAt(c - 1);
if(prev == ' '){
title += Character.toString(Character.toUpperCase(this.value.charAt(c)));
} else if (Character.isUpperCase(this.value.charAt(c))){
title += Character.toString(Character.toLowerCase(this.value.charAt(c)));
} else {
title += Character.toString(this.value.charAt(c));
}
c ++;
}
return new Str(title);
}

@org.python.Method(
Expand Down
18 changes: 18 additions & 0 deletions tests/datatypes/test_str.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,24 @@ def test_iscase(self):
print(s.isupper())
""")

def test_isdigit(self):
self.assertCodeExecution("""
for s in ['112358132134', '3.14159', '12312344df', '']:
print(s.isdigit())
""")

def test_isspace(self):
self.assertCodeExecution("""
for s in [''' \t \r''', ' ', '\t\tnope\t\t', '']:
print(s.isspace())
""")

def test_istitle(self):
self.assertCodeExecution("""
for s in ['Hello World', 'hello wORLd.', 'Hello world.', '']:
print(s.istitle())
""")

def test_join(self):
self.assertCodeExecution("""
print(','.join(None))
Expand Down

0 comments on commit 61bc3a9

Please sign in to comment.