GigaPasjans
Loading...
Searching...
No Matches
String.h
1// Wykonane przez Piotra ChudziƄskiego w dniu 10.04.2025
2
3#pragma once
4
5#include <cstring>
6#include <fmt/format.h>
7#include <string>
8#include "../DataTypes.h"
9#include "../Macro.h"
10
15class String
16{
17private:
18 char *m_Data;
19 int32 m_Length;
20
21public:
25 String() : m_Data(nullptr), m_Length(0) {};
26
31 String(const std::string &data) : m_Data(nullptr), m_Length(0)
32 {
33 Set(const_cast<char *>(data.data()), static_cast<int32>(data.size()));
34 }
35
40 String(const char *data) : m_Data(nullptr), m_Length(0)
41 {
42 Set(const_cast<char *>(data), static_cast<int32>(strlen(data)));
43 }
44
48 String(const char *data, const int32 length) : m_Data(nullptr), m_Length(0)
49 {
50 Set(const_cast<char *>(data), m_Length);
51 }
52
56 String(const char *start, const char *end) : m_Data(nullptr), m_Length(0)
57 {
58 Set(const_cast<char *>(start), static_cast<int32>(end - start));
59 }
60
64 explicit String(char c) : m_Data(nullptr), m_Length(0) { Set(&c, 1); }
65
66 ~String()
67 {
68 if (m_Data)
69 {
70 free(m_Data);
71 m_Data = nullptr;
72 }
73 }
74
78 String(const String &str) : m_Data(nullptr), m_Length(0)
79 {
80 if (str.Get())
81 {
82 Set(str.Get(), str.m_Length);
83 }
84 };
85
86 String(String &&str) noexcept : m_Data(str.m_Data), m_Length(str.m_Length)
87 {
88 str.m_Data = nullptr;
89 str.m_Length = 0;
90 };
91
92 String &operator=(const String &str)
93 {
94 if (this == &str)
95 return *this;
96 Set(str.Get(), str.m_Length);
97
98 return *this;
99 }
100
101 String &operator=(String &&str) noexcept
102 {
103 if (this == &str)
104 return *this;
105 Set(str.Get(), str.m_Length);
106
107 free(str.m_Data);
108 str.m_Length = 0;
109 str.m_Data = nullptr;
110
111 return *this;
112 }
113
114 bool operator==(const String &str) const { return strcmp(this->Get(), str.Get()) == 0; };
115
116 String operator+(const char *str) const
117 {
118 int32 newLength = this->GetLength() + static_cast<int32>(strlen(str)) + 1;
119 char *data = new char[newLength];
120
121 memcpy(data, this->Get(), this->GetLength() * sizeof(wchar_t));
122 strcat(data, str);
123
124 String concated(data, newLength);
125
126 delete[] data;
127 return concated;
128 };
129
130 String operator+(const String &str) const
131 {
132 int32 newLength = this->GetLength() + static_cast<int32>(strlen(str.Get())) + 1;
133 char *data = new char[newLength];
134
135 memcpy(data, this->Get(), this->GetLength() * sizeof(wchar_t));
136 strcat(data, str.Get());
137
138 String concated(data, newLength);
139
140 delete[] data;
141 return concated;
142 };
143
150 void Set(char *data, int32 length);
151
157 void Allocate(int32 length);
158
163 [[nodiscard]] FORCE_INLINE char *Get() const { return m_Data; }
164
165 [[nodiscard]] FORCE_INLINE int32 GetLength() const { return m_Length; }
166
167 [[nodiscard]] bool StartsWith(const String &other) const;
168
169 [[nodiscard]] bool EndsWith(const String &other) const;
170
171 [[nodiscard]] int32 HashCode() const;
172};
Represents a mutable sequence of characters, providing various member functions for string manipulati...
Definition String.h:16
String(char c)
Initializes a new instance of the String class.
Definition String.h:64
String(const char *data, const int32 length)
Initializes a new instance of the String class.
Definition String.h:48
FORCE_INLINE char * Get() const
Gets a raw char sequence.
Definition String.h:163
String(const String &str)
Initializes a new instance of the String class.
Definition String.h:78
void Allocate(int32 length)
Use only when string is empty.
Definition Strings.cpp:20
String(const char *data)
Initializes a new instance of the String class.
Definition String.h:40
String()
Initializes a new instance of the String class.
Definition String.h:25
String(const char *start, const char *end)
Initializes a new instance of the String class.
Definition String.h:56
String(const std::string &data)
Initializes a new instance of the String class.
Definition String.h:31
void Set(char *data, int32 length)
Sets an array of characters to the string.
Definition Strings.cpp:7