Skip to content

Commit

Permalink
Refactored codes
Browse files Browse the repository at this point in the history
- Removed redundant else statement
- Changed while loop entry/terminate conditions
  • Loading branch information
BPYap committed Mar 3, 2018
1 parent fa380c6 commit d19aff5
Showing 1 changed file with 11 additions and 16 deletions.
27 changes: 11 additions & 16 deletions python/common/org/python/types/Str.java
Original file line number Diff line number Diff line change
Expand Up @@ -285,7 +285,7 @@ public org.python.Object __getitem__(org.python.Object index) {
if (slice.start == null && slice.stop == null && slice.step == null) {
sliced = this.value;
} else {
long start;
long start = 0;
if (slice.start != null) {
if (slice.start.value < 0) {
// computes starting index from end of string
Expand All @@ -294,11 +294,9 @@ public org.python.Object __getitem__(org.python.Object index) {
} else {
start = slice.start.value;
}
} else {
start = 0;
}

long stop;
long stop = this.value.length();
if (slice.stop != null) {
if (slice.stop.value < 0) {
// computes ending index from end of string
Expand All @@ -308,31 +306,27 @@ public org.python.Object __getitem__(org.python.Object index) {
// if stop goes beyond string length, sets it to the length
stop = Math.min(this.value.length(), slice.stop.value);
}
} else {
stop = this.value.length();
}

long step;
long step = 1;
if (slice.step != null) {
step = slice.step.value;
} else {
step = 1;
}

if (step == 1) {
sliced = "";
if (start < stop) {
sliced = this.value.substring((int) start, (int) stop);
} else {
sliced = "";
}
} else {
java.lang.StringBuffer buffer = new java.lang.StringBuffer();
long i = start;
while (true) {
if ((i >= stop && step > 0) || (i <= stop && step < 0)
|| i >= this.value.length() || i < 0) {
break;
}
while (
!((i >= stop) && (step > 0))
&& !((i <= stop) && (step < 0))
&& !(i >= this.value.length())
&& !(i < 0)
) {
buffer.append(this.value.charAt((int) i));
i += step;
}
Expand Down Expand Up @@ -2584,3 +2578,4 @@ private static String getPaddingOfLength(char filling, long length) {
public static final char SIGN_NEGATIV = '+';
public static final char SIGN_UNDEFINED = '\0';
}

0 comments on commit d19aff5

Please sign in to comment.