001 /** 002 * Implements the CFML Function arraymin 003 */ 004 package railo.runtime.functions.arrays; 005 006 import railo.runtime.PageContext; 007 import railo.runtime.exp.FunctionException; 008 import railo.runtime.exp.PageException; 009 import railo.runtime.functions.BIF; 010 import railo.runtime.op.Caster; 011 import railo.runtime.type.Array; 012 import railo.runtime.type.ArrayImpl; 013 014 public final class ArraySlice extends BIF { 015 016 private static final long serialVersionUID = 7309769117464009924L; 017 018 public static Array call(PageContext pc , Array arr,double offset) throws PageException { 019 return call(pc , arr, offset,0); 020 } 021 public static Array call(PageContext pc , Array arr,double offset,double length) throws PageException { 022 023 int len=arr.size(); 024 if(offset>0) { 025 if(len<offset)throw new FunctionException(pc,"arraySlice",2,"offset","Offset cannot be greater than size of the array"); 026 027 int to=0; 028 if(length>0)to=(int)(offset+length-1); 029 else if(length<0)to=(int)(len+length); 030 if(len<to) 031 throw new FunctionException(pc,"arraySlice",3,"length","Offset+length cannot be greater than size of the array"); 032 033 return get(arr,(int)offset,to); 034 } 035 return call(pc ,arr,len+offset,length); 036 } 037 038 @Override 039 public Object invoke(PageContext pc, Object[] args) throws PageException { 040 if(args.length==2)return call(pc,Caster.toArray(args[0]),Caster.toDoubleValue(args[1])); 041 return call(pc,Caster.toArray(args[0]),Caster.toDoubleValue(args[1]),Caster.toDoubleValue(args[2])); 042 } 043 044 private static Array get(Array arr, int from, int to) throws PageException { 045 Array rtn=new ArrayImpl(arr.getDimension()); 046 int[] keys=arr.intKeys(); 047 for(int i=0;i<keys.length;i++) { 048 int key=keys[i]; 049 if(key<from)continue; 050 if(to>0 && key>to)break; 051 rtn.append(arr.getE(key)); 052 } 053 return rtn; 054 } 055 056 }