fix substr command

git-svn-id: svn://svn.code.sf.net/p/actiongame/code/trunk/ac@6358 e016b1c5-ed68-44aa-a2f7-31cac88cffde
This commit is contained in:
Lucas GAUTHERON
2011-04-26 12:12:33 +00:00
parent d8f93a4bf3
commit ad9826cac8
2 changed files with 16 additions and 9 deletions
+4 -4
View File
@@ -435,20 +435,20 @@
<arguments>
<argument token="S" description="the original string"/>
<argument token="A" description="start position"/>
<argument token="B" description="end position"/>
<argument token="L" description="substring length (optionnal)"/>
</arguments>
<return token="S" description="the substring" />
<remarks>
<remark>Character indexes begins at 0. The end position is the char that is not included anymore.</remark>
<remark>Character indexes begins at 0.</remark>
</remarks>
<examples>
<example>
<code><![CDATA[echo (substr abcdefgh 2 5)
cde]]></code>
cdefg]]></code>
</example>
<example>
<code><![CDATA[echo (substr abcdefgh 2 3)
c]]></code>
cde]]></code>
</example>
<example>
<code><![CDATA[echo (substr abcdefgh 2)
+12 -5
View File
@@ -1242,20 +1242,27 @@ 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 *pb)
void substr_(char *fs, char *pa, char *len)
{
int ia = atoi(pa);
int ib = atoi(pb);
int ilen = atoi(len);
string subq;
string subr;
subr[0] = '\0';
if(ia>(int)strlen(fs))
{
return;
}
if(ia>=0)
{
if(ib<=0 || ib>=(int)strlen(fs)) ib = strlen(fs);
if(!ilen)
{
ilen = strlen(fs)-ia;
}
copystring(subq, fs + ia);
strncpy(subr, subq, ib - ia);
strncpy(subr, subq, ilen);
}
subr[ib-ia] = '\0';
subr[ilen] = '\0';
result(subr);
}
void l0(int p, int v) { string f; string r; formatstring(f)("%%0%dd", p); formatstring(r)(f, v); result(r); }