From e7466e5e46805c4e859d923c7a9ed7877fd59eda Mon Sep 17 00:00:00 2001 From: skotlex Date: Wed, 24 Jan 2007 15:11:42 +0000 Subject: - Improved a bit the menu entries counting code (using a while with strchr should yield better performance that a for done on the string's strlen, right? However, it bugs me that strchr's man-page says it won't work on multi-byte characters. But does a strchr(str,':') works any worse than (str[i]== ':')? git-svn-id: https://rathena.svn.sourceforge.net/svnroot/rathena/trunk@9706 54d463be-8e91-2dee-dedb-b68131a5f0ec --- src/map/script.c | 44 +++++++++++++++++++++++--------------------- 1 file changed, 23 insertions(+), 21 deletions(-) (limited to 'src/map/script.c') diff --git a/src/map/script.c b/src/map/script.c index 03ff19973..7abbe83df 100644 --- a/src/map/script.c +++ b/src/map/script.c @@ -4371,8 +4371,8 @@ int buildin_close2(struct script_state *st) */ int buildin_menu(struct script_state *st) { - char *buf; - int len,i, max = 1; + char *buf, *ptr; + int len,i; struct map_session_data *sd = script_rid2sd(st); nullpo_retr(0, sd); @@ -4399,11 +4399,11 @@ int buildin_menu(struct script_state *st) strcat(buf,":"); } } - for(i=0; (unsigned int)i < strlen(buf); i++){ - if(buf[i] == ':') - max++; - } - sd->npc_menu = max; //Reuse to store max menu entries. Avoids the need of an extra variable. + + ptr = buf; + sd->npc_menu = 0; //Reuse to store max menu entries. Avoids the need of an extra variable. + while (ptr && (ptr = strchr(ptr, ':')) != NULL) + { sd->npc_menu++; ptr++; } clif_scriptmenu(sd,st->oid,buf); aFree(buf); } else if(sd->npc_menu==0xff){ // cancel @@ -10614,8 +10614,8 @@ int buildin_jump_zero(struct script_state *st) { int buildin_select(struct script_state *st) { - char *buf; - int len,i,max = 1; + char *buf, *ptr; + int len,i; struct map_session_data *sd; sd=script_rid2sd(st); @@ -10633,11 +10633,12 @@ int buildin_select(struct script_state *st) strcat(buf,st->stack->stack_data[i].u.str); strcat(buf,":"); } - for(i=0; (unsigned int)i < strlen(buf); i++){ - if(buf[i] == ':') - max++; - } - sd->npc_menu = max; //Reuse to store max menu entries. Avoids the need of an extra variable. + + ptr = buf; + sd->npc_menu = 0; //Reuse to store max menu entries. Avoids the need of an extra variable. + while (ptr && (ptr = strchr(ptr, ':')) != NULL) + { sd->npc_menu++; ptr++; } + clif_scriptmenu(sd,st->oid,buf); aFree(buf); } else if(sd->npc_menu==0xff){ @@ -10659,8 +10660,8 @@ int buildin_select(struct script_state *st) int buildin_prompt(struct script_state *st) { - char *buf; - int len,i,max = 1; + char *buf, *ptr; + int len,i; struct map_session_data *sd; sd=script_rid2sd(st); @@ -10679,11 +10680,12 @@ int buildin_prompt(struct script_state *st) strcat(buf,st->stack->stack_data[i].u.str); strcat(buf,":"); } - for(i=0; (unsigned int)i < strlen(buf); i++){ - if(buf[i] == ':') - max++; - } - sd->npc_menu = max; //Reuse to store max menu entries. Avoids the need of an extra variable. + + ptr = buf; + sd->npc_menu = 0; //Reuse to store max menu entries. Avoids the need of an extra variable. + while (ptr && (ptr = strchr(ptr, ':')) != NULL) + { sd->npc_menu++; ptr++; } + clif_scriptmenu(sd,st->oid,buf); aFree(buf); } else { -- cgit v1.2.3-70-g09d2