Rocksolid Light

Welcome to novaBBS (click a section below)

mail  files  register  newsreader  groups  login

Message-ID:  

Evolution is a million line computer program falling into place by accident.


devel / comp.lang.c / Re: improve that function (bytes_dig_int)

SubjectAuthor
* improve that function (bytes_dig_int)fir
+* Re: improve that function (bytes_dig_int)fir
|`- Re: improve that function (bytes_dig_int)fir
`* Re: improve that function (bytes_dig_int)Tim Rentsch
 `* Re: improve that function (bytes_dig_int)fir
  `- Re: improve that function (bytes_dig_int)Tim Rentsch

1
improve that function (bytes_dig_int)

<uukcop$3vhcj$1@i2pn2.org>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=34912&group=comp.lang.c#34912

  copy link   Newsgroups: comp.lang.c
Path: i2pn2.org!.POSTED!not-for-mail
From: fir...@grunge.pl (fir)
Newsgroups: comp.lang.c
Subject: improve that function (bytes_dig_int)
Date: Wed, 03 Apr 2024 22:04:37 +0200
Organization: i2pn2 (i2pn.org)
Message-ID: <uukcop$3vhcj$1@i2pn2.org>
MIME-Version: 1.0
Content-Type: text/plain; charset=ISO-8859-1; format=flowed
Content-Transfer-Encoding: 7bit
Injection-Date: Wed, 3 Apr 2024 20:04:42 -0000 (UTC)
Injection-Info: i2pn2.org;
logging-data="4179347"; mail-complaints-to="usenet@i2pn2.org";
posting-account="+ydHcGjgSeBt3Wz3WTfKefUptpAWaXduqfw5xdfsuS0";
User-Agent: Mozilla/5.0 (Windows NT 5.1; rv:27.0) Gecko/20100101 Firefox/27.0 SeaMonkey/2.24
X-Spam-Checker-Version: SpamAssassin 4.0.0
 by: fir - Wed, 3 Apr 2024 20:04 UTC

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"

void SkipBytesTillNumberBegin()
{
for(;;)
{
if(bytes[bytes_cursor]==0) break;
if(IsDigit(bytes[bytes_cursor])) break;
if(bytes[bytes_cursor]=='-' & bytes_cursor+1<bytes_size &
IsDigit(bytes[bytes_cursor+1])) break;
bytes_cursor++;
if(bytes_cursor==bytes_size) break;
}
}

