[svn.haxx.se] · SVN Dev · SVN Users · SVN Org · TSVN Dev · TSVN Users · Subclipse Dev · Subclipse Users · this month's index

Re: UTF-8

From: Philip Martin <philip_at_codematters.co.uk>
Date: 2002-05-23 01:10:19 CEST

Marcus Comstedt <marcus@mc.pp.se> writes:

> +static svn_error_t *
> +svn_utf_convert_to_stringbuf (iconv_t cd,
> + const char *src_data, apr_size_t src_length,
> + svn_stringbuf_t **dest,
> + apr_pool_t *pool)
> +{
> + /* 2 bytes per character will be enough in most cases.
> + If not, we'll make a larger buffer and try again. */
> + apr_size_t buflen = src_length * 2 + 1;
> +
> + if (cd == (iconv_t)-1)
> + return svn_error_create (0, errno, NULL, pool, "recoding string");

Create the dest stringbuf here.

> +
> + do {
> +
> + char *destbuf = apr_palloc (pool, buflen);

Get rid of destbuf and do do svn_stringbuf_ensure instead of apr_palloc.

> +
> + /* Set up state variables for iconv */
> + const char *srcptr = src_data;
> + char *destptr = destbuf;

Use dest->data instead of destbuf.

> + size_t srclen = src_length;
> + size_t destlen = buflen;
> +
> + /* Attempt the conversion */
> + if (iconv(cd, &srcptr, &srclen, &destptr, &destlen) != (size_t)-1) {
> +
> + /* Conversion succeeded. Return buffer */
> + *dest = svn_stringbuf_ncreate (destbuf, buflen - destlen, pool);

And set dest->len here instead of creating a stringbuf.

> +
> + return SVN_NO_ERROR;
> + }
> +
> + /* In case we got here because the buffer was too small,
> + double the size for the next iteration... */
> + buflen *= 2;
> +
> + } while (errno == E2BIG);
> +
> + /* Some other error occured. */
> + return svn_error_create (0, errno, NULL, pool, "recoding string");
> +}
> +

This is a memory optimisation, I don't know if it is significant or
desirable, that depends on how often this function is called. It
optimises by avoiding the second allocation that currently occurs when
the stringbuf is created. In addition if APR ever provides a realloc
function then the svn_stringbuf_ensure call may be able to expand
in-place when E2BIG occurs.

-- 
Philip
---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@subversion.tigris.org
For additional commands, e-mail: dev-help@subversion.tigris.org
Received on Thu May 23 01:11:14 2002

This is an archived mail posted to the Subversion Dev mailing list.