private boolean delete(String from) { if (null != from && from.length() == 3 && from.charAt(1) == '-') { char from_start = from.charAt(0); char from_end = from.charAt(2); for (int i = 0; i < sb_.length(); ++i) { char current_char = sb_.charAt(i); if (current_char >= from_start && current_char <= from_end) { sb_.deleteCharAt(i); --i; } } return true; } else { boolean changed = false; for (int i = 0; i < from.length(); i++) { int index = sb_.toString().indexOf(from.charAt(i)); while (index >= 0) { sb_.deleteCharAt(index); changed = true; index = sb_.toString().indexOf(from.charAt(i)); } } return changed; } }
/// @return false if no change made // TODO handle more situations private boolean transform(String from, String to, boolean remove_duplicate) { String oldString = sb_.toString(); if (from.length() == 3 && to.length() == 3 && from.charAt(1) == '-' && to.charAt(1) == '-') { char from_start = from.charAt(0); char from_end = from.charAt(2); char to_start = to.charAt(0); char to_end = to.charAt(2); char last_char = 0; for (int i = 0; i < sb_.length(); ++i) { char current_char = sb_.charAt(i); if (current_char >= from_start && current_char <= from_end) { if (remove_duplicate && last_char == current_char) { sb_.deleteCharAt(i); --i; } else { int replace_char = (current_char - from_start) + to_start; sb_.setCharAt(i, replace_char < to_end ? (char) replace_char : to_end); last_char = current_char; } } } } else { char last_char = 0; for (int i = 0; i < sb_.length(); ++i) { char current_char = sb_.charAt(i); int index = from.indexOf(current_char); if (index >= 0) { if (remove_duplicate && last_char == current_char) { sb_.deleteCharAt(i); --i; } else { char replace_char = to.charAt(index < to.length() ? index : to.length()); sb_.setCharAt(i, replace_char); last_char = current_char; } } } } return !oldString.equals(sb_.toString()); }
private boolean squeeze(String from) { if (null != from && from.length() == 3 && from.charAt(1) == '-') { char from_start = from.charAt(0); char from_end = from.charAt(2); char last_char = 0; for (int i = 0; i < sb_.length(); ++i) { char current_char = sb_.charAt(i); if (current_char >= from_start && current_char <= from_end) { if (last_char == current_char) { sb_.deleteCharAt(i); --i; } else { last_char = current_char; } } } return true; } // TODO handle more situations return false; }