Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
| From | JohnF <john@please.see.sig.for.email.com> |
|---|---|
| Newsgroups | comp.lang.c |
| Subject | Re: strsup - supplementary string functions |
| Date | 2013-01-25 10:57 +0000 |
| Organization | PANIX Public Access Internet and UNIX, NYC |
| Message-ID | <kdtoen$n4p$1@reader1.panix.com> (permalink) |
| References | <2dc6b2f9-f029-43a0-b7e6-6258548a1a2f@googlegroups.com> |
Malcolm McLean <malcolm.mclean5@btinternet.com> wrote:
> I'm putting together a little library of supplementary string functions,
> strsup.c.
>
> The functions are intended to be fairly short, and to operate on char *s.
> They should ideally be implementable as a single function which can be
> snipped and pasted.
>
> Unlike the string.h functions they can depend on malloc().
>
> The obvious first function is strdup().
>
> I've also got
>
> strcount - count the instances of character ch in str.
> trim - remove leadign and trailing whitespace from a string
> singlespace - repalce all runs of whitespace in a string with a single
> space character.
> split - return an array of fields, split on a delimiter character.
> compnumeric - compare two strings with embedded numbers.
> replace - replace all substrings with new passed string.
>
> Any ideas for more?
Some might be implemented as macros, especially the snipped-and-pasted
variety. Several snipped-and-pasted examples from my programs are below.
Some do little more than arg-checking, e.g., to avoid segfaulting if
used with a NULL ptr.
Want about ten zillion other ideas (some arguably a bit over-the-top)?
Look at the VMS documentation about lexical string functions in DCL
(DEC's shell DCL="digital command language"),
openvms.compaq.com/doc/73final/6489/6489pro_047.html#66_manipulatingstrings
/* ---
* macro to skip whitespace
* ------------------------ */
#define WHITESPACE " \t\n\r\f\v" /* skipped whitespace chars */
#define skipwhite(thisstr) if ( (thisstr) != NULL ) \
thisstr += strspn(thisstr,WHITESPACE)
/* ---
* macros to check if a string is empty
* ------------------------------------ */
#define isempty(s) ((s)==NULL?1:(*(s)=='\000'?1:0))
/* ---
* macro to strip leading and trailing whitespace
* ---------------------------------------------- */
#define trimwhite(thisstr) if ( (thisstr) != NULL ) { \
int thislen = strlen(thisstr); \
while ( --thislen >= 0 ) \
if ( isthischar((thisstr)[thislen],WHITESPACE) ) \
(thisstr)[thislen] = '\000'; \
else break; \
if ( (thislen = strspn((thisstr),WHITESPACE)) > 0 ) \
{strsqueeze((thisstr),thislen);} } else /*user adds ;*/
/* ---
* macro to remove all 'c' chars from s
* ------------------------------------ */
#define compress(s,c) if(!isempty(s)) /* remove embedded c's from s */ \
{ char *p; while((p=strchr((s),(c)))!=NULL) {strsqueeze(p,1);} } else
/* ---
* macro to strcpy(s,s+n) using memmove() (also works for negative n)
* ------------------------------------------------------------------ */
#define strsqueeze(s,n) if((n)!=0) { if(!isempty((s))) { \
int thislen3=strlen(s); \
if ((n) >= thislen3) *(s) = '\000'; \
else memmove((s),((s)+(n)),(1+thislen3-(n))); }} else /*user adds ;*/
/* ---
* macro to strncpy() n bytes and make sure it's null-terminated
* ------------------------------------------------------------- */
#define strninit(target,source,n) \
if( (target)!=NULL && (n)>=0 ) { \
char *thissource = (source); \
(target)[0] = '\000'; \
if ( (n)>0 && thissource!=NULL ) { \
strncpy((target),thissource,(n)); \
(target)[(n)] = '\000'; } }
/* ---
* macro to check for thischar inthisstr
* ------------------------------------- */
#define isthischar(thischar,inthisstr) \
( (thischar)!='\000' && *(inthisstr)!='\000' \
&& strchr(inthisstr,(thischar))!=(char *)NULL )
/* ---
* macro for last char of a string
* ------------------------------- */
#define lastchar(s) (isempty(s)?'\000':*((s)+(strlen(s)-1)))
--
John Forkosh ( mailto: j@f.com where j=john and f=forkosh )
Back to comp.lang.c | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread
strsup - supplementary string functions Malcolm McLean <malcolm.mclean5@btinternet.com> - 2013-01-22 10:14 -0800
Re: strsup - supplementary string functions Keith Thompson <kst-u@mib.org> - 2013-01-22 11:23 -0800
Re: strsup - supplementary string functions James Kuyper <jameskuyper@verizon.net> - 2013-01-22 14:34 -0500
Re: strsup - supplementary string functions Keith Thompson <kst-u@mib.org> - 2013-01-22 11:44 -0800
Re: strsup - supplementary string functions Johann Klammer <klammerj@NOSPAM.a1.net> - 2013-01-22 22:57 +0100
Re: strsup - supplementary string functions Jorgen Grahn <grahn+nntp@snipabacken.se> - 2013-01-23 21:13 +0000
Re: strsup - supplementary string functions Ben Bacarisse <ben.usenet@bsb.me.uk> - 2013-01-23 23:08 +0000
Re: strsup - supplementary string functions Bjorn Augestad <bjorn.augestad@gmail.com> - 2013-01-23 22:54 +0100
Re: strsup - supplementary string functions Dr Nick <nospam-4@temporary-address.org.uk> - 2013-01-24 07:44 +0000
Re: strsup - supplementary string functions Malcolm McLean <malcolm.mclean5@btinternet.com> - 2013-01-24 01:12 -0800
Re: strsup - supplementary string functions "BartC" <bc@freeuk.com> - 2013-01-25 18:27 +0000
Re: strsup - supplementary string functions Malcolm McLean <malcolm.mclean5@btinternet.com> - 2013-01-28 01:24 -0800
Re: strsup - supplementary string functions John McCue <jmccue@jmcbsd0.hsd1.ma.comcast.net> - 2013-01-24 22:28 +0000
Re: strsup - supplementary string functions JohnF <john@please.see.sig.for.email.com> - 2013-01-25 10:57 +0000
Re: strsup - supplementary string functions 88888 Dihedral <dihedral88888@googlemail.com> - 2013-01-25 12:45 -0800
Re: strsup - supplementary string functions ssmitch@gmail.com - 2013-01-29 08:17 -0800
Re: strsup - supplementary string functions Malcolm McLean <malcolm.mclean5@btinternet.com> - 2013-01-29 09:57 -0800
Re: strsup - supplementary string functions Ben Bacarisse <ben.usenet@bsb.me.uk> - 2013-01-30 03:46 +0000
Re: strsup - supplementary string functions Tim Rentsch <txr@alumni.caltech.edu> - 2013-01-31 14:50 -0800
Re: strsup - supplementary string functions Ben Bacarisse <ben.usenet@bsb.me.uk> - 2013-02-02 02:45 +0000
Re: strsup - supplementary string functions Tim Rentsch <txr@alumni.caltech.edu> - 2013-02-03 14:33 -0800
csiph-web