summaryrefslogtreecommitdiff
path: root/src/common/string.cpp
blob: 27b65d4fd24e368d60e91f9920aa348a8a0d30c5 (plain)
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
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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
#include "string.hpp"

#include "common/memory.hpp"

#include <stdlib.h>

namespace common
{

String::String(char *c_string) : c_str(c_string)
{
}

String::String(unsigned int size) : c_str(malloc_s<char>(size + 1))
{
}

String::String(const String &smart_str)
	: c_str(malloc_s<char>(strlen(smart_str.c_str) + 1))
{
	memcpy(c_str, smart_str.c_str, strlen(smart_str.c_str) + 1);
}

String::String(String &&smart_str) noexcept : c_str(smart_str.c_str)
{
	smart_str.c_str = nullptr;
}

String &String::operator=(const String &smart_str)
{
	if (&smart_str != this)
	{
		free(c_str);
		c_str = nullptr;

		auto str_size = strlen(smart_str.c_str) + 1;

		c_str = malloc_s<char>(str_size);
		memcpy(c_str, smart_str.c_str, str_size);
	}

	return *this;
}

String &String::operator=(String &&smart_str) noexcept
{
	if (&smart_str != this)
	{
		free(c_str);
		c_str = smart_str.c_str;
		smart_str.c_str = nullptr;
	}

	return *this;
}

String::~String()
{
	if (c_str != nullptr)
	{
		free(c_str);
	}
}

String::operator char *() const
{
	return c_str;
}

} // namespace common