substr & negative start position

git-svn-id: svn://svn.code.sf.net/p/actiongame/code/trunk/ac@6362 e016b1c5-ed68-44aa-a2f7-31cac88cffde
This commit is contained in:
Lucas GAUTHERON
2011-04-26 15:00:27 +00:00
parent b48939c516
commit 9a0b0abb9a
2 changed files with 9 additions and 5 deletions
+3 -3
View File
@@ -439,7 +439,7 @@
</arguments>
<return token="S" description="the substring" />
<remarks>
<remark>Character indexes begins at 0.</remark>
<remark>Character indexes begins at 0. If "start position" is negative, the reference is the end of the string.</remark>
</remarks>
<examples>
<example>
@@ -447,8 +447,8 @@
cdefg]]></code>
</example>
<example>
<code><![CDATA[echo (substr abcdefgh 2 3)
cde]]></code>
<code><![CDATA[echo (substr abcdefgh -3 2)
fg]]></code>
</example>
<example>
<code><![CDATA[echo (substr abcdefgh 2)
+6 -2
View File
@@ -1266,15 +1266,19 @@ void timestring_()
int millis_() { extern int totalmillis; return totalmillis; }
void strlen_(char *s) { string r; formatstring(r)("%d", strlen(s)); result(r); }
void substr_(char *fs, char *pa, char *len)
{
int ia = atoi(pa);
int ilen = atoi(len);
if(ia>(int)strlen(fs) || ia < 0 || ilen < 0) return;
int fslen = (int)strlen(fs);
if(ia<0) ia += fslen;
if(ia>fslen || ia < 0 || ilen < 0) return;
if(!ilen) ilen = strlen(fs)-ia;
if(!ilen) ilen = fslen-ia;
result(newstring(fs + ia, ilen));
}
void l0(int p, int v) { string f; string r; formatstring(f)("%%0%dd", p); formatstring(r)(f, v); result(r); }
COMMANDN(millis, millis_, ARG_IVAL);