package org.apache.vxquery.runtime.functions.strings;


public class SubstringCharacterIterator implements ICharacterIterator {
    private final ICharacterIterator in;
    private int start;
    private int end;
    private int charOffset;
    
    public SubstringCharacterIterator(ICharacterIterator in) {
        this.in = in;
    }

    public void setBounds(int start, int end) {
        this.start = start;
        this.end = end;
    }

    @Override
    public char next() {
        char c = in.next();
        if (charOffset <= start) {
            ++charOffset;
            return next();
        }
        else if (charOffset <= end) {
            ++charOffset;
            return c;
        }
        else {
            return (char) 0;
        }
    }

    @Override
    public void reset() {
        charOffset = 0;
    }

}

@@ -48,17 +48,16 @@
 rings;%0A%0A
-%0A
 public c
@@ -200,19 +200,22 @@
 ate int 
+l
 en
-d
+gth
 ;%0A    pr
@@ -236,20 +236,16 @@
 Offset;%0A
-    
 %0A    pub
@@ -370,19 +370,22 @@
 rt, int 
+l
 en
-d
+gth
 ) %7B%0A    
@@ -425,17 +425,23 @@
 his.
+l
 en
-d
+gth
  = 
+l
 en
-d
+gth
 ;%0A  
@@ -520,16 +520,152 @@
         
+// Only drill down if there is more to the string.%0A        if (c == ICharacterIterator.EOS_CHAR) %7B%0A            return c;%0A        %7D else 
 if (char
@@ -672,17 +672,16 @@
 Offset %3C
-=
  start) 
@@ -740,32 +740,24 @@
 );%0A        %7D
-%0A       
  else if (ch
@@ -770,13 +770,23 @@
 et %3C
-= end
+ start + length
 ) %7B%0A
@@ -847,27 +847,8 @@
   %7D%0A
-        else %7B%0A    
     
@@ -868,26 +868,16 @@
 har) 0;%0A
-        %7D%0A
     %7D%0A%0A 
@@ -911,24 +911,44 @@
 d reset() %7B%0A
+        in.reset();%0A
         char

Updated the substring character iterator to improve performance and correct the parameters passed into the function.