001/**
002 *
003 * Copyright (c) 2014, the Railo Company Ltd. All rights reserved.
004 *
005 * This library is free software; you can redistribute it and/or
006 * modify it under the terms of the GNU Lesser General Public
007 * License as published by the Free Software Foundation; either 
008 * version 2.1 of the License, or (at your option) any later version.
009 * 
010 * This library is distributed in the hope that it will be useful,
011 * but WITHOUT ANY WARRANTY; without even the implied warranty of
012 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
013 * Lesser General Public License for more details.
014 * 
015 * You should have received a copy of the GNU Lesser General Public 
016 * License along with this library.  If not, see <http://www.gnu.org/licenses/>.
017 * 
018 **/
019/**
020 * Implements the CFML Function repeatstring
021 */
022package lucee.runtime.functions.string;
023
024import lucee.commons.lang.StringUtil;
025import lucee.runtime.PageContext;
026import lucee.runtime.exp.ExpressionException;
027import lucee.runtime.exp.FunctionException;
028import lucee.runtime.exp.PageException;
029import lucee.runtime.functions.BIF;
030import lucee.runtime.op.Caster;
031
032public final class RepeatString extends BIF {
033
034        private static final long serialVersionUID = 6041471441971348584L;
035
036        public static String call(PageContext pc , String str, double count) throws ExpressionException {
037        if(count<0) throw new ExpressionException("Parameter 2 of function repeatString which is now ["+Caster.toString(count)+"] must be a non-negative integer");
038        return StringUtil.repeatString(str,(int)count);
039    }
040    public static String _call(PageContext pc , String str, double count) throws ExpressionException {
041        int len=(int) count;
042        if(len<0) throw new ExpressionException("Parameter 2 of function repeatString which is now ["+len+"] must be a non-negative integer");
043        char[] chars=str.toCharArray();
044        StringBuilder cb=new StringBuilder(chars.length*len);
045        for(int i=0;i<len;i++)cb.append(chars);
046        return cb.toString();
047    }
048    public static StringBuilder call(StringBuilder sb,String str, double count) throws ExpressionException {
049        int len=(int) count;
050        if(len<0) throw new ExpressionException("Parameter 1 of function repeatString which is now ["+len+"] must be a non-negative integer");
051        
052        for(int i=0;i<len;i++)sb.append(str);
053        return sb;
054    }
055    public static StringBuilder call(StringBuilder sb,char c, double count) throws ExpressionException {
056        int len=(int) count;
057        if(len<0) throw new ExpressionException("Parameter 1 of function repeatString which is now ["+len+"] must be a non-negative integer");
058        
059        for(int i=0;i<len;i++)sb.append(c);
060        return sb;
061    }
062
063    @Override
064        public Object invoke(PageContext pc, Object[] args) throws PageException {
065        if(args.length==2)
066                        return call(pc, Caster.toString(args[0]), Caster.toDoubleValue(args[1]));
067        
068                throw new FunctionException(pc, "RepeatString", 2, 2, args.length);
069        }
070}