TextUtils { static int fixSelectionLength( String fullText, int selectionOffset, int selectionLength ) { if( selectionLength < 0 ) selectionLength = 0; else if( selectionOffset + selectionLength > fullText.length()) selectionLength = fullText.length() - selectionOffset; for( ; selectionOffset + selectionLength < fullText.length(); selectionLength ++ ) { char c = fullText.charAt( selectionOffset + selectionLength ); if( isLineBreak( c )) break; } return selectionLength; } private  TextUtils(); static String removeComments( String line ); static boolean isLineBreak( char c ); static SelectionRange findRegionFromSelection( String fullText, int selectionOffset, int selectionLength );  }
@Test public void testFixSelectionLength() { String text = ""; Assert.assertEquals( 0, TextUtils.fixSelectionLength( text, 0, -1 )); Assert.assertEquals( 0, TextUtils.fixSelectionLength( text, 0, 0 )); Assert.assertEquals( 0, TextUtils.fixSelectionLength( text, 0, 1 )); Assert.assertEquals( 0, TextUtils.fixSelectionLength( text, 0, 100 )); text = "a"; Assert.assertEquals( 1, TextUtils.fixSelectionLength( text, 0, -1 )); Assert.assertEquals( 1, TextUtils.fixSelectionLength( text, 0, 0 )); Assert.assertEquals( 1, TextUtils.fixSelectionLength( text, 0, 1 )); Assert.assertEquals( 1, TextUtils.fixSelectionLength( text, 0, 100 )); text = "a\nb\nc"; Assert.assertEquals( 1, TextUtils.fixSelectionLength( text, 0, -1 )); Assert.assertEquals( 1, TextUtils.fixSelectionLength( text, 2, 1 )); Assert.assertEquals( 1, TextUtils.fixSelectionLength( text, 4, 1 )); Assert.assertEquals( text.length(), TextUtils.fixSelectionLength( text, 0, 100 )); }