1 #include "String.h"
2
3 template<class T>
4 inline String<T>::String() :pstart(nullptr), length(0)
5 {
6 if (strcmp(typeid(T).name(), "char") == 0)
7 {
8
9 }
10 else if (strcmp(typeid(T).name(), "wchar_t") == 0)
11 {
12
13 }
14 else
15 {
16 cout << "类型不匹配,请认真检查" << endl;;
17 }
18 }
19
20 template<class T>
21 String<T>::~String()
22 {
23 if (this->pstart != nullptr)
24 {
25 delete[] this->pstart;
26 }
27 }
28
29 template<class T>
30 String<T>::String(const String & str)
31 {
32 if (this->pstart != nullptr)
33 {
34 delete[] this->pstart;
35 }
36 //长度归零
37 this->length = 0;
38
39 this->length = str.length;
40 this->pstart = new T[this->length + 1 ];
41 if (strcmp(typeid(T).name(), "char") == 0)
42 {
43 strcpy(reinterpret_cast<char *>(this->pstart), reinterpret_cast<const char *>(str.pstart));
44 }
45 else if (strcmp(typeid(T).name(), "wchar_t") == 0)
46 {
47 wcscpy(reinterpret_cast<wchar_t *>(this->pstart), reinterpret_cast<const wchar_t *>(str.pstart));
48 }
49 }
50
51 template<class T>
52 String<T>::String(const T * p)
53 {
54 if (strcmp(typeid(T).name(), "char") == 0)
55 {
56 this->length = strlen(reinterpret_cast<const char*>(p));
57 this->pstart = new T[this->length + 1 ];
58 //初始化
59 strcpy(reinterpret_cast<char*>(this->pstart), reinterpret_cast<const char*>(p));
60 }
61 else if(strcmp(typeid(T).name(), "wchar_t") == 0)
62 {
63 this->length = wcslen(reinterpret_cast<const wchar_t*>(p));
64 this->pstart = new T[this->length + 1 ];
65 wcscpy(reinterpret_cast<wchar_t*>(this->pstart), reinterpret_cast<const wchar_t*>(p));
66 }
67 else
68 {
69 cout << "类型不匹配,请认真检查" << endl;;
70 }
71 }
72
73 template<class T>
74 void String<T>::operator=(const String & str)
75 {
76 if (this->pstart != nullptr)
77 {
78 delete[] this->pstart;
79 }
80 //长度归零
81 this->length = 0;
82
83 this->length = str.length;
84 this->pstart = new T[this->length + 1];
85 if (strcmp(typeid(T).name(), "char") == 0)
86 {
87 strcpy(reinterpret_cast<char *>(this->pstart), reinterpret_cast<const char *>(str.pstart));
88 }
89 else if (strcmp(typeid(T).name(), "wchar_t") == 0)
90 {
91 wcscpy(reinterpret_cast<wchar_t *>(this->pstart), reinterpret_cast<const wchar_t *>(str.pstart));
92 }
93 }
94
95 template<class T>
96 void String<T>::operator+=(const String & str)
97 {
98 this->length += str.length;
99 T *temp = new T[this->length + 1];
100 if (strcmp(typeid(T).name(), "char") == 0)
101 {
102 strcpy(reinterpret_cast<char*>(temp), reinterpret_cast<const char*>(this->pstart));
103 strcat(reinterpret_cast<char*>(temp), reinterpret_cast<const char*>(str.pstart));
104 delete[] this->pstart;
105 this->pstart = temp;
106 }
107 else if (strcmp(typeid(T).name(), "wchar_t") == 0)
108 {
109 wcscpy(reinterpret_cast<wchar_t*>(temp), reinterpret_cast<const wchar_t*>(this->pstart));
110 wcscat(reinterpret_cast<wchar_t*>(temp), reinterpret_cast<const wchar_t*>(str.pstart));
111 delete[] this->pstart;
112 this->pstart = temp;
113 }
114 else
115 {
116 cout << "类型不匹配,请认真检查" << endl;;
117 }
118 }
119
120 template<class T>
121 String<T> String<T>::operator+(const String & str)
122 {
123 String<T> temp;
124 temp.length = this->length + str.length;
125 temp.pstart = new T[temp.length+1];
126
127 if (strcmp(typeid(T).name(), "char") == 0)
128 {
129 strcpy(reinterpret_cast<char*>(temp.pstart), reinterpret_cast<const char*>(this->pstart));
130 strcat(reinterpret_cast<char*>(temp.pstart), reinterpret_cast<const char*>(str.pstart));
131 }
132 else if (strcmp(typeid(T).name(), "wchar_t") == 0)
133 {
134 wcscpy(reinterpret_cast<wchar_t*>(temp.pstart), reinterpret_cast<const wchar_t*>(this->pstart));
135 wcscat(reinterpret_cast<wchar_t*>(temp.pstart), reinterpret_cast<const wchar_t*>(str.pstart));
136 }
137 else
138 {
139 cout << "类型不匹配,请认真检查" << endl;;
140 }
141 return temp;
142 }
143
144 //template<class T>
145 //T String<T>::operator[](int id) const
146 //{
147 // if (id < 0 || id >= this->length)
148 // {
149 // //抛出匿名异常
150 // throw outofrange();
151 // }
152 // return this->pstart[id];
153 //}
154
155 template<class T>
156 T& String<T>::operator[] (int id) const
157 {
158 if (id < 0 || id >= this->length)
159 {
160 //抛出匿名异常
161 throw outofrange();
162 }
163 return this->pstart[id];
164 }
165
166 template<class T>
167 void String<T>::replacefirst(const String & oldstr, const String & newstr)
168 {
169 if (strcmp(typeid(T).name(), "char") == 0)
170 {
171 char *p = strstr(reinterpret_cast<char*>(this->pstart), reinterpret_cast<char*>(oldstr.pstart));
172 if (p != nullptr)
173 {
174 int oldlength = strlen(reinterpret_cast<char*>(oldstr.pstart));
175 int newlength = strlen(reinterpret_cast<char*>(newstr.pstart));
176 if (oldlength == newlength)
177 {
178 memcpy(p, newstr.pstart, newstr.length);
179 }
180 else if (oldlength > newlength)
181 {
182 memcpy(p, newstr.pstart, newstr.length);
183 char *pstr = (char *)(p + strlen(reinterpret_cast<char*>(newstr.pstart)));
184 int mlength = oldlength - newlength;
185 while (*(pstr + mlength) != '