Advertisement
Guest User

Untitled

a guest
Oct 18th, 2017
60
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 9.18 KB | None | 0 0
  1. Tim Noye
  2.  
  3. #include "stdafx.h"
  4. #include "myString.h"
  5.  
  6. namespace notstd
  7. {
  8. myString::myString()
  9. : dataPtr(NULL), arrSize(0), currLength(0), minResize(5)
  10. {
  11. }
  12.  
  13. myString::myString(const myString& msIn)
  14. : currLength(msIn.length()), minResize(5)
  15. {
  16. if (currLength == 0)
  17. {
  18. dataPtr = NULL;
  19. arrSize = 0;
  20. }
  21. else
  22. {
  23. arrSize = currLength + minResize - currLength % minResize;
  24. dataPtr = new char[arrSize]();
  25. char *tempPtr = msIn.data();
  26. std::copy(tempPtr, tempPtr + currLength, dataPtr);
  27. delete[] tempPtr;
  28. }
  29. }
  30.  
  31. //pre: charPtrIn must be a NULL terminated array
  32. myString::myString(char* charPtrIn)
  33. : currLength(0), minResize(5)
  34. {
  35. char *cPtr = charPtrIn;
  36.  
  37. while (*(cPtr + currLength) != NULL)
  38. ++currLength;
  39.  
  40. if (currLength == 0)
  41. {
  42. arrSize = 0;
  43. dataPtr = NULL;
  44. }
  45. else
  46. {
  47. arrSize = currLength + minResize - currLength % minResize;
  48. dataPtr = new char[arrSize]();
  49. std::copy(charPtrIn, charPtrIn + currLength, dataPtr);
  50. }
  51. }
  52.  
  53. myString::~myString()
  54. {
  55. delete[] dataPtr;
  56. }
  57.  
  58. myString& myString::operator=(const myString& msIn)
  59. {
  60. if (&msIn != this)
  61. {
  62. if (msIn.length() == 0)
  63. {
  64. delete[] dataPtr;
  65. dataPtr = NULL;
  66. arrSize = 0;
  67. currLength = 0;
  68. }
  69. else
  70. {
  71. currLength = msIn.length();
  72. char *tempPtr = msIn.data();
  73.  
  74. if (msIn.length() > arrSize)
  75. {
  76. arrSize = currLength + minResize - currLength % minResize;
  77. delete[] dataPtr;
  78. dataPtr = new char[arrSize]();
  79. }
  80.  
  81. std::copy(tempPtr, tempPtr + currLength, dataPtr);
  82. delete[] tempPtr;
  83. }
  84. }
  85. return *this;
  86. }
  87.  
  88.  
  89. myString& myString::operator+=(const myString& msIn)
  90. {
  91. if (!msIn.isEmpty())
  92. {
  93. arrSize = msIn.length() + currLength;
  94. arrSize += minResize - (arrSize % minResize);
  95. char* tPtr = new char[arrSize]();
  96. std::copy(dataPtr, dataPtr + currLength, tPtr);
  97. char* msInPtr = msIn.data();
  98. std::copy(msInPtr, msInPtr + msIn.length(), tPtr + currLength);
  99. delete[] dataPtr;
  100. delete[] msInPtr;
  101. dataPtr = tPtr;
  102. currLength = msIn.length() + currLength;
  103. }
  104. return *this;
  105. }
  106.  
  107. myString& myString::operator+=(const char charIn)
  108. {
  109. if (currLength + 1 > arrSize)
  110. {
  111. arrSize += minResize;
  112. char* tPtr = new char[arrSize]();
  113. std::copy(dataPtr, dataPtr + currLength, tPtr);
  114. delete[] dataPtr;
  115. dataPtr = tPtr;
  116. }
  117. dataPtr[currLength] = charIn;
  118. ++currLength;
  119. return *this;
  120. }
  121.  
  122. char& myString::operator[](const size_t indexIn)
  123. {
  124. return dataPtr[indexIn];
  125. }
  126.  
  127. const char& myString::operator[](const size_t indexIn)const
  128. {
  129. return dataPtr[indexIn];
  130. }
  131.  
  132. char& myString::at(const size_t indexIn)
  133. {
  134. return dataPtr[indexIn];
  135. }
  136.  
  137. char& myString::front()
  138. {
  139. return dataPtr[0];
  140. }
  141.  
  142. const char& myString::front()const
  143. {
  144. return dataPtr[0];
  145. }
  146.  
  147. char& myString::back()
  148. {
  149. return dataPtr[currLength - 1];
  150. }
  151.  
  152. const char& myString::back()const
  153. {
  154. return dataPtr[currLength - 1];
  155. }
  156.  
  157. void myString::insert(const size_t posIn, const myString& msIn)
  158. {
  159. if (!msIn.isEmpty() && posIn < currLength)
  160. {
  161. char* cPtr = NULL;
  162. cPtr = msIn.data();
  163. if (currLength + msIn.length() > arrSize)
  164. {
  165. arrSize = msIn.length() + currLength;
  166. arrSize += minResize - (arrSize % minResize);
  167. }
  168. char* tPtr = new char[arrSize]();
  169. std::copy(dataPtr, dataPtr + posIn, tPtr);
  170. std::copy(cPtr, cPtr + msIn.length(), tPtr + posIn);
  171. std::copy(dataPtr + posIn, dataPtr + currLength, tPtr + msIn.length() + posIn);
  172. delete[] dataPtr;
  173. delete[] cPtr;
  174. dataPtr = tPtr;
  175. currLength += msIn.length();
  176. }
  177. }
  178.  
  179. void myString::replace(const size_t fromIn, size_t lenIn, const myString& msIn)
  180. {
  181. if (fromIn <= currLength || (msIn.isEmpty() && lenIn == 0))
  182. {
  183. if (fromIn + lenIn > currLength)
  184. {
  185. lenIn = currLength - fromIn;
  186. }
  187.  
  188. erase(fromIn, lenIn);
  189. insert(fromIn, msIn);
  190. }
  191. }
  192.  
  193. void myString::erase(const size_t posIn, const size_t lenIn)
  194. {
  195. if (currLength != 0 && lenIn > 0 && posIn < currLength)
  196. {
  197. if (posIn + lenIn >= currLength)
  198. {
  199. currLength = posIn;
  200. }
  201. else
  202. {
  203. char* tPtr = new char[arrSize]();
  204. if (posIn == 0)
  205. {
  206. std::copy(dataPtr + lenIn, dataPtr + currLength, tPtr);
  207. }
  208. else
  209. {
  210. std::copy(dataPtr, dataPtr + posIn, tPtr);
  211. std::copy(dataPtr + posIn + lenIn, dataPtr + currLength, tPtr + posIn);
  212. }
  213. delete[] dataPtr;
  214. dataPtr = tPtr;
  215. currLength -= lenIn;
  216. }
  217. }
  218. }
  219.  
  220. void myString::clear()
  221. {
  222. delete[]dataPtr;
  223. dataPtr = NULL;
  224. currLength = 0;
  225. arrSize = 0;
  226. }
  227.  
  228. char* myString::data()const
  229. {
  230. char *tempPtr = new char[currLength + 1]();
  231. std::copy(dataPtr, dataPtr + currLength, tempPtr);
  232. tempPtr[currLength] = NULL;
  233. return tempPtr;
  234. }
  235.  
  236. size_t myString::find(const myString& msIn, const size_t posIn)const
  237. {
  238. bool mismatch;
  239. if (currLength >= posIn + msIn.length())
  240. {
  241. for (size_t datai = posIn; datai <= currLength - msIn.length(); ++datai)
  242. {
  243. mismatch = false;
  244. for (size_t msIni = 0; msIni < msIn.length() && !mismatch; ++msIni)
  245. if (msIn[msIni] != dataPtr[datai + msIni])
  246. mismatch = true;
  247. if (mismatch == false)
  248. return datai;
  249. }
  250. }
  251. return -1;
  252. }
  253.  
  254.  
  255. myString myString::substr(const size_t posIn, size_t lenIn)const
  256. {
  257. if (lenIn == 0 || posIn > currLength)
  258. return myString();
  259.  
  260.  
  261. if (currLength < posIn + lenIn)
  262. lenIn = currLength - posIn;
  263.  
  264. char* tempData;
  265. tempData = new char[lenIn + 1]();
  266. std::copy(dataPtr + posIn, dataPtr + posIn + lenIn, tempData);
  267. myString tempMS(tempData);
  268. delete[] tempData;
  269.  
  270. return tempMS;
  271. }
  272.  
  273. size_t myString::length()const
  274. {
  275. return currLength;
  276. }
  277.  
  278. bool myString::isEmpty()const
  279. {
  280. return currLength == 0;
  281. }
  282.  
  283. myString myString::operator+(const myString& msIn)const
  284. {
  285. myString tempMS(*this);
  286. tempMS += msIn;
  287. return tempMS;
  288. }
  289.  
  290. std::istream& operator >>(std::istream& in, myString& msIn)
  291. {
  292. msIn.clear();
  293. char cIn;
  294. while (in.get(cIn) && cIn != '\n')
  295. {
  296. if (msIn.currLength == msIn.arrSize)
  297. msIn.resize(msIn.arrSize + msIn.minResize);
  298. msIn.dataPtr[msIn.currLength] = cIn;
  299. ++msIn.currLength;
  300. }
  301.  
  302. return in;
  303. }
  304.  
  305. std::ostream& operator <<(std::ostream& out, myString& msIn)
  306. {
  307. char* tempPtr = msIn.data();
  308. out << tempPtr;
  309. delete[] tempPtr;
  310. return out;
  311. }
  312.  
  313. void myString::resize(const size_t newSize)
  314. {
  315. if (newSize > arrSize)
  316. {
  317. char* tempPtr = new char[newSize]();
  318. std::copy(dataPtr, dataPtr + currLength, tempPtr);
  319. delete[] dataPtr;
  320. dataPtr = tempPtr;
  321. arrSize = newSize;
  322. }
  323. }
  324.  
  325. char* myString::begin()
  326. {
  327. return dataPtr;
  328. }
  329.  
  330. char* myString::end()
  331. {
  332. return dataPtr + currLength;
  333. }
  334. //Doesn't check for a string that is empty
  335. char& myString::rsearch(char& charPtrIn, notstd::myString msIn)
  336. {
  337.  
  338. for (size_t i = currLength; i >= 0; i--) {
  339. if (msIn[i] == charPtrIn)
  340. {
  341. return charPtrIn;
  342. }
  343.  
  344. }
  345. }
  346.  
  347. myString& myString::rsearchstring(myString& stringPtrIn, notstd::myString msIn)
  348. {
  349. for (size_t i = currLength; i >= 0; i--) {
  350. if(stringPtrIn == msIn)
  351.  
  352. }
  353. }
  354.  
  355. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement