Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.c > #30641

Re: strsup - supplementary string functions

From "BartC" <bc@freeuk.com>
Newsgroups comp.lang.c
References <2dc6b2f9-f029-43a0-b7e6-6258548a1a2f@googlegroups.com> <878v7jdnzj.fsf@temporary-address.org.uk> <9a56dea9-ecaa-495d-ab28-4366bf38c8bc@googlegroups.com>
Subject Re: strsup - supplementary string functions
Message-ID <zaAMs.26275$LO7.17163@fx02.fr7> (permalink)
Organization virginmedia.com
Date 2013-01-25 18:27 +0000

Show all headers | View raw


"Malcolm McLean" <malcolm.mclean5@btinternet.com> wrote in message 
news:9a56dea9-ecaa-495d-ab28-4366bf38c8bc@googlegroups.com...
> On Thursday, January 24, 2013 7:44:16 AM UTC, Dr Nick wrote:

>> Unless you change your mind and create (yet another, we've all done it)
>> structure based string type, here's a suggestion: have a very clear
>> convention in the naming as to which of your functions return the
>> original string, perhaps modified (convert case, trim the
>> right-hand-end) and which return a new string.  In fact, think hard
>> about this all over (should replace always create a new string, create a
>> new string when lengthening but not shortening, or realloc when
>> necessary?).
>>
> The function will all take nul-terminated strings passed as char *s or
> const char *s. The idea is to write functions that should have been in
> string.h but weren't, either because of perceived need, or because 
> string.h
> isn't allowed to depend on malloc.
>
> I think it's probably best to return a malloced char * where the return 
> may be larger than the input.

How will the caller know that? And in the case of a trim function, the 
result will never be bigger than the input, but the output may need to be 
stored elsewhere because of the need to zero-terminate a sub-string of the 
input.

As was mentioned, there are just too many ways of dealing with such 
functions: results can be in-place, in a caller-supplied destination, or in 
allocated memory.

Sometimes also, the caller has useful length information for a string, but 
the standard library doesn't often have no way to impart that information to 
the function; that would be handy to be able to do.

For ideas about new string functions, I mainly use the following:

o Convert a string to upper/lower case (some Cs will have this already). I 
also allow just the first N characters to be modified.

o Return the leftmost or rightmost N characters. When N is negative, then 
all *except* the abs(N) rightmost or leftmost characters are returned. When 
N is longer than the string, then it's padded with spaces, or a 
caller-supplied fill character (or string). (Some of this can be done, with 
a bit more trouble, with sprintf().)

o Split or join strings as I  think you've already mentioned.

o Several functions to deal with filespecs:  extract a path, filename, 
basefile, or extension; or to change or add an extension to a filespec.

-- 
Bartc 

Back to comp.lang.c | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread


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