aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorBananeweizen <bananeweizen@gmx.de>2013-06-08 15:54:11 +0200
committerBananeweizen <bananeweizen@gmx.de>2013-06-08 15:54:11 +0200
commit88eda55ecfd95e562f7af410fa374f8d22eb6c9e (patch)
tree99967e142662d1a981cf7100ac5f2f699ae399d5
parentfa7b86c367f2fbd003cf324189e3737ec6b23575 (diff)
downloadcgeo-88eda55ecfd95e562f7af410fa374f8d22eb6c9e.zip
cgeo-88eda55ecfd95e562f7af410fa374f8d22eb6c9e.tar.gz
cgeo-88eda55ecfd95e562f7af410fa374f8d22eb6c9e.tar.bz2
refactoring: remove duplicated code, add tests
-rw-r--r--main/src/cgeo/geocaching/utils/CryptUtils.java52
-rw-r--r--tests/src/cgeo/geocaching/utils/CryptUtilsTest.java2
2 files changed, 27 insertions, 27 deletions
diff --git a/main/src/cgeo/geocaching/utils/CryptUtils.java b/main/src/cgeo/geocaching/utils/CryptUtils.java
index 5ddae96..aecf717 100644
--- a/main/src/cgeo/geocaching/utils/CryptUtils.java
+++ b/main/src/cgeo/geocaching/utils/CryptUtils.java
@@ -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();
}
@@ -111,22 +119,12 @@ public final class CryptUtils {
// 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;
}
diff --git a/tests/src/cgeo/geocaching/utils/CryptUtilsTest.java b/tests/src/cgeo/geocaching/utils/CryptUtilsTest.java
index fff24f2..e727747 100644
--- a/tests/src/cgeo/geocaching/utils/CryptUtilsTest.java
+++ b/tests/src/cgeo/geocaching/utils/CryptUtilsTest.java
@@ -9,6 +9,8 @@ public class CryptUtilsTest extends TestCase {
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"));
}