View Single Post

   
  #6 (permalink)  
Old 02-16-2008, 04:19 AM
M Khomo
 
Posts: n/a
Default Re: strlen("NotDone'\0'Yet");

M Khomo wrote:
> Tom wrote:
>> If it's no bother to you MK, please post your "pointer swizzling
>> utilities." I
>> think others will appreciate that; I know I will.
>>

> Will do.
>

Here we go. Just the integer versions of strcmp, strchr and strlen
(istrcmp, strint and istrln respectively), and other obvious utilities.
They do linear searches only.

::::Begin Excerpts::::

int istrcmp(str1,str2)
int *str1, *str2;
{
while(*str1&&*str2&&*str1==*str2) str1++,str2++;
return (0+*str1)-*str2;
}


int *strint(str,chr)
int *str, chr;
{
while(*str&&*str!=chr) str++;
return !chr||*str ? str : (int*) 0;
}


istrln(str)
int *str;
{
int num = 0;
while(*str++) num++;
return num;
}


adigit(num) int num; {return num>='0'&&num<='9';}

alower(let) int let;
{return (let>='a'&&let<='i')||(let>='j'&&let<='r')||(let>= 's'&&let<='z');}

anupper(let) int let;
{return (let>='A'&&let<='I')||(let>='J'&&let<='R')||(let>= 'S'&&let<='Z');}

analpha(let) int let; {return alower(let)||anupper(let);}

analnum(chr) int chr; {return analpha(chr)||adigit(chr);}

::::End Excerpts::::

NB: There are many more non-obvious ones including conversions from int
to char strings and back, but they have application context that would
require too much explaining.

The other option would be to just donate the entire package to obsd
after normalizing the copyright to the obsd license. The actual
application is 'Arbitrary Precision Arithmetic Expression APAX(),
written long ago in K&R C.

Perhaps the sampling of conversions below should satisfy the curiosity
so the package becomes unnecessary.

::::::Begin Conversions::::::::

int *cstois(str)
char *str;
{
int *int1ptr(), *irun, *istr;
irun = istr = int1ptr(strlen(str)+1);
while(*irun++=(int)*str++);
return istr;
}


char *istocs(str)
int *str;
{
int nmem = istrln(str);
char *chr1ptr(), *crun, *cstr = chr1ptr(nmem+1);
crun = cstr;
while(*crun++=(char)*str++);
return cstr;
}

::::::: Allocation Utils::::::::

int *cpystr(str)
int *str;
{
int *copied, *run, *int1ptr();
run = copied = int1ptr(istrln(str)+1);
while(*run++ = *str++);
return copied;
}

char *chr1ptr(nmem)
int nmem;
{
char *ptr;
if((ptr = (char *) calloc(nmem,sizeof(char)))==NULL){
err_prnt("insufficient storage space for character string");
exit(1);
}
return ptr;
}



int *int1ptr(nmem)
int nmem;
{
int *ptr;
if((ptr = (int *) calloc(nmem,sizeof(int)))==(int*)0){
err_prnt("insufficient storage space for integer array");
exit(1);
}
return ptr;
}


::::::: Miscellaneous I/O::::::::

void
err_prnt(msg) char *msg;{fprintf(stderr,"%s\n",msg);}


:::End Excerpts:::





Reply With Quote