Menu

#5 user can enter bad names

open
dan
None
5
2005-08-09
2005-08-08
No

Make sure that users can't enter Variable and function names, types,
and Template names that aren't allowed. (spaces, strange
characters, reserved, etc...)

Discussion

  • Reuben Grinberg

    Reuben Grinberg - 2005-08-09
    • assigned_to: nobody --> dmalconian
     
  • Reuben Grinberg

    Reuben Grinberg - 2005-08-09

    Logged In: YES
    user_id=174152

    User shouldn't be able to enter a Template name that already exists.

     
  • Reuben Grinberg

    Reuben Grinberg - 2005-08-11

    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)

     

Log in to post a comment.

Monday.com Logo