public final int insString(int off, int hfb, String str) {
      if (off < 0 || off > len)
         throw new ArrayIndexOutOfBoundsException();

      // This way of counting length matches behaviour of loop below
      // (ignores invalid high surrogate, passes through invalid low
      // surrogate)
      char[] chars = str.toCharArray();
      int cnt = 0;
      for (char ch : chars) {
         if (!Character.isHighSurrogate(ch))
            cnt++;
      }

      mkgap(off, cnt);

      int attr = hfb << Console.ATTRSET_SHIFT;
      char hi = 0;
      for (char ch : chars) {
         if (Character.isHighSurrogate(ch)) {
            hi = ch;
            continue;
         }
         int cp = hi != 0 && Character.isSurrogatePair(hi, ch) ?
            Character.toCodePoint(hi, ch) : ch;
         hi = 0;
         arr[off++] = attr | cp;
      } 
      
      return cnt;
   }