浏览代码

Reworked String to remove shared memory pools and just include hashes directly on the object. This makes the String class thread-safe, but still keeps it around double the speed of std::string for comparison operations.

Removed unnecessary functions from StringUtilities
Lloyd Weehuizen 15 年之前
父节点
当前提交
57047fe69e

+ 12 - 14
Include/Rocket/Core/StringBase.h

@@ -29,8 +29,6 @@
 #define ROCKETCORESTRINGBASE_H
 
 #include <Rocket/Core/Debug.h>
-#include <Rocket/Core/StringStorage.h>
-//#include <Rocket/Core/Types.h>
 
 namespace Rocket {
 namespace Core {
@@ -62,8 +60,13 @@ public:
 
 	/// The length of the string
 	inline size_type Length() const;
+	/// Get the hash value of this string
+	inline unsigned int Hash() const;
 	/// Access the string as a standard C string
 	inline const T* CString() const;
+	
+	/// Reserve space for at least this much data
+	inline void Reserve(size_type size);
 
 	/// Find the given string within this string
 	/// @param find The string to search for
@@ -183,22 +186,17 @@ public:
 
 protected:	
 
-	mutable T* value;
+	T* value;
+	size_type buffer_size;
 	size_type length;
-
-	mutable StringStorage::StringID string_id;
+	mutable unsigned int hash;
+	static const size_type LOCAL_BUFFER_SIZE = 8;
+	char local_buffer[LOCAL_BUFFER_SIZE];
 
 	size_type GetLength(const T* string) const;
 
-	// Ensure the string is now in the storage
-	inline void AddStorage() const;
-	// Modify this string, moving it out of storage if necessary
-	// By default, never shrink the memory allocation
-	inline void Modify(size_type new_size, bool shrink = false);
 	// Copies the source string to target string
-	inline void Copy(T* target, const T* src, size_type length, bool terminate = false) const;
-	// Release the string
-	inline void Release() const;
+	inline void Copy(T* target, const T* src, size_type length, bool terminate = false);
 
 	// Internal implementations of the public interfaces,
 	// all these functions take the length of the const T*'s they're
@@ -211,7 +209,7 @@ protected:
 	inline StringBase<T>& _Assign(const T* assign, size_type assign_length, size_type count = StringBase<T>::npos);
 	inline void _Insert(size_type index, const T* insert, size_type insert_length, size_type count = StringBase<T>::npos);
 };
-
+	
 #include <Rocket/Core/StringBase.inl>
 
 }

+ 115 - 140
Include/Rocket/Core/StringBase.inl

@@ -26,67 +26,57 @@
  */
 
 template< typename T >
-StringBase< T >::StringBase()
+StringBase< T >::StringBase() : value((T*)local_buffer), buffer_size(LOCAL_BUFFER_SIZE), length(0), hash(0)
 {
-	string_id = 0;
-	length = 0;
-	value = (T*)StringStorage::empty_string;
+	value[0] = 0;
 }
 
 template< typename T >
-StringBase< T >::StringBase(const StringBase< T >& copy)
+StringBase< T >::StringBase(const StringBase< T >& copy) : value((T*)local_buffer), buffer_size(LOCAL_BUFFER_SIZE), length(0), hash(0)
 {
-	string_id = 0;
-	length = 0;
-	value = (T*)StringStorage::empty_string;
+	value[0] = 0;
 	*this = copy;
 }
 
 template< typename T >
-StringBase< T >::StringBase(const T* string)
+StringBase< T >::StringBase(const T* string) : value((T*)local_buffer), buffer_size(LOCAL_BUFFER_SIZE), length(0), hash(0)
 {
-	string_id = 0;
-	length = 0;
-	value = (T*)StringStorage::empty_string;
+	value[0] = 0;
 	*this = string;
 }
 
 template< typename T >
-StringBase< T >::StringBase(const T* string_start, const T* string_end)
+StringBase< T >::StringBase(const T* string_start, const T* string_end) : value((T*)local_buffer), buffer_size(LOCAL_BUFFER_SIZE), length(0), hash(0)
 {
-	string_id = 0;
+	value[0] = 0;
 	length = (string_end - string_start);
 
-	if (length == 0)
+	if (length > 0)
 	{
-		value = (T*)StringStorage::empty_string;
-	}
-	else
-	{
-		value = (T*)StringStorage::ReallocString(NULL, 0, length, sizeof(T));
-		Copy(value, string_start, length, true);
+		Reserve(length);
+		Copy(value, string_start, length, true);		
 	}
 }
 
 template< typename T >
-StringBase< T >::StringBase(size_type count, const T character)
+StringBase< T >::StringBase(size_type count, const T character) : value((T*)local_buffer), buffer_size(LOCAL_BUFFER_SIZE), length(0), hash(0)
 {
-	string_id = 0;
+	value[0] = 0;
 	length = count;
 
-	value = (T*)StringStorage::ReallocString(NULL, 0, length, sizeof(T));
-	for (size_type i = 0; i < length; i++)
-		value[i] = character;
-	value[length] = 0;
+	if (length > 0)
+	{
+		Reserve(length);
+		for (size_type i = 0; i < length; i++)
+			value[i] = character;
+		value[length] = '\0';
+	}
 }
 
 template< typename T >
-StringBase< T >::StringBase(size_type ROCKET_UNUSED(max_length), const T* ROCKET_UNUSED(fmt), ...)
+StringBase< T >::StringBase(size_type ROCKET_UNUSED(max_length), const T* ROCKET_UNUSED(fmt), ...) : value((T*)local_buffer), buffer_size(LOCAL_BUFFER_SIZE), length(0), hash(0)
 {
-	string_id = 0;
-	length = 0;
-	value = (T*)StringStorage::empty_string;
-
+	value[0] = 0;
 	// Can't implement this at the base level, requires template specialisation
 	ROCKET_ERRORMSG("Not implemented.");
 }
@@ -94,7 +84,8 @@ StringBase< T >::StringBase(size_type ROCKET_UNUSED(max_length), const T* ROCKET
 template< typename T >
 StringBase< T >::~StringBase()
 {
-	Release();
+	if (value != (T*)local_buffer)
+		free(value);
 }
 
 template< typename T >
@@ -106,11 +97,13 @@ bool StringBase< T >::Empty() const
 template< typename T >
 void StringBase< T >::Clear()
 {
-	Release();
+	if (value != (T*)local_buffer)
+		free(value);
 
 	length = 0;
-	string_id = 0;
-	value = (T*)StringStorage::empty_string;
+	hash = 0;
+	value = (T*)local_buffer;
+	buffer_size = LOCAL_BUFFER_SIZE;
 }
 
 template< typename T >
@@ -119,12 +112,65 @@ typename StringBase< T >::size_type StringBase< T >::Length() const
 	return length;
 }
 
+template< typename T >
+unsigned int StringBase< T >::Hash() const
+{
+	if (hash == 0 && length > 0)
+	{
+		// FNV-1 hash algorithm
+		unsigned char* bp = (unsigned char *)value;	// start of buffer
+		unsigned char* be = (unsigned char *)value + (length * sizeof(T));
+		
+		// FNV-1a hash each octet in the buffer
+		while (bp < be) 
+		{
+			// xor the bottom with the current octet
+			hash ^= *bp++;
+			
+			/* multiply by the 32 bit FNV magic prime mod 2^32 */
+			#if !defined(__GNUC__)
+				const unsigned int FNV_32_PRIME = ((unsigned int)16777619);
+				hash *= FNV_32_PRIME;
+			#else
+				hash += (hash<<1) + (hash<<4) + (hash<<7) + (hash<<8) + (hash<<24);
+			#endif
+		}
+	}
+	return hash;
+}
+
 template< typename T >
 const T* StringBase< T >::CString() const
 {
 	return value;
 }
 
