Index: trunk/src/lib/tinyxml/tinystr.cc
===================================================================
--- trunk/src/lib/tinyxml/tinystr.cc	(revision 5070)
+++ trunk/src/lib/tinyxml/tinystr.cc	(revision 5819)
@@ -3,15 +3,15 @@
 Original file by Yves Berquin.
 
-This software is provided 'as-is', without any express or implied 
-warranty. In no event will the authors be held liable for any 
+This software is provided 'as-is', without any express or implied
+warranty. In no event will the authors be held liable for any
 damages arising from the use of this software.
 
-Permission is granted to anyone to use this software for any 
-purpose, including commercial applications, and to alter it and 
+Permission is granted to anyone to use this software for any
+purpose, including commercial applications, and to alter it and
 redistribute it freely, subject to the following restrictions:
 
-1. The origin of this software must not be misrepresented; you must 
-not claim that you wrote the original software. If you use this 
-software in a product, an acknowledgment in the product documentation 
+1. The origin of this software must not be misrepresented; you must
+not claim that you wrote the original software. If you use this
+software in a product, an acknowledgment in the product documentation
 would be appreciated but is not required.
 
@@ -19,298 +19,95 @@
 must not be misrepresented as being the original software.
 
-3. This notice may not be removed or altered from any source 
+3. This notice may not be removed or altered from any source
 distribution.
 */
 
-#include "tinyxml.h"
+/*
+ * THIS FILE WAS ALTERED BY Tyge Løvset, 7. April 2005.
+ */
+
 
 #ifndef TIXML_USE_STL
 
-
-#include <stdlib.h>
-#include <string.h>
-#include <ctype.h>
-
 #include "tinystr.h"
 
-// TiXmlString constructor, based on a C string
-TiXmlString::TiXmlString (const char* instring)
+// Error value for find primitive
+const TiXmlString::size_type TiXmlString::npos = static_cast< size_type >(-1);
+
+// Null rep.
+TiXmlString::Rep TiXmlString::nullrep_ = { 0, 0, '\0' };
+
+
+void TiXmlString::reserve (size_type cap)
 {
-    size_t newlen;
-    char * newstring;
-
-    if (!instring)
-    {
-        allocated = 0;
-        cstring = NULL;
-        current_length = 0;
-        return;
-    }
-    newlen = strlen (instring) + 1;
-    newstring = new char [newlen];
-    memcpy (newstring, instring, newlen);
-    // strcpy (newstring, instring);
-    allocated = newlen;
-    cstring = newstring;
-    current_length = newlen - 1;
-}
-
-// TiXmlString copy constructor
-TiXmlString::TiXmlString (const TiXmlString& copy)
-{
-    size_t newlen;
-    char * newstring;
-
-	// Prevent copy to self!
-	if ( &copy == this )
-		return;
-
-    if (! copy . allocated)
-    {
-        allocated = 0;
-        cstring = NULL;
-        current_length = 0;
-        return;
-    }
-    newlen = copy . length () + 1;
-    newstring = new char [newlen];
-    // strcpy (newstring, copy . cstring);
-    memcpy (newstring, copy . cstring, newlen);
-    allocated = newlen;
-    cstring = newstring;
-    current_length = newlen - 1;
-}
-
-// TiXmlString = operator. Safe when assign own content
-void TiXmlString ::operator = (const char * content)
-{
-    size_t newlen;
-    char * newstring;
-
-    if (! content)
-    {
-        empty_it ();
-        return;
-    }
-    newlen = strlen (content) + 1;
-    newstring = new char [newlen];
-    // strcpy (newstring, content);
-    memcpy (newstring, content, newlen);
-    empty_it ();
-    allocated = newlen;
-    cstring = newstring;
-    current_length = newlen - 1;
-}
-
-// = operator. Safe when assign own content
-void TiXmlString ::operator = (const TiXmlString & copy)
-{
-    size_t newlen;
-    char * newstring;
-
-    if (! copy . length ())
-    {
-        empty_it ();
-        return;
-    }
-    newlen = copy . length () + 1;
-    newstring = new char [newlen];
-    // strcpy (newstring, copy . c_str ());
-    memcpy (newstring, copy . c_str (), newlen);
-    empty_it ();
-    allocated = newlen;
-    cstring = newstring;
-    current_length = newlen - 1;
+	if (cap > capacity())
+	{
+		TiXmlString tmp;
+		tmp.init(length(), cap);
+		memcpy(tmp.start(), data(), length());
+		swap(tmp);
+	}
 }
 
 
-// append a const char * to an existing TiXmlString
-void TiXmlString::append( const char* str, size_t len )
+TiXmlString& TiXmlString::assign(const char* str, size_type len)
 {
-    char * new_string;
-    size_t new_alloc, new_size, size_suffix;
-	
-	// don't use strlen - it can overrun the len passed in!
-	const char* p = str;
-	size_suffix = 0;
-
-	while ( *p && size_suffix < (unsigned)len )
+	size_type cap = capacity();
+	if (len > cap || cap > 3*(len + 8))
 	{
-		++p;
-		++size_suffix;
+		TiXmlString tmp;
+		tmp.init(len);
+		memcpy(tmp.start(), str, len);
+		swap(tmp);
 	}
-    if ( !size_suffix)
-        return;
-
-    new_size = length () + size_suffix + 1;
-    // check if we need to expand
-    if (new_size > allocated)
-    {
-        // compute new size
-        new_alloc = assign_new_size (new_size);
-
-        // allocate new buffer
-        new_string = new char [new_alloc];        
-        new_string [0] = 0;
-
-        // copy the previous allocated buffer into this one
-        if (allocated && cstring)
-            // strcpy (new_string, cstring);
-            memcpy (new_string, cstring, length ());
-
-        // append the suffix. It does exist, otherwize we wouldn't be expanding 
-        // strncat (new_string, str, len);
-        memcpy (new_string + length (), 
-                str,
-                size_suffix);
-
-        // return previsously allocated buffer if any
-        if (allocated && cstring)
-            delete [] cstring;
-
-        // update member variables
-        cstring = new_string;
-        allocated = new_alloc;
-    }
-    else
-    {
-        // we know we can safely append the new string
-        // strncat (cstring, str, len);
-        memcpy (cstring + length (), 
-                str,
-                size_suffix);
-    }
-    current_length = new_size - 1;
-    cstring [current_length] = 0;
+	else
+	{
+		memmove(start(), str, len);
+		set_size(len);
+	}
+	return *this;
 }
 
 
