GapContent resetting offset to 0?
Hi,
I'm trying to parse lines in a Document using segments and the getText(int,int,segment) method. And in my parser i read the: offset, count , and array variables of the segment returned.
I've noticed that in some cases the segment offset is reset back to 0 rather than pointing at the offset into the document where my segment was taken from. And sometimes the "array" variable contains the whole document, where other times it only contains the text for the line.
I looked at the GapContent.getChars() source and I can see that it resets the offset to 0 IF the text is spanning the gap? What does that mean? and how can I not get it to reset the offset?
I was relying on the offset to be stable and the array to be stable too. But if those two keep changing I can't reliably find the correct offset when finding words in my text.
I suppose I could pass in the offset to my method and the string text instead of the Segment, but I was hoping to be as efficient as possible.
Can anyone explain why this is happening?
Here is the getChars() method from GapContent in JDK1.4.2_11
/**
* Retrieves a portion of the content. If the desired content spans
* the gap, we copy the content. If the desired content does not
* span the gap, the actual store is returned to avoid the copy since
* it is contiguous.
*
* @param where the starting position >= 0, where + len <= length()
* @param len the number of characters to retrieve >= 0
* @param chars the Segment object to return the characters in
* @exception BadLocationException if the specified position is invalid
* @see AbstractDocument.Content#getChars
*/
publicvoid getChars(int where,int len, Segment chars)throws BadLocationException{
int end = where + len;
if (where < 0 || end < 0){
thrownew BadLocationException("Invalid location", -1);
}
if (end > length() || where > length()){
thrownew BadLocationException("Invalid location", length() + 1);
}
int g0 = getGapStart();
int g1 = getGapEnd();
char[] array = (char[]) getArray();
if ((where + len) <= g0){
// below gap
chars.array = array;
chars.offset = where;
}elseif (where >= g0){
// above gap
chars.array = array;
chars.offset = g1 + where - g0;
}else{
// spans the gap
int before = g0 - where;
if (chars.isPartialReturn()){
// partial return allowed, return amount before the gap
chars.array = array;
chars.offset = where;
chars.count = before;
return;
}
// partial return not allowed, must copy
chars.array =newchar[len];
chars.offset = 0;
System.arraycopy(array, where, chars.array, 0, before);
System.arraycopy(array, g1, chars.array, before, len - before);
}
chars.count = len;
}
Thanks,
- Tim

