-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstack_string.h
80 lines (78 loc) · 2.21 KB
/
stack_string.h
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
71
72
73
74
75
76
77
78
79
80
//
// Created by Anonymous275 on 9/5/2021.
//
#pragma once
#include <cstring>
#include <iostream>
namespace fst {
template<size_t Cap>
class stack_string {
public:
stack_string() noexcept {
memset(Data, 0, Cap);
}
explicit stack_string(const char* Ptr) {
size_t len = strlen(Ptr);
Copy(Ptr, len);
memset(Data + len, 0, Cap - len);
}
stack_string(const char* Ptr, size_t PSize) {
Copy(Ptr, PSize);
memset(Data + PSize, 0, Cap - PSize);
}
inline size_t capacity() noexcept {
return Cap;
}
inline size_t size() noexcept {
return Size;
}
inline size_t length() noexcept {
return Size;
}
inline char* get() {
return Data;
}
[[nodiscard]] inline const char* c_str() const noexcept {
return Data;
}
char& operator[](size_t idx) {
if (idx >= Size) {
throw std::exception("stack_string out of boundaries operator[]");
}
return Data[idx];
}
inline void resize(size_t newSize) noexcept {
Size = newSize;
}
inline void push_back(const char* Ptr) {
Copy(Ptr, strlen(Ptr));
}
inline void push_back(const char* Ptr, size_t Count) {
Copy(Ptr, Count);
}
inline void push_back(char Ptr) {
Copy(&Ptr, 1);
}
friend std::ostream& operator<<(std::ostream& os, const stack_string& obj) {
os << obj.Data;
return os;
}
inline stack_string& operator+=(const char* Ptr) {
push_back(Ptr);
return *this;
}
inline stack_string& operator+=(char Ptr) {
push_back(Ptr);
return *this;
}
private:
inline void Copy(const char* Ptr, size_t PSize) {
if((PSize + Size) <= Cap) {
memcpy(&Data[Size], Ptr, PSize);
Size += PSize;
} else throw std::exception("stack_string out of boundaries copy");
}
char Data[Cap]{};
size_t Size{0};
};
}