Sujet : Re: improve that function (bytes_dig_int)
De : tr.17687 (at) *nospam* z991.linuxsc.com (Tim Rentsch)
Groupes : comp.lang.cDate : 09. Apr 2024, 06:32:28
Autres entêtes
Organisation : A noiseless patient Spider
Message-ID : <86jzl72j9v.fsf@linuxsc.com>
References : 1
User-Agent : Gnus/5.11 (Gnus v5.11) Emacs/22.4 (gnu/linux)
fir <
fir@grunge.pl> writes:
it seems that this group likes such kind of topics, so
here you got
>
improve that function - i wrote it in quick and feel tired so i
dont want to improve it as for now
>
i got "list" of bytes (by list i mean pointer to ram area of
elements - loke reallocked by reallock, by aray i would rather
aybe name fully static array and heap seeds are more like lists
especially if you use them as lists - and got methods like "add"
(remove etc)
>
unsigned char* bytes = NULL;
int bytes_size = 0;
int bytes_allocked = 0;
int bytes_cursor = 0;
>
i wint to write a function DigInt that "digs" one int form that
list and returns its, then the "cursor" is set after that and you
can call it again untill all ints are "digged"
>
[code]
I usually prefer (and recommend) writing code that doesn't make
use of global state, as for example:
long
next_value( char *s0, char **next_s ){
char *s = s0 + strcspn( s0, "0123456789" );
return
!*s
? *next_s = s0, 0
: strtol( s - (s!=s0 && s[-1]=='-'), next_s, 10 );
}
Retrieving and processing successive values can be done using
this function by means of a simple for() loop, as illustrated by
the code below:
void
print_longs( char *s ){
long v;
printf( " input: %s\n", s );
for( char *next; v = next_value( s, &next ), s != next; s = next ){
printf( " value: %12ld\n", v );
}
printf( "\n" );
}
Note that the local variable 'next' here takes the place of a
cursor for the next input.