001    /**
002     * Implements the CFML Function mid
003     */
004    package railo.runtime.functions.string;
005    
006    import railo.runtime.PageContext;
007    import railo.runtime.exp.ExpressionException;
008    import railo.runtime.ext.function.Function;
009    
010    public final class Mid implements Function {
011            public static String call(PageContext pc , String str, double start) throws ExpressionException {
012                    return call(pc,str,start,-1);
013            }
014            public static String call(PageContext pc , String str, double start, double count) throws ExpressionException {
015                    int s=(int) (start-1);
016                    int c=(int) count;
017                    
018                    if(s<0) throw new ExpressionException("Parameter 2 of function mid which is now ["+(s+1)+"] must be a positive integer");
019                    if(c==-1) c=str.length();
020                    else if(c<-1) throw new ExpressionException("Parameter 3 of function mid which is now ["+c+"] must be a non-negative integer or -1 (for string length)");
021                    c+=s;
022                    if(s>str.length()) return "";
023                    else if(c>=str.length())return str.substring(s);
024                    else {
025                            return str.substring(s,c);
026                    }
027            }
028    }