Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

string: add string impl #31

Merged
merged 5 commits into from
Oct 1, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
String -> BasicString
  • Loading branch information
Sunrisepeak committed Oct 1, 2023
commit 2df5dff64ac740cb2c0447980719d112d8448a9f
80 changes: 49 additions & 31 deletions core/ds/string/String.hpp → core/ds/string/BasicString.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,48 +7,48 @@
// ProjectLinks: https://github.com/Sunrisepeak/DStruct
//

#ifndef __STRING_HPP__DSTRUCT
#define __STRING_HPP__DSTRUCT
#ifndef __BASIC_STRING_HPP__DSTRUCT
#define __BASIC_STRING_HPP__DSTRUCT

#include <core/common.hpp>
#include <core/ds/array/Vector.hpp>

namespace dstruct {

// simple implement for string
class String {
template <typename CharType, typename Alloc>
class BasicString {
private:
using __CharList = Vector<char>;
using __CharList = Vector<CharType, Alloc>;

DSTRUCT_TYPE_SPEC_HELPER(__CharList);

public:
String() : _mCharList {1, '\0'} {
BasicString() : _mCharList {1, '\0'} {
_mCharList.resize(15);
}

DSTRUCT_COPY_SEMANTICS(String) {
DSTRUCT_COPY_SEMANTICS(BasicString) {
_mCharList = ds._mCharList;
return *this;
}

DSTRUCT_MOVE_SEMANTICS(String) {
DSTRUCT_MOVE_SEMANTICS(BasicString) {
_mCharList = dstruct::move(ds._mCharList);
return *this;
}

String(const char *str) : String() {
BasicString(const char *str) : BasicString() {
_mCharList[-1] = *str;
while (*(++str) != '\0') {
_mCharList.push_back(*str);
}
_mCharList.push_back('\0');
}

~String() = default;
~BasicString() = default;

String & operator=(const char *str) {
*this = String(str);
BasicString & operator=(const char *str) {
*this = BasicString(str);
return *this;
}

Expand Down Expand Up @@ -110,7 +110,7 @@ class String {
return &(_mCharList[0]);
}

String & operator+=(const String &str) {
BasicString & operator+=(const BasicString &str) {
if (_mCharList.capacity() < _mCharList.size() + str._mCharList.size() - 1)
_mCharList.resize(_mCharList.size() + str._mCharList.size() - 1);
_mCharList[-1] = str[0];
Expand All @@ -120,28 +120,36 @@ class String {
return *this;
}

String & operator+=(const char * str) {
return *this += String(str);
BasicString & operator+=(const char * str) {
return *this += BasicString(str);
}

protected:
__CharList _mCharList;
};

String operator+(const String s1, const String s2) {
String s = s1;
template <typename CharType, typename Alloc>
static BasicString<CharType, Alloc>
operator+(const BasicString<CharType, Alloc> s1, const BasicString<CharType, Alloc> s2) {
BasicString<CharType, Alloc> s = s1;
return s += s2;
}

String operator+(const String s1, const char *s2) {
return operator+(s1, String(s2));
template <typename CharType, typename Alloc>
static BasicString<CharType, Alloc>
operator+(const BasicString<CharType, Alloc> s1, const char *s2) {
return operator+(s1, BasicString<CharType, Alloc>(s2));
}

String operator+(const char *s1, const String s2) {
return operator+(String(s1), s2);
template <typename CharType, typename Alloc>
static BasicString<CharType, Alloc>
operator+(const char *s1, const BasicString<CharType, Alloc> s2) {
return operator+(BasicString<CharType, Alloc>(s1), s2);
}

bool operator==(const String s1, const String s2) {
template <typename CharType, typename Alloc>
static bool
operator==(const BasicString<CharType, Alloc> s1, const BasicString<CharType, Alloc> s2) {
if (s1.size() != s2.size())
return false;
for (int i = 0; i < s1.size(); i++) {
Expand All @@ -152,24 +160,34 @@ bool operator==(const String s1, const String s2) {
return true;
}

bool operator==(const String s1, const char *s2) {
return operator==(s1, String(s2));
template <typename CharType, typename Alloc>
static bool
operator==(const BasicString<CharType, Alloc> s1, const char *s2) {
return operator==(s1, BasicString<CharType, Alloc>(s2));
}

bool operator==(const char *s1, const String s2) {
return operator==(String(s1), s2);
template <typename CharType, typename Alloc>
static bool
operator==(const char *s1, const BasicString<CharType, Alloc> s2) {
return operator==(BasicString<CharType, Alloc>(s1), s2);
}

bool operator!=(const String s1, const String s2) {
template <typename CharType, typename Alloc>
static bool
operator!=(const BasicString<CharType, Alloc> s1, const BasicString<CharType, Alloc> s2) {
return !operator==(s1, s2);
}

bool operator!=(const String s1, const char *s2) {
return operator!=(s1, String(s2));
template <typename CharType, typename Alloc>
static bool
operator!=(const BasicString<CharType, Alloc> s1, const char *s2) {
return operator!=(s1, BasicString<CharType, Alloc>(s2));
}

bool operator!=(const char *s1, const String s2) {
return operator!=(String(s1), s2);
template <typename CharType, typename Alloc>
static bool
operator!=(const char *s1, const BasicString<CharType, Alloc> s2) {
return operator!=(BasicString<CharType, Alloc>(s1), s2);
}

}
Expand Down
8 changes: 7 additions & 1 deletion dstruct-static.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,9 @@
#include <core/ds/linked-list/SinglyLinkedList.hpp>
#include <core/ds/linked-list/DoublyLinkedList.hpp>

// String
#include <core/ds/string/BasicString.hpp>

// tree
#include <core/ds/tree/BinarySearchTree.hpp>

Expand All @@ -40,7 +43,7 @@ namespace dstruct {

namespace smemory {
// SMA
#define SMA_MEM_SIZE 1024 * 2 // 2k
#define SMA_MEM_SIZE 1024 * 10 // 10K
using SMA = dstruct::StaticMemAllocator<SMA_MEM_SIZE>;

// Array
Expand All @@ -51,6 +54,9 @@ namespace smemory {
template <typename T>
using Vector = dstruct::Vector<T, SMA>;

// String
using String = BasicString<char, SMA>;

// EmbeddedList
template <typename T, typename Link = _DoublyLink>
using EListNode = _EmbeddedListNode<T, Link>;
Expand Down
4 changes: 3 additions & 1 deletion dstruct.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,8 @@
#include <core/ds/linked-list/SinglyLinkedList.hpp>
#include <core/ds/linked-list/DoublyLinkedList.hpp>

#include <core/ds/string/String.hpp>
// String
#include <core/ds/string/BasicString.hpp>

// tree
#include <core/ds/tree/BinarySearchTree.hpp>
Expand All @@ -50,6 +51,7 @@ namespace dstruct {
// Vector

// String
using String = BasicString<char, dstruct::Alloc>;

// EmbeddedList
template <typename T, typename Link = _DoublyLink>
Expand Down