summaryrefslogtreecommitdiff
path: root/src/std/smart_string.cpp
blob: b24a1a5c05909e65d510875caa23d4292c45889e (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
#include "smart_string.hpp"

#include "std/memory.hpp"

#include <stdlib.h>

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

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

SmartString::SmartString(const SmartString &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);
}

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

SmartString &SmartString::operator=(const SmartString &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;
}

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

	return *this;
}

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

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