Ejemplo n.º 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);
  }
Ejemplo n.º 2
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;
  }
Ejemplo n.º 3
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;
    }
  }
Ejemplo n.º 4
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;
 }
Ejemplo n.º 5
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());
  }
Ejemplo n.º 6
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;
  }
Ejemplo n.º 7
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;
  }
Ejemplo n.º 8
0
  // @RubyLevelMethod(name="ord")
  public RubyValue ord() {
    if (sb_ == null || sb_.length() == 0) return ObjectFactory.createFixnum(0);

    return ObjectFactory.createFixnum(sb_.charAt(0));
  }
Ejemplo n.º 9
0
 // @RubyLevelMethod(name="bytesize")
 public RubyFixnum rubyBytesize() {
   return ObjectFactory.createFixnum(sb_.length());
 }
Ejemplo n.º 10
0
 public int length() {
   return sb_.length();
 }