Make sure that users can't enter Variable and function names, types,
and Template names that aren't allowed. (spaces, strange
characters, reserved, etc...)
I can still enter types, variables, and function names with spaces and
strange characters. In addition, I can make a new Type with a type
name that already exists, which is a bad.
Tthe way ReservedTest currently works is somewhat slow. It loops
through all of the names and checks to see if they are equal. That is
about 50 String comparisons each time. This probably isn't noticeable,
but a better way to implement this is to use a Set and to see if the name
is in the set:
import java.util.*;
Set reserved = new HashSet();
//The list of reserved key words
public static final String[] reservedNamesList = {
"if", "catch", "do", "while", "import" , ...};
//Put all of the reserved words into the reserved set
static {
for (int i = 0; i < reservedNamesList.length; i++)
reserved.add(reservedNamesList[i]);
}
public static boolean isReserved(String s) {
return reserved.contains(s);
}
/////////////
HashSets (like Hashtables) are very efficient at finding if they contain an
element. It costs O( cost_of_calculating_hashCode() + 1) time to see if
they contain an entry, which is essentially O(number of chars in string)
~ O(1).
One more note, instead of writing
a.compareTo(b) == 0
to check equality for Strings,
you can write
a.equals(b)
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
Logged In: YES
user_id=174152
User shouldn't be able to enter a Template name that already exists.
Logged In: YES
user_id=174152
I can still enter types, variables, and function names with spaces and
strange characters. In addition, I can make a new Type with a type
name that already exists, which is a bad.
Tthe way ReservedTest currently works is somewhat slow. It loops
through all of the names and checks to see if they are equal. That is
about 50 String comparisons each time. This probably isn't noticeable,
but a better way to implement this is to use a Set and to see if the name
is in the set:
import java.util.*;
Set reserved = new HashSet();
//The list of reserved key words
public static final String[] reservedNamesList = {
"if", "catch", "do", "while", "import" , ...};
//Put all of the reserved words into the reserved set
static {
for (int i = 0; i < reservedNamesList.length; i++)
reserved.add(reservedNamesList[i]);
}
public static boolean isReserved(String s) {
return reserved.contains(s);
}
/////////////
HashSets (like Hashtables) are very efficient at finding if they contain an
element. It costs O( cost_of_calculating_hashCode() + 1) time to see if
they contain an entry, which is essentially O(number of chars in string)
~ O(1).
One more note, instead of writing
a.compareTo(b) == 0
to check equality for Strings,
you can write
a.equals(b)