From d59fb43913e6ed2fff68925759884715a81482b8 Mon Sep 17 00:00:00 2001 From: Ari Johnson Date: Fri, 23 Feb 2007 03:02:33 +0000 Subject: [PATCH] delete() can use a negative argument a la MUX --- game/txt/hlp/cobra_func.hlp | 25 +++++++++++++++++++------ src/funstr.c | 24 +++++++++++++++++++----- 2 files changed, 38 insertions(+), 11 deletions(-) diff --git a/game/txt/hlp/cobra_func.hlp b/game/txt/hlp/cobra_func.hlp index 03a3a22..c273899 100644 --- a/game/txt/hlp/cobra_func.hlp +++ b/game/txt/hlp/cobra_func.hlp @@ -900,12 +900,15 @@ Return a modified , with characters starting after the character at position deleted. In other words, it copies characters, skips characters>, and then copies the remainder of - the string. - - Example: - > say [delete(abcdefgh, 3, 2)] + the string. If is negative, deletes characters leftwards from . + 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(, [, ]) @@ -2332,7 +2335,17 @@ for an object named "Test", preferring a thing over other types. Mid returns a segment of the string, the characters to the right of the character. Note that the first character in a - string is numbered zero, and not one. + string is numbered zero, and not one. If is negative, it + returns characters to the left of the character. The + character is always part of the characters. + + Examples: + > say mid(foobar, 2, 2) + You say, "ob" + > say mid(foobar, 2, -2) + You say, "oo" + + See also: LEFT(), RIGHT() & MIN() min(, , ..., ...) diff --git a/src/funstr.c b/src/funstr.c index f938cc4..f58f459 100644 --- a/src/funstr.c +++ b/src/funstr.c @@ -301,12 +301,19 @@ FUNCTION(fun_mid) 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); } @@ -397,8 +404,7 @@ FUNCTION(fun_strinsert) 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); @@ -415,14 +421,22 @@ FUNCTION(fun_delete) 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); } -- 2.30.2