class StringUtils {

  public static String normalizeId(String name) {
    char[] buffer = name.toCharArray();

    int outix = 0;
    boolean whitespacerun = false;
    boolean firstchar = false;
    for (int inix = 0; inix < buffer.length; inix++) {
      char ch = buffer[inix];
      
      // discard high characters
      if (ch > 0x00FF)
        continue;

      // handle whitespace
      if (ch == ' ' || ch == '\t' || ch == '\r' || ch == '\n') {
        if (!firstchar || whitespacerun)
          continue;
        ch = '-';
        whitespacerun = true;
      } else
        whitespacerun = false;
      
      // check mapping table
      char mapsto = charmap[ch];
      if (mapsto == 0 && firstchar)
        continue;        

      // update buffer
      if (mapsto == 0)
        // discards before first NAME char turn into underscores
        buffer[outix++] = '_';
      else {
        buffer[outix++] = mapsto;
        firstchar = true;
      }
    }

    // whitespace at end will leave a trailing '-', which needs to go
    if (whitespacerun)
      outix--; // leave out last '-'

    // check if we have a valid name start character first
    if (outix < 1)
      return null;
    else if (isNameStart(buffer[0]))
      return new String(buffer, 0, outix);
    else
      return "_" + new String(buffer, 0, outix);
  }

}