Re: Faster StrNCpy - Mailing list pgsql-hackers

From Alvaro Herrera
Subject Re: Faster StrNCpy
Date
Msg-id 20060926204937.GA22101@alvh.no-ip.org
Whole thread Raw
In response to Re: Faster StrNCpy  (Martijn van Oosterhout <kleptog@svana.org>)
Responses Re: Faster StrNCpy  (Tom Lane <tgl@sss.pgh.pa.us>)
List pgsql-hackers
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


pgsql-hackers by date:

Previous
From: Martijn van Oosterhout
Date:
Subject: Re: Faster StrNCpy
Next
From: Tom Lane
Date:
Subject: Re: Faster StrNCpy