Return a modified <string>, with <len> characters starting after the
character at position <first> deleted. In other words, it copies <first>
characters, skips <len> characters>, and then copies the remainder of
- the string.
-
- Example:
- > say [delete(abcdefgh, 3, 2)]
+ the string. If <len> is negative, deletes characters leftwards from <first>.
+ Characters are numbered starting at 0.
+
+ Examples:
+ > say delete(abcdefgh, 3, 2)
You say, "abcfgh"
-
+ > say delete(abcdefgh, 3, -2)
+ You say, "abefgh"
+
& DIE()
die(<number of times to roll die>, <number of sides on die>[, <show>])
Mid returns a segment of the string, the <length> characters to the
right of the <first> character. Note that the first character in a
- string is numbered zero, and not one.
+ string is numbered zero, and not one. If <length> is negative, it
+ returns characters to the left of the <first> character. The <first>
+ character is always part of the <length> characters.
+
+ Examples:
+ > say mid(foobar, 2, 2)
+ You say, "ob"
+ > say mid(foobar, 2, -2)
+ You say, "oo"
+
+ See also: LEFT(), RIGHT()
& MIN()
min(<num1>, <num2>, ..., ...)
pos = parse_integer(args[1]);
len = parse_integer(args[2]);
- if ((pos < 0) || (len < 0)) {
+ if (pos < 0) {
safe_str(T(e_range), buff, bp);
free_ansi_string(as);
return;
}
+ if (len < 0) {
+ pos = pos + len + 1;
+ if (pos < 0)
+ pos = 0;
+ len = -len;
+ }
+
safe_ansi_string(as, pos, len, buff, bp);
free_ansi_string(as);
}
FUNCTION(fun_delete)
{
ansi_string *as;
- int pos, num;
-
+ int pos, pos2, num;
if (!is_integer(args[1]) || !is_integer(args[2])) {
safe_str(T(e_ints), buff, bp);
as = parse_ansi_string(args[0]);
- if ((size_t) pos > as->len || num <= 0) {
+ if ((size_t) pos > as->len || num == 0) {
safe_strl(args[0], arglens[0], buff, bp);
free_ansi_string(as);
return;
}
+ if (num < 0) {
+ pos2 = pos + 1;
+ pos = pos2 + num;
+ if (pos < 0)
+ pos = 0;
+ } else
+ pos2 = pos + num;
+
safe_ansi_string(as, 0, pos, buff, bp);
- safe_ansi_string(as, pos + num, as->len, buff, bp);
+ safe_ansi_string(as, pos2, as->len, buff, bp);
free_ansi_string(as);
}