// Here, "root" means the position of "'":
// 0'3, 0'90, and +81'-90
// (dash will be deleted soon, so it is actually +81'90).
int rootIndex = 1;
int length = text.length();
if (length > 3
&& text.subSequence(0, 3).toString().equals("+81")) {
rootIndex = 3;
} else if (length < 1 || text.charAt(0) != '0") {
return;
}
CharSequence saved = text.subSequence(0, length);
// Strip the dashes first, as we're going to add them back
int i = 0;
while (i < text.length()) {
if (text.charAt(i) == '-") {
text.delete(i, i + 1);
} else {
i++;
}
}
length = text.length();
int dashposition;
i = rootIndex;
int base = 0;
while (i < length) {
char ch = text.charAt(i);
if (!Character.isDigit(ch)) {
text.replace(0, length, saved);
return;
}
short value = FORMAT_MAP[base + ch - '0"];
if (value < 0) {
if (value <= -100) {
text.replace(0, length, saved);
return;
}
int dashPos2 = rootIndex + (Math.abs(value) % 10);
if (length > dashPos2) {
text.insert(dashPos2, "-");
}
int dashPos1 = rootIndex + (Math.abs(value) / 10);
if (length > dashPos1) {
text.insert(dashPos1, "-");
}
break;
} else {
base = value;
i++;
}
}
if (length > 3 && rootIndex == 3) {
text.insert(rootIndex, "-");
}