001 /** 002 * Implements the Cold Fusion 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.ext.function.Function; 010 import railo.runtime.type.Array; 011 import railo.runtime.type.ArrayImpl; 012 013 public final class ArraySlice implements Function { 014 015 private static final long serialVersionUID = 7309769117464009924L; 016 017 public static Array call(PageContext pc , Array arr,double offset) throws PageException { 018 return call(pc , arr, offset,0); 019 } 020 public static Array call(PageContext pc , Array arr,double offset,double length) throws PageException { 021 022 int len=arr.size(); 023 if(offset>0) { 024 if(len<offset)throw new FunctionException(pc,"arraySlice",2,"offset","offset cannot be greater than size of the array"); 025 026 int to=0; 027 if(length>0)to=(int)(offset+length-1); 028 else if(length<0)to=(int)(len+length); 029 if(len<to) 030 throw new FunctionException(pc,"arraySlice",3,"length","offset+length cannot be greater than size of the array"); 031 032 return get(arr,(int)offset,to); 033 } 034 return call(pc ,arr,len+offset,length); 035 } 036 037 private static Array get(Array arr, int from, int to) throws PageException { 038 Array rtn=new ArrayImpl(arr.getDimension()); 039 int[] keys=arr.intKeys(); 040 for(int i=0;i<keys.length;i++) { 041 int key=keys[i]; 042 if(key<from)continue; 043 if(to>0 && key>to)break; 044 rtn.append(arr.getE(key)); 045 } 046 return rtn; 047 } 048 049 }