Re: Faster StrNCpy

From: Alvaro Herrera <alvherre(at)commandprompt(dot)com>
To: Martijn van Oosterhout <kleptog(at)svana(dot)org>
Cc: Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>, pgsql-hackers(at)postgreSQL(dot)org
Subject: Re: Faster StrNCpy
Date: 2006-09-26 20:49:37
Message-ID: 20060926204937.GA22101@alvh.no-ip.org
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-hackers pgsql-patches

Martijn van Oosterhout wrote:
> On Tue, Sep 26, 2006 at 04:24:51PM -0400, Tom Lane wrote:
> > David Strong points out here
> > http://archives.postgresql.org/pgsql-hackers/2006-09/msg02071.php
> > that some popular implementations of strncpy(dst,src,n) are quite
> > inefficient when strlen(src) is much less than n, because they don't
> > optimize the zero-pad step that is required by the standard.
>
> I think that's why strlcpy was invented, to deal with the issues with
> strncpy.
>
> http://www.gratisoft.us/todd/papers/strlcpy.html
>
> There's an implementation here (used in glib), though you could
> probably find more.
>
> http://mail.gnome.org/archives/gtk-devel-list/2000-May/msg00029.html

That one would be LGPL (glib's license). Here is OpenBSD's version,
linked from that one:

ftp://ftp.openbsd.org/pub/OpenBSD/src/lib/libc/string/strlcpy.c

You'll notice that it iterates once per char. Between that and the
strlen() call in Tom's version, not sure which is the lesser evil.

--
Alvaro Herrera http://www.CommandPrompt.com/
PostgreSQL Replication, Consulting, Custom Development, 24x7 support

In response to

Responses

Browse pgsql-hackers by date

  From Date Subject
Next Message Tom Lane 2006-09-26 20:53:59 Re: Faster StrNCpy
Previous Message Martijn van Oosterhout 2006-09-26 20:40:18 Re: Faster StrNCpy

Browse pgsql-patches by date

  From Date Subject
Next Message Tom Lane 2006-09-26 20:53:59 Re: Faster StrNCpy
Previous Message Martijn van Oosterhout 2006-09-26 20:40:18 Re: Faster StrNCpy