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; } }
private int count(String s) { int n = 0; for (int i = 0; i < sb_.length(); ++i) { if (s.indexOf(sb_.charAt(i)) >= 0) { ++n; } } return n; }
/// @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()); }
// @RubyLevelMethod(name="lstrip!") public RubyValue lstripBang() { int i = 0; while (i < sb_.length() && CharacterMe.isWhitespace(sb_.charAt(i))) { i++; } if (0 == i) { // No change return RubyConstant.QNIL; } sb_.delete(0, i); return this; }
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; }
// @RubyLevelMethod(name="ord") public RubyValue ord() { if (sb_ == null || sb_.length() == 0) return ObjectFactory.createFixnum(0); return ObjectFactory.createFixnum(sb_.charAt(0)); }