//it sets bytes_cursor on bytes_size+1 if not found
int bytes_dig_int_from_cursor()
{
if(bytes_cursor>=bytes_size)
{ bytes_cursor = bytes_size +1; return 'none'; }

for(;;)
{
SkipBytesTillNumberBegin();
if(bytes_cursor>=bytes_size)
{ bytes_cursor = bytes_size +1; return 'none'; }

unsigned char* h = &bytes[bytes_cursor];
int value = strtol(h, &h, 10);

if(h>&bytes[bytes_size]) ERROR_EXIT("\n*** ERROR of conversion in
DigInt routine ");
if(h==&bytes[bytes_cursor]) ERROR_EXIT("\n*** ERROR of conversion
in DigInt routine ");
if (errno) ERROR_EXIT("\n*** ERROR of conversion in DigInt
routine ");

bytes_cursor = (int)(h-bytes);

return value;
}

return 0;
}

void BytesTest()
{
bytes_load("some.txt");

while(bytes_cursor<bytes_size)
{
int value = bytes_dig_int_from_cursor();
if(bytes_cursor>bytes_size) break;
printf("\n %d", value);
}
}

thic function isnt optimal by many means but im to tired to
improve it by now but some discussion on this may be eventually fruitfull

some.txt

i - 3 04 -9 am 0001fir and -02fir -99and --3firfir4

result

3
4
-9
1
-2
-99
-3
4

Re: improve that function (bytes_dig_int)

<uukf9l$3vkll$1@i2pn2.org>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=34913&group=comp.lang.c#34913

  copy link   Newsgroups: comp.lang.c
Path: i2pn2.org!.POSTED!not-for-mail
From: fir...@grunge.pl (fir)
Newsgroups: comp.lang.c
Subject: Re: improve that function (bytes_dig_int)
Date: Wed, 03 Apr 2024 22:47:45 +0200
Organization: i2pn2 (i2pn.org)
Message-ID: <uukf9l$3vkll$1@i2pn2.org>
References: <uukcop$3vhcj$1@i2pn2.org>
MIME-Version: 1.0
Content-Type: text/plain; charset=ISO-8859-1; format=flowed
Content-Transfer-Encoding: 7bit
Injection-Date: Wed, 3 Apr 2024 20:47:49 -0000 (UTC)
Injection-Info: i2pn2.org;
logging-data="4182709"; mail-complaints-to="usenet@i2pn2.org";
posting-account="+ydHcGjgSeBt3Wz3WTfKefUptpAWaXduqfw5xdfsuS0";
User-Agent: Mozilla/5.0 (Windows NT 5.1; rv:27.0) Gecko/20100101 Firefox/27.0 SeaMonkey/2.24
In-Reply-To: <uukcop$3vhcj$1@i2pn2.org>
X-Spam-Checker-Version: SpamAssassin 4.0.0
 by: fir - Wed, 3 Apr 2024 20:47 UTC

fir wrote:
> void BytesTest()
> {
> bytes_load("some.txt");
>
> while(bytes_cursor<bytes_size)
> {
> int value = bytes_dig_int_from_cursor();
> if(bytes_cursor>bytes_size) break;
> printf("\n %d", value);
> }
> }
>

as to this part i was thinking that using the bytes_cursor
(or how to call it better) would be good idea and would simplify
the usage of it but this loop is also overcomp0-lex so im not sure
if to using sorta global variable (like "none") wouldnt be better here

thsi is odlschool idea and i dont seen its used today but here it
is just simple

for(;;)
{ int value = dig_int();
if(none) break;

}

Re: improve that function (bytes_dig_int)

<uukivg$3vq2p$1@i2pn2.org>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=34914&group=comp.lang.c#34914

  copy link   Newsgroups: comp.lang.c
Path: i2pn2.org!.POSTED!not-for-mail
From: fir...@grunge.pl (fir)
Newsgroups: comp.lang.c
Subject: Re: improve that function (bytes_dig_int)
Date: Wed, 03 Apr 2024 23:50:37 +0200
Organization: i2pn2 (i2pn.org)
Message-ID: <uukivg$3vq2p$1@i2pn2.org>
References: <uukcop$3vhcj$1@i2pn2.org> <uukf9l$3vkll$1@i2pn2.org>
MIME-Version: 1.0
Content-Type: text/plain; charset=ISO-8859-1; format=flowed
Content-Transfer-Encoding: 7bit
Injection-Date: Wed, 3 Apr 2024 21:50:41 -0000 (UTC)
Injection-Info: i2pn2.org;
logging-data="4188249"; mail-complaints-to="usenet@i2pn2.org";
posting-account="+ydHcGjgSeBt3Wz3WTfKefUptpAWaXduqfw5xdfsuS0";
User-Agent: Mozilla/5.0 (Windows NT 5.1; rv:27.0) Gecko/20100101 Firefox/27.0 SeaMonkey/2.24
In-Reply-To: <uukf9l$3vkll$1@i2pn2.org>
X-Spam-Checker-Version: SpamAssassin 4.0.0
 by: fir - Wed, 3 Apr 2024 21:50 UTC

fir wrote:
> fir wrote:
>> void BytesTest()
>> {
>> bytes_load("some.txt");
>>
>> while(bytes_cursor<bytes_size)
>> {
>> int value = bytes_dig_int_from_cursor();
>> if(bytes_cursor>bytes_size) break;
>> printf("\n %d", value);
>> }
>> }
>>
>
> as to this part i was thinking that using the bytes_cursor
> (or how to call it better) would be good idea and would simplify
> the usage of it but this loop is also overcomp0-lex so im not sure
> if to using sorta global variable (like "none") wouldnt be better here
>
> thsi is odlschool idea and i dont seen its used today but here it
> is just simple
>
> for(;;)
> {
> int value = dig_int();
> if(none) break;
>
> }
>
>
so i revrited no wto use global variable (module variable - it should be
rather named module variable
than global variable as i once noted variables in modern c like on
windows are not global but module scope - as programs are modules)

int bytes_dig_int()
{
if(bytes_cursor>=bytes_size){ found =0; return 'none'; }

for(;;)
{
SkipBytesTillNumberBegin();
if(bytes_cursor>=bytes_size) { found =0; return 'none'; }

unsigned char* h = &bytes[bytes_cursor];
int value = strtol(h, &h, 10);

if(h>&bytes[bytes_size]) ERROR_EXIT("\n*** ERROR in DigInt -
strtol reached after bytes ");
if(h==&bytes[bytes_cursor]) ERROR_EXIT("\n*** ERROR in DigInt -
not a number ");
if (errno) ERROR_EXIT("\n*** ERROR in DigInt - out of range ");

bytes_cursor = (int)(h-bytes);

found = 1;
return value;
}

return 0;
}

it is not tested for potential errors though

imo it is also cool fo write a procedure that finds goven
name and then digs int right after that - such simple function
then can be used to read settings from a text file and this is
kinda fun as it assumes nothing about the file format
do no ini no xml etc - you just find a string and take an int after that
string and this is funny way (i get a little merry after
noticing that) its also very easy to write

int compare_bytes_at_to_str(int at, unsigned char* str, int len )
{
for(int i=0; i<len;i++)
{
if(bytes[at+i]!=str[i]) return 0;
}
return 1;
}
int bytes_find(char* str)
{
int len = StrLen(str);

for(int i=bytes_cursor; i<bytes_size-len+1; i++)
{
if(compare_bytes_at_to_str(i, str, len))
{
bytes_cursor = i + len;
found = 1;
return i;
}
}
found = 0;
return -1;

}

int bytes_take_named_int_value(char* str)
{
bytes_find(str);
if(!found) return 'none';
int value = bytes_dig_int();
if(found) return value;
else return 'none';

}

Re: improve that function (bytes_dig_int)

<86jzl72j9v.fsf@linuxsc.com>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=34981&group=comp.lang.c#34981

  copy link   Newsgroups: comp.lang.c
Path: i2pn2.org!i2pn.org!eternal-september.org!feeder3.eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail
From: tr.17...@z991.linuxsc.com (Tim Rentsch)
Newsgroups: comp.lang.c
Subject: Re: improve that function (bytes_dig_int)
Date: Mon, 08 Apr 2024 22:32:28 -0700
Organization: A noiseless patient Spider
Lines: 56
Message-ID: <86jzl72j9v.fsf@linuxsc.com>
References: <uukcop$3vhcj$1@i2pn2.org>
MIME-Version: 1.0
Content-Type: text/plain; charset=us-ascii
Injection-Date: Tue, 09 Apr 2024 05:32:31 +0200 (CEST)
Injection-Info: dont-email.me; posting-host="47558c70f7e061adbec47d824d7ff660";
logging-data="59695"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1/BjPZ6BHmWbtphXuUteQdeCrJ71TXg7X8="
User-Agent: Gnus/5.11 (Gnus v5.11) Emacs/22.4 (gnu/linux)
Cancel-Lock: sha1:WiWGLRz8kv/OS3DdmEvlu05tG8o=
sha1:Nl9SfWGkMzyBgk7XvFH2uKBymcU=
 by: Tim Rentsch - Tue, 9 Apr 2024 05:32 UTC

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.

Re: improve that function (bytes_dig_int)

<uvbg3i$sm6s$1@i2pn2.org>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=35071&group=comp.lang.c#35071

  copy link   Newsgroups: comp.lang.c
Path: i2pn2.org!.POSTED!not-for-mail
From: fir...@grunge.pl (fir)
Newsgroups: comp.lang.c
Subject: Re: improve that function (bytes_dig_int)
Date: Fri, 12 Apr 2024 16:22:41 +0200
Organization: i2pn2 (i2pn.org)
Message-ID: <uvbg3i$sm6s$1@i2pn2.org>
References: <uukcop$3vhcj$1@i2pn2.org> <86jzl72j9v.fsf@linuxsc.com>
MIME-Version: 1.0
Content-Type: text/plain; charset=ISO-8859-1; format=flowed
Content-Transfer-Encoding: 7bit
Injection-Date: Fri, 12 Apr 2024 14:22:43 -0000 (UTC)
Injection-Info: i2pn2.org;
logging-data="940252"; mail-complaints-to="usenet@i2pn2.org";
posting-account="+ydHcGjgSeBt3Wz3WTfKefUptpAWaXduqfw5xdfsuS0";
User-Agent: Mozilla/5.0 (Windows NT 5.1; rv:27.0) Gecko/20100101 Firefox/27.0 SeaMonkey/2.24
In-Reply-To: <86jzl72j9v.fsf@linuxsc.com>
X-Spam-Checker-Version: SpamAssassin 4.0.0
 by: fir - Fri, 12 Apr 2024 14:22 UTC

Tim Rentsch wrote:
> 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.
>

well ok..this is kinda tricky style but thats a matter of personal style
i prefer more "strightforward" long descriptive names etc

two remarks here
1) people shoudl avoid imo talking a word "global" becouse in
normal desctop computing at least those variables are not global but
typically belong to library (dll) so they are library scope/module
scope not global (this is my own remark as i noticed this) (thay may
also be exe scope but thsi is also module scope

so global dont much exist, though there is this problem that when same
symbol is uset in various libraries it may cause serious conflict

2) as to avaidingf global state i also thing would not agree

if the satae is "meaningfull" - like if variable name is really
meanigfull for given module - it could be used (i mean it should
be as meaningful as functions provided, than can be "global" (module
scoe) too imo by analogy

various "global" (id est module scope) arrays for exampel are quite
meaningfull so no problem them being module scope (and dont listen
to c++ ponys what they say imo, fuck them - in short - i would say ;c)

Re: improve that function (bytes_dig_int)

<867ch1z5j7.fsf@linuxsc.com>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=35081&group=comp.lang.c#35081

  copy link   Newsgroups: comp.lang.c
Path: i2pn2.org!i2pn.org!eternal-september.org!feeder3.eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail
From: tr.17...@z991.linuxsc.com (Tim Rentsch)
Newsgroups: comp.lang.c
Subject: Re: improve that function (bytes_dig_int)
Date: Sat, 13 Apr 2024 07:44:12 -0700
Organization: A noiseless patient Spider
Lines: 93
Message-ID: <867ch1z5j7.fsf@linuxsc.com>
References: <uukcop$3vhcj$1@i2pn2.org> <86jzl72j9v.fsf@linuxsc.com> <uvbg3i$sm6s$1@i2pn2.org>
MIME-Version: 1.0
Content-Type: text/plain; charset=us-ascii
Injection-Date: Sat, 13 Apr 2024 16:44:14 +0200 (CEST)
Injection-Info: dont-email.me; posting-host="44a1207aeaadfda1483c7b96a3737e57";
logging-data="3216084"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX193jdFUPj8VDx0RkkVjr6gqxpUfOhXPkOs="
User-Agent: Gnus/5.11 (Gnus v5.11) Emacs/22.4 (gnu/linux)
Cancel-Lock: sha1:f15XOPzy2r8aP6ugitwUTtK3ZBE=
sha1:Bfi6Ct7+uPYzyO9M1RTnR4yEgdg=
 by: Tim Rentsch - Sat, 13 Apr 2024 14:44 UTC

fir <fir@grunge.pl> writes:

> Tim Rentsch wrote:
>
>> 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.
>
> well ok..this is kinda tricky style but thats a matter of personal
> style i prefer more "strightforward" long descriptive names etc

The two aspects are related but my point was about the overall
structure of the algorithm more than about matters of syntactic
and lexical style.

> two remarks here
> 1) people shoudl avoid imo talking a word "global" becouse in
> normal desctop computing at least those variables are not global
> but typically belong to library (dll) so they are library
> scope/module scope not global (this is my own remark as i noticed
> this) (thay may also be exe scope but thsi is also module scope
> [...]

Yes the word global may have been misleading. What I meant
was any object whose lifetime outlives the duration of the
relevant function call(s). For example, in this function

int
whatever( char *s ){
static int something;
...
}

the variable 'something' continues past the point where the
function returns, even though its scope is confined to that of
the function (and so from that point of view it is not "global").
Unfortunately there isn't a common and convenient term that
conveys this meaning. Whatever it is we call it, this "duration
past the point of function return" is the property I was meaning
to identify and that in most cases should be avoided.

1
server_pubkey.txt

rocksolid light 0.9.8
clearnet tor