Esempio n. 1
0
  // @RubyLevelMethod(name="downcase")
  public RubyString downcase() {
    int length = this.sb_.length();
    if (length == 0) {
      return ObjectFactory.createString();
    }

    char[] ca = new char[length];
    this.sb_.getChars(0, length, ca, 0);

    for (int i = 0; i < ca.length; i++) {
      ca[i] = Character.toLowerCase(ca[i]);
    }

    return ObjectFactory.createString(new StringBuffer().append(ca));
  }
Esempio n. 2
0
  private RubyString sub(RubyString g, RubyArray args) {
    if (null == args || args.size() != 2) {
      int actual_argc = (null == args) ? 0 : args.size();
      throw new RubyException(
          RubyRuntime.ArgumentErrorClass,
          "in `sub': wrong number of arguments (" + actual_argc + " for 2)");
    }

    if (!(args.get(1) instanceof RubyString)) {
      throw new RubyException(
          RubyRuntime.ArgumentErrorClass,
          "can't convert " + args.get(1).getRubyClass().getName() + " into String");
    }

    RubyString s = (RubyString) args.get(1);

    if (args.get(0) instanceof RubyRegexp) {
      RubyRegexp r = (RubyRegexp) args.get(0);
      return r.sub(g, s);
    } else if (args.get(0) instanceof RubyString) {
      RubyString r = (RubyString) args.get(0);
      String result = StringMe.replaceFirst(g.toString(), r.toString(), s.toString());
      return ObjectFactory.createString(result);
    } else {
      throw new RubyException(
          RubyRuntime.ArgumentErrorClass,
          "wrong argument type " + args.get(0).getRubyClass().getName() + " (expected Regexp)");
    }
  }
Esempio n. 3
0
 // @RubyLevelMethod(name="tr_s")
 public RubyValue tr_s(RubyValue arg1, RubyValue arg2) {
   RubyString string = ObjectFactory.createString(toString());
   RubyString from = (RubyString) arg1;
   RubyString to = (RubyString) arg2;
   string.tr_s(from.toString(), to.toString());
   return string;
 }
Esempio n. 4
0
  // @RubyLevelMethod(name="split")
  public RubyValue split(RubyArray args) {
    RubyValue r = (null == args) ? GlobalVariables.get("$;") : args.get(0);

    Collection /*<String>*/ splitResult;
    boolean bSkipFirstEmptyItem = false;
    if (r == RubyConstant.QNIL) {
      splitResult = split(this, " ");
    } else if (r instanceof RubyRegexp) {
      RubyRegexp reg = (RubyRegexp) r;
      splitResult = split(this, reg, args);

      if (reg.getPattern().getPattern().startsWith("(?=")) bSkipFirstEmptyItem = true;
    } else if (r instanceof RubyString) {
      splitResult = split(this, ((RubyString) r).toString());
    } else {
      throw new RubyException(
          RubyRuntime.ArgumentErrorClass,
          "wrong argument type " + r.getRubyClass() + " (expected Regexp)");
    }

    RubyArray a = new RubyArray(splitResult.size());
    int i = 0;
    //        for (String str : splitResult) {
    for (Iterator iter = splitResult.iterator(); iter.hasNext(); ) {
      String str = (String) iter.next();
      if (!(bSkipFirstEmptyItem && 0 == i && (str == null || str.equals("")))) {
        // To conform ruby's behavior, discard the first empty element
        a.add(ObjectFactory.createString(str));
      }
      ++i;
    }
    return a;
  }
Esempio n. 5
0
 // @RubyLevelMethod(name="to_f")
 public RubyFloat toRubyFloat() {
   double d;
   try {
     d = this.toFloat();
   } catch (NumberFormatException e) {
     throw new RubyException(RubyRuntime.ArgumentErrorClass, e.toString());
   }
   return ObjectFactory.createFloat(d);
 }
