On Fri, Feb 23, 2001 at 04:45:52PM -0800, [EMAIL PROTECTED] wrote:
> On Fri, 23 Feb 2001, Jim Jagielski wrote:
> 
> > Greg Stein wrote:
> > >
> > > In the future, if we want to optimize away strlen() calls, then we should
> > > introduced a "counted string" type. Subversion has one, along with a bunch
> > > of support functions, which is APR pool-aware. We could snarf those if we
> > > chose. [and place them into APRUTIL]
> > >
> >
> > Hows the performance? Does it provide too much overheard for
> > it to be a generic solution? But I'm +1 for this addition.

Performance? Not a problem. They're visible structures:

/* The svn_string_t data type.  Pretty much what you expected. */
typedef struct svn_string_t
{
  char *data;                /* pointer to the bytestring */
  apr_size_t len;            /* length of bytestring */
  apr_size_t blocksize;      /* total size of buffer allocated */
  /* pool from which this string was originally allocated, and is not
     necessarily specific to this string.  This is used only for
     allocating more memory from when the string needs to grow.  */
  apr_pool_t *pool;          
} svn_string_t;
                    
We pass around "svn_string_t *" values. Accessing the data is simply
str->data. The string is guaranteed to be null-terminated, which simplifies
interop with OS functions. It is also counted, so things like memcpy() can
be used. (the null-term is not counted in "len")

Since it is counted, it is also useful for binary data.

> I'm +1 for it too.  I would put it directly into APR though.  Two string
> types, counted and regular.  But that's just an opinion.

Actually, a good one. I said APRUTIL simply because it's totally portable.
But APR itself would want to use this datatype, I'm sure.


That is two +1 votes, and a +1 from me. I've attached the header for review.

Cheers,
-g

-- 
Greg Stein, http://www.lyra.org/
/*
 * svn_string.h :  counted_length strings for Subversion
 *                 (using apr's memory pools)
 *
 * ====================================================================
 * Copyright (c) 2000 CollabNet.  All rights reserved.
 *
 * This software is licensed as described in the file COPYING, which
 * you should have received as part of this distribution.  The terms
 * are also available at http://subversion.tigris.org/license-1.html.
 * If newer versions of this license are posted there, you may use a
 * newer version instead, at your option.
 * ====================================================================
 */


#ifdef __cplusplus
extern "C" {
#endif /* __cplusplus */

#ifndef SVN_STRING_H
#define SVN_STRING_H

#include <apr_pools.h>       /* APR memory pools for everyone. */
#include <apr_strings.h>
#include "svn_types.h"



/* The svn_string_t data type.  Pretty much what you expected. */
typedef struct svn_string_t
{
  char *data;                /* pointer to the bytestring */
  apr_size_t len;            /* length of bytestring */
  apr_size_t blocksize;      /* total size of buffer allocated */
  /* pool from which this string was originally allocated, and is not
     necessarily specific to this string.  This is used only for
     allocating more memory from when the string needs to grow.  */
  apr_pool_t *pool;          
} svn_string_t;



/* Create a new bytestring containing a C string (null-terminated), or
   containing a generic string of bytes (NON-null-terminated) */
svn_string_t * svn_string_create (const char *cstring, 
                                  apr_pool_t *pool);
svn_string_t * svn_string_ncreate (const char *bytes, const apr_size_t size, 
                                   apr_pool_t *pool);

/* Create a new bytestring by formatting CSTRING (null-terminated)
   from varargs, which are as appropriate for apr_psprintf. */
svn_string_t *svn_string_createf (apr_pool_t *pool,
                                  const char *fmt,
                                  ...)
#ifdef __GNUC__
    __attribute__ ((format (printf, 2, 3)))
#endif /* __GNUC__ */
;

/* Create a new bytestring by formatting CSTRING (null-terminated)
   from a va_list (see svn_string_createf). */
svn_string_t *svn_string_createv (apr_pool_t *pool,
                                  const char *fmt,
                                  va_list ap)
#ifdef __GNUC__
    __attribute__ ((format (printf, 2, 0)))
#endif /* __GNUC__ */
;

/* Make sure that the string STR has at least MINIMUM_SIZE bytes of
   space available in the memory block.  (MINIMUM_SIZE should include
   space for the terminating null character.)  */
void svn_string_ensure (svn_string_t *str,
                        apr_size_t minimum_size);

/* Set a bytestring STR to empty (0 length). */
void svn_string_setempty (svn_string_t *str);

/* Return true if a bytestring is empty (has length zero). */
svn_boolean_t svn_string_isempty (const svn_string_t *str);

/* Chop NBYTES bytes off end of STR, but not more than STR->len. */
void svn_string_chop (svn_string_t *str, apr_size_t bytes);

/* Fill bytestring STR with character C. */
void svn_string_fillchar (svn_string_t *str, const unsigned char c);

/* Append either a string of bytes, an svn_string_t, or a C-string
   onto TARGETSTR.  reallocs() if necessary.  TARGETSTR is affected,
   nothing else is. */
void svn_string_appendbytes (svn_string_t *targetstr,
                             const char *bytes, 
                             const apr_size_t count);
void svn_string_appendstr (svn_string_t *targetstr, 
                           const svn_string_t *appendstr);
void svn_string_appendcstr (svn_string_t *targetstr,
                            const char *cstr);

/* Return a duplicate of ORIGNAL_STRING. */
svn_string_t *svn_string_dup (const svn_string_t *original_string,
                              apr_pool_t *pool);


/* Return TRUE iff STR1 and STR2 have identical length and data. */
svn_boolean_t svn_string_compare (const svn_string_t *str1, 
                                  const svn_string_t *str2);

/** convenience routines **/

/* Return offset of first non-whitespace character in STR, or -1 if none. */
apr_size_t svn_string_first_non_whitespace (const svn_string_t *str);

/* Strips whitespace from both sides of STR (modified in place). */
void svn_string_strip_whitespace (svn_string_t *str);

/* Return position of last occurrence of CHAR in STR, or return
   STR->len if no occurrence. */ 
apr_size_t svn_string_find_char_backward (const svn_string_t *str, char ch);

/* Chop STR back to CHAR, inclusive.  Returns number of chars
   chopped, so if no such CHAR in STR, chops nothing and returns 0. */
apr_size_t svn_string_chop_back_to_char (svn_string_t *str, char ch);
#endif  /* SVN_STRING_H */

#ifdef __cplusplus
}
#endif /* __cplusplus */


/* ----------------------------------------------------------------
 * local variables:
 * eval: (load-file "../svn-dev.el")
 * end:
 */

Reply via email to