[sword-cvs] sword/include swbuf.h,1.18,1.19

sword@www.crosswire.org sword@www.crosswire.org
Wed, 16 Jul 2003 05:26:11 -0700


Update of /usr/local/cvsroot/sword/include
In directory www:/tmp/cvs-serv24686/include

Modified Files:
	swbuf.h 
Log Message:
Some very small optimizations


Index: swbuf.h
===================================================================
RCS file: /usr/local/cvsroot/sword/include/swbuf.h,v
retrieving revision 1.18
retrieving revision 1.19
diff -u -d -r1.18 -r1.19
--- swbuf.h	13 Jul 2003 16:00:46 -0000	1.18
+++ swbuf.h	16 Jul 2003 12:26:09 -0000	1.19
@@ -37,20 +37,33 @@
 class SWDLLEXPORT SWBuf {
 	char *buf;
 	char *end;
+	char *endAlloc;
 	char fillByte;
 	unsigned int allocSize;
 	static char *nullStr;
 	static char junkBuf[JUNKBUFSIZE];
 
+	inline void assureMore(unsigned int pastEnd) {
+		if (end+pastEnd>=endAlloc) {
+			int newsize = (end-buf)+pastEnd;
+			allocSize = newsize + 16;
+			long size = (end - buf);
+			buf = (char *)((buf) ? realloc(buf, allocSize) : calloc(allocSize, 1));
+			end = (buf + size);
+			endAlloc = buf + allocSize-1;
+		}
+	}
 	inline void assureSize(unsigned int newsize) {
 		if (newsize > allocSize) {
-			allocSize = newsize + 5;
+			allocSize = newsize + 16;
 			long size = (end - buf);
 			buf = (char *)((buf) ? realloc(buf, allocSize) : calloc(allocSize, 1));
 			end = (buf + size);
+			endAlloc = buf + allocSize-1;
 		}
 	}
-	void init();
+
+	void init(unsigned int initSize);
 
 public:
 	/**
@@ -58,21 +71,21 @@
  	* 		to a value from a const char *
  	*
  	*/
-	SWBuf(const char *initVal = 0);
+	SWBuf(const char *initVal = 0, unsigned int initSize = 0);
 
 	/**
 	* SWBuf Constructor - Creates an SWBuf initialized
 	* 		to a value from a char
 	*
 	*/
-	SWBuf(char initVal);
+	SWBuf(char initVal, unsigned int initSize = 0);
 
 	/**
 	* SWBuf Constructor - Creates an SWBuf initialized
 	* 		to a value from another SWBuf
 	*
 	*/
-	SWBuf(const SWBuf &other);
+	SWBuf(const SWBuf &other, unsigned int initSize = 0);
 
 	inline void setFillByte(char ch) { fillByte = ch; }
 	inline char getFillByte() { return fillByte; }
@@ -85,13 +98,14 @@
 	/**
 	* @return a pointer to the buffer content (null-terminated string)
 	*/
-	inline const char *c_str() const{	return buf;	}
+	inline const char *c_str() const{ return buf; }
 
 	/**
 	*	@param pos The position of the requested character.
 	* @return The character at the specified position
 	*/
 	inline char &charAt(unsigned int pos) { return ((pos <= (unsigned int)(end - buf)) ? buf[pos] : nullStr[0]); }
+//	inline char &charAt(unsigned int pos) { return ((buf+pos)<=end) ? buf[pos] : nullStr[0]; }
 
 	/** 
 	* size() and length() return only the number of characters of the string.
@@ -147,9 +161,8 @@
 	* @param ch Append this.
 	*/
 	inline void append(char ch) {
-		assureSize((end-buf) + 2);
-		*end = ch;
-		end++;
+		assureMore(1);
+		*end++ = ch;
 		*end = 0;
 	}