From 3475b398fc925cd7d144a2a36f8ec1737a510915 Mon Sep 17 00:00:00 2001 From: JustinAJ Date: Tue, 27 May 2014 15:19:20 -0400 Subject: [PATCH] Moved string shifting to its own class. --- Jupiter/Shift_String.h | 64 ++++++++++++++++++++++++++++++++++++++ Jupiter/Shift_String_Imp.h | 44 ++++++++++++++++++++++++++ 2 files changed, 108 insertions(+) create mode 100644 Jupiter/Shift_String.h create mode 100644 Jupiter/Shift_String_Imp.h diff --git a/Jupiter/Shift_String.h b/Jupiter/Shift_String.h new file mode 100644 index 0000000..5f28bd7 --- /dev/null +++ b/Jupiter/Shift_String.h @@ -0,0 +1,64 @@ +/** + * Copyright (C) 2014 Justin James. + * + * This license must be preserved. + * Any applications, libraries, or code which make any use of any + * component of this program must not be commercial, unless explicit + * permission is granted from the original author. The use of this + * program for non-profit purposes is permitted. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. + * + * In the event that this license restricts you from making desired use of this program, contact the original author. + * Written by Justin James + */ + +#if !defined _SHIFT_STRING_H_HEADER +#define _SHIFT_STRING_H_HEADER + +/** + * @file Shift_String.h + * @brief Allows for separation of a string's representation from its memory base. + */ + +#include "String_Type.h" + +namespace Jupiter +{ + + /** + * @brief Provides the basis for String classes by providing implementations for operators, comparative operations, and defining abstract functions. + * Note: This is an abstract type. + * + * @param T Element type which the String will store. Defaults to char. + */ + template class Shift_String_Type : public Jupiter::String_Type + { + public: + + /** + * @brief Shifts the string pointer to the left. + * + * @param length Number of elements to shift + * @return Number of elements shifted to the left. + */ + size_t shiftLeft(size_t length); + + /** + * @brief Shifts the string pointer to the right. + * + * @param length Number of elements to shift + * @return Number of elements shifted. + */ + size_t shiftRight(size_t length); + + protected: + T *base; /** Base pointer for the underlying String's memory allocation */ + }; +} + +#include "Shift_String_Imp.h" + +#endif // _SHIFT_STRING_H_HEADER \ No newline at end of file diff --git a/Jupiter/Shift_String_Imp.h b/Jupiter/Shift_String_Imp.h new file mode 100644 index 0000000..c01151e --- /dev/null +++ b/Jupiter/Shift_String_Imp.h @@ -0,0 +1,44 @@ +/** + * Copyright (C) 2014 Justin James. + * + * This license must be preserved. + * Any applications, libraries, or code which make any use of any + * component of this program must not be commercial, unless explicit + * permission is granted from the original author. The use of this + * program for non-profit purposes is permitted. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. + * + * In the event that this license restricts you from making desired use of this program, contact the original author. + * Written by Justin James + */ + +#if !defined _SHIFT_STRING_IMP_H_HEADER +#define _SHIFT_STRING_IMP_H_HEADER + +/** + * @file Shift_String_Imp.h + * @brief Provides the implementations for Shift_String_Type functions. + * Note: Modification of this file is not supported in any way. + */ + +template size_t Jupiter::Shift_String_Type::shiftLeft(size_t length) +{ + size_t offset = Jupiter::String_Type::str - Jupiter::Shift_String_Type::base; + if (length > offset) length = offset; + Jupiter::String_Type::str -= length; + Jupiter::String_Type::length += length; + return length; +} + +template size_t Jupiter::Shift_String_Type::shiftRight(size_t length) +{ + if (length > Jupiter::String_Type::length) length = Jupiter::String_Type::length; + Jupiter::String_Type::str += length; + Jupiter::String_Type::length -= length; + return length; +} + +#endif // _SHIFT_STRING_IMP_H_HEADER \ No newline at end of file