示例#1
0
  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);
  }
示例#2
0
 // @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());
   }
 }
示例#3
0
 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;
 }
示例#4
0
 // @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);
 }
示例#5
0
  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();
  }
示例#6
0
  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;
  }
示例#7
0
  // @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;
  }
示例#8
0
 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;
 }
示例#9
0
  /// @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());
  }
示例#10
0
  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;
    }
  }
示例#11
0
  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;
  }
示例#12
0
  // @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;
  }
示例#13
0
 // RHO_COMMENT
 public RubyString appendChars(char[] bytes, int len) {
   sb_.append(bytes, 0, len);
   return this;
 }
示例#14
0
 private RubyString appendString(RubyString v) {
   sb_.append(v.sb_);
   return this;
 }
示例#15
0
 // @RubyLevelMethod(name="reverse!")
 public RubyString reverse_danger() {
   sb_.reverse();
   return this;
 }
示例#16
0
 // @RubyLevelMethod(name="+")
 public RubyString plus(RubyValue v) {
   StringBuffer sb = new StringBuffer();
   sb.append(this.sb_);
   sb.append(v.toRubyString().sb_);
   return ObjectFactory.createString(sb);
 }
示例#17
0
 public int toInt() {
   return Integer.parseInt(sb_.toString());
 }
示例#18
0
 public String toString() {
   return sb_.toString();
 }
示例#19
0
 public RubyString appendString(String v) {
   sb_.append(v);
   return this;
 }
示例#20
0
  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();
  }
示例#21
0
  // @RubyLevelMethod(name="ord")
  public RubyValue ord() {
    if (sb_ == null || sb_.length() == 0) return ObjectFactory.createFixnum(0);

    return ObjectFactory.createFixnum(sb_.charAt(0));
  }
示例#22
0
 // RHO_MOBILE
 // @RubyLevelMethod(name = "include?")
 public RubyValue include_p(RubyValue arg) {
   int nIndex = sb_.toString().indexOf(arg.toStr());
   return ObjectFactory.createBoolean(nIndex >= 0);
 }
示例#23
0
 public long toLong() {
   return Long.parseLong(sb_.toString());
 }
示例#24
0
 // @RubyLevelMethod(name="strip")
 public RubyString strip() {
   return ObjectFactory.createString(sb_.toString().trim());
 }
示例#25
0
 // @RubyLevelMethod(name="bytesize")
 public RubyFixnum rubyBytesize() {
   return ObjectFactory.createFixnum(sb_.length());
 }
示例#26
0
 public int hashCode() {
   return sb_.toString().hashCode();
 }
示例#27
0
 public int length() {
   return sb_.length();
 }