-// append a const char * to an existing TiXmlString
-void TiXmlString::append( const char * suffix )
+TiXmlString& TiXmlString::append(const char* str, size_type len)
 {
-    char * new_string;
-    size_t new_alloc, new_size;
-
-    new_size = length () + strlen (suffix) + 1;
-    // check if we need to expand
-    if (new_size > allocated)
-    {
-        // compute new size
-        new_alloc = assign_new_size (new_size);
-
-        // allocate new buffer
-        new_string = new char [new_alloc];        
-        new_string [0] = 0;
-
-        // copy the previous allocated buffer into this one
-        if (allocated && cstring)
-            memcpy (new_string, cstring, 1 + length ());
-            // strcpy (new_string, cstring);
-
-        // append the suffix. It does exist, otherwize we wouldn't be expanding 
-        // strcat (new_string, suffix);
-        memcpy (new_string + length (), 
-                suffix,
-                strlen (suffix) + 1);
-
-        // return previsously allocated buffer if any
-        if (allocated && cstring)
-            delete [] cstring;
-
-        // update member variables
-        cstring = new_string;
-        allocated = new_alloc;
-    }
-    else
-    {
-        // we know we can safely append the new string
-        // strcat (cstring, suffix);
-        memcpy (cstring + length (), 
-                suffix, 
-                strlen (suffix) + 1);
-    }
-    current_length = new_size - 1;
-}
-
-// Check for TiXmlString equuivalence
-//bool TiXmlString::operator == (const TiXmlString & compare) const
-//{
-//    return (! strcmp (c_str (), compare . c_str ()));
-//}
-
-//unsigned TiXmlString::length () const
-//{
-//    if (allocated)
-//        // return strlen (cstring);
-//        return current_length;
-//    return 0;
-//}
-
-
-unsigned TiXmlString::find (char tofind, unsigned offset) const
-{
-    char * lookup;
-
-    if (offset >= length ())
-        return (unsigned) notfound;
-    for (lookup = cstring + offset; * lookup; lookup++)
-        if (* lookup == tofind)
-            return (unsigned)(lookup - cstring);
-    return (unsigned) notfound;
+	size_type newsize = length() + len;
+	if (newsize > capacity())
+	{
+		reserve (newsize + capacity());
+	}
+	memmove(finish(), str, len);
+	set_size(newsize);
+	return *this;
 }
 
 
-bool TiXmlString::operator == (const TiXmlString & compare) const
+TiXmlString operator + (const TiXmlString & a, const TiXmlString & b)
 {
-	if ( allocated && compare.allocated )
-	{
-		assert( cstring );
-		assert( compare.cstring );
-		return ( strcmp( cstring, compare.cstring ) == 0 );
- 	}
-	else if ( length() == 0 && compare.length() == 0 )
-	{
-		return true;
-	}
-	return false;
+	TiXmlString tmp;
+	tmp.reserve(a.length() + b.length());
+	tmp += a;
+	tmp += b;
+	return tmp;
 }
 
-
-bool TiXmlString::operator == (const char* compare) const
+TiXmlString operator + (const TiXmlString & a, const char* b)
 {
-	if ( allocated && compare && *compare )
-	{
-		assert( cstring );
-		return ( strcmp( cstring, compare ) == 0 );
- 	}
-	else if ( length() == 0 && (!compare || !*compare ) )	// this is a little dubious, but try to duplicate behavior in other operator==
-	{
-		return true;
-	}
-	return false;	
+	TiXmlString tmp;
+	TiXmlString::size_type b_len = static_cast<TiXmlString::size_type>( strlen(b) );
+	tmp.reserve(a.length() + b_len);
+	tmp += a;
+	tmp.append(b, b_len);
+	return tmp;
 }
 
-
-bool TiXmlString::operator < (const TiXmlString & compare) const
+TiXmlString operator + (const char* a, const TiXmlString & b)
 {
-	if ( allocated && compare.allocated )
-	{
-		assert( cstring );
-		assert( compare.cstring );
-		return ( strcmp( cstring, compare.cstring ) > 0 );
- 	}
-	return false;
-}
-
-
-bool TiXmlString::operator > (const TiXmlString & compare) const
-{
-	if ( allocated && compare.allocated )
-	{
-		assert( cstring );
-		assert( compare.cstring );
-		return ( strcmp( cstring, compare.cstring ) < 0 );
- 	}
-	return false;
+	TiXmlString tmp;
+	TiXmlString::size_type a_len = static_cast<TiXmlString::size_type>( strlen(a) );
+	tmp.reserve(a_len + b.length());
+	tmp.append(a, a_len);
+	tmp += b;
+	return tmp;
 }
 