Esempio n. 6
0
  // @RubyLevelMethod(name="delete")
  public RubyValue delete(RubyArray args) {
    if (null == args) {
      throw new RubyException(RubyRuntime.ArgumentErrorClass, "wrong number of arguments");
    }

    RubyString string = ObjectFactory.createString(toString());
    String arg = ((RubyString) args.get(0)).toString();
    string.delete(arg);
    return string;
  }
Esempio n. 7
0
  public RubyString appendString(RubyValue v) {
    if (v instanceof RubyString) {
      return appendString((RubyString) v);
    } else {
      RubyValue r = RubyAPI.callPublicNoArgMethod(v, null, RubyID.toSID);
      if (r instanceof RubyString) return appendString((RubyString) r);

      return ObjectFactory.createString(r.toString());
    }
  }
Esempio n. 8
0
  // @RubyLevelMethod(name="[]")
  public RubyValue array_access(RubyArray args) {
    String string = toString();
    if (args.size() == 1) {
      RubyValue arg = args.get(0);
      if (arg instanceof RubyString) {
        String str = ((RubyString) arg).toString();
        if (string.indexOf(str) >= 0) {
          return ObjectFactory.createString(str);
        } else {
          return RubyConstant.QNIL;
        }
      } else if (arg instanceof RubyRange) {
        RubyRange range = (RubyRange) arg;
        int start = range.getLeft().toInt();
        int end = range.getRight().toInt();
        return substring(string, start, end, range.isExcludeEnd());
      } else if (arg instanceof RubyRegexp) {
        RubyRegexp regexp = (RubyRegexp) arg;
        RubyMatchData match = regexp.match(string);
        if (match != null) {
          return ObjectFactory.createString(match.toString());
        } else {
          return RubyConstant.QNIL;
        }
      } else {
        int index = arg.toInt();
        if (index < 0) {
          index = string.length() + index;
        }

        if (index < 0 || index >= string.length()) {
          return RubyConstant.QNIL;
        } else {
          return ObjectFactory.createFixnum(string.charAt(index));
        }
      }
    } else {
      int start = args.get(0).toInt();
      int length = args.get(1).toInt() - 1;

      return substring(string, start, start + length, false);
    }
  }
Esempio n. 9
0
  // @RubyLevelMethod(name="swapcase")
  public RubyString swapcase() {
    int length = this.sb_.length();
    if (length == 0) {
      return ObjectFactory.createString();
    }

    char[] ca = new char[length];
    this.sb_.getChars(0, length, ca, 0);

    for (int i = 0; i < length; i++) {
      char c = ca[i];
      if (Character.isUpperCase(c)) {
        ca[i] = Character.toLowerCase(c);
      } else if (Character.isLowerCase(c)) {
        ca[i] = Character.toUpperCase(c);
      }
    }

    return ObjectFactory.createString(new StringBuffer().append(ca));
  }
Esempio n. 10
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);
 }
Esempio n. 11
0
  // @RubyLevelMethod(name="hex")
  public RubyValue hex() {
    String s = toString();
    if (s.startsWith("0x")) {
      s = s.substring("0x".length());
    }

    try {
      return ObjectFactory.createFixnum(Long.parseLong(s, 16));
    } catch (NumberFormatException e) {
      return ObjectFactory.FIXNUM0;
    }
  }
Esempio n. 12
0
 // @RubyLevelMethod(name="=~")
 public RubyValue operator_match(RubyValue arg) {
   if (arg instanceof RubyRegexp) {
     RubyRegexp reg = (RubyRegexp) arg;
     int p = reg.matchPosition(toString());
     if (p >= 0) {
       return ObjectFactory.createFixnum(p);
     } else {
       return RubyConstant.QNIL;
     }
   } else {
     return RubyAPI.callPublicOneArgMethod(arg, this, null, RubyID.matchID);
   }
 }
