Buggy code:
public class UITextField extends UIComponent < UITextField > implements IScrollable { protected StringBuilder text = new StringBuilder ( ) ; protected List < String > lines = new LinkedList < > ( ) ; protected boolean multiLine = false ; protected UITextField . CursorPosition cursorPosition ; protected UITextField . CursorPosition selectionPosition ; protected boolean selectingText = false ; protected int charOffset = 0 ; protected int lineOffset = 0 ; protected int lineSpacing = 1 ; protected long startTimer ; private boolean selectAllOnRelease = false ; protected boolean autoSelectOnFocus = false ; protected int textColor = 16777215 ; protected UISlimScrollbar scrollBar ; protected float fontScale = 1 ; protected GuiShape cursorShape ; protected GuiShape selectShape ; protected GuiIcon iconTextfield ; protected GuiIcon iconTextfieldDisabled ; public UITextField ( MalisisGui gui , String text , boolean multiLine ) { } public UITextField ( MalisisGui gui , String text ) { } public UITextField ( MalisisGui gui , boolean multiLine ) { } protected void createShape ( MalisisGui gui ) { } public String getText ( ) { } public void setText ( String text ) { } @ Override public UITextField setSize ( int width , int height ) { } @ Override public void setFocused ( boolean focused ) { } public UITextField setFontScale ( float fontScale ) { } public float getFontScale ( ) { } public int getLineSpacing ( ) { } public void setLineOffset ( int line ) { } public UITextField setLineSpacing ( int lineSpacing ) { } public int getLineHeight ( ) { } public UITextField . CursorPosition getCursorPosition ( ) { } public UITextField . CursorPosition getSelectionPosition ( ) { } public void setCursorPosition ( int x , int y ) { } public UITextField setTextColor ( int color ) { } public int getTextColor ( ) { } public UITextField setAutoSelectOnFocus ( boolean auto ) { } public UISlimScrollbar getScrollbar ( ) { } protected void buildLines ( ) { } public void addText ( String text ) { } protected boolean validateText ( String text ) { } public String getSelectedText ( ) { } public void deleteSelectedText ( ) { } public void deleteFromCursor ( int amount ) { } public void deleteWord ( boolean backwards ) { } @ Override public int getContentWidth ( ) { } @ Override public int getContentHeight ( ) { } @ Override public float getOffsetX ( ) { } @ Override public void setOffsetX ( float offsetX , int delta ) { } @ Override public float getOffsetY ( ) { } @ Override public void setOffsetY ( float offsetY , int delta ) { } @ Override public float getScrollStep ( ) { } @ Override public int getVerticalPadding ( ) { } @ Override public int getHorizontalPadding ( ) { } protected int visibleLines ( ) { } public void onCursorUpdated ( ) { if ( ! ( multiLine ) ) { if ( ( cursorPosition . character ) < ( charOffset ) ) charOffset = cursorPosition . character ; else if ( ( text . length ( ) ) != 0 ) { <START_BUG> while ( ( GuiRenderer . getStringWidth ( text . substring ( charOffset , cursorPosition . textPosition ) , fontScale ) ) >= ( ( width ) - 4 ) ) <END_BUG> ( charOffset ) ++ ; } } else { if ( ( cursorPosition . line ) < ( lineOffset ) ) setLineOffset ( cursorPosition . line ) ; else if ( ( cursorPosition . line ) > ( ( ( lineOffset ) + ( visibleLines ( ) ) ) - 1 ) ) setLineOffset ( ( ( ( cursorPosition . line ) - ( visibleLines ( ) ) ) + 1 ) ) ; } startTimer = System . currentTimeMillis ( ) ; } @ Override public void drawBackground ( GuiRenderer renderer , int mouseX , int mouseY , float partialTick ) { } @ Override public void drawForeground ( GuiRenderer renderer , int mouseX , int mouseY , float partialTick ) { } public void drawText ( GuiRenderer renderer ) { } public void drawCursor ( GuiRenderer renderer ) { } public void drawSelectionBox ( GuiRenderer renderer ) { } @ Subscribe public void onMousePress ( MouseEvent . Press event ) { } @ Subscribe public void onMouseRelease ( MouseEvent . Release event ) { } @ Subscribe public void onDrag ( MouseEvent . Drag event ) { } @ Subscribe public void onDoubleClick ( MouseEvent . DoubleClick event ) { } protected void startSelecting ( ) { } @ Subscribe public void keyTyped ( KeyboardEvent event ) { } protected boolean handleCtrlKeyDown ( int keyCode ) { } @ Override public String getPropertyString ( ) { } protected class CursorPosition { protected int textPosition ; protected int character ; protected int line ; public void set ( UITextField . CursorPosition position ) { } public void jumpTo ( int pos ) { } public void jumpBy ( int amount ) { } public void jumpToBeginning ( ) { } public void jumpToEnd ( ) { } public void jumpToLineStart ( ) { } public void jumpToLineEnd ( ) { } public void shiftLeft ( ) { } public void shiftRight ( ) { } public void jumpToNextSpace ( boolean backwards ) { } public void jumpLine ( boolean backwards ) { } private String currentLineText ( ) { } public void setPosition ( int x , int y ) { } private void updateTextPosition ( ) { } private int lineFromY ( int y ) { } private int characterFromX ( int x ) { } public int getXOffset ( ) { } public int getYOffset ( ) { } } }

Fix:
while ( ( GuiRenderer . getStringWidth ( text . substring ( charOffset , cursorPosition . textPosition ) , fontScale ) ) >= ( ( getWidth ( ) ) - 4 ) )