Afficher un message
Vieux 17/10/2007, 16h00   #1
¬a\\/b
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Bug/Gross InEfficiency in HeathField's fgetline program

In data Mon, 15 Oct 2007 18:23:54 -0700, Peter Nilsson scrisse:
>Tor Rustad <tor_rus...@hotmail.com> wrote:
>> Richard Heathfield wrote:
>> > > What about the non-trivial case, where buffers overlap?
>> >
>> > I'm not entirely sure this can be done in standard C, at
>> > least not the obvious way, although I'd be glad to be proved
>> > wrong.

>>
>> Hmm.. haven't written it before, my first try would be:
>>
>> /* pre-condition: checking for overlapped objects */
>> N = strlen(s);
>> for (i=0; i<N; i++) {
>> for (j=0; j<N; j++)
>> assert(t + i != s + j);
>>
>> }

>
>Slightly less Grossly InEfficient ...
>
> /* test: if n bytes starting at s
> // overlaps n bytes starting at t
> */
> int mem_overlap(const void *s, const void *t, size_t n)
> {
> const char *u, *v;
> size_t m = n;
> if (n == 0) return (s == t);
> for (u = t; m--; u++) if (u == s) return 1;
> for (v = s; n--; v++) if (v == t) return 1;
> return 0;
> }



/* test: if n bytes starting at s
// overlaps n bytes starting at t
*/
/* assume a pointer has the same size of an int and one unsigned */
/* return 1 if error or overlap 0 otherwise */
int mem_overlap123(char* s, int n, char* t, int m)
{if(s==0|| t==0 || n<0 || m<0) return 0;

/* no array can have the address 0
if( ((int)s)>=0 &&((int)(s+n-1))<= 0) return 1;
if( ((int)t)>=0 &&((int)(t+n-1))<= 0) return 1;

if( ((int)s)<=0 &&((int)(s+n-1))>= 0) return 1;
if( ((int)t)<=0 &&((int)(t+n-1))>= 0) return 1;

/* s----- t------ || t----- s------*/
if( (unsigned)(s+n-1) < (unsigned) t) return 0;
if( (unsigned)(t+m-1) < (unsigned) s) return 0;
return 1;
}

not tested
how many errors do you see?

>
> /* test: if destination s
> // overlaps source string t
> */
> int str_overlap(const char *s, const char *t)
> {
> const char *u = t;
> do { if (u == s) return 1; } while (*u++);
> while (--u != t) if (++s == t) return 1;
> return 0;
> }

  Réponse avec citation
 
Page generated in 0,11394 seconds with 9 queries