Skip to content

Commit

Permalink
Created a String class to replace std::string. For #102.
Browse files Browse the repository at this point in the history
  • Loading branch information
end2endzone committed Dec 30, 2021
1 parent af0dd4d commit 88722ad
Show file tree
Hide file tree
Showing 6 changed files with 463 additions and 0 deletions.
106 changes: 106 additions & 0 deletions include/shellanything/String.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
/**********************************************************************************
* MIT License
*
* Copyright (c) 2018 Antoine Beauchamp
*
* 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.
*********************************************************************************/

namespace shellanything
{

/// <summary>
/// A structure that contains a list of Configuration class pointers.
/// </summary>
class String
{
public:
String();
String(const char* str);
String(const String& str);
virtual ~String();

static const size_t INVALID_INDEX = (size_t)-1;

String& operator=(const char* str);
String& operator=(const String& str);
char & operator[](size_t index);
const char& operator[](size_t index) const;
bool operator==(const char* str) const;
bool operator==(const String& str) const;
bool operator!=(const char* str) const;
bool operator!=(const String& str) const;
String& operator+=(const char* str);
String& operator+=(const String& str);

/// <summary>
/// Get the internal value of the string.
/// </summary>
const char* GetValue() const;
inline const char * c_str() const { return GetValue(); } // for std::vector and std::list code compatibility.

/// <summary>
/// Define if the string is empty.
/// </summary>
bool IsEmpty() const;
inline bool empty() const { return IsEmpty(); } // for std::vector and std::list code compatibility.

/// <summary>
/// Get the length of the string.
/// </summary>
size_t GetSize() const;
inline size_t size() const { return GetSize(); } // for std::vector and std::list code compatibility.

/// <summary>
/// Empty the string.
/// </summary>
void Clear();
inline void clear() { Clear(); } // for std::vector and std::list code compatibility.

/// <summary>
/// Find a value in the list.
/// </summary>
/// <param name="value">A value to find within the string.</param>
/// <returns>Returns the position where the search value was found. Returns INVALID_INDEX otherwise.</returns>
size_t Find(const char value) const;
size_t Find(const char * value) const;
size_t Find(const String & value) const;
inline size_t find(const char value) const { return Find(value); } // for std::vector and std::list code compatibility.
inline size_t find(const char* value) const { return Find(value); } // for std::vector and std::list code compatibility.
inline size_t find(const String& value) const { return Find(value); } // for std::vector and std::list code compatibility.

/// <summary>
/// Find a value in the list, starting at the given offset.
/// </summary>
/// <param name="value">A value to find within the string.</param>
/// <param name="offset">Position at which the search must start.</param>
/// <returns>Returns the position where the search value was found. Returns INVALID_INDEX otherwise.</returns>
size_t Find(const char value, size_t offset) const;
size_t Find(const char * value, size_t offset) const;
size_t Find(const String & value, size_t offset) const;
inline size_t find(const char value, size_t offset) const { return Find(value, offset); } // for std::vector and std::list code compatibility.
inline size_t find(const char* value, size_t offset) const { return Find(value, offset); } // for std::vector and std::list code compatibility.
inline size_t find(const String& value, size_t offset) const { return Find(value, offset); } // for std::vector and std::list code compatibility.

private:
struct PImpl;
PImpl* mPImpl;
};

} //namespace shellanything
2 changes: 2 additions & 0 deletions src/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ set(SHELLANYTHING_HEADER_FILES ""
${CMAKE_SOURCE_DIR}/include/shellanything/ListsBody.inc
${CMAKE_SOURCE_DIR}/include/shellanything/ListsDeclaration.inc
${CMAKE_SOURCE_DIR}/include/shellanything/Menu.h
${CMAKE_SOURCE_DIR}/include/shellanything/String.h
${CMAKE_SOURCE_DIR}/include/shellanything/Validator.h
)

Expand Down Expand Up @@ -73,6 +74,7 @@ add_library(shellanything STATIC
Menu.cpp
ObjectFactory.h
ObjectFactory.cpp
String.cpp
Unicode.h
Unicode.cpp
Validator.cpp
Expand Down
162 changes: 162 additions & 0 deletions src/String.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,162 @@
/**********************************************************************************
* MIT License
*
* Copyright (c) 2018 Antoine Beauchamp
*
* 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 "shellanything/String.h"
#include <string>

namespace shellanything
{

struct String::PImpl
{
std::string str;
};

String::String()
{
mPImpl = new String::PImpl();
}

String::String(const char* str)
{
mPImpl = new String::PImpl();
mPImpl->str = str;
}

String::String(const String& str)
{
mPImpl = new String::PImpl();
mPImpl->str = str.mPImpl->str;
}

String::~String()
{
delete mPImpl;
}

String& String::operator=(const char* str)
{
mPImpl->str = str;
return (*this);
}

String& String::operator=(const String& str)
{
mPImpl->str = str.mPImpl->str;
return (*this);
}

char& String::operator[](size_t index)
{
return mPImpl->str[index];
}

const char& String::operator[](size_t index) const
{
return mPImpl->str[index];
}

bool String::operator==(const char* str) const
{
return mPImpl->str == str;
}

bool String::operator==(const String& str) const
{
return mPImpl->str == str.mPImpl->str;
}

bool String::operator!=(const char* str) const
{
return mPImpl->str != str;
}

bool String::operator!=(const String& str) const
{
return mPImpl->str != str.mPImpl->str;
}

String& String::operator+=(const char* str)
{
mPImpl->str += str;
return (*this);
}

String& String::operator+=(const String& str)
{
mPImpl->str += str.mPImpl->str;
return (*this);
}

const char* String::GetValue() const
{
return mPImpl->str.c_str();
}

bool String::IsEmpty() const
{
return mPImpl->str.empty();
}

size_t String::GetSize() const
{
return mPImpl->str.size();
}

void String::Clear()
{
return mPImpl->str.clear();
}

size_t String::Find(const char value) const
{
return mPImpl->str.find(value);
}

size_t String::Find(const char* value) const
{
return mPImpl->str.find(value);
}

size_t String::Find(const String& value) const
{
return mPImpl->str.find(value.mPImpl->str);
}

size_t String::Find(const char value, size_t offset) const
{
return mPImpl->str.find(value, offset);
}

size_t String::Find(const char* value, size_t offset) const
{
return mPImpl->str.find(value, offset);
}

size_t String::Find(const String& value, size_t offset) const
{
return mPImpl->str.find(value.mPImpl->str, offset);
}

} //namespace shellanything
2 changes: 2 additions & 0 deletions test/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,8 @@ add_executable(shellanything_unittest
TestPropertyManager.h
TestShellExtension.cpp
TestShellExtension.h
TestString.cpp
TestString.h
TestUnicode.cpp
TestUnicode.h
TestValidator.cpp
Expand Down
Loading

0 comments on commit 88722ad

Please sign in to comment.