Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

StringIndexOutOfBoundsException in breakText() #3

Open
GoogleCodeExporter opened this issue Jun 12, 2015 · 5 comments
Open

StringIndexOutOfBoundsException in breakText() #3

GoogleCodeExporter opened this issue Jun 12, 2015 · 5 comments

Comments

@GoogleCodeExporter
Copy link

This code is bug prone (line 514, method 

while( input.charAt(pos) != ' ' ) {
    pos--;
}

Under some cases, if there's no space at all, you can easily go below the 
range. The while() should be rather:

while( (pos >= 0) && (input.charAt(pos) != ' ' ) )

Original issue reported on code.google.com by [email protected] on 27 Feb 2012 at 11:17

@GoogleCodeExporter
Copy link
Author

The error still occurs even with the above proposed fix. How about this?

if (input.substring(0, pos).matches("\\s") {
  while ((pos >= 0) && (input.charAt(pos) != ' ')) {
    pos--;
  }
}

Not sure about the performance implications though..

Original comment by [email protected] on 12 Sep 2012 at 8:33

@GoogleCodeExporter
Copy link
Author

I have tried all solutions provided above but this problem is still not solved, 
this is buggy code.

Original comment by mohdtausif25 on 13 Sep 2012 at 7:22

@GoogleCodeExporter
Copy link
Author

[deleted comment]

@GoogleCodeExporter
Copy link
Author

why do we have to find the blank space using the while loop?
cant we use lastindex of instead?


i tried to fix the error using the code below. but then the string started to 
overlap and then i modified a bit more and now i get a blank string
else {
   // Backup until we are at a space.
   pos = input.lastIndexOf(" ");
   // This line includes up to the space.
   if(-1 != pos){
             if(maxWidth > pos){
             mLines.add(new int[] { posStartThisLine, pos });
         }else{
                 mLines.add(new int[] { posStartThisLine, maxWidth });
         pos = maxWidth;
          }                              
     }                       
    }

i cant work through it as i cant understand the logic used here, can anyone 
help me with the same

Original comment by [email protected] on 21 Feb 2013 at 7:46

@GoogleCodeExporter
Copy link
Author

i think i figured out a work around, it works for me, but more testing is 
required.

// Start breaking.
                int posStartThisLine = -1;
                float lengthThisLine = 0.0f;
                boolean breakWords = true;
                int pos = 0;
                int backup = 0;
                while (pos < input.length()) {


                    if (posStartThisLine == -1) {
                        posStartThisLine = pos;
                    }

                    if (mLines.size() == maxLines) {
                        mRequiredEllipsis = true;
                        break;
                    }

                    float widthOfChar = tp.measureText(input.charAt(pos) + "");
                    boolean newLineRequired = false;

                    // Check for a new line character or if we've run over max width.
                    if (input.charAt(pos) == '\n') {
                        newLineRequired = true;

                        // We want the current line to go up to the character right before the
                        // new line char, and we want the next line to start at the char after
                        // this new line char.
                        mLines.add(new int[] { posStartThisLine, pos-1 });
                    }
                    else if (lengthThisLine + widthOfChar >= maxWidth) {
                        newLineRequired = true;
                        // We need to backup if we are in the middle of a word.
                        if (input.charAt(pos) == ' ' || breakWords == false) {
                            // Backup one character, because it doesn't fit on this line.
                            pos--;

                            // So this line includes up to the character before the space.
                            mLines.add(new int[] { posStartThisLine, pos });
                        }
                        else {
                            // Backup until we are at a space.
                            backup = pos;
                            while( input.charAt(pos) != ' ' ) {
                                pos--;
                                if(pos == -1){
                                    break;
                                }
                            }
//                         while( (pos >= 0) && (input.charAt(pos) != ' ' ) ) {
//                              pos--;
//                          }
                            // This line includes up to the space.
                            if(-1 != pos){
                                mLines.add(new int[] { posStartThisLine, pos });
                                }else{
                                    mLines.add(new int[] { posStartThisLine,backup });

                            }                       
                        }
                    }

                    if (newLineRequired) {
                        // The next cycle should reset the position if it sees it's -1 (to whatever i is).
                        posStartThisLine = -1;

                        // Reset line length for next iteration.
                        lengthThisLine = 0.0f;

                        // When we get to the last line, subtract the width of the ellipsis.
                        if (mLines.size() == maxLines - 1) {
                            maxWidth -= (mLengthEllipsis + mLengthEllipsisMore);
                            // We also don't need to break on a full word, it'll look a little
                            // cleaner if all breaks on the final lines break in the middle of
                            // the last word.
                            breakWords = false;                        
                           }
                        if(pos == -1){
                            breakWords = true;
                            pos = backup;
                        }else{
                            breakWords = false;
                        }

                    }
                    else {
                        lengthThisLine += widthOfChar;

                        // If we're on the last character of the input string, add on whatever we have leftover.
                        if (pos == input.length() - 1) {
                            mLines.add(new int[] { posStartThisLine, pos });
                        }
                    }

                    pos++;
                }

Original comment by [email protected] on 21 Feb 2013 at 11:57

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant