Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.
Comment: fixe dHornig erf

...

This noncompliant code example attempts to trim leading letters from the string. It fails to accomplish this task because Character.isLetter() lacks support for supplementary and combining characters [Hornig 2007]characters. Because it only examines one character at a time, this method also can corrupt combining characters.

Code Block
bgColor#FFcccc
// Fails for supplementary or combining characters
public static String trim(String string) {
  char ch;
  int i;
  for (i = 0; i < string.length(); i += 1) {
    ch = string.charAt(i);
    if (!Character.isLetter(ch)) {
      break;
    }
  }
  return string.substring(i);
}

...

This noncompliant code example attempts to correct corrects the problem with supplementary characters by using the String.codePointAt() method, which accepts an int argument. This works for supplementary characters but fails for combining characters [Hornig 2007]However, it still fails to handle combining characters because it only examines one character at a time.

Code Block
bgColor#FFcccc
// Fails for combining characters
public static String trim(String string) {
  int ch;
  int i;
  for (i = 0; i < string.length(); i += Character.charCount(ch)) {
    ch = string.codePointAt(i);
    if (!Character.isLetter(ch)) {
      break;
    }
  } 
  return string.substring(i);
}

...

This compliant solution works both for supplementary and for combining characters [Hornig 2007Tutorials 2008]. According to the Java API [API 2006] class java.text.BreakIterator documentation:

...

[API 2006]

Classes Character and BreakIterator

 [Hornig 2007Tutorials 2008]

Problem Areas: CharactersCharacter Boundaries