[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 1 part 1: svn_string functions.

From: C. Scott Ananian <cananian_at_lesser-magoo.lcs.mit.edu>
Date: 2001-09-05 21:32:16 CEST

The attached patch implements the first part of the first "bite-sized
task" (see additional comments from Karl) by making svn_string_t a
fully-featured type, with methods of its own. I'll do the rest of
issue #406 as well, but this is a prerequisite.
 --s

RNC Bejing Kennedy Morwenstow overthrow IDEA Nader DES Waco, Texas
OVER THE HORIZON RADAR MI5 Boston Cheney shotgun fissionable LA payment
              ( http://lesser-magoo.lcs.mit.edu/~cananian )

diff -ruHp svn.orig/trunk/subversion/include/svn_string.h svn/trunk/subversion/include/svn_string.h
--- svn.orig/trunk/subversion/include/svn_string.h Wed Sep 5 14:50:51 2001
+++ svn/trunk/subversion/include/svn_string.h Wed Sep 5 15:25:41 2001
@@ -87,6 +87,60 @@ 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,
@@ -94,6 +148,10 @@ svn_stringbuf_t * svn_stringbuf_create (
 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. */
diff -ruHp svn.orig/trunk/subversion/libsvn_subr/svn_string.c svn/trunk/subversion/libsvn_subr/svn_string.c
--- svn.orig/trunk/subversion/libsvn_subr/svn_string.c Wed Sep 5 14:50:51 2001
+++ svn/trunk/subversion/libsvn_subr/svn_string.c Wed Sep 5 15:25:41 2001
@@ -50,7 +50,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;
@@ -80,7 +250,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);
 }
 
 
@@ -92,12 +262,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:40 2006

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