Skip to content

Commit

Permalink
refactoring: remove duplicated code, add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Bananeweizen committed Jun 8, 2013
1 parent fa7b86c commit 88eda55
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 27 deletions.
52 changes: 25 additions & 27 deletions main/src/cgeo/geocaching/utils/CryptUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -37,28 +37,36 @@ public final class CryptUtils {
}
}

private static class Rot13Encryption {
private boolean plaintext = false;

char getNextEncryptedCharacter(final char c) {
int result = c;
if (result == '[') {
plaintext = true;
} else if (result == ']') {
plaintext = false;
} else if (!plaintext) {
int capitalized = result & 32;
result &= ~capitalized;
result = ((result >= 'A') && (result <= 'Z') ? ((result - 'A' + 13) % 26 + 'A') : result)
| capitalized;
}
return (char) result;
}
}

public static String rot13(String text) {
if (text == null) {
return "";
}
final StringBuilder result = new StringBuilder();
// plaintext flag (do not convert)
boolean plaintext = false;
Rot13Encryption rot13 = new Rot13Encryption();

final int length = text.length();
for (int index = 0; index < length; index++) {
int c = text.charAt(index);
if (c == '[') {
plaintext = true;
} else if (c == ']') {
plaintext = false;
} else if (!plaintext) {
int capitalized = c & 32;
c &= ~capitalized;
c = ((c >= 'A') && (c <= 'Z') ? ((c - 'A' + 13) % 26 + 'A') : c)
| capitalized;
}
result.append((char) c);
char c = text.charAt(index);
result.append(rot13.getNextEncryptedCharacter(c));
}
return result.toString();
}
Expand Down Expand Up @@ -111,22 +119,12 @@ public static CharSequence rot13(final Spannable span) {
// a SpannableStringBuilder instead of the pure text and we must replace each character inline.
// Otherwise we loose all the images, colors and so on...
final SpannableStringBuilder buffer = new SpannableStringBuilder(span);
boolean plaintext = false;
Rot13Encryption rot13 = new Rot13Encryption();

final int length = span.length();
for (int index = 0; index < length; index++) {
int c = span.charAt(index);
if (c == '[') {
plaintext = true;
} else if (c == ']') {
plaintext = false;
} else if (!plaintext) {
int capitalized = c & 32;
c &= ~capitalized;
c = ((c >= 'A') && (c <= 'Z') ? ((c - 'A' + 13) % 26 + 'A') : c)
| capitalized;
}
buffer.replace(index, index + 1, String.valueOf((char) c));
char c = span.charAt(index);
buffer.replace(index, index + 1, String.valueOf(rot13.getNextEncryptedCharacter(c)));
}
return buffer;
}
Expand Down
2 changes: 2 additions & 0 deletions tests/src/cgeo/geocaching/utils/CryptUtilsTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ public static void testROT13() {
assertEquals("", CryptUtils.rot13(""));
assertEquals("", CryptUtils.rot13((String) null));
assertEquals("Pnpur uvag", CryptUtils.rot13("Cache hint"));
assertEquals("Pnpur [plain] uvag", CryptUtils.rot13("Cache [plain] hint"));
assertEquals("[all plain]", CryptUtils.rot13("[all plain]"));
assertEquals("123", CryptUtils.rot13("123"));
}

Expand Down

0 comments on commit 88eda55

Please sign in to comment.