Esempio n. 13
0
  // @RubyLevelMethod(name="casecmp")
  RubyValue run(RubyValue arg) {
    if (!(arg instanceof RubyString)) {
      return RubyConstant.QNIL;
    }

    RubyString value2 = (RubyString) arg;
    int compare = toString().toUpperCase().compareTo(value2.toString().toUpperCase());
    if (compare > 0) {
      compare = 1;
    } else if (compare < 0) {
      compare = -1;
    }
    return ObjectFactory.createFixnum(compare);
  }
Esempio n. 14
0
  // @RubyLevelMethod(name="count")
  public RubyValue count(RubyArray args) {
    if (null == args) {
      throw new RubyException(RubyRuntime.ArgumentErrorClass, "wrong number of arguments");
    }

    // TODO incomplete
    int n = 0;
    //        for (RubyValue v : args) {
    for (Iterator iter = args.iterator(); iter.hasNext(); ) {
      RubyValue v = (RubyValue) iter.next();
      RubyString other_str = (RubyString) v;
      n += count(other_str.toString());
    }
    return ObjectFactory.createFixnum(n);
  }
Esempio n. 15
0
 // @RubyLevelMethod(name="%")
 public RubyValue format(RubyValue arg) {
   String format = toString();
   String s;
   // RHO_MOBILE
   if (arg instanceof RubyArray) {
     // s = StringMe.format(format,
     // com.xruby.runtime.lang.RubyKernelModule.buildFormatArg((RubyArray)arg, 0));
     s = StringMe.format(format, (RubyArray) arg);
   } else {
     // s = StringMe.format(format, com.xruby.runtime.lang.RubyKernelModule.buildFormatArg(new
     // RubyArray(arg), 0));
     RubyArray args = new RubyArray(arg);
     s = StringMe.format(format, args);
   }
   return ObjectFactory.createString(s);
 }
Esempio n. 16
0
  // @RubyLevelMethod(name="each_byte")
  public RubyValue each_byte(RubyBlock block) {
    if (block == null) return this;

    String string = toString();
    byte bytes[] = null;
    try {
      bytes = string.getBytes("UTF-8");
    } catch (java.io.UnsupportedEncodingException exc) {
    }

    for (int i = 0; bytes != null && i < bytes.length; ++i) {
      int nByte = bytes[i];
      if (nByte < 0) nByte = 256 + nByte;

      block.invoke(this, ObjectFactory.createFixnum(nByte));
    }

    return this;
  }
Esempio n. 17
0
  // @RubyLevelMethod(name="==")
  public RubyValue opEqual(RubyValue v) {
    if (this == v) {
      return RubyConstant.QTRUE;
    }

    if (v instanceof RubyString) {
      RubyString str = ((RubyString) v);
      if ((this.sb_.length() == str.sb_.length() && this.cmp(str) == 0)) {
        return RubyConstant.QTRUE;
      } else {
        return RubyConstant.QFALSE;
      }
    }

    if (v.respondTo(RubyID.toStrID)) {
      return ObjectFactory.createBoolean(v.equals(this));
    } else {
      return RubyConstant.QFALSE;
    }
  }
Esempio n. 18
0
  private RubyValue substring(String string, int begin, int end, boolean isExcludeEnd) {

    if (begin < 0) {
      begin = string.length() + begin;
    }

    if (end < 0) {
      end = string.length() + end;
    }

    if (!isExcludeEnd) {
      ++end;
    }

    if (begin < 0 || end < 0 || begin > end || begin > string.length() || end > string.length()) {
      return RubyConstant.QNIL;
    }

    return ObjectFactory.createString(string.substring(begin, end));
  }
Esempio n. 19
0
 // @RubyLevelMethod(name="strip")
 public RubyString strip() {
   return ObjectFactory.createString(sb_.toString().trim());
 }
Esempio n. 20
0
  // @RubyLevelMethod(name="ord")
  public RubyValue ord() {
    if (sb_ == null || sb_.length() == 0) return ObjectFactory.createFixnum(0);

    return ObjectFactory.createFixnum(sb_.charAt(0));
  }
