1+ /* *
2+ * CPPShift Memory Pool v2.0.0
3+ *
4+ * Copyright 2020-present Sapir Shemer, DevShift (devshift.biz)
5+ *
6+ * Licensed under the Apache License, Version 2.0 (the "License");
7+ * you may not use this file except in compliance with the License.
8+ * You may obtain a copy of the License at
9+ *
10+ * http://www.apache.org/licenses/LICENSE-2.0
11+ *
12+ * Unless required by applicable law or agreed to in writing, software
13+ * distributed under the License is distributed on an "AS IS" BASIS,
14+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+ * See the License for the specific language governing permissions and
16+ * limitations under the License.
17+ *
18+ * @author Sapir Shemer
19+ */
20+
21+ #include " STDString.h"
22+
23+ namespace CPPShift {
24+ STDString::STDString (const char * str)
25+ {
26+ this ->length = strlen (str);
27+ this ->start = new char [this ->length ];
28+ memcpy (this ->start , str, this ->length );
29+ }
30+
31+ STDString::~STDString () { delete this ->start ; }
32+
33+ char * STDString::data () const { return this ->start ; }
34+
35+ size_t STDString::size () const { return this ->length ; }
36+
37+ STDString& STDString::operator =(const char * str)
38+ {
39+ delete this ->start ;
40+ this ->length = strlen (str);
41+ this ->start = new char [this ->length ];
42+ memcpy (this ->start , str, this ->length );
43+ return *this ;
44+ }
45+
46+ STDString& STDString::operator =(const STDString& str)
47+ {
48+ delete this ->start ;
49+ this ->length = str.size ();
50+ this ->start = new char [this ->length ];
51+ memcpy (this ->start , str.data (), this ->length );
52+ return *this ;
53+ }
54+
55+ STDString& STDString::operator +=(const char * str)
56+ {
57+ int add_length = strlen (str);
58+ char * prev = this ->start ;
59+ this ->start = (char *) realloc (this ->start , this ->length + add_length);
60+ if (this ->start == NULL ) {
61+ this ->start = prev;
62+ return *this ;
63+ }
64+ memcpy (this ->start + this ->length , str, add_length);
65+ this ->length += add_length;
66+ return *this ;
67+ }
68+
69+ STDString& STDString::operator +=(const STDString& str)
70+ {
71+ char * prev = this ->start ;
72+ this ->start = (char *) realloc (this ->start , this ->length + str.size ());
73+ if (start == NULL ) {
74+ this ->start = prev;
75+ return *this ;
76+ }
77+ memcpy (this ->start + this ->length , str.data (), str.size ());
78+ this ->length += str.size ();
79+ return *this ;
80+ }
81+
82+ std::ostream& operator <<(std::ostream& os, const STDString& str)
83+ {
84+ os << str.data ();
85+ os.flush ();
86+ return os;
87+ }
88+ }
0 commit comments