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


Groups > comp.lang.c > #3665

Re: memmem()

From Dr Nick <3-nospam@temporary-address.org.uk>
Newsgroups comp.lang.c
Subject Re: memmem()
Date 2011-05-10 07:35 +0100
Message-ID <87mxivhxl5.fsf@temporary-address.org.uk> (permalink)
References <iq50jo$aue$1@dont-email.me> <87pqnrkel5.fsf@blp.benpfaff.org> <pan.2011.05.10.06.19.19.15000@nowhere.com>

Show all headers | View raw


Nobody <nobody@nowhere.com> writes:

> On Mon, 09 May 2011 09:45:26 -0700, Ben Pfaff wrote:
>
>>> Does anyone know of a POSIX-equivalent for GNU's memmem(), which
>>> searches for a sequence of bytes within a larger block of memory?
>> 
>> It's not hard to write such a function.  Have you considered
>> doing that?
>
> It's not hard to write something which works, but it's easy to end up with
> something which is far less efficient than it needs to be.

Do you need it now, or do you want to make sure your code is portable.
If you want it now then I think you do either have to write your own or
use GNU's if the license is compatible.

If you're "just" worried about the future, what I do is (putting the
relevant code for all such functions together in one header/source file
pair) do something like this:

use something external to C to check if the function is available
(autoconf in my case).
if it is, I conditionally #define another name, comfortably in user
namespace, for it
#define search_memory memmem
if not, #error

then a future porter "just" needs to put the prototype in the header
file where the #error is, and write the code in the C file and there he
is.

I've got "caseless_strcmp" in my code, built on strcasecmp (and, on
porting to Windows, stricmp)

Thusly:

#ifdef HAVE_STRCASECMP
  #define caseless_strcmp(x,y) strcasecmp(x,y)
#else
  #ifdef HAVE_STRICMP
    #define caseless_strcmp(x,y) stricmp(x,y)
  #else
    #error Need to implement caseless string comparison here
  #endif
#endif

So I push the problem off for the time being, avoid writing code I don't
need to, but don't leave anyone porting it in the future to a non GNU
system foundering with no idea of what has gone wrong.
-- 
Online waterways route planner            | http://canalplan.eu
Plan trips, see photos, check facilities  | http://canalplan.org.uk

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


Thread

memmem() barcaroller <barcaroller@music.net> - 2011-05-07 22:53 -0400
  Re: memmem() "Bill Cunningham" <nospam@nspam.invalid> - 2011-05-08 02:33 -0400
  Re: memmem() puppi <fabricio.puppi@gmail.com> - 2011-05-08 00:11 -0700
    Re: memmem() "Bill Cunningham" <nospam@nspam.invalid> - 2011-05-08 04:36 -0400
    Re: memmem() James Kuyper <jameskuyper@verizon.net> - 2011-05-08 09:33 -0400
      Re: memmem() barcaroller <barcaroller@music.net> - 2011-05-08 10:48 -0400
    Re: memmem() barcaroller <barcaroller@gmail.com> - 2011-05-08 10:46 -0400
      Re: memmem() Angel <angel+news@spamcop.net> - 2011-05-08 15:56 +0000
        Re: memmem() barcaroller <barcaroller@music.net> - 2011-05-08 12:03 -0400
          Re: memmem() puppi <fabricio.puppi@gmail.com> - 2011-05-09 04:58 -0700
            Re: memmem() Keith Thompson <kst-u@mib.org> - 2011-05-09 08:45 -0700
      Re: memmem() puppi <fabricio.puppi@gmail.com> - 2011-05-09 04:47 -0700
  Re: memmem() blp@cs.stanford.edu (Ben Pfaff) - 2011-05-09 09:45 -0700
    Re: memmem() Nobody <nobody@nowhere.com> - 2011-05-10 07:19 +0100
      Re: memmem() Dr Nick <3-nospam@temporary-address.org.uk> - 2011-05-10 07:35 +0100
        Re: memmem() pete <pfiland@mindspring.com> - 2011-05-10 04:27 -0400
      Re: memmem() Ben Pfaff <blp@cs.stanford.edu> - 2011-05-10 07:16 -0700
        Re: memmem() Nobody <nobody@nowhere.com> - 2011-05-10 21:07 +0100

csiph-web