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

[PATCH] bite-sized task: update string types.

From: C. Scott Ananian <cananian_at_lesser-magoo.lcs.mit.edu>
Date: 2001-08-20 22:54:22 CEST

As a prologue to actually cleaning up the string usage in
svn_fs__get_prop() and friends, here is a patch to svn_string.[ch]
which adds the svn_string_* functions to parallel svn_stringbuf_*
functions. Most of the code is ripped directly from the existing
stringbuf functions; one *could* do some extremely sketchy tricks
with typcasting to use it directly, but I figured the duplication
probably gained enough in readability to be worth it.

Two very questionably function names: we need a stringbuf-to-string
and a string-to-stringbuf function. In the patch below these functions
are called
  svn_string_create_from_buf
and
  svn_stringbuf_create_from_string

I'm rather unhappy about these names, though. Any better suggestions?
 --s

SSBN 731 shotgun Pakistan algorithm non-violent protest Cocaine cryptographic
D5 SLBM Columbia Flintlock Nader assassination Yeltsin Washington
              ( http://lesser-magoo.lcs.mit.edu/~cananian )

Index: subversion/include/svn_string.h
===================================================================
RCS file: /usr/local/tigris/data/helm/cvs/repository/subversion/subversion/include/svn_string.h,v
retrieving revision 1.50
diff -u -p -r1.50 svn_string.h
--- subversion/include/svn_string.h 2001/06/13 09:10:32 1.50
+++ subversion/include/svn_string.h 2001/08/20 20:49:47
@@ -83,13 +83,71 @@ typedef struct
 
 
 
+/* svn_string_t functions. */
+
 /* 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 string with the contents of the given stringbuf */
+svn_string_t * svn_string_create_from_buf (const svn_stringbuf_t *strbuf,
+ 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,
+ ...)
+ __attribute__ ((format (printf, 2, 3)));
+
+/* Create a new bytestring by formatting CSTRING (null-terminated)
+ from a va_list (see svn_stringbuf_createf). */
+svn_string_t *svn_string_createv (apr_pool_t *pool,
+ const char *fmt,
+ va_list ap)
+ __attribute__ ((format (printf, 2, 0)));
+
+/* Return true if a bytestring is empty (has length zero). */
+svn_boolean_t svn_string_isempty (const svn_string_t *str);
+
+/* 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);
+
+
+/* svn_stringbuf_t functions. */
+
+/* Create a new bytestring containing a C string (null-terminated), or
+ containing a generic string of bytes (NON-null-terminated) */
 svn_stringbuf_t * svn_stringbuf_create (const char *cstring,
                                      apr_pool_t *pool);
 svn_stringbuf_t * svn_stringbuf_ncreate (const char *bytes,
                                       const apr_size_t size,
                                       apr_pool_t *pool);
+
+/* Create a new stringbuf with the contents of the given string */
+svn_stringbuf_t * svn_string_create_from_string (const svn_string_t *str,
+ apr_pool_t *pool);
 
 /* Create a new bytestring by formatting CSTRING (null-terminated)
    from varargs, which are as appropriate for apr_psprintf. */
Index: subversion/libsvn_subr/svn_string.c
===================================================================
RCS file: /usr/local/tigris/data/helm/cvs/repository/subversion/subversion/libsvn_subr/svn_string.c,v
retrieving revision 1.15
diff -u -p -r1.15 svn_string.c
--- subversion/libsvn_subr/svn_string.c 2001/06/13 09:10:34 1.15
+++ subversion/libsvn_subr/svn_string.c 2001/08/20 20:49:47
@@ -46,7 +46,177 @@ my__realloc (char *data, const apr_size_
   return new_area;
 }
 
-static svn_stringbuf_t *create_string (char *data, apr_size_t size,
+
+/* svn_string functions */
+
+static svn_string_t *create_string (const char *data, apr_size_t size,
+ apr_pool_t *pool)
+{
+ svn_string_t *new_string;
+
+ new_string = (svn_string_t *) apr_palloc (pool, sizeof (*new_string));
+
+ new_string->data = data;
+ new_string->len = size;
+
+ return new_string;
+}
+
+svn_string_t *
+svn_string_ncreate (const char *bytes, const apr_size_t size,
+ apr_pool_t *pool)
+{
+ char *data;
+
+ data = apr_palloc (pool, size + 1);
+ memcpy (data, bytes, size);
+
+ /* Null termination is the convention -- even if we suspect the data
+ to be binary, it's not up to us to decide, it's the caller's
+ call. Heck, that's why they call it the caller! */
+ data[size] = '\0';
+
+ /* wrap an svn_string_t around the new data */
+ return create_string (data, size, pool);
+}
+
+
+svn_string_t *
+svn_string_create (const char *cstring, apr_pool_t *pool)
+{
+ return svn_string_ncreate (cstring, strlen (cstring), pool);
+}
+
+
+svn_string_t *
+svn_string_create_from_buf (const svn_stringbuf_t *strbuf, apr_pool_t *pool)
+{
+ return svn_string_ncreate (strbuf->data, strbuf->len, pool);
+}
+
+
+svn_string_t *
+svn_string_createv (apr_pool_t *pool, const char *fmt, va_list ap)
+{
+ char *data = apr_pvsprintf (pool, fmt, ap);
+
+ /* wrap an svn_string_t around the new data */
+ return create_string (data, strlen (data), pool);
+}
+
+
+svn_string_t *
+svn_string_createf (apr_pool_t *pool, const char *fmt, ...)
+{
+ svn_string_t *str;
+
+ va_list ap;
+ va_start (ap, fmt);
+ str = svn_string_createv (pool, fmt, ap);
+ va_end (ap);
+
+ return str;
+}
+
+
+svn_boolean_t
+svn_string_isempty (const svn_string_t *str)
+{
+ return (str->len == 0);
+}
+
+
+svn_string_t *
+svn_string_dup (const svn_string_t *original_string, apr_pool_t *pool)
+{
+ return (svn_string_ncreate (original_string->data,
+ original_string->len, pool));
+}
+
+
+
+svn_boolean_t
+svn_string_compare (const svn_string_t *str1, const svn_string_t *str2)
+{
+ /* easy way out :) */
+ if (str1->len != str2->len)
+ return FALSE;
+
+ /* now that we know they have identical lengths... */
+
+ if (memcmp (str1->data, str2->data, str1->len))
+ return FALSE;
+ else
+ return TRUE;
+}
+
+
+
+apr_size_t
+svn_string_first_non_whitespace (const svn_string_t *str)
+{
+ apr_size_t i;
+
+ for (i = 0; i < str->len; i++)
+ {
+ if (! apr_isspace (str->data[i]))
+ {
+ return i;
+ }
+ }
+
+ /* if we get here, then the string must be entirely whitespace */
+ return (-1);
+}
+
+
+void
+svn_string_strip_whitespace (svn_string_t *str)
+{
+ apr_size_t i;
+
+ /* Find first non-whitespace character */
+ apr_size_t offset = svn_string_first_non_whitespace (str);
+
+ /* Go ahead! Waste some RAM, we've got pools! :) */
+ str->data += offset;
+ str->len -= offset;
+
+ /* Now that we've chomped whitespace off the front, search backwards
+ from the end for the first non-whitespace. */
+
+ for (i = (str->len - 1); i >= 0; i--)
+ {
+ if (! apr_isspace (str->data[i]))
+ {
+ break;
+ }
+ }
+
+ /* Mmm, waste some more RAM */
+ str->len = i + 1;
+}
+
+
+apr_size_t
+svn_string_find_char_backward (const svn_string_t *str, char ch)
+{
+ int i; /* signed! */
+
+ for (i = (str->len - 1); i >= 0; i--)
+ {
+ if (str->data[i] == ch)
+ return i;
+ }
+
+ return str->len;
+}
+
+
+
+/* svn_stringbuf functions */
+
+static svn_stringbuf_t *create_stringbuf (char *data, apr_size_t size,
                                     apr_pool_t *pool)
 {
   svn_stringbuf_t *new_string;
@@ -76,7 +246,7 @@ svn_stringbuf_ncreate (const char *bytes
   data[size] = '\0';
 
   /* wrap an svn_stringbuf_t around the new data */
- return create_string (data, size, pool);
+ return create_stringbuf (data, size, pool);
 }
 
 
@@ -88,12 +258,19 @@ svn_stringbuf_create (const char *cstrin
 
 
 svn_stringbuf_t *
+svn_stringbuf_create_from_string (const svn_string_t *str, apr_pool_t *pool)
+{
+ return svn_stringbuf_ncreate (str->data, str->len, pool);
+}
+
+
+svn_stringbuf_t *
 svn_stringbuf_createv (apr_pool_t *pool, const char *fmt, va_list ap)
 {
   char *data = apr_pvsprintf (pool, fmt, ap);
 
   /* wrap an svn_stringbuf_t around the new data */
- return create_string (data, strlen (data), pool);
+ return create_stringbuf (data, strlen (data), pool);
 }
 
 

---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@subversion.tigris.org
For additional commands, e-mail: dev-help@subversion.tigris.org
Received on Sat Oct 21 14:36:36 2006

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

This site is subject to the Apache Privacy Policy and the Apache Public Forum Archive Policy.