Updated Atbash to run faster

This commit is contained in:
2022-01-25 23:23:49 -05:00
parent 05024f931f
commit c65947a630
2 changed files with 255 additions and 239 deletions

View File

@@ -5,6 +5,9 @@
package com.mattrixwv.CipherStreamJava.monoSubstitution;
import com.mattrixwv.CipherStreamJava.exceptions.InvalidInputException;
public class Atbash{
private String inputString; //Holds the string that needs encoded or decoded
private String outputString; //The encoded/decoded string
@@ -20,12 +23,13 @@ public class Atbash{
//Decode if the letter is alphabetic
if(Character.isAlphabetic(currentChar)){
//Use either uppercase or lowercase for the base
char letterBase = 'a';
//(letterbase + 25 - (currentChar - letterBase))
if(Character.isUpperCase(currentChar)){
letterBase = 'A';
output.append((char)(155 - currentChar));
}
else{
output.append((char)(219 - currentChar));
}
//TODO: Test and see if there is a more efficient way to do this
output.append((char)(currentChar + 25 - (2 * (currentChar - letterBase))));
}
//Keep any punctuation/whitespace the way it is
else{
@@ -45,12 +49,13 @@ public class Atbash{
//Decode if the letter is alphabetic
if(Character.isAlphabetic(currentChar)){
//Use either uppercase or lowercase for the base
char letterBase = 'a';
//(letterbase + 25 - (currentChar - letterBase))
if(Character.isUpperCase(currentChar)){
letterBase = 'A';
output.append((char)(155 - currentChar));
}
else{
output.append((char)(219 - currentChar));
}
//TODO: Test and see if there is a more efficient way to do this
output.append((char)(currentChar + 25 - (2 * (currentChar - letterBase))));
}
//Keep any punctuatio/whitespace the way it is
else{
@@ -62,21 +67,30 @@ public class Atbash{
return outputString;
}
//Removes all invalid characters and sets inputString
private void setInputString(String input){
private void setInputString(String inputString) throws InvalidInputException{
if(inputString == null){
throw new NullPointerException("Input cannot be null");
}
if(!preserveCapitals){
//Convert all letters to lowercase
input = input.toLowerCase();
inputString = inputString.toLowerCase();
}
if(!preserveWhitespace){
//Remove all characters except capital letters
input = input.replaceAll("\\s+", "");
inputString = inputString.replaceAll("\\s+", "");
}
if(!preserveSymbols){
//Remove all non-alpha numeric and whitespace symbols
input = input.replaceAll("[^a-zA-Z0-9\\s]", "");
inputString = inputString.replaceAll("[^a-zA-Z\\s]", "");
}
//Save the string
inputString = input;
this.inputString = inputString;
if(this.inputString.isBlank()){
throw new InvalidInputException("Input must contain at least 1 character");
}
}
@@ -98,16 +112,16 @@ public class Atbash{
public String getOutputString(){
return outputString;
}
public String encode(String input){
public String encode(String inputString) throws InvalidInputException{
//Make sure everything is empty before you begin
reset();
setInputString(input);
setInputString(inputString);
return encode();
}
public String decode(String input){
public String decode(String inputString) throws InvalidInputException{
//Make sure everything is empty before you begin
reset();
setInputString(input);
setInputString(inputString);
return decode();
}
public void reset(){