[geany/geany] 227b80: Grab the complete uctags vString implementation

Jiří Techet git-noreply at xxxxx
Mon Dec 17 21:05:28 UTC 2018


Branch:      refs/heads/master
Author:      Jiří Techet <techet at gmail.com>
Committer:   Jiří Techet <techet at gmail.com>
Date:        Thu, 06 Oct 2016 13:33:24 UTC
Commit:      227b8087993505348188fd75f22b9dcd74b50ee8
             https://github.com/geany/geany/commit/227b8087993505348188fd75f22b9dcd74b50ee8

Log Message:
-----------
Grab the complete uctags vString implementation

All the changes in the first half of .c/.h were introduced by me in uctags
(and thus they are guaranteed to be great ;-).

The following functions were missing in our ctags implementation and are
added by this commit (currently unused):

extern vString *vStringNewOrClear (vString *const string);
extern char    *vStringDeleteUnwrap (vString *const string);
extern void vStringCatSWithEscaping (vString* b, const char *s);
extern void vStringCatSWithEscapingAsPattern (vString *output, const char* input);

read.c has been updated to use vStringResize() instead of
vStringAutoResize().


Modified Paths:
--------------
    ctags/main/read.c
    ctags/main/vstring.c
    ctags/main/vstring.h

Modified: ctags/main/read.c
8 lines changed, 3 insertions(+), 5 deletions(-)
===================================================================
@@ -572,11 +572,9 @@ extern char *readLineRaw (vString *const vLine, MIO *const mio)
 					 *pLastChar != '\n'  &&  *pLastChar != '\r')
 			{
 				/*  buffer overflow */
-				reReadLine = vStringAutoResize (vLine);
-				if (reReadLine)
-					mio_seek (mio, startOfLine, SEEK_SET);
-				else
-					error (FATAL | PERROR, "input line too big; out of memory");
+				vStringResize (vLine, vStringSize (vLine) * 2);
+				mio_seek (mio, startOfLine, SEEK_SET);
+				reReadLine = true;
 			}
 			else
 			{


Modified: ctags/main/vstring.c
214 lines changed, 168 insertions(+), 46 deletions(-)
===================================================================
@@ -29,45 +29,33 @@ static const size_t vStringInitialSize = 32;
 *   FUNCTION DEFINITIONS
 */
 
-static void vStringResize (vString *const string, const size_t newSize)
-{
-	char *const newBuffer = xRealloc (string->buffer, newSize, char);
-
-	string->size = newSize;
-	string->buffer = newBuffer;
-}
-
 /*
 *   External interface
 */
 
-extern bool vStringAutoResize (vString *const string)
+extern void vStringResize (vString *const string, const size_t newSize)
 {
-	bool ok = true;
+	size_t size = vStringInitialSize;
 
-	if (string->size <= INT_MAX / 2)
-	{
-		const size_t newSize = string->size * 2;
+	while (size < newSize)
+		size *= 2;
 
-		vStringResize (string, newSize);
+	if (size > string->size)
+	{
+		string->size = size;
+		string->buffer = xRealloc (string->buffer, size, char);
 	}
-	return ok;
 }
 
 extern void vStringTruncate (vString *const string, const size_t length)
 {
 	Assert (length <= string->length);
 	string->length = length;
-	vStringPut(string, '\0');
+	string->buffer[string->length] = '\0';
 	DebugStatement ( memset (string->buffer + string->length, 0,
 	                         string->size - string->length); )
 }
 
-extern void vStringClear (vString *const string)
-{
-	vStringTruncate (string, 0);
-}
-
 extern void vStringDelete (vString *const string)
 {
 	if (string != NULL)
@@ -95,30 +83,14 @@ extern vString *vStringNew (void)
 extern void vStringPut (vString *const string, const int c)
 {
 	if (string->length + 1 == string->size)  /*  check for buffer overflow */
-		vStringAutoResize (string);
+		vStringResize (string, string->size * 2);
 
 	string->buffer [string->length] = c;
 	if (c != '\0')
 		string->buffer [++string->length] = '\0';
 }
 #endif
 
-extern void vStringCatS (vString *const string, const char *const s)
-{
-#if 1
-	const size_t len = strlen (s);
-	while (string->length + len + 1 >= string->size)/*  check for buffer overflow */
-		vStringAutoResize (string);
-	strcpy (string->buffer + string->length, s);
-	string->length += len;
-#else
-	const char *p = s;
-	do
-		vStringPut (string, *p);
-	while (*p++ != '\0');
-#endif
-}
-
 extern vString *vStringNewCopy (const vString *const string)
 {
 	vString *vs = vStringNew ();
@@ -133,25 +105,58 @@ extern vString *vStringNewInit (const char *const s)
 	return vs;
 }
 
+static void stringCat (
+		vString *const string, const char *const s, const size_t length)
+{
+	if (string->length + length + 1 > string->size)
+		vStringResize (string, string->length + length + 1);
+
+	strncpy (string->buffer + string->length, s, length);
+	string->length += length;
+	vStringPut (string, '\0');
+}
+
+extern void vStringNCat (
+		vString *const string, const vString *const s, const size_t length)
+{
+	size_t len = vStringLength (s);
+
+	len = len < length ? len: length;
+	stringCat (string, s->buffer, len);
+}
+
 extern void vStringNCatS (
 		vString *const string, const char *const s, const size_t length)
 {
-	const char *p = s;
-	size_t remain = length;
+	size_t len = strlen (s);
 
-	while (*p != '\0'  &&  remain > 0)
-	{
-		vStringPut (string, *p);
-		--remain;
-		++p;
-	}
+	len = len < length ? len : length;
+	stringCat (string, s, len);
+}
+
+extern void vStringCat (vString *const string, const vString *const s)
+{
+	size_t len = vStringLength (s);
+
+	stringCat (string, s->buffer, len);
+}
+
+extern void vStringCatS (vString *const string, const char *const s)
+{
+	size_t len = strlen (s);
+
+	stringCat (string, s, len);
 }
 
 /*  Strip trailing newline from string.
  */
 extern void vStringStripNewline (vString *const string)
 {
 	const size_t final = string->length - 1;
+
+	if (string->length == 0)
+		return;
+
 	if (string->buffer [final] == '\n')
 	{
 		string->buffer [final] = '\0';
@@ -196,12 +201,25 @@ extern void vStringChop (vString *const string)
 	}
 }
 
+extern void vStringCopy (vString *const string, const vString *const s)
+{
+	vStringClear (string);
+	vStringCat (string, s);
+}
+
 extern void vStringCopyS (vString *const string, const char *const s)
 {
 	vStringClear (string);
 	vStringCatS (string, s);
 }
 
+extern void vStringNCopy (
+		vString *const string, const vString *const s, const size_t length)
+{
+	vStringClear (string);
+	vStringNCat (string, s, length);
+}
+
 extern void vStringNCopyS (
 		vString *const string, const char *const s, const size_t length)
 {
@@ -242,3 +260,107 @@ extern vString *vStringNewOwn (char *s)
 
 	return r;
 }
+
+extern char    *vStringDeleteUnwrap       (vString *const string)
+{
+	char *buffer = NULL;
+
+
+	if (string != NULL)
+	{
+		buffer = string->buffer;
+		string->buffer = NULL;
+
+		string->size = 0;
+		string->length = 0;
+
+		eFree (string);
+	}
+
+	return buffer;
+}
+
+static char valueToXDigit (int v)
+{
+	Assert (v >= 0 && v <= 0xF);
+
+	if (v >= 0xA)
+		return 'A' + (v - 0xA);
+	else
+		return '0' + v;
+}
+
+extern void vStringCatSWithEscaping (vString* b, const char *s)
+{
+	for(; *s; s++)
+	{
+		int c = *s;
+
+		/* escape control characters (incl. \t) */
+		if ((c > 0x00 && c <= 0x1F) || c == 0x7F || c == '\\')
+		{
+			vStringPut (b, '\\');
+
+			switch (c)
+			{
+				/* use a short form for known escapes */
+			case '\a':
+				c = 'a'; break;
+			case '\b':
+				c = 'b'; break;
+			case '\t':
+				c = 't'; break;
+			case '\n':
+				c = 'n'; break;
+			case '\v':
+				c = 'v'; break;
+			case '\f':
+				c = 'f'; break;
+			case '\r':
+				c = 'r'; break;
+			case '\\':
+				c = '\\'; break;
+			default:
+				vStringPut (b, 'x');
+				vStringPut (b, valueToXDigit ((c & 0xF0) >> 4));
+				vStringPut (b, valueToXDigit (c & 0x0F));
+				continue;
+			}
+		}
+		vStringPut (b, c);
+	}
+}
+
+extern void vStringCatSWithEscapingAsPattern (vString *output, const char* input)
+{
+	while (*input)
+	{
+		switch (*input)
+		{
+		case '\\':
+			vStringPut(output, '\\');
+			vStringPut(output, '\\');
+			break;
+		case '/':
+			vStringPut(output, '\\');
+			vStringPut(output, '/');
+			break;
+		default:
+			vStringPut(output, *input);
+			break;
+
+		}
+		input++;
+	}
+}
+
+extern vString *vStringNewOrClear (vString *const string)
+{
+	if (string)
+	{
+		vStringClear (string);
+		return string;
+	}
+	else
+		return vStringNew ();
+}


Modified: ctags/main/vstring.h
29 lines changed, 22 insertions(+), 7 deletions(-)
===================================================================
@@ -18,6 +18,10 @@
 # include <stdlib.h>  /* to define size_t */
 #endif
 
+#include <stdio.h>
+
+#include "mio.h"
+
 /*
 *   MACROS
 */
@@ -26,7 +30,7 @@
 #endif
 #ifdef VSTRING_PUTC_MACRO
 #define vStringPut(s,c) \
-	(void)(((s)->length + 1 == (s)->size ? vStringAutoResize (s) : 0), \
+	(void)(((s)->length + 1 == (s)->size ? vStringResize ((s), (s)->size * 2) : 0), \
 	((s)->buffer [(s)->length] = (c)), \
 	((c) == '\0' ? 0 : ((s)->buffer [++(s)->length] = '\0')))
 #endif
@@ -36,13 +40,15 @@
 #define vStringLast(vs)       ((vs)->buffer[(vs)->length - 1])
 #define vStringLength(vs)     ((vs)->length)
 #define vStringSize(vs)       ((vs)->size)
-#define vStringCat(vs,s)      vStringCatS((vs), vStringValue((s)))
-#define vStringNCat(vs,s,l)   vStringNCatS((vs), vStringValue((s)), (l))
-#define vStringCopy(vs,s)     vStringCopyS((vs), vStringValue((s)))
-#define vStringNCopy(vs,s,l)  vStringNCopyS((vs), vStringValue((s)), (l))
 #define vStringChar(vs,i)     ((vs)->buffer[i])
 #define vStringLower(vs)      toLowerString((vs)->buffer)
 #define vStringUpper(vs)      toUpperString((vs)->buffer)
+#define vStringClear(string) \
+	do { \
+		vString *vStringClear_s = (string); \
+		vStringClear_s->length = 0; \
+		vStringClear_s->buffer[0] = '\0'; \
+	} while (false)
 
 /*
 *   DATA DECLARATIONS
@@ -57,8 +63,7 @@ typedef struct sVString {
 /*
 *   FUNCTION PROTOTYPES
 */
-extern bool vStringAutoResize (vString *const string);
-extern void vStringClear (vString *const string);
+extern void vStringResize (vString *const string, const size_t newSize);
 extern vString *vStringNew (void);
 extern void vStringDelete (vString *const string);
 #ifndef VSTRING_PUTC_MACRO
@@ -68,16 +73,26 @@ extern void vStringStripNewline (vString *const string);
 extern void vStringStripLeading (vString *const string);
 extern void vStringChop (vString *const string);
 extern void vStringStripTrailing (vString *const string);
+extern void vStringCat (vString *const string, const vString *const s);
 extern void vStringCatS (vString *const string, const char *const s);
+extern void vStringNCat (vString *const string, const vString *const s, const size_t length);
 extern void vStringNCatS (vString *const string, const char *const s, const size_t length);
 extern vString *vStringNewCopy (const vString *const string);
 extern vString *vStringNewInit (const char *const s);
+extern void vStringCopy (vString *const string, const vString *const s);
 extern void vStringCopyS (vString *const string, const char *const s);
+extern void vStringNCopy (vString *const string, const vString *const s, const size_t length);
 extern void vStringNCopyS (vString *const string, const char *const s, const size_t length);
 extern void vStringCopyToLower (vString *const dest, const vString *const src);
 extern void vStringSetLength (vString *const string);
 extern void vStringTruncate (vString *const string, const size_t length);
 
+extern vString *vStringNewOrClear (vString *const string);
+
 extern vString *vStringNewOwn (char *s);
+extern char    *vStringDeleteUnwrap (vString *const string);
+
+extern void vStringCatSWithEscaping (vString* b, const char *s);
+extern void vStringCatSWithEscapingAsPattern (vString *output, const char* input);
 
 #endif  /* CTAGS_MAIN_VSTRING_H */



--------------
This E-Mail was brought to you by github_commit_mail.py (Source: https://github.com/geany/infrastructure).


More information about the Commits mailing list