boolean lastWasWhitespace;
int strlen = str.length();
// Calculate how long our array needs to be.
int len = 1;
lastWasWhitespace = true;
for (int i=0; i<strlen; i++) {
char c = str.charAt(i);
if (Character.isWhitespace(c)) {
if (!lastWasWhitespace) {
len++;
lastWasWhitespace = true;
}
} else {
if (!lastWasWhitespace) {
len++;
}
lastWasWhitespace = false;
len += pattern(c).length;
}
}
// Generate the pattern array. Note that we put an extra element of 0
// in at the beginning, because the pattern always starts with the pause,
// not with the vibration.
long[] result = new long[len+1];
result[0] = 0;
int pos = 1;
lastWasWhitespace = true;
for (int i=0; i<strlen; i++) {
char c = str.charAt(i);
if (Character.isWhitespace(c)) {
if (!lastWasWhitespace) {
result[pos] = WORD_GAP;
pos++;
lastWasWhitespace = true;
}
} else {
if (!lastWasWhitespace) {
result[pos] = LETTER_GAP;
pos++;
}
lastWasWhitespace = false;
long[] letter = pattern(c);
System.arraycopy(letter, 0, result, pos, letter.length);
pos += letter.length;
}
}
return result;