Esempio n. 21
0
 // @RubyLevelMethod(name="squeeze")
 public RubyValue squeeze(RubyArray args) {
   RubyString string = ObjectFactory.createString(toString());
   String arg = ((null == args) ? null : ((RubyString) args.get(0)).toString());
   string.squeeze(arg);
   return string;
 }
Esempio n. 22
0
 // @RubyLevelMethod(name="dump")
 public RubyString rubyDump() {
   return ObjectFactory.createString(this.dump());
 }
Esempio n. 23
0
 // @RubyLevelMethod(name="to_s")
 public RubyString to_s() {
   return ObjectFactory.createString(this.sb_.toString());
 }
Esempio n. 24
0
 // @RubyAllocMethod
 public static RubyString alloc(RubyValue receiver) {
   return ObjectFactory.createString((RubyClass) receiver, "");
 }
Esempio n. 25
0
 // @RubyLevelMethod(name="reverse")
 public RubyValue reverse() {
   RubyString string = ObjectFactory.createString(toString());
   return string.reverse_danger();
 }
Esempio n. 26
0
  // @RubyLevelMethod(name="[]=")
  public RubyValue array_set(RubyArray args) {
    String string = toString();
    String replacement;

    int start, end;

    if (args.size() == 2) {
      RubyValue arg = args.get(0);
      replacement = ((RubyString) args.get(1)).toString();

      if (arg instanceof RubyString) {
        String str = ((RubyString) arg).toString();
        start = string.indexOf(str);
        if (start >= 0) {
          end = start + str.length();
        } else {
          throw new RubyException(RubyRuntime.IndexErrorClass, "string not matched");
        }
      } else if (arg instanceof RubyRange) {
        RubyRange range = (RubyRange) arg;
        start = range.getLeft().toInt();
        end = range.getRight().toInt();
        if (start >= string.length()) {
          throw new RubyException(RubyRuntime.RangeClass, range.toString() + " out of range");
        }
      } else if (arg instanceof RubyRegexp) {
        RubyRegexp regexp = (RubyRegexp) arg;
        RubyMatchData match = regexp.match(string);
        if (match != null) {
          String matched = match.toString();
          start = string.indexOf(matched);
          end = matched.length() + start;
        } else {
          throw new RubyException(RubyRuntime.IndexErrorClass, "regexp not matched");
        }
      } else {
        start = arg.toInt();
        end = start + 1;
      }
    } else {
      replacement = ((RubyString) args.get(2)).toString();

      start = args.get(0).toInt();
      end = args.get(1).toInt() + start;
      if (start > /*=*/ string.length()) {
        throw new RubyException(
            RubyRuntime.RangeClass,
            "Index '"
                + start
                + "' out of string:"
                + string
                + ";end: '"
                + end
                + "';replacement:"
                + replacement
                + "");
      }
    }
    setString(replace(string, start, end, replacement));
    return ObjectFactory.createString(replacement);
  }
Esempio n. 27
0
 // @RubyLevelMethod(name="bytesize")
 public RubyFixnum rubyBytesize() {
   return ObjectFactory.createFixnum(sb_.length());
 }
Esempio n. 28
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);
 }
Esempio n. 29
0
  // RHO_COMMENT
  // @RubyLevelMethod(name="encoding")
  public RubyValue getEncoding() {
    if (m_valEncoding != null) return m_valEncoding;

    return ObjectFactory.createString("ASCII-8BIT"); // "UTF-8");
  }
Esempio n. 30
0
 // @RubyLevelMethod(name="chomp")
 public RubyValue chomp(RubyArray args) {
   RubyString string = ObjectFactory.createString(toString());
   RubyValue separator = (null != args) ? args.get(0) : GlobalVariables.get("$/");
   string.chomp(((RubyString) separator).toString());
   return string;
 }