import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class Line
{
    private String text;

    public Line(String input)
    {
        text = normalize(input.trim());
    }
    
    @Override
    public String toString()
    {
        return text;
    }
    
    // Returns the token at the front of the Line using whitespace or ',' as delimiter.
    // The Line is not modified.
    //
    public String peekFirst()
    {
        String copy = text;
        String token = extractToken();
        text = copy;
        return token;
    }

    // Similar to peekFirst, but extracts the token from the end of the string.
    // The Line is not modified.
    //
    public String peekSecond()
    {
        String copy = text;
        String discard = extractToken();  // discard first
        String token = extractToken();
        text = copy;
        return token;
    }

    // Extracts from the Line the first token using whitespace or ',' as delimiter.
    //
    // Returns the empty string "" if no token can be found.
    //
    // The Line is modified.
    //
    public String extractToken()
    {
        int space = text.indexOf(' ');
        if (space == -1) {
            String token = text;  // empty or single token
            text = "";
            return token;
        }
	
        String token = text.substring(0, space);
        text = text.substring(space + 1, text.length());
        text = text.trim();
	
        return token;
    }

    // Similar to extractToken, but extracts the token from the end of the string.
    // The Line is modified.
    //
    public String extractTokenLast()
    {
        int space = text.lastIndexOf(' ');
        if (space == -1) {
            String token = text;  // empty or single token
            text = "";
            return token;
        }
	
        String token = text.substring(space + 1);
        text = text.substring(0, space);
        text = text.trim();

        return token;
    }

    // Modifies the Line by removing from its front and end the given symbols.
    //
    // Line line = new Line("$<<hello world>>$");
    // line.strip("$<>");
    //
    // line text is now "hello world"
    //
    public void strip(String symbols)
    {
        String escaped = Pattern.quote(symbols);
        text = text.replaceAll("^[" + escaped + "]+", "");
        text = text.replaceAll("[" + escaped + "]+$", "");
    }

    // Same as previous but for String.
    //
    // String str = "$<<hello world>>$";
    // str = Line.strip(str, "$<>");
    //
    // str is now "hello world"
    //
    public static String strip(String str, String symbols)
    {
        Line line = new Line(str);
        line.strip(symbols);
        return line.toString();
    }

    // private methods below
    
    private static String normalize(String input)
    {
        String str = input;
        str = replaceAll(str, ";.*", " ");	   // remove comments
        str = replaceAll(str, ",", " ");	   // replace commas with spaces
        str = replaceAll(str, "^\\s+|\\s+$", "");  // trim and pack whitespace
        str = replaceAll(str, "\\s+", " ");

        return str;
    }
    
    private static String replaceAll(String str, String regex, String replacement)
    {
        Pattern p = Pattern.compile(regex);
        Matcher m = p.matcher(str);
        String result = m.replaceAll(replacement);

        return result;
    }
}
