public String getChars(int nPos, int nLen) { if (nPos + nLen > sb_.length()) nLen = sb_.length() - nPos; char[] ca = new char[nLen]; this.sb_.getChars(nPos, nPos + nLen, ca, 0); return new String(ca); }
// @RubyLevelMethod(name="scan") public RubyValue scan(RubyValue arg, RubyBlock block) { RubyRegexp regex = (RubyRegexp) arg; if (null != block) { regex.scan(sb_.toString(), block); return this; } else { return regex.scan(sb_.toString()); } }
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; }
// @RubyLevelMethod(name="*") public RubyValue operator_star(RubyValue arg) { String string = toString(); int count = arg.toInt(); if (count < 0) { throw new RubyException(RubyRuntime.ArgumentErrorClass, "negative argument"); } StringBuffer result = new StringBuffer(); for (int i = 0; i < count; ++i) { result.append(string); } return ObjectFactory.createString(result); }
private String replace(String source, int start, int end, String replacement) { AssertMe.rho_assert(start <= source.length() /* - 1*/); if (end < start) { end = start + 1; } StringBuffer result = new StringBuffer(source.substring(0, start)); result.append(replacement); result.append(source.substring(end)); return result.toString(); }
private boolean chomp(String seperator) { int nSepLen = 0; if (seperator.equals("\n") && sb_.toString().endsWith("\r\n")) nSepLen = 2; else if (sb_.toString().endsWith(seperator)) nSepLen = seperator.length(); else if (sb_.toString().endsWith("\r")) nSepLen = 1; else return false; int start = sb_.length() - nSepLen; int end = sb_.length(); sb_.delete(start, end); return true; }
// @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; }
public RubyValue clone() { // RubyString s = (RubyString)super.clone(); // s.sb_ = new StringBuffer(sb_); // return s; RubyString cl = new RubyString(this.sb_); cl.sb_ = new StringBuffer(sb_.toString()); cl.doClone(this); return cl; }
/// @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 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 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="force_encoding") public RubyValue force_encoding(RubyValue arg) { if ((m_valEncoding != null && !m_valEncoding.toString().equalsIgnoreCase("UTF-8")) && arg.toString().equalsIgnoreCase("UTF-8")) { try { byte bytes[] = sb_.toString().getBytes("ISO8859_1"); String str1 = new String(bytes, "UTF-8"); sb_ = new StringBuffer(str1); } catch (java.io.UnsupportedEncodingException exc) { sb_ = new StringBuffer(""); } } m_valEncoding = arg; return this; }
// RHO_COMMENT public RubyString appendChars(char[] bytes, int len) { sb_.append(bytes, 0, len); return this; }
private RubyString appendString(RubyString v) { sb_.append(v.sb_); return this; }
// @RubyLevelMethod(name="reverse!") public RubyString reverse_danger() { sb_.reverse(); return this; }
// @RubyLevelMethod(name="+") public RubyString plus(RubyValue v) { StringBuffer sb = new StringBuffer(); sb.append(this.sb_); sb.append(v.toRubyString().sb_); return ObjectFactory.createString(sb); }
public int toInt() { return Integer.parseInt(sb_.toString()); }
public String toString() { return sb_.toString(); }
public RubyString appendString(String v) { sb_.append(v); return this; }
public String dump() { int length = this.sb_.length(); StringBuffer buf = new StringBuffer(); buf.append('"'); for (int i = 0; i < length; i++) { char c = this.sb_.charAt(i); if (c == '"' || c == '\\') { buf.append('\\'); buf.append(c); } else if (c == '#') { if (isEvstr(c, i, length - 1)) { buf.append('\\'); } buf.append('#'); } else if (isPrint(c)) { buf.append(c); } else if (c == '\n') { buf.append('\\'); buf.append('n'); } else if (c == '\r') { buf.append('\\'); buf.append('r'); } else if (c == '\t') { buf.append('\\'); buf.append('t'); } else if (c == '\f') { buf.append('\\'); buf.append('f'); } else if (c == '\013') { buf.append('\\'); buf.append('v'); } else if (c == '\010') { buf.append('\\'); buf.append('b'); } else if (c == '\007') { buf.append('\\'); buf.append('a'); } else if (c == '\033') { buf.append('\\'); buf.append('e'); } else { buf.append('\\'); buf.append(formatForDump("%03o", c)); } } buf.append('"'); return buf.toString(); }
// @RubyLevelMethod(name="ord") public RubyValue ord() { if (sb_ == null || sb_.length() == 0) return ObjectFactory.createFixnum(0); return ObjectFactory.createFixnum(sb_.charAt(0)); }
// RHO_MOBILE // @RubyLevelMethod(name = "include?") public RubyValue include_p(RubyValue arg) { int nIndex = sb_.toString().indexOf(arg.toStr()); return ObjectFactory.createBoolean(nIndex >= 0); }
public long toLong() { return Long.parseLong(sb_.toString()); }
// @RubyLevelMethod(name="strip") public RubyString strip() { return ObjectFactory.createString(sb_.toString().trim()); }
// @RubyLevelMethod(name="bytesize") public RubyFixnum rubyBytesize() { return ObjectFactory.createFixnum(sb_.length()); }
public int hashCode() { return sb_.toString().hashCode(); }
public int length() { return sb_.length(); }