|
1
|
1 #pragma once
|
|
|
2 #include "array.h"
|
|
|
3 namespace pfc {
|
|
|
4 // FOR LEGACY CODE ONLY
|
|
|
5 // DO NOT USE IN NEW CODE
|
|
|
6 template<typename t_char>
|
|
|
7 class string_simple_t {
|
|
|
8 private:
|
|
|
9 typedef string_simple_t<t_char> t_self;
|
|
|
10 public:
|
|
|
11 t_size length() const {
|
|
|
12 t_size s = m_buffer.get_size();
|
|
|
13 if (s == 0) return 0; else return s - 1;
|
|
|
14 }
|
|
|
15 bool is_empty() const { return m_buffer.get_size() == 0; }
|
|
|
16
|
|
|
17 void set_string_nc(const t_char* p_source, t_size p_length) {
|
|
|
18 if (p_length == 0) {
|
|
|
19 m_buffer.set_size(0);
|
|
|
20 } else {
|
|
|
21 m_buffer.set_size(p_length + 1);
|
|
|
22 pfc::memcpy_t(m_buffer.get_ptr(), p_source, p_length);
|
|
|
23 m_buffer[p_length] = 0;
|
|
|
24 }
|
|
|
25 }
|
|
|
26 void set_string(const t_char* p_source) {
|
|
|
27 set_string_nc(p_source, pfc::strlen_t(p_source));
|
|
|
28 }
|
|
|
29 void set_string(const t_char* p_source, t_size p_length) {
|
|
|
30 set_string_nc(p_source, strlen_max_t(p_source, p_length));
|
|
|
31 }
|
|
|
32 void add_string(const t_char* p_source, t_size p_length) {
|
|
|
33 add_string_nc(p_source, strlen_max_t(p_source, p_length));
|
|
|
34 }
|
|
|
35 void add_string(const t_char* p_source) {
|
|
|
36 add_string_nc(p_source, strlen_t(p_source));
|
|
|
37 }
|
|
|
38 void add_string_nc(const t_char* p_source, t_size p_length) {
|
|
|
39 if (p_length > 0) {
|
|
|
40 t_size base = length();
|
|
|
41 m_buffer.set_size(base + p_length + 1);
|
|
|
42 memcpy_t(m_buffer.get_ptr() + base, p_source, p_length);
|
|
|
43 m_buffer[base + p_length] = 0;
|
|
|
44 }
|
|
|
45 }
|
|
|
46 string_simple_t() {}
|
|
|
47 string_simple_t(const t_char* p_source) { set_string(p_source); }
|
|
|
48 string_simple_t(const t_char* p_source, t_size p_length) { set_string(p_source, p_length); }
|
|
|
49 const t_self& operator=(const t_char* p_source) { set_string(p_source); return *this; }
|
|
|
50 operator const t_char* () const { return get_ptr(); }
|
|
|
51 const t_char* get_ptr() const { return m_buffer.get_size() > 0 ? m_buffer.get_ptr() : pfc::empty_string_t<t_char>(); }
|
|
|
52 const t_char* c_str() const { return get_ptr(); }
|
|
|
53 private:
|
|
|
54 pfc::array_t<t_char> m_buffer;
|
|
|
55 };
|
|
|
56
|
|
|
57 template<typename t_char> class traits_t<string_simple_t<t_char> > : public traits_t<array_t<t_char> > {};
|
|
|
58
|
|
|
59 } |