+template< typename T >
+void StringBase< T >::Reserve(size_type size)
+{
+	size_type new_size = (size + 1) * sizeof(T);
+	
+	if (buffer_size >= new_size)
+		return;
+	
+	// Pad out to a block of 16 bytes
+	const int BLOCK_SIZE = 16;
+	new_size = (new_size+BLOCK_SIZE-1)&(~(BLOCK_SIZE-1));
+	
+	buffer_size = new_size;
+	
+	if (value == (T*)local_buffer)
+	{
+		T* new_value = (T*)realloc(NULL, buffer_size);
+		Copy(new_value, (T*)local_buffer, LOCAL_BUFFER_SIZE / sizeof(T));
+		value = new_value;
+	}
+	else
+	{
+		value = (T*)realloc(value, buffer_size);
+	}
+}
+
 template< typename T >
 typename StringBase< T >::size_type StringBase< T >::Find(const T* find, size_type offset) const
 {
@@ -209,19 +255,7 @@ StringBase< T >& StringBase< T >::Assign(const T* assign, const T* end)
 template< typename T >
 StringBase< T >& StringBase< T >::Assign(const StringBase< T >& assign, size_type count)
 {
-	if (count == npos)
-	{
-		// We can do the complete assignment really fast as we're
-		// just reference counting
-		*this = assign;
-	}
-	else
-	{
-		// Do a normal (slow) assign
-		Assign(assign.CString(), count);
-	}
-
-	return *this;
+	return _Assign(assign.CString(), assign.length, count);
 }
 
 // Insert a string into this string
@@ -235,7 +269,7 @@ void StringBase< T >::Insert(size_type index, const T* insert, size_type count)
 template< typename T >
 void StringBase< T >::Insert(size_type index, const StringBase< T >& insert, size_type count)
 {
-	return _Insert(index, insert.CString(), insert.Length(), count);
+	return _Insert(index, insert.value, insert.length, count);
 }
 
 // Insert a character into this string
@@ -259,8 +293,7 @@ void StringBase< T >::Erase(size_type index, size_type count)
 	else
 	{
 		size_type erase_amount = count < length - index ? count : length - index;
-		
-		Modify(length);		
+			
 		Copy(&value[index], &value[index + erase_amount], length - index - erase_amount, true);		
 
 		length -= erase_amount;
@@ -280,8 +313,9 @@ int StringBase< T >::FormatString(size_type ROCKET_UNUSED(max_length), const T*
 template< typename T >
 void StringBase< T >::Resize(size_type new_length)
 {
-	Modify(new_length, true);
-	length = new_length;	
+	Reserve(new_length);
+	length = new_length;
+	value[length] = '\0';
 
 	if (length == 0)
 		Clear();
@@ -355,18 +389,23 @@ template< typename T >
 bool StringBase< T >::operator==(const T* compare) const
 {
 	size_type index = 0;
-	while (compare[index] && value[index] && compare[index] == value[index])
+	
+	while (index < length && compare[index] == value[index])
 		index++;
 
-	return index == length && compare[index] == 0;	
+	return index == length && compare[index] == '\0';	
 }
 
 template< typename T >
 bool StringBase< T >::operator==(const StringBase< T >& compare) const
 {
-	AddStorage();
-	compare.AddStorage();
-	return compare.string_id == string_id;
+	if (length != compare.length)
+		return false;
+	
+	if (Hash() != compare.Hash())
+		return false;
+		
+	return (*this) == compare.value;
 }
 
 template< typename T >
@@ -385,7 +424,7 @@ template< typename T >
 bool StringBase< T >::operator<(const T* compare) const
 {
 	size_type index = 0;
-	while (index < length && compare[index] && compare[index] == value[index])
+	while (index < length && compare[index] == value[index])
 		index++;
 
 	// Check if we reached the end of the string
@@ -427,15 +466,7 @@ StringBase< T >& StringBase< T >::operator=(const T* assign)
 template< typename T >
 StringBase< T >& StringBase< T >::operator=(const StringBase< T >& assign)
 {	
-	assign.AddStorage();
-	StringStorage::AddReference(assign.string_id);
-	
-	Release();
-	string_id = assign.string_id;
-	value = assign.value;
-	length = assign.length;
-
-	return *this;
+	return Assign(assign);
 }
 
 template< typename T >
@@ -465,7 +496,7 @@ StringBase< T >& StringBase< T >::operator+=(const T* add)
 template< typename T >
 StringBase< T >& StringBase< T >::operator+=(const StringBase< T >& add)
 {	
-	return Append(add.CString());
+	return _Append(add.CString(), add.length);
 }
 
 template< typename T >
@@ -502,55 +533,7 @@ typename StringBase< T >::size_type StringBase< T >::GetLength(const T* string)
 }
 
 template< typename T >
-void StringBase< T >::AddStorage() const
-{	
-	if (string_id > 0 || value == (T*)StringStorage::empty_string)
-		return;
-
-	const char* str = (const char*)value;
-	string_id = StringStorage::AddString(str, length, sizeof(T));
-	value = (T*)str;
-}
-
-template< typename T >
-void StringBase< T >::Modify(size_type new_size, bool shrink)
-{
-	T* new_value = value;
-	if (string_id > 0)
-	{
-		// If the string is in storage, we have to allocate a new buffer
-		// and copy the string into the new buffer (including NULL)
-		// Its up to the calling function to release it from storage when the
-		// modifcations are done
-		new_value = (T*)StringStorage::ReallocString(NULL, 0, new_size, sizeof(T));
-		Copy(new_value, value, new_size > length ? length : new_size, true);
-
-		// Release the old string value and assign the newly-allocated value as this string's value.
-		Release();
-		value = new_value;
-	}
-	else
-	{
-		// If we're not in storage and we're growing, do a realloc, otherwise we'll stay the same size
-		if (new_size > length)
-		{
-			new_value = (T*)StringStorage::ReallocString((char*)value, length, new_size, sizeof(T));
-			value = new_value;
-		}
-		else if (new_size < length && shrink)
-		{
-			new_value = (T*)StringStorage::ReallocString(NULL, 0, new_size, sizeof(T));
-			Copy(new_value, value, new_size, true);
-
-			// Release the old value and assign the newly-allocated value as this string's value.
-			Release();
-			value = new_value;
-		}
-	}
-}
-
-template< typename T >
-void StringBase< T >::Copy(T* target, const T* src, size_type length, bool terminate) const
+void StringBase< T >::Copy(T* target, const T* src, size_type length, bool terminate)
 {
 	// Copy values
 	for (size_type i = 0; i < length; i++)
@@ -564,22 +547,6 @@ void StringBase< T >::Copy(T* target, const T* src, size_type length, bool termi
 	}
 }
 
-template< typename T >
-void StringBase< T >::Release() const
-{
-	// If theres a valid string id remove the reference
-	// otherwise ask the storage to release our local buffer
-	if (string_id > 0)
-	{
-		StringStorage::RemoveReference(string_id);
-		string_id = 0;
-	}
-	else if (value != (T*)StringStorage::empty_string)
-	{
-		StringStorage::ReleaseString((char*)value, length);
-	}
-}
-
 template< typename T >
 typename StringBase< T >::size_type StringBase< T >::_Find(const T* find, size_type find_length, size_type offset) const
 {
@@ -674,6 +641,8 @@ StringBase< T > StringBase< T >::_Replace(const T* find, size_type find_length,
 		// Advance the find position
 		offset = pos + find_length;
 	}
+	
+	hash = 0;
 
 	return result;
 }
@@ -686,10 +655,12 @@ StringBase< T >& StringBase< T >::_Append(const T* append, size_type append_leng
 	if (add_length == 0)
 		return *this;
 
-	Modify(length + add_length);
+	Reserve(length + add_length);
 	Copy(&value[length], append, add_length, true);
 	length += add_length;
 	
+	hash = 0;
+	
 	return *this;
 }
 
@@ -700,17 +671,18 @@ StringBase< T >& StringBase< T >::_Assign(const T* assign, size_type assign_leng
 
 	if (new_length == 0)
 	{
-		Release();
-		value = (T*)StringStorage::empty_string;		
+		Clear();
 	}
 	else
 	{
-		Modify(new_length, true);
+		Reserve(new_length);
 		Copy(value, assign, new_length, true);
 	}
 
 	length = new_length;
 	
+	hash = 0;
+	
 	return *this;
 }
 
@@ -725,10 +697,13 @@ void StringBase< T >::_Insert(size_type index, const T* insert, size_type insert
 	
 	size_type add_length = count < insert_length ? count : insert_length;
 
-	Modify(length + add_length);
+	Reserve(length + add_length);
+	
 	for (size_type i = length + 1; i > index; i--)
 		value[i + add_length - 1] = value[i - 1];
 
 	Copy(&value[index], insert, add_length);
 	length += add_length;
+	
+	hash = 0;
 }

+ 0 - 77
Include/Rocket/Core/StringStorage.h

@@ -1,77 +0,0 @@
-/*
- * This source file is part of libRocket, the HTML/CSS Interface Middleware
- *
- * For the latest information, see http://www.librocket.com
- *
- * Copyright (c) 2008-2010 CodePoint Ltd, Shift Technology Ltd
- *
- * Permission is hereby granted, free of charge, to any person obtaining a copy
- * of this software and associated documentation files (the "Software"), to deal
- * in the Software without restriction, including without limitation the rights
- * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
- * copies of the Software, and to permit persons to whom the Software is
- * furnished to do so, subject to the following conditions:
- *
- * The above copyright notice and this permission notice shall be included in
- * all copies or substantial portions of the Software.
- * 
- * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
- * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
- * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
- * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
- * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
- * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
- * THE SOFTWARE.
- *
- */
-
-#ifndef ROCKETCORESTRINGSTORAGE_H
-#define ROCKETCORESTRINGSTORAGE_H
-
-#include <stddef.h>
-
-namespace Rocket {
-namespace Core {
-
-/**
-	Storage interface for StringBase
-	@author Lloyd Weehuizen
- */
-
-class ROCKETCORE_API StringStorage
-{
-public:
-	typedef void* StringID;
-	static char* empty_string;
-
-	/// Clears all shared strings from the string pools.
-	static void ClearPools();
-
-	/// Alloc/Realloc a string
-	static char* ReallocString(char* string, size_t old_length, size_t new_length, size_t character_size);
-	/// Release a previously allocated string
-	static void ReleaseString(char* string, size_t length);
-
-	/// Adds or increases the reference count on the given string
-	/// @param string[in,out] String to add to the storage, returns new address of string
-	/// @param string_length The length of the new string
-	/// @param character_size Number of bytes of an individual parameter	
-	static StringID AddString(const char* &string, size_t string_length, size_t character_size);
-	/// Adds a reference to the given string id
-	/// @param id Id of the string to add a reference to
-	static void AddReference(StringID id);
-	/// Removes a reference from the given string id
-	/// @param id Id of the string to remove a reference from
-	static void RemoveReference(StringID id);
-
-private:
-	/// Shutdown the storage pool
-	static void OnLibraryShutdown();
-
-	friend class LibraryMain;
-};
-
-}
-}
-
-#endif

+ 1 - 45
Include/Rocket/Core/StringUtilities.h

@@ -56,42 +56,11 @@ public:
 	/// @param[in] delimiter Delimiter to insert between the individual values.
 	static void JoinString(String& string, const StringList& string_list, const char delimiter = ',');
 
-	/// Hashes a string of data to an 32-character MD5 value.
-	/// @param[in] data Data to hash.
-	/// @param[in] length Length of the string to hash. If this is -1, the data will be interpreted as a C string.
-	/// @return MD5 hash of the data.
-	static String MD5Hash(const char* data, int length = -1);
 	/// Hashes a string of data to an integer value using the FNV algorithm.
 	/// @param[in] data Data to hash.
 	/// @param[in] length Length of the string to hash. If this is -1, the data will be interpreted as a C string.
 	/// @return Integer hash of the data.
-	static Hash FNVHash(const char* data);
-
-	/// Encodes a string with URL-encoding.
-	/// @param[in] input Input ASCII string to encode.
-	/// @param[in] input_length Length of the input string.
-	/// @param[out] output Output URL-encoded string.
-	/// @return True if the encoding was successful, false otherwise.
-	static bool URLEncode(const char* input, size_t input_length, String& output);
-	/// Decodes a URL-encoded string.
-	/// @param[in] input Input URL-encoded string.
-	/// @param[out] output Output buffer for the decoded characters.
-	/// @param[in] Length of the output buffer.
-	/// @return False if the decoding failed or the output buffer was too short, true otherwise.
-	static bool URLDecode(const String& input, char* output, size_t output_length);
-
-	/// Encodes a string with base64-encoding.
-	/// @param[in] input Input ASCII string to encode.
-	/// @param[in] input_length Length of the input string.
-	/// @param[out] output Output base-64 encoded string.
-	/// @return True if the encoding was successful, false otherwise.
-	static bool Base64Encode(const char* input, size_t input_length, String& output);
-	/// Decodes a base64-encoded string.
-	/// @param[in] input Input base-64 encoded string.
-	/// @param[out] output Output buffer for the decoded characters.
-	/// @param[in] Length of the output buffer.
-	/// @return False if the decoding failed or the output buffer was too short, true otherwise.
-	static bool Base64Decode(const String& input, char* output, size_t output_length);
+	static Hash FNVHash(const char* data, int length = -1);
 
 	/// Converts a character array in UTF-8 encoding to a vector of words. The UCS-2 words will be encoded as
 	/// either big- or little-endian, depending on the host processor.
@@ -127,19 +96,6 @@ public:
 	/// @return The stripped string.
 	static String StripWhitespace(const String& string);
 
-	struct ROCKETCORE_API ArgumentState
-	{
-		ArgumentState();
-
-		int index;
-		char option;
-		const char* argument;
-		bool display_errors;
-	};
-
-	/// getopt program argument processing.
-	static int GetOpt(int nargc, char* nargv[], char* optstring, ArgumentState& arg_state);
-
 	/// Operator for STL containers using strings.
 	struct ROCKETCORE_API StringComparei
 	{

+ 7 - 7
Include/Rocket/Core/Types.h

@@ -28,14 +28,20 @@
 #ifndef ROCKETCORETYPES_H
 #define ROCKETCORETYPES_H
 
-#include <Rocket/Core/Debug.h>
+// Define NULL as zero.
+#if !defined NULL
+#define NULL 0
+#endif
+
 #include <float.h>
 #include <limits.h>
 #include <string>
 #include <map>
 #include <set>
 #include <vector>
+
 #include <Rocket/Core/Platform.h>
+#include <Rocket/Core/Debug.h>
 
 namespace Rocket {
 namespace Core {
@@ -46,7 +52,6 @@ typedef unsigned short word;
 typedef double Time;
 typedef float TimeDelta;
 typedef unsigned int Hash;
-typedef unsigned short TypeHash;
 typedef void* ScriptObject;
 
 }
@@ -65,11 +70,6 @@ typedef unsigned __int64 uint64_t;
 namespace Rocket {
 namespace Core {
 
-// Define NULL as zero.
-#if !defined NULL
-#define NULL 0
-#endif
-
 // Default colour types.
 typedef Colour< float, 1 > Colourf;
 typedef Colour< byte, 255 > Colourb;

+ 45 - 33
Source/Core/String.cpp

@@ -59,12 +59,8 @@ int ROCKETCORE_API RocketStringFormatString(StringBase<char>& string, int max_si
 }
 
 template <>
-StringBase<char>::StringBase(StringBase<char>::size_type max_size, const char* fmt, ...)
+StringBase<char>::StringBase(StringBase<char>::size_type max_size, const char* fmt, ...) : value(local_buffer), buffer_size(LOCAL_BUFFER_SIZE), length(0), hash(0) 
 {
-	string_id = 0;
-	length = 0;
-	value = StringStorage::empty_string;
-
 	va_list argument_list;
 	va_start(argument_list, fmt);
 
@@ -93,10 +89,12 @@ String operator+(const char* cstring, const String& string)
 
 //#define ENABLE_STRING_TESTS
 #ifdef ENABLE_STRING_TESTS
-#include <Rocket/Core/STL/string>
-#include <SYSClock.h>
-ROCKETCORE_API void EMPStringTests()
+#include <string>
+#include "Rocket/Core/SystemInterface.h"
+ROCKETCORE_API void StringTests()
 {
+	SystemInterface* sys = Rocket::Core::GetSystemInterface();
+	
 	std::string ss = "test";
 	String es = "test";
 
@@ -108,8 +106,9 @@ ROCKETCORE_API void EMPStringTests()
 
 	String sub1 = es.Replace("lo", "l");
 	sub1 = sub1.Replace("h", "!");
+	ROCKET_ASSERT(sub1 == "!el");
 
-	EMPTime start;
+	Time start;
 
 	{
 		// Create a few free buffers
@@ -118,35 +117,35 @@ ROCKETCORE_API void EMPStringTests()
 		String tempstring2("buffer2");
 	}	
 
-	start = SYSClock::GetRealTime();
+	start = sys->GetElapsedTime();
 	for (int i = 0; i < 100000; i++)
 	{
 		std::string str("test");	
 	}
-	Log::Message(LC_CORE, Log::LT_ALWAYS, "SS Assign Short: %f", SYSClock::GetRealTime() - start);
+	printf( "SS Assign Short: %f\n", sys->GetElapsedTime() - start);
 	
-	start = SYSClock::GetRealTime();	
+	start = sys->GetElapsedTime();	
 	for (int i = 0; i < 100000; i++)
 	{
 		String str("test");
 	}
-	Log::Message(LC_CORE, Log::LT_ALWAYS, "ES Assign Short: %f", SYSClock::GetRealTime() - start);
+	printf( "ES Assign Short: %f\n", sys->GetElapsedTime() - start);
 
-	start = SYSClock::GetRealTime();
+	start = sys->GetElapsedTime();
 	for (int i = 0; i < 100000; i++)
 	{
 		std::string str("test this really long string that won't fit in a local buffer");	
 	}
-	Log::Message(LC_CORE, Log::LT_ALWAYS, "SS Assign Long: %f", SYSClock::GetRealTime() - start);
+	printf( "SS Assign Long: %f\n", sys->GetElapsedTime() - start);
 	
-	start = SYSClock::GetRealTime();	
+	start = sys->GetElapsedTime();	
 	for (int i = 0; i < 100000; i++)
 	{
 		String str("test this really long string that won't fit in a local buffer");
 	}
-	Log::Message(LC_CORE, Log::LT_ALWAYS, "ES Assign Long: %f", SYSClock::GetRealTime() - start);
+	printf( "ES Assign Long: %f\n", sys->GetElapsedTime() - start);
 
-	start = SYSClock::GetRealTime();
+	start = sys->GetElapsedTime();
 	for (int i = 0; i < 100000; i++)
 	{
 		if (ss == "hello")
@@ -154,20 +153,26 @@ ROCKETCORE_API void EMPStringTests()
 			int bob = 10;
 		}
 	}
-	Log::Message(LC_CORE, Log::LT_ALWAYS, "SS Compare: %f (char*)", SYSClock::GetRealTime() - start);
+	printf( "SS Compare: %f (char*)\n", sys->GetElapsedTime() - start);
 
-	std::string compare = "hello";
-	start = SYSClock::GetRealTime();
+	ss = "bo1";
+	std::string oss = ss;
+	std::string nss = "bob";
+	start = sys->GetElapsedTime();
 	for (int i = 0; i < 100000; i++)
 	{
-		if (ss == compare)
+		//if (ss == oss)
+		{
+			int bob = 10;
+		}
+		if (ss == nss)
 		{
 			int bob = 10;
 		}
 	}
-	Log::Message(LC_CORE, Log::LT_ALWAYS, "SS Compare: %f (std::string)", SYSClock::GetRealTime() - start);
+	printf( "SS Compare: %f (std::string)\n", sys->GetElapsedTime() - start);
 
-	start = SYSClock::GetRealTime();
+	start = sys->GetElapsedTime();
 	for (int i = 0; i < 100000; i++)
 	{
 		if (es == "hello")
@@ -175,38 +180,45 @@ ROCKETCORE_API void EMPStringTests()
 			int bob = 10;
 		}
 	}
-	Log::Message(LC_CORE, Log::LT_ALWAYS, "ES Compare: %f (char*)", SYSClock::GetRealTime() - start);
+	printf( "ES Compare: %f (char*)\n", sys->GetElapsedTime() - start);
 	
+	es = "bo1";
 	String oes = es;
-	start = SYSClock::GetRealTime();
+	String nes = "bob";
+	start = sys->GetElapsedTime();
 	for (int i = 0; i < 100000; i++)
 	{
-		if (es == oes)
+		//if (es == oes)
+		{
+			int bob = 10;
+		}
+		
+		if (nes == oes)
 		{
 			int bob = 10;
 		}
 	}
-	Log::Message(LC_CORE, Log::LT_ALWAYS, "ES Compare: %f (String)", SYSClock::GetRealTime() - start);
+	printf( "ES Compare: %f (String)\n", sys->GetElapsedTime() - start);
 
-	start = SYSClock::GetRealTime();
+	start = sys->GetElapsedTime();
 	std::string ss_concat = "hello";
 	for (int i = 0; i < 100000; i++)
 	{
 		ss_concat += "y";
 	}
-	Log::Message(LC_CORE, Log::LT_ALWAYS, "SS +=: %f", SYSClock::GetRealTime() - start);
+	printf( "SS +=: %f\n", sys->GetElapsedTime() - start);
 
 	String es_concat = "hello";
-	start = SYSClock::GetRealTime();
+	start = sys->GetElapsedTime();
 	for (int i = 0; i < 100000; i++)
 	{
-		if (i == 1016)
+		if (i == 42)
 		{
 			int bob = 10;
 		}
 		es_concat += "y";
 	}
-	Log::Message(LC_CORE, Log::LT_ALWAYS, "ES +=: %f", SYSClock::GetRealTime() - start);
+	printf( "ES +=: %f\n", sys->GetElapsedTime() - start);
 
 	const char* x1 = "bob";
 	String s;

+ 0 - 317
Source/Core/StringStorage.cpp

@@ -1,317 +0,0 @@
-/*
- * This source file is part of libRocket, the HTML/CSS Interface Middleware
- *
- * For the latest information, see http://www.librocket.com
- *
- * Copyright (c) 2008-2010 CodePoint Ltd, Shift Technology Ltd
- *
- * Permission is hereby granted, free of charge, to any person obtaining a copy
- * of this software and associated documentation files (the "Software"), to deal
- * in the Software without restriction, including without limitation the rights
- * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
- * copies of the Software, and to permit persons to whom the Software is
- * furnished to do so, subject to the following conditions:
- *
- * The above copyright notice and this permission notice shall be included in
- * all copies or substantial portions of the Software.
- * 
- * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
- * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
- * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
- * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
- * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
- * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
- * THE SOFTWARE.
- *
- */
-
-#include "precompiled.h"
-#include <Rocket/Core/StringStorage.h>
-
-namespace Rocket {
-namespace Core {
-
-const int MIN_STRING = 16;		// Smallest string size.
-const int NUM_POOLS = 4;		// Number of pools we have, in powers of 2 starting from the smallest string (16, 32, 64 and 128)
-const int ROCKET_MAX_POOL_SIZE = 128;	// Maximum number of free strings to keep in a pool
-const int ROCKET_MAX_HASH_LENGTH = 32;	// Maximum number of character to use when calculating the string hash (saves hashing HUGE strings)
-
-#define HASH(hval, string, length)										\
-{																		\
-	hval = 0;															\
-	unsigned char *bp = (unsigned char *)string;						\
-	unsigned char *be = bp + length;									\
-																		\
-	while (bp < be)														\
-	{																	\
-		hval += (hval<<1) + (hval<<4) + (hval<<7) + (hval<<8) + (hval<<24);	\
-		hval ^= *bp++;													\
-	}																	\
-}
-
-
-struct StringEntry
-{
-	char* buffer;
-	size_t reference_count;
-	StringEntry* next;
-	StringEntry* prev;
-	Hash hash;
-};
-
-struct Storage
-{
-	typedef std::map<Hash, StringEntry*> Lookup;
-	Lookup lookup;
-
-	typedef std::list< char* > StringPool;
-	StringPool pools[NUM_POOLS];
-};
-
-static char empty_string_buffer[] = {0, 0, 0, 0};
-char* StringStorage::empty_string = empty_string_buffer;
-static Storage* storage = NULL;
-
-#define BLOCK_SIZE(string_size) (string_size >= 1024 ? 1024 : (string_size >= 128 ? 128 : (string_size > MIN_STRING ? Math::ToPowerOfTwo(string_size) : MIN_STRING)))
-#define ALLOC_SIZE(string_size) (((string_size / BLOCK_SIZE(string_size)) + 1) * BLOCK_SIZE(string_size))
-
-// Clears all shared strings from the string pools.
-void StringStorage::ClearPools()
-{
-	for (int i = 0; i < NUM_POOLS; ++i)
-	{
-		for (Storage::StringPool::iterator iterator = storage->pools[i].begin(); iterator != storage->pools[i].end(); ++iterator)
-			free(*iterator);
-
-		storage->pools[i].clear();
-	}
-}
-
-char* StringStorage::ReallocString(char* string, size_t old_length, size_t new_length, size_t character_size)
-{
-	ROCKET_ASSERT(old_length < (1 << 24));
-	ROCKET_ASSERT(new_length < (1 << 24));
-	ROCKET_ASSERT(new_length >= old_length);
-	size_t new_size = (new_length + 1) * character_size;
-
-	if (string == empty_string)
-	{
-		string = NULL;
-	}
-	else if (string != NULL)
-	{
-		size_t old_size = (old_length + 1) * character_size;		
-		if (new_size < ALLOC_SIZE(old_size))
-			return string;
-	}
-
-	size_t alloc_size = ALLOC_SIZE(new_size);
-	ROCKET_ASSERT(alloc_size > new_size);
-
-	// Check if we can use an old allocation from our pools
-	if (alloc_size < (MIN_STRING << NUM_POOLS))
-	{
-		// Ensure we have storage for strings
-		if (!storage)
-			storage = new Storage();
-
-		size_t pool_index = 0;
-		size_t size = alloc_size;
-		while (size > MIN_STRING)
-		{
-			pool_index++;
-			size = size >> 1;
-		}
-		ROCKET_ASSERT(pool_index < NUM_POOLS);
-
-		if (!storage->pools[pool_index].empty())
-		{
-			char* new_string = storage->pools[pool_index].front();
-			storage->pools[pool_index].pop_front();
-			if (string)
-			{
-				// Copy the correct number of values across and null terminate
-				size_t copy_size = (old_length < new_length ? old_length : new_length) * character_size;
-				memcpy(new_string, string, copy_size);
-				memset(&new_string[copy_size], 0, character_size);
-
-				ReleaseString(string, old_length);
-			}
-			return new_string;
-		}
-	}	
-
-	// Standard realloc
-	return (char*) realloc(string, alloc_size);
-}
-
-void StringStorage::ReleaseString(char* string, size_t size)
-{
-	if (string == empty_string)
-		return;
-
-	if (storage == NULL)
-	{
-		free(string);
-		return;
-	}
-
-	size_t alloc_size = ALLOC_SIZE(size);
-	if (alloc_size < (MIN_STRING << NUM_POOLS))
-	{
-		size_t pool_index = 0;
-		size_t size = alloc_size;
-		while (size > MIN_STRING)
-		{
-			pool_index++;
-			size = size >> 1;
-		}
-		ROCKET_ASSERT(pool_index < NUM_POOLS);
-
-		if (storage->pools[pool_index].size() < ROCKET_MAX_POOL_SIZE)
-		{
-			storage->pools[pool_index].push_back(string);
-			return;
-		}
-	}
-
-	free(string);
-}
-
-StringStorage::StringID StringStorage::AddString(const char* &string, size_t string_length, size_t character_size)
-{
-	size_t length = string_length * character_size;
-
-	// Ensure we have storage for strings
-	if (!storage)
-		storage = new Storage();
-
-	// Hash the incoming string
-	Hash hash;
-	size_t hash_length = (length < ROCKET_MAX_HASH_LENGTH ? length : ROCKET_MAX_HASH_LENGTH);
-	HASH(hash, string, hash_length);		
-
-	// See if we can find the entry group for this hash ( strings with the same hash )
-	Storage::Lookup::iterator itr = storage->lookup.find(hash);
-	
-	StringEntry* entry_group = NULL;
-	if (itr != storage->lookup.end())	
-	{		
-		// If we found it, iterate all the strings in the group
-		// looking for this specific string, if its found,
-		// increase reference count and return it
-		entry_group = (*itr).second;
-		StringEntry* entry = entry_group;
-		while (entry)
-		{
-			// If the memory check passes and the null terminator exists in the correct place
-			if (memcmp(entry->buffer, string, length) == 0 && memcmp(&entry->buffer[length], empty_string, character_size) == 0)
-			{				
-				free((char*)string);
-
-				entry->reference_count++;
-				string = entry->buffer;
-				return (StringID)entry;
-			}
-
-			entry = entry->next;
-		}		
-	}
-
-	// Create a new entry
-	StringEntry* entry = new StringEntry();
-	entry->reference_count = 1;
-	entry->next = NULL;
-	entry->prev = NULL;
-	entry->hash = hash;	
-	entry->buffer = (char*)string;
-
-	// If we found an entry group for this string earlier,
-	// insert the string size_to this entry group
-	if (entry_group)
-	{				
-		if (entry_group->next)
-		{
-			entry_group->next->prev = entry;
-			entry->next = entry_group->next;		
-		}
-		entry->prev = entry_group;
-		entry_group->next = entry;		
-	}
-	else
-	{
-		// Otherwise add as a new entry group
-		storage->lookup[hash] = entry;
-	}
-
-	return (StringID)entry;
-}
-
-
-void StringStorage::AddReference(StringID string_id)
-{
-	if (string_id == 0)
-		return;
-
-	// Simply increase the reference count on the given string id
-	StringEntry* entry = (StringEntry*)string_id;
-	entry->reference_count++;
-}
-
-void StringStorage::RemoveReference(StringID string_id)
-{
-	if (string_id == 0)
-		return;
-
-	StringEntry* entry = (StringEntry*)string_id;
-
-	ROCKET_ASSERT(entry->reference_count > 0);
-	entry->reference_count--;
-	if (entry->reference_count > 0)
-		return;
-
-	if (storage)
-	{
-		// If this is the only string in the hash group (hopefully most common case), 
-		// then we just remove it from the lookup table
-		if (entry->prev == NULL && entry->next == NULL)
-		{
-			storage->lookup.erase(entry->hash);
-		}
-
-		// If we have a next and a previous, just remove us from the middle
-		else if (entry->prev && entry->next)
-		{
-			entry->prev->next = entry->next;
-			entry->next->prev = entry->prev;
-		}
-
-		// If we have a next only, we need to update the map index
-		else if (entry->next)
-		{
-			storage->lookup[entry->hash] = entry->next;
-			entry->next->prev = NULL;
-		}
-		
-		// Otherwise we only have a previous, just remove us from the chain
-		else
-		{
-			entry->prev->next = NULL;
-		}
-		
-		ROCKET_ASSERT(storage->lookup.find(entry->hash) == storage->lookup.end() || (*storage->lookup.find(entry->hash)).second != entry);
-	}
-
-	free(entry->buffer);
-	delete entry;
-}
-
-void StringStorage::OnLibraryShutdown()
-{
-	ClearPools();
-	delete storage;
-	storage = NULL;
-}
-
-}
-}

+ 56 - 681
Source/Core/StringUtilities.cpp

@@ -33,12 +33,6 @@
 namespace Rocket {
 namespace Core {
 
-StringUtilities::ArgumentState::ArgumentState()
-{
-	index = 1;
-	display_errors = true;
-}
-
 // Expands character-delimited list of values in a single string to a whitespace-trimmed list of values.
 void StringUtilities::ExpandString(StringList& string_list, const String& string, const char delimiter)
 {	
@@ -98,232 +92,49 @@ void StringUtilities::JoinString(String& string, const StringList& string_list,
 			string.Append(delimiter);
 	}
 }
-
-// Forward declare the MD5 function
-String MD5String(const char* string, int length);
-
-// Hashes a string of data to an 32-character MD5 value.
-String StringUtilities::MD5Hash(const char* data, int length)
-{
-	return MD5String(data, length);
-}
-
+	
 // Hashes a string of data to an integer value using the FNV algorithm.
-Hash StringUtilities::FNVHash(const char *string)
+Hash StringUtilities::FNVHash(const char *string, int length)
 {
 	// FNV-1 hash algorithm
 	Hash hval = 0;
-	unsigned char *bp = (unsigned char *)string;	// start of buffer
-    
+	unsigned char* bp = (unsigned char *)string;	// start of buffer
+	unsigned char* be = (unsigned char *)string + length;
+	
 	// FNV-1 hash each octet in the buffer
-	while (*bp) 
+	while (*bp || (length >= 0 && bp < be)) 
 	{
-		// multiply by the 32 bit FNV magic prime mod 2^32 
-		hval += (hval<<1) + (hval<<4) + (hval<<7) + (hval<<8) + (hval<<24);
-
 		// xor the bottom with the current octet
 		hval ^= *bp++;
+		
+		/* multiply by the 32 bit FNV magic prime mod 2^32 */
+#if !defined(__GNUC__)		
+		const unsigned int FNV_32_PRIME = ((unsigned int)16777619);
+		hval *= FNV_32_PRIME;
+#else
+		hval += (hval<<1) + (hval<<4) + (hval<<7) + (hval<<8) + (hval<<24);
+#endif
 	}
-
+			 
 	return hval;
 }
-
-static unsigned char hexchars[] = "0123456789ABCDEF";
-
-// Encodes a string with URL-encoding.
-bool StringUtilities::URLEncode(const char* input, size_t input_length, String& output)
-{	
-	for (size_t i = 0; i < input_length; i++) 
-	{		
-		if (input[i] == ' ') 
-		{
-			output += '+';
-		} 
-		else if (isalnum((unsigned char)input[i]) || input[i] == '_' || input[i] == '-' || input[i] == '.')
-		{
-			/* Allow only alphanumeric chars and '_', '-', '.'; escape the rest */
-			output += input[i];
-		}
-		else
-		{
-			output += '%';
-			output += hexchars[(unsigned char) input[i] >> 4];
-			output += hexchars[(unsigned char) input[i] & 0x0F];
-		}
-	}
-
-	return true;
-}
-
-// Decodes a URL-encoded string.
-bool StringUtilities::URLDecode(const String& input, char* output, size_t output_length)
-{
-	char* dest = output;
-	const char* data = input.CString();
-	int len = (int) input.Length();
-	size_t used = 0;
-
-	while (len-- && used < output_length) 
-	{
-		if (*data == '+')
-		{
-			*dest = ' ';
-		}
-		else if (*data == '%' && len >= 2 && isxdigit((int) *(data + 1)) && isxdigit((int) *(data + 2))) 
-		{
-			int value;
-			int c;
-
-			c = ((unsigned char*) data)[1];
-			if (isupper(c))
-				c = tolower(c);
-			value = (c >= '0' && c <= '9' ? c - '0' : c - 'a' + 10) * 16;
-
-			c = ((unsigned char*) data)[2];
-			if (isupper(c))
-				c = tolower(c);
-			value += c >= '0' && c <= '9' ? c - '0' : c - 'a' + 10;
-
-			data += 2;
-			len -= 2;
-
-			*dest = (char) value;
-		} 
-		else
-		{
-			*dest = *data;
-		}
-
-		data++;
-		dest++;
-		used++;
-	}
-
-	return true;
-}
-
-
-
-static const char base64digits[] =
-   "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
-
-#define BAD	-1
-static const char base64val[] = {
-    BAD,BAD,BAD,BAD, BAD,BAD,BAD,BAD, BAD,BAD,BAD,BAD, BAD,BAD,BAD,BAD,
-    BAD,BAD,BAD,BAD, BAD,BAD,BAD,BAD, BAD,BAD,BAD,BAD, BAD,BAD,BAD,BAD,
-    BAD,BAD,BAD,BAD, BAD,BAD,BAD,BAD, BAD,BAD,BAD, 62, BAD,BAD,BAD, 63,
-     52, 53, 54, 55,  56, 57, 58, 59,  60, 61,BAD,BAD, BAD,BAD,BAD,BAD,
-    BAD,  0,  1,  2,   3,  4,  5,  6,   7,  8,  9, 10,  11, 12, 13, 14,
-     15, 16, 17, 18,  19, 20, 21, 22,  23, 24, 25,BAD, BAD,BAD,BAD,BAD,
-    BAD, 26, 27, 28,  29, 30, 31, 32,  33, 34, 35, 36,  37, 38, 39, 40,
-     41, 42, 43, 44,  45, 46, 47, 48,  49, 50, 51,BAD, BAD,BAD,BAD,BAD
-};
-#define DECODE64(c)  (isascii(c) ? base64val[c] : BAD)
-
-// Encodes a string with base64-encoding.
-bool StringUtilities::Base64Encode( const char* input, size_t input_length, String& output )
-{
-	output.Clear();
-
-	size_t i = input_length;
-	for (; i >= 3; i -= 3)
-	{
-		output += base64digits[input[0] >> 2];
-		output += base64digits[((input[0] << 4) & 0x30) | (input[1] >> 4)];
-		output += base64digits[((input[1] << 2) & 0x3c) | (input[2] >> 6)];
-		output += base64digits[input[2] & 0x3f];
-		input += 3;
-	}
-
-    if (i > 0)
-    {
-		unsigned char fragment;
-
-		output += base64digits[input[0] >> 2];
-		fragment = (input[0] << 4) & 0x30;
-		if (i > 1)
-			fragment |= input[1] >> 4;
-		output += base64digits[fragment];
-		output += (i < 2) ? '=' : base64digits[(input[1] << 2) & 0x3c];
-		output += '=';
-	}
-
-	return true;
-}
-
-// Decodes a base64-encoded string.
-bool StringUtilities::Base64Decode( const String& input, char* output, size_t output_length )
-{
-	const char* in = input.CString();
-
-	size_t len = 0;
-	unsigned char digit1, digit2, digit3, digit4;
-
-    if (in[0] == '+' && in[1] == ' ')
-		in += 2;
-    if (*in == '\r')
-		return false;
-
-    do
-	{
-		digit1 = in[0];
-		if (DECODE64(digit1) == BAD)
-			return false;
-		digit2 = in[1];
-		if (DECODE64(digit2) == BAD)
-			return false;
-		digit3 = in[2];
-		if (digit3 != '=' && DECODE64(digit3) == BAD)
-			return false;
-		digit4 = in[3];
-		if (digit4 != '=' && DECODE64(digit4) == BAD)
-			return false;
-		in += 4;
-		++len;
-		if (output_length && len > output_length)
-			return false;
-
-		*output++ = (DECODE64(digit1) << 2) | (DECODE64(digit2) >> 4);
-		if (digit3 != '=')
-		{
-			++len;
-			if (output_length && len > output_length)
-				return false;
-
-			*output++ = ((DECODE64(digit2) << 4) & 0xf0) | (DECODE64(digit3) >> 2);
-		    if (digit4 != '=')
-		    {
-			    ++len;
-				if (output_length && len > output_length)
-					return false;
-
-				*output++ = ((DECODE64(digit3) << 6) & 0xc0) | DECODE64(digit4);
-			}
-		}
-    }
-	while (*in && *in != '\r' && digit4 != '=');
-
-    return true;
-}
-
-
-
-// Defines, helper functions for the UTF8 / UCS2 conversion functions.
+	
+	// Defines, helper functions for the UTF8 / UCS2 conversion functions.
 #define _NXT	0x80
 #define _SEQ2	0xc0
 #define _SEQ3	0xe0
 #define _SEQ4	0xf0
 #define _SEQ5	0xf8
 #define _SEQ6	0xfc
-
+	
 #define _BOM	0xfeff
-
+	
 static int __wchar_forbidden(unsigned int sym)
 {
 	// Surrogate pairs
 	if (sym >= 0xd800 && sym <= 0xdfff)
 		return -1;
-
+	
 	return 0;
 }
 
@@ -336,7 +147,7 @@ static int __utf8_forbidden(unsigned char octet)
 		case 0xf5:
 		case 0xff:
 			return -1;
-
+			
 		default:
 			return 0;
 	}
@@ -349,24 +160,24 @@ bool StringUtilities::UTF8toUCS2(const String& input, std::vector< word >& outpu
 {
 	if (input.Empty())
 		return true;
-
+	
 	unsigned char* p = (unsigned char*) input.CString();
 	unsigned char* lim = p + input.Length();
-
+	
 	// Skip the UTF-8 byte order marker if it exists.
 	if (input.Substring(0, 3) == "\xEF\xBB\xBF")
 		p += 3;
-
+	
 	int num_bytes;
 	for (; p < lim; p += num_bytes)
 	{
 		if (__utf8_forbidden(*p) != 0)
 			return false;
-
+		
 		// Get number of bytes for one wide character.
 		word high;
 		num_bytes = 1;
-
+		
 		if ((*p & 0x80) == 0)
 		{
 			high = (wchar_t)*p;
@@ -400,13 +211,13 @@ bool StringUtilities::UTF8toUCS2(const String& input, std::vector< word >& outpu
 		{
 			return false;
 		}
-
+		
 		// Does the sequence header tell us the truth about length?
 		if (lim - p <= num_bytes - 1)
 		{
 			return false;
 		}
-
+		
 		// Validate the sequence. All symbols must have higher bits set to 10xxxxxx.
 		if (num_bytes > 1)
 		{
@@ -416,13 +227,13 @@ bool StringUtilities::UTF8toUCS2(const String& input, std::vector< word >& outpu
 				if ((p[i] & 0xc0) != _NXT)
 					break;
 			}
-
+			
 			if (i != num_bytes)
 			{
 				return false;
 			}
 		}
-
+		
 		// Make up a single UCS-4 (32-bit) character from the required number of UTF-8 tokens. The first byte has
 		// been determined earlier, the second and subsequent bytes contribute the first six of their bits into the
 		// final character code.
@@ -434,19 +245,19 @@ bool StringUtilities::UTF8toUCS2(const String& input, std::vector< word >& outpu
 			num_bits += 6;
 		}
 		ucs4_char |= high << num_bits;
-
+		
 		// Check for surrogate pairs.
 		if (__wchar_forbidden(ucs4_char) != 0)
 		{
 			return false;
 		}
-
+		
 		// Only add the character to the output if it exists in the Basic Multilingual Plane (ie, fits in a single
 		// word).
 		if (ucs4_char <= 0xffff)
 			output.push_back((word) ucs4_char);
 	}
-
+	
 	output.push_back(0);
 	return true;
 }
@@ -462,19 +273,19 @@ bool StringUtilities::UCS2toUTF8(const word* input, size_t input_size, String& o
 {
 	unsigned char *oc;
 	size_t n;
-
+	
 	word* w = (word*) input;
 	word* wlim = w + input_size;
-
+	
 	//Log::Message(LC_CORE, Log::LT_ALWAYS, "UCS2TOUTF8 size: %d", input_size);
 	for (; w < wlim; w++)
 	{
 		if (__wchar_forbidden(*w) != 0)
 			return false;
-
+		
 		if (*w == _BOM)
 			continue;
-
+		
 		//if (*w < 0)
 		//	return false;
 		if (*w <= 0x007f)
@@ -484,48 +295,48 @@ bool StringUtilities::UCS2toUTF8(const word* input, size_t input_size, String& o
 		else //if (*w <= 0x0000ffff)
 			n = 3;
 		/*else if (*w <= 0x001fffff)
-			n = 4;
-		else if (*w <= 0x03ffffff)
-			n = 5;
-		else // if (*w <= 0x7fffffff)
-			n = 6;*/
-
+		 n = 4;
+		 else if (*w <= 0x03ffffff)
+		 n = 5;
+		 else // if (*w <= 0x7fffffff)
+		 n = 6;*/
+		
 		// Convert to little endian.
 		word ch = (*w >> 8) & 0x00FF;
 		ch |= (*w << 8) & 0xFF00;
-//		word ch = EMPConvertEndian(*w, ROCKET_ENDIAN_BIG);
-
+		//		word ch = EMPConvertEndian(*w, ROCKET_ENDIAN_BIG);
+		
 		oc = (unsigned char *)&ch;
 		switch (n)
 		{
 			case 1:
 				output += oc[1];
 				break;
-
+				
 			case 2:
 				output += (_SEQ2 | (oc[1] >> 6) | ((oc[0] & 0x07) << 2));
 				output += (_NXT | oc[1] & 0x3f);
 				break;
-
+				
 			case 3:
 				output += (_SEQ3 | ((oc[0] & 0xf0) >> 4));
 				output += (_NXT | (oc[1] >> 6) | ((oc[0] & 0x0f) << 2));
 				output += (_NXT | oc[1] & 0x3f);
 				break;
-
+				
 			case 4:
 				break;
-
+				
 			case 5:
 				break;
-
+				
 			case 6:
 				break;
 		}
-
+		
 		//Log::Message(LC_CORE, Log::LT_ALWAYS, "Converting...%c(%d) %d -> %d", *w, *w, w - input, output.Length());
 	}
-
+	
 	return true;
 }
 
@@ -534,455 +345,19 @@ String StringUtilities::StripWhitespace(const String& string)
 {
 	const char* start = string.CString();
 	const char* end = start + string.Length();
-
+	
 	while (start < end && IsWhitespace(*start))
 		start++;
-
+	
 	while (end > start && IsWhitespace(*(end - 1)))
 		end--;
-
+	
 	if (start < end)
 		return String(start, end);
-
+	
 	return String();
 }
 
-
-
-////////////////////////////////////////////////////////////////////////////
-// GetOpt - Public Domain Software
-////////////////////////////////////////////////////////////////////////////
-
-/* transcript/src/getopt.c
- *
- * public domain getopt from mod.sources
- * RCSID: $Header: getopt.c,v 2.1 85/11/24 11:49:10 shore Rel $
- */
-
-/*
-**  This is a public domain version of getopt(3).
-**  Bugs, fixes to:
-**		Keith Bostic
-**			ARPA: keith@seismo
-**			UUCP: seismo!keith
-**  Added NO_STDIO, opterr handling, Rich $alz (mirror!rs).
-*/
-
-/*
-**  Error macro.  Maybe we want stdio, maybe we don't.
-**  The (undocumented?) variable opterr tells us whether or not
-**  to print errors.
-*/
-
-#define tell(s) \
-	if (arg_state.display_errors) \
-    { \
-	    (void)fputs(*nargv, stderr); \
-	    (void)fputs(s,stderr); \
-	    (void)fputc(arg_state.option, stderr); \
-	    (void)fputc('\n', stderr); \
-	}
-
-/* Global variables. */
-static char	 EMSG[] = "";
-
-int StringUtilities::GetOpt( int nargc, char* nargv[], char* ostr, ArgumentState& arg_state )
-{
-    static char		 *place = EMSG;	/* option letter processing	*/
-    register char	 *oli;		/* option letter list index	*/
-
-    if (!*place)			/* update scanning pointer	*/
-    {
-		if (arg_state.index >= nargc || *(place = nargv[arg_state.index]) != '-' || !*++place)
-			return(EOF);
-		if (*place == '-')		/* found "--"			*/
-		{
-			arg_state.index++;
-			return(EOF);
-		}
-    }
-    /* option letter okay? */
-    if ((arg_state.option = *place++) == ':' || (oli = strchr(ostr, arg_state.option)) == NULL)
-    {
-		if (!*place)
-			arg_state.index++;
-		tell(": illegal option -- ");
-		goto Bad;
-    }
-	if (*++oli != ':')			/* don't need argument		*/
-	{
-		arg_state.argument = NULL;
-		if (!*place)
-			arg_state.index++;
-    }
-    else				/* need an argument		*/
-    {
-		if (*place)
-			arg_state.argument = place;		/* no white space		*/
-		else
-			if (nargc <= ++arg_state.index)
-			{
-				place = EMSG;
-				tell(": option requires an argument -- ");
-				goto Bad;
-			}
-			else
-				arg_state.argument = nargv[arg_state.index];	/* white space			*/
-		place = EMSG;
-		arg_state.index++;
-    }
-    return(arg_state.option);			/* dump back option letter	*/
-Bad:
-    return('?');
-}
-
-
-////////////////////////////////////////////////////////////////////////////
-// MD5 Algorithm
-////////////////////////////////////////////////////////////////////////////
-
-
-/* This function is the RSA Data Security, Inc. MD5 Message-Digest Algorithm
-
-BE WARNED: This code is ripped straight from the RFC, and is very ugly. Read at your own peril.
-The only function that is human written is the last one, right at the bottom of this file */
-
-
-// POINTER defines a generic pointer type
-typedef unsigned char *POINTER;
-
-// UINT2 defines a two byte word
-typedef unsigned short int UINT2;
-
-// UINT4 defines a four byte word
-typedef unsigned long int UINT4;
-
-#define PROTO_LIST(list) list
-
-typedef struct
-{
-  UINT4 state[4];                                   // state (ABCD)
-  UINT4 count[2];        // number of bits, modulo 2^64 (lsb first)
-  unsigned char buffer[64];                         // input buffer
-} MD5_CTX;
-
-void MD5Init PROTO_LIST ((MD5_CTX *));
-void MD5Update PROTO_LIST ((MD5_CTX *, unsigned char *, unsigned int));
-void MD5Final PROTO_LIST ((unsigned char [16], MD5_CTX *));
-
-
-// Constants for MD5Transform routine.
-
-#define S11 7
-#define S12 12
-#define S13 17
-#define S14 22
-#define S21 5
-#define S22 9
-#define S23 14
-#define S24 20
-#define S31 4
-#define S32 11
-#define S33 16
-#define S34 23
-#define S41 6
-#define S42 10
-#define S43 15
-#define S44 21
-
-void MD5Transform PROTO_LIST ((UINT4 [4], unsigned char [64]));
-void Encode PROTO_LIST ((unsigned char *, UINT4 *, unsigned int));
-void Decode PROTO_LIST ((UINT4 *, unsigned char *, unsigned int));
-void MD5_memcpy PROTO_LIST ((POINTER, POINTER, unsigned int));
-void MD5_memset PROTO_LIST ((POINTER, int, unsigned int));
-
-static unsigned char MD5_PADDING[64] = {
-	0x80, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
-	0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
-	0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
-};
-
-// F, G, H and I are basic MD5 functions.
-#define F(x, y, z) (((x) & (y)) | ((~x) & (z)))
-#define G(x, y, z) (((x) & (z)) | ((y) & (~z)))
-#define H(x, y, z) ((x) ^ (y) ^ (z))
-#define I(x, y, z) ((y) ^ ((x) | (~z)))
-
-// ROTATE_LEFT rotates x left n bits.
-#define ROTATE_LEFT(x, n) (((x) << (n)) | ((x) >> (32-(n))))
-
-/* FF, GG, HH, and II transformations for rounds 1, 2, 3, and 4.
-Rotation is separate from addition to prevent recomputation. */
-#define FF(a, b, c, d, x, s, ac) \
-{ \
-	(a) += F ((b), (c), (d)) + (x) + (UINT4)(ac); \
-	(a) = ROTATE_LEFT ((a), (s)); \
-	(a) += (b); \
-}
-#define GG(a, b, c, d, x, s, ac) \
-{ \
-	(a) += G ((b), (c), (d)) + (x) + (UINT4)(ac); \
-	(a) = ROTATE_LEFT ((a), (s)); \
-	(a) += (b); \
-}
-#define HH(a, b, c, d, x, s, ac) \
-{ \
-	(a) += H ((b), (c), (d)) + (x) + (UINT4)(ac); \
-	(a) = ROTATE_LEFT ((a), (s)); \
-	(a) += (b); \
-}
-#define II(a, b, c, d, x, s, ac) \
-{ \
-	(a) += I ((b), (c), (d)) + (x) + (UINT4)(ac); \
-	(a) = ROTATE_LEFT ((a), (s)); \
-	(a) += (b); \
-}
-
-// MD5 initialization. Begins an MD5 operation, writing a new context.
-void MD5Init (MD5_CTX *context)
-{
-	context->count[0] = context->count[1] = 0;
-	/* Load magic initialization constants.*/
-	context->state[0] = 0x67452301;
-	context->state[1] = 0xefcdab89;
-	context->state[2] = 0x98badcfe;
-	context->state[3] = 0x10325476;
-}
-
-/* MD5 block update operation. Continues an MD5 message-digest
-  operation, processing another message block, and updating the
-  context. */
-void MD5Update (MD5_CTX *context, unsigned char *input, unsigned int inputLen)
-{
-	unsigned int i, index, partLen;
-
-	// Compute number of bytes mod 64
-	index = (unsigned int)((context->count[0] >> 3) & 0x3F);
-
-	// Update number of bits
-	if ((context->count[0] += ((UINT4)inputLen << 3)) < ((UINT4)inputLen << 3))
-		context->count[1]++;
-
-	context->count[1] += ((UINT4)inputLen >> 29);
-
-	partLen = 64 - index;
-
-	// Transform as many times as possible.
-	if (inputLen >= partLen)
-	{
-		MD5_memcpy ((POINTER)&context->buffer[index], (POINTER)input, partLen);
-		MD5Transform (context->state, context->buffer);
-
-		for (i = partLen; i + 63 < inputLen; i += 64)
-			MD5Transform (context->state, &input[i]);
-
-		index = 0;
-	}
-	else
-		i = 0;
-
-	// Buffer remaining input
-	MD5_memcpy ((POINTER)&context->buffer[index], (POINTER)&input[i],
-	inputLen-i);
-}
-
-/* MD5 finalization. Ends an MD5 message-digest operation, writing the
-  the message digest and zeroizing the context. */
-void MD5Final (unsigned char digest[16], MD5_CTX *context)
-{
-	unsigned char bits[8];
-	unsigned int index, padLen;
-
-	// Save number of bits
-	Encode (bits, context->count, 8);
-
-	// Pad out to 56 mod 64.
-	index = (unsigned int)((context->count[0] >> 3) & 0x3f);
-	padLen = (index < 56) ? (56 - index) : (120 - index);
-	MD5Update (context, MD5_PADDING, padLen);
-
-	// Append length (before padding)
-	MD5Update (context, bits, 8);
-
-	// Store state in digest
-	Encode (digest, context->state, 16);
-
-	// Zeroize sensitive information.
-	MD5_memset ((POINTER)context, 0, sizeof (*context));
-}
-
-// MD5 basic transformation. Transforms state based on block.
-void MD5Transform (UINT4 state[4], unsigned char block[64])
-{
-	UINT4 a = state[0], b = state[1], c = state[2], d = state[3], x[16];
-
-	Decode (x, block, 64);
-
-	// Round 1
-	FF (a, b, c, d, x[ 0], S11, 0xd76aa478); /* 1 */
-	FF (d, a, b, c, x[ 1], S12, 0xe8c7b756); /* 2 */
-	FF (c, d, a, b, x[ 2], S13, 0x242070db); /* 3 */
-	FF (b, c, d, a, x[ 3], S14, 0xc1bdceee); /* 4 */
-	FF (a, b, c, d, x[ 4], S11, 0xf57c0faf); /* 5 */
-	FF (d, a, b, c, x[ 5], S12, 0x4787c62a); /* 6 */
-	FF (c, d, a, b, x[ 6], S13, 0xa8304613); /* 7 */
-	FF (b, c, d, a, x[ 7], S14, 0xfd469501); /* 8 */
-	FF (a, b, c, d, x[ 8], S11, 0x698098d8); /* 9 */
-	FF (d, a, b, c, x[ 9], S12, 0x8b44f7af); /* 10 */
-	FF (c, d, a, b, x[10], S13, 0xffff5bb1); /* 11 */
-	FF (b, c, d, a, x[11], S14, 0x895cd7be); /* 12 */
-	FF (a, b, c, d, x[12], S11, 0x6b901122); /* 13 */
-	FF (d, a, b, c, x[13], S12, 0xfd987193); /* 14 */
-	FF (c, d, a, b, x[14], S13, 0xa679438e); /* 15 */
-	FF (b, c, d, a, x[15], S14, 0x49b40821); /* 16 */
-
-	// Round 2
-	GG (a, b, c, d, x[ 1], S21, 0xf61e2562); /* 17 */
-	GG (d, a, b, c, x[ 6], S22, 0xc040b340); /* 18 */
-	GG (c, d, a, b, x[11], S23, 0x265e5a51); /* 19 */
-	GG (b, c, d, a, x[ 0], S24, 0xe9b6c7aa); /* 20 */
-	GG (a, b, c, d, x[ 5], S21, 0xd62f105d); /* 21 */
-	GG (d, a, b, c, x[10], S22,  0x2441453); /* 22 */
-	GG (c, d, a, b, x[15], S23, 0xd8a1e681); /* 23 */
-	GG (b, c, d, a, x[ 4], S24, 0xe7d3fbc8); /* 24 */
-	GG (a, b, c, d, x[ 9], S21, 0x21e1cde6); /* 25 */
-	GG (d, a, b, c, x[14], S22, 0xc33707d6); /* 26 */
-	GG (c, d, a, b, x[ 3], S23, 0xf4d50d87); /* 27 */
-	GG (b, c, d, a, x[ 8], S24, 0x455a14ed); /* 28 */
-	GG (a, b, c, d, x[13], S21, 0xa9e3e905); /* 29 */
-	GG (d, a, b, c, x[ 2], S22, 0xfcefa3f8); /* 30 */
-	GG (c, d, a, b, x[ 7], S23, 0x676f02d9); /* 31 */
-	GG (b, c, d, a, x[12], S24, 0x8d2a4c8a); /* 32 */
-
-	// Round 3
-	HH (a, b, c, d, x[ 5], S31, 0xfffa3942); /* 33 */
-	HH (d, a, b, c, x[ 8], S32, 0x8771f681); /* 34 */
-	HH (c, d, a, b, x[11], S33, 0x6d9d6122); /* 35 */
-	HH (b, c, d, a, x[14], S34, 0xfde5380c); /* 36 */
-	HH (a, b, c, d, x[ 1], S31, 0xa4beea44); /* 37 */
-	HH (d, a, b, c, x[ 4], S32, 0x4bdecfa9); /* 38 */
-	HH (c, d, a, b, x[ 7], S33, 0xf6bb4b60); /* 39 */
-	HH (b, c, d, a, x[10], S34, 0xbebfbc70); /* 40 */
-	HH (a, b, c, d, x[13], S31, 0x289b7ec6); /* 41 */
-	HH (d, a, b, c, x[ 0], S32, 0xeaa127fa); /* 42 */
-	HH (c, d, a, b, x[ 3], S33, 0xd4ef3085); /* 43 */
-	HH (b, c, d, a, x[ 6], S34,  0x4881d05); /* 44 */
-	HH (a, b, c, d, x[ 9], S31, 0xd9d4d039); /* 45 */
-	HH (d, a, b, c, x[12], S32, 0xe6db99e5); /* 46 */
-	HH (c, d, a, b, x[15], S33, 0x1fa27cf8); /* 47 */
-	HH (b, c, d, a, x[ 2], S34, 0xc4ac5665); /* 48 */
-
-	// Round 4
-	II (a, b, c, d, x[ 0], S41, 0xf4292244); /* 49 */
-	II (d, a, b, c, x[ 7], S42, 0x432aff97); /* 50 */
-	II (c, d, a, b, x[14], S43, 0xab9423a7); /* 51 */
-	II (b, c, d, a, x[ 5], S44, 0xfc93a039); /* 52 */
-	II (a, b, c, d, x[12], S41, 0x655b59c3); /* 53 */
-	II (d, a, b, c, x[ 3], S42, 0x8f0ccc92); /* 54 */
-	II (c, d, a, b, x[10], S43, 0xffeff47d); /* 55 */
-	II (b, c, d, a, x[ 1], S44, 0x85845dd1); /* 56 */
-	II (a, b, c, d, x[ 8], S41, 0x6fa87e4f); /* 57 */
-	II (d, a, b, c, x[15], S42, 0xfe2ce6e0); /* 58 */
-	II (c, d, a, b, x[ 6], S43, 0xa3014314); /* 59 */
-	II (b, c, d, a, x[13], S44, 0x4e0811a1); /* 60 */
-	II (a, b, c, d, x[ 4], S41, 0xf7537e82); /* 61 */
-	II (d, a, b, c, x[11], S42, 0xbd3af235); /* 62 */
-	II (c, d, a, b, x[ 2], S43, 0x2ad7d2bb); /* 63 */
-	II (b, c, d, a, x[ 9], S44, 0xeb86d391); /* 64 */
-
-	state[0] += a;
-	state[1] += b;
-	state[2] += c;
-	state[3] += d;
-
-	// Zeroize sensitive information.
-	MD5_memset ((POINTER)x, 0, sizeof (x));
-}
-
-// Encodes input (UINT4) into output (unsigned char). Assumes len is a multiple of 4.
-void Encode (unsigned char *output, UINT4 *input, unsigned int len)
-{
-	unsigned int i, j;
-
-	for (i = 0, j = 0; j < len; i++, j += 4)
-	{
-		output[j] = (unsigned char)(input[i] & 0xff);
-		output[j+1] = (unsigned char)((input[i] >> 8) & 0xff);
-		output[j+2] = (unsigned char)((input[i] >> 16) & 0xff);
-		output[j+3] = (unsigned char)((input[i] >> 24) & 0xff);
-	}
-}
-
-// Decodes input (unsigned char) into output (UINT4). Assumes len is a multiple of 4.
-void Decode (UINT4 *output, unsigned char *input, unsigned int len)
-{
-	unsigned int i, j;
-
-	for (i = 0, j = 0; j < len; i++, j += 4)
-		output[i] = ((UINT4)input[j]) | (((UINT4)input[j+1]) << 8) | (((UINT4)input[j+2]) << 16) | (((UINT4)input[j+3]) << 24);
-}
-
-// Note: Replace "for loop" with standard memcpy if possible.
-
-void MD5_memcpy (POINTER output, POINTER input, unsigned int len)
-{
-	unsigned int i;
-
-	for (i = 0; i < len; i++)
-		output[i] = input[i];
-}
-
-// Note: Replace "for loop" with standard memset if possible.
-void MD5_memset (POINTER output, int value, unsigned int len)
-{
-	unsigned int i;
-
-	for (i = 0; i < len; i++)
-		((char *)output)[i] = (char)value;
-}
-
-// Length of test block, number of test blocks.
-#define TEST_BLOCK_LEN 1000
-#define TEST_BLOCK_COUNT 1000
-
-void MDString PROTO_LIST ((char *));
-void MDTimeTrial PROTO_LIST ((void));
-void MDTestSuite PROTO_LIST ((void));
-void MDFile PROTO_LIST ((char *));
-void MDFilter PROTO_LIST ((void));
-String MDPrint PROTO_LIST ((unsigned char [16]));
-
-// Digests a string and returns the result.
-String MD5String (const char *string, int length)
-{
-	MD5_CTX context;
-	unsigned char digest[16];
-	unsigned int len;
-	if (length < 0)
-		len = (unsigned int) strlen (string);
-	else
-		len = (unsigned int) length;
-
-	MD5Init (&context);
-	MD5Update (&context, (unsigned char*)string, len);
-	MD5Final (digest, &context);
-
-	return MDPrint(digest);
-}
-
-// Prints a message digest in hexadecimal.
-String MDPrint (unsigned char digest[16])
-{
-	char hex_digest[33];
-
-	for (unsigned int i = 0; i < 16; i++)
-		sprintf(&(hex_digest[i * 2]), "%02x", digest[i]);
-
-	return String(hex_digest);
-}
-
-
-
 // Operators for STL containers using strings.
 bool StringUtilities::StringComparei::operator()(const String& lhs, const String& rhs) const
 {