/** This helper method will return the end of the next word in the buffer. */ private static int getNextWordEnd(Segment text, int startPos) { for (char ch = text.setIndex(startPos); ch != Segment.DONE; ch = text.next()) { if (!Character.isLetterOrDigit(ch)) { return text.getIndex(); } } return text.getEndIndex(); }
/** * Removes any spaces or tabs from the end of the segment. * * @param segment The segment from which to remove tailing whitespace. * @return <code>segment</code> with trailing whitespace removed. */ private static Segment removeEndingWhitespace(Segment segment) { int toTrim = 0; char currentChar = segment.setIndex(segment.getEndIndex() - 1); while ((currentChar == ' ' || currentChar == '\t') && currentChar != CharacterIterator.DONE) { toTrim++; currentChar = segment.previous(); } String stringVal = segment.toString(); String newStringVal = stringVal.substring(0, stringVal.length() - toTrim); return new Segment(newStringVal.toCharArray(), 0, newStringVal.length()); }