String.cpp 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924
  1. #include "String.h"
  2. USING_NS_BF;
  3. //////////////////////////////////////////////////////////////////////////
  4. static const uint8 sStringCharTab[256] = {
  5. 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F,
  6. 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, 0x18, 0x19, 0x1A, 0x1B, 0x1C, 0x1D, 0x1E, 0x1F,
  7. 0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27, 0x28, 0x29, 0x2A, 0x2B, 0x2C, 0x2D, 0x2E, 0x2F,
  8. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37, 0x38, 0x39, 0x3A, 0x3B, 0x3C, 0x3D, 0x3E, 0x3F,
  9. 0x40, 0x41, 0x42, 0x43, 0x44, 0x45, 0x46, 0x47, 0x48, 0x49, 0x4A, 0x4B, 0x4C, 0x4D, 0x4E, 0x4F,
  10. 0x50, 0x51, 0x52, 0x53, 0x54, 0x55, 0x56, 0x57, 0x58, 0x59, 0x5A, 0x5B, 0x5C, 0x5D, 0x5E, 0x5F,
  11. 0x60, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66, 0x67, 0x68, 0x69, 0x6A, 0x6B, 0x6C, 0x6D, 0x6E, 0x6F,
  12. 0x70, 0x71, 0x72, 0x73, 0x74, 0x75, 0x76, 0x77, 0x78, 0x79, 0x7A, 0x7B, 0x7C, 0x7D, 0x7E, 0x7F,
  13. 0x80, 0x81, 0x82, 0x83, 0x84, 0x85, 0x86, 0x87, 0x88, 0x89, 0x8A, 0x8B, 0x8C, 0x8D, 0x8E, 0x8F,
  14. 0x90, 0x91, 0x92, 0x93, 0x94, 0x95, 0x96, 0x97, 0x98, 0x99, 0x9A, 0x9B, 0x9C, 0x9D, 0x9E, 0x9F,
  15. 0xA0, 0xA1, 0xA2, 0xA3, 0xA4, 0xA5, 0xA6, 0xA7, 0xA8, 0xA9, 0xAA, 0xAB, 0xAC, 0xAD, 0xAE, 0xAF,
  16. 0xB0, 0xB1, 0xB2, 0xB3, 0xB4, 0xB5, 0xB6, 0xB7, 0xB8, 0xB9, 0xBA, 0xBB, 0xBC, 0xBD, 0xBE, 0xBF,
  17. 0xC0, 0xC1, 0xC2, 0xC3, 0xC4, 0xC5, 0xC6, 0xC7, 0xC8, 0xC9, 0xCA, 0xCB, 0xCC, 0xCD, 0xCE, 0xCF,
  18. 0xD0, 0xD1, 0xD2, 0xD3, 0xD4, 0xD5, 0xD6, 0xD7, 0xD8, 0xD9, 0xDA, 0xDB, 0xDC, 0xDD, 0xDE, 0xDF,
  19. 0xE0, 0xE1, 0xE2, 0xE3, 0xE4, 0xE5, 0xE6, 0xE7, 0xE8, 0xE9, 0xEA, 0xEB, 0xEC, 0xED, 0xEE, 0xEF,
  20. 0xF0, 0xF1, 0xF2, 0xF3, 0xF4, 0xF5, 0xF6, 0xF7, 0xF8, 0xF9, 0xFA, 0xFB, 0xFC, 0xFD, 0xFE, 0xFF};
  21. StringView::StringView(const StringImpl& str)
  22. {
  23. mPtr = str.GetPtr();
  24. mLength = str.mLength;
  25. }
  26. StringView::StringView(const StringImpl& str, int offset)
  27. {
  28. mPtr = str.GetPtr() + offset;
  29. mLength = str.mLength - offset;
  30. }
  31. StringView::StringView(const StringImpl& str, int offset, int length)
  32. {
  33. mPtr = str.GetPtr() + offset;
  34. mLength = length;
  35. }
  36. StringView::StringView(char c)
  37. {
  38. mPtr = (const char*)&sStringCharTab[(uint8)c];
  39. mLength = 1;
  40. }
  41. StringView& StringView::operator=(const StringImpl& str)
  42. {
  43. mPtr = str.GetPtr();
  44. mLength = str.mLength;
  45. return *this;
  46. }
  47. bool StringView::operator==(const StringImpl& strB) const
  48. {
  49. if (this->mLength != strB.mLength)
  50. return false;
  51. return strncmp(this->mPtr, strB.GetPtr(), this->mLength) == 0;
  52. }
  53. bool StringView::operator!=(const StringImpl& strB) const
  54. {
  55. if (this->mLength != strB.mLength)
  56. return true;
  57. return strncmp(this->mPtr, strB.GetPtr(), this->mLength) != 0;
  58. }
  59. bool StringView::StartsWith(const StringView& b, StringView::CompareKind comparisonType) const
  60. {
  61. if (this->mLength < b.mLength)
  62. return false;
  63. if (comparisonType == StringView::CompareKind_OrdinalIgnoreCase)
  64. return String::EqualsIgnoreCaseHelper(mPtr, b.mPtr, b.mLength);
  65. return String::EqualsHelper(mPtr, b.mPtr, b.mLength);
  66. }
  67. bool StringView::EndsWith(const StringView& b, StringView::CompareKind comparisonType) const
  68. {
  69. if (this->mLength < b.mLength)
  70. return false;
  71. if (comparisonType == StringView::CompareKind_OrdinalIgnoreCase)
  72. return String::EqualsIgnoreCaseHelper(this->mPtr + this->mLength - b.mLength, b.mPtr, b.mLength);
  73. return String::EqualsHelper(this->mPtr + this->mLength - b.mLength, b.mPtr, b.mLength);
  74. }
  75. intptr StringView::IndexOf(const StringView& subStr, bool ignoreCase) const
  76. {
  77. for (intptr ofs = 0; ofs <= mLength - subStr.mLength; ofs++)
  78. {
  79. if (String::Compare(*this, ofs, subStr, 0, subStr.mLength, ignoreCase) == 0)
  80. return ofs;
  81. }
  82. return -1;
  83. }
  84. intptr StringView::IndexOf(const StringView& subStr, int startIdx) const
  85. {
  86. return IndexOf(subStr, (int64)startIdx);
  87. }
  88. intptr StringView::IndexOf(const StringView& subStr, int64 startIdx) const
  89. {
  90. const char* ptr = mPtr;
  91. const char* subStrPtr = subStr.mPtr;
  92. for (intptr ofs = (intptr)startIdx; ofs <= (intptr)(mLength - subStr.mLength); ofs++)
  93. {
  94. if (strncmp(ptr + ofs, subStrPtr, subStr.mLength) == 0)
  95. return ofs;
  96. }
  97. return -1;
  98. }
  99. intptr StringView::IndexOf(char c, int startIdx) const
  100. {
  101. auto ptr = mPtr;
  102. for (intptr i = startIdx; i < mLength; i++)
  103. if (ptr[i] == c)
  104. return i;
  105. return -1;
  106. }
  107. intptr StringView::IndexOf(char c, int64 startIdx) const
  108. {
  109. auto ptr = mPtr;
  110. for (int64 i = startIdx; i < mLength; i++)
  111. if (ptr[i] == c)
  112. return (intptr)i;
  113. return -1;
  114. }
  115. intptr StringView::LastIndexOf(char c) const
  116. {
  117. auto ptr = mPtr;
  118. for (intptr i = mLength - 1; i >= 0; i--)
  119. if (ptr[i] == c)
  120. return i;
  121. return -1;
  122. }
  123. intptr StringView::LastIndexOf(char c, int startCheck) const
  124. {
  125. auto ptr = mPtr;
  126. for (intptr i = startCheck; i >= 0; i--)
  127. if (ptr[i] == c)
  128. return i;
  129. return -1;
  130. }
  131. intptr StringView::LastIndexOf(char c, int64 startCheck) const
  132. {
  133. auto ptr = mPtr;
  134. for (intptr i = (intptr)startCheck; i >= 0; i--)
  135. if (ptr[i] == c)
  136. return i;
  137. return -1;
  138. }
  139. String StringView::ToString() const
  140. {
  141. return String(this->mPtr, this->mLength);
  142. }
  143. void StringView::ToString(StringImpl& str) const
  144. {
  145. str.Append(mPtr, mLength);
  146. }
  147. StringSplitEnumerator StringView::Split(char c)
  148. {
  149. return StringSplitEnumerator(mPtr, (int)mLength, c, 0x7FFFFFFF, false);
  150. }
  151. //////////////////////////////////////////////////////////////////////////
  152. String Beefy::operator+(const StringImpl& lhs, const StringImpl& rhs)
  153. {
  154. String str;
  155. str.Reserve(lhs.mLength + rhs.mLength + 1);
  156. str.Append(lhs);
  157. str.Append(rhs);
  158. return str;
  159. }
  160. String Beefy::operator+(const StringImpl& lhs, const StringView& rhs)
  161. {
  162. String str;
  163. str.Reserve(lhs.mLength + rhs.mLength + 1);
  164. str.Append(lhs);
  165. str.Append(rhs);
  166. return str;
  167. }
  168. String Beefy::operator+(const StringImpl& lhs, const char* rhs)
  169. {
  170. String str;
  171. int rhsLen = (int)strlen(rhs);
  172. str.Reserve(lhs.mLength + rhsLen + 1);
  173. str.Append(lhs);
  174. str.Append(rhs, rhsLen);
  175. return str;
  176. }
  177. String Beefy::operator+(const StringImpl& lhs, char rhs)
  178. {
  179. String str;
  180. str.Reserve(lhs.mLength + 1 + 1);
  181. str.Append(lhs);
  182. str.Append(rhs);
  183. return str;
  184. }
  185. String Beefy::operator+(const char* lhs, const StringImpl& rhs)
  186. {
  187. String str;
  188. int lhsLen = (int)strlen(lhs);
  189. str.Reserve(rhs.mLength + lhsLen + 1);
  190. str.Append(lhs, lhsLen);
  191. str.Append(rhs);
  192. return str;
  193. }
  194. String Beefy::operator+(const char* lhs, const StringView& rhs)
  195. {
  196. String str;
  197. int lhsLen = (int)strlen(lhs);
  198. str.Reserve(rhs.mLength + lhsLen + 1);
  199. str.Append(lhs, lhsLen);
  200. str.Append(rhs);
  201. return str;
  202. }
  203. bool Beefy::operator==(const char* lhs, const StringImpl& rhs)
  204. {
  205. return rhs == lhs;
  206. }
  207. bool Beefy::operator!=(const char* lhs, const StringImpl& rhs)
  208. {
  209. return rhs != lhs;
  210. }
  211. // bool Beefy::operator==(const StringView& lhs, const StringImpl& rhs)
  212. // {
  213. // if (lhs.mLength != rhs.mLength)
  214. // return false;
  215. // return strncmp(lhs.mPtr, rhs.GetPtr(), lhs.mLength) == 0;
  216. // }
  217. //
  218. // bool Beefy::operator!=(const StringView& lhs, const StringImpl& rhs)
  219. // {
  220. // if (lhs.mLength != rhs.mLength)
  221. // return true;
  222. // return strncmp(lhs.mPtr, rhs.GetPtr(), lhs.mLength) != 0;
  223. // }
  224. //////////////////////////////////////////////////////////////////////////
  225. StringImpl::StringImpl(const StringView& str)
  226. {
  227. Init(str.mPtr, str.mLength);
  228. }
  229. void StringImpl::Reference(const char* str)
  230. {
  231. Reference(str, strlen(str));
  232. }
  233. void StringImpl::Reference(const char* str, intptr length)
  234. {
  235. if (IsDynAlloc())
  236. DeletePtr();
  237. mPtr = (char*)str;
  238. mLength = (int_strsize)length;
  239. mAllocSizeAndFlags = mLength | StrPtrFlag;
  240. }
  241. void StringImpl::Reference(const StringView& strView)
  242. {
  243. Reference(strView.mPtr, strView.mLength);
  244. }
  245. void StringImpl::Reference(const StringImpl& str)
  246. {
  247. Reference(str.GetPtr(), str.mLength);
  248. }
  249. String StringImpl::CreateReference(const StringView& strView)
  250. {
  251. String str;
  252. str.Reference(strView);
  253. return str;
  254. }
  255. intptr StringImpl::CalcNewSize(intptr minSize)
  256. {
  257. // Grow factor is 1.5
  258. intptr bumpSize = GetAllocSize();
  259. bumpSize += bumpSize / 2;
  260. return (bumpSize > minSize) ? bumpSize : minSize;
  261. }
  262. void StringImpl::Realloc(intptr newSize, bool copyStr)
  263. {
  264. BF_ASSERT((uint32)newSize < 0x40000000);
  265. char* newPtr = AllocPtr(newSize);
  266. if (copyStr)
  267. memcpy(newPtr, GetPtr(), mLength + 1);
  268. if (IsDynAlloc())
  269. DeletePtr();
  270. mPtr = newPtr;
  271. mAllocSizeAndFlags = (uint32)newSize | DynAllocFlag | StrPtrFlag;
  272. }
  273. void StringImpl::Realloc(char* newPtr, intptr newSize)
  274. {
  275. BF_ASSERT((uint32)newSize < 0x40000000);
  276. // We purposely don't copy the terminating NULL here, it's assumed the caller will do so
  277. memcpy(newPtr, GetPtr(), mLength);
  278. if (IsDynAlloc())
  279. DeletePtr();
  280. mPtr = newPtr;
  281. mAllocSizeAndFlags = (uint32)newSize | DynAllocFlag | StrPtrFlag;
  282. }
  283. void StringImpl::Reserve(intptr newSize)
  284. {
  285. if (GetAllocSize() < newSize)
  286. Realloc(newSize, true);
  287. }
  288. bool StringImpl::EqualsHelper(const char * a, const char * b, intptr length)
  289. {
  290. return strncmp(a, b, length) == 0;
  291. }
  292. bool StringImpl::EqualsIgnoreCaseHelper(const char * a, const char * b, intptr length)
  293. {
  294. const char* curA = a;
  295. const char* curB = b;
  296. intptr curLength = length;
  297. /*Contract.Requires(strA != null);
  298. Contract.Requires(strB != null);
  299. Contract.EndContractBlock();*/
  300. while (curLength != 0)
  301. {
  302. int_strsize char8A = (int_strsize)*curA;
  303. int_strsize char8B = (int_strsize)*curB;
  304. //Contract.Assert((char8A | char8B) <= 0x7F, "strings have to be ASCII");
  305. // uppercase both char8s - notice that we need just one compare per char8
  306. if ((uint32)(char8A - 'a') <= (uint32)('z' - 'a')) char8A -= 0x20;
  307. if ((uint32)(char8B - 'a') <= (uint32)('z' - 'a')) char8B -= 0x20;
  308. //Return the (case-insensitive) difference between them.
  309. if (char8A != char8B)
  310. return false;
  311. // Next char8
  312. curA++; curB++;
  313. curLength--;
  314. }
  315. return true;
  316. }
  317. int StringImpl::CompareOrdinalIgnoreCaseHelper(const StringImpl & strA, const StringImpl & strB)
  318. {
  319. /*Contract.Requires(strA != null);
  320. Contract.Requires(strB != null);
  321. Contract.EndContractBlock();*/
  322. int_strsize length = BF_MIN(strA.mLength, strB.mLength);
  323. const char* a = strA.GetPtr();
  324. const char* b = strB.GetPtr();
  325. while (length != 0)
  326. {
  327. int_strsize char8A = (int_strsize)*a;
  328. int_strsize char8B = (int_strsize)*b;
  329. //Contract.Assert((char8A | char8B) <= 0x7F, "strings have to be ASCII");
  330. // uppercase both char8s - notice that we need just one compare per char8
  331. if ((uint32)(char8A - 'a') <= (uint32)('z' - 'a')) char8A -= 0x20;
  332. if ((uint32)(char8B - 'a') <= (uint32)('z' - 'a')) char8B -= 0x20;
  333. //Return the (case-insensitive) difference between them.
  334. if (char8A != char8B)
  335. return char8A - char8B;
  336. // Next char8
  337. a++; b++;
  338. length--;
  339. }
  340. return strA.mLength - strB.mLength;
  341. }
  342. intptr StringImpl::CompareOrdinalIgnoreCaseHelper(const char * strA, intptr lengthA, const char * strB, intptr lengthB)
  343. {
  344. const char* a = strA;
  345. const char* b = strB;
  346. intptr length = BF_MIN(lengthA, lengthB);
  347. while (length != 0)
  348. {
  349. int_strsize char8A = (int_strsize)*a;
  350. int_strsize char8B = (int_strsize)*b;
  351. //Contract.Assert((char8A | char8B) <= 0x7F, "strings have to be ASCII");
  352. // uppercase both char8s - notice that we need just one compare per char8
  353. if ((uint32)(char8A - 'a') <= (uint32)('z' - 'a')) char8A -= 0x20;
  354. if ((uint32)(char8B - 'a') <= (uint32)('z' - 'a')) char8B -= 0x20;
  355. //Return the (case-insensitive) difference between them.
  356. if (char8A != char8B)
  357. return char8A - char8B;
  358. // Next char8
  359. a++; b++;
  360. length--;
  361. }
  362. return lengthA - lengthB;
  363. }
  364. intptr StringImpl::CompareOrdinalIgnoreCaseHelper(const StringImpl & strA, intptr indexA, intptr lengthA, const StringImpl & strB, intptr indexB, intptr lengthB)
  365. {
  366. return CompareOrdinalIgnoreCaseHelper(strA.GetPtr() + indexA, lengthA, strB.GetPtr() + indexB, lengthB);
  367. }
  368. intptr StringImpl::CompareOrdinalHelper(const char * strA, intptr lengthA, const char * strB, intptr lengthB)
  369. {
  370. const char* a = strA;
  371. const char* b = strB;
  372. intptr length = BF_MIN(lengthA, lengthB);
  373. while (length != 0)
  374. {
  375. int_strsize char8A = (int_strsize)*a;
  376. int_strsize char8B = (int_strsize)*b;
  377. //Return the (case-insensitive) difference between them.
  378. if (char8A != char8B)
  379. return char8A - char8B;
  380. // Next char8
  381. a++; b++;
  382. length--;
  383. }
  384. return lengthA - lengthB;
  385. }
  386. intptr StringImpl::CompareOrdinalHelper(const StringImpl & strA, intptr indexA, intptr lengthA, const StringImpl & strB, intptr indexB, intptr lengthB)
  387. {
  388. return CompareOrdinalHelper(strA.GetPtr() + indexA, lengthA, strB.GetPtr() + indexB, lengthB);
  389. }
  390. void StringImpl::Append(const char* appendPtr)
  391. {
  392. Append(appendPtr, (int)strlen(appendPtr));
  393. }
  394. void StringImpl::Append(const char* appendPtr, intptr length)
  395. {
  396. intptr newCurrentIndex = mLength + length;
  397. char* ptr;
  398. if (newCurrentIndex >= GetAllocSize())
  399. {
  400. // This handles appending to ourselves, we invalidate 'ptr' after calling Realloc
  401. intptr newSize = CalcNewSize(newCurrentIndex + 1);
  402. char* newPtr = AllocPtr(newSize);
  403. memcpy(newPtr + mLength, appendPtr, length);
  404. Realloc(newPtr, newSize);
  405. ptr = newPtr;
  406. }
  407. else
  408. {
  409. ptr = GetMutablePtr();
  410. memcpy(ptr + mLength, appendPtr, length);
  411. }
  412. mLength = (int_strsize)newCurrentIndex;
  413. ptr[mLength] = 0;
  414. }
  415. void StringImpl::Append(const StringView& value)
  416. {
  417. //Contract.Ensures(Contract.Result<String>() != null);
  418. Append(value.mPtr, value.mLength);
  419. }
  420. void StringImpl::Append(const StringImpl& value)
  421. {
  422. //Contract.Ensures(Contract.Result<String>() != null);
  423. Append(value.GetPtr(), value.mLength);
  424. }
  425. void StringImpl::Append(const StringImpl& str, const StringImpl& str2)
  426. {
  427. Append(str.GetPtr(), str.mLength);
  428. Append(str2.GetPtr(), str2.mLength);
  429. }
  430. void StringImpl::Append(const StringImpl& str, const StringImpl& str2, const StringImpl& str3)
  431. {
  432. Append(str.GetPtr(), str.mLength);
  433. Append(str2.GetPtr(), str2.mLength);
  434. Append(str3.GetPtr(), str3.mLength);
  435. }
  436. void StringImpl::Append(char c, int count)
  437. {
  438. if (count == 0)
  439. return;
  440. if (mLength + count >= GetAllocSize())
  441. Realloc(CalcNewSize(mLength + count + 1));
  442. auto ptr = GetMutablePtr();
  443. for (int_strsize i = 0; i < count; i++)
  444. ptr[mLength++] = c;
  445. ptr[mLength] = 0;
  446. BF_ASSERT(mLength < GetAllocSize());
  447. }
  448. String StringImpl::Substring(intptr startIdx) const
  449. {
  450. BF_ASSERT((uintptr)startIdx <= (uintptr)mLength);
  451. return String(GetPtr() + startIdx, mLength - startIdx);
  452. }
  453. String StringImpl::Substring(intptr startIdx, intptr length) const
  454. {
  455. BF_ASSERT((startIdx >= 0) && (length >= 0) && (startIdx + length <= mLength));
  456. return String(GetPtr() + startIdx, length);
  457. }
  458. void StringImpl::Remove(intptr startIdx, intptr length)
  459. {
  460. BF_ASSERT((startIdx >= 0) && (length >= 0) && (startIdx + length <= mLength));
  461. intptr moveCount = mLength - startIdx - length;
  462. auto ptr = GetMutablePtr();
  463. if (moveCount > 0)
  464. memmove(ptr + startIdx, ptr + startIdx + length, mLength - startIdx - length);
  465. mLength -= (int_strsize)length;
  466. ptr[mLength] = 0;
  467. }
  468. void StringImpl::Remove(intptr char8Idx)
  469. {
  470. Remove(char8Idx, 1);
  471. }
  472. void StringImpl::RemoveToEnd(intptr startIdx)
  473. {
  474. Remove(startIdx, mLength - startIdx);
  475. }
  476. void StringImpl::RemoveFromEnd(intptr length)
  477. {
  478. Remove(mLength - length, length);
  479. }
  480. void StringImpl::Insert(intptr idx, const char* str, intptr length)
  481. {
  482. BF_ASSERT(idx >= 0);
  483. int_strsize newLength = mLength + (int_strsize)length;
  484. if (newLength >= GetAllocSize())
  485. {
  486. intptr newSize = max((int_strsize)GetAllocSize() * 2, newLength + 1);
  487. Realloc(newSize);
  488. }
  489. auto moveChars = mLength - idx;
  490. auto ptr = GetMutablePtr();
  491. if (moveChars > 0)
  492. memmove(ptr + idx + length, ptr + idx, moveChars);
  493. memcpy(ptr + idx, str, length);
  494. mLength = newLength;
  495. ptr[mLength] = 0;
  496. }
  497. void StringImpl::Insert(intptr idx, const StringImpl& addString)
  498. {
  499. BF_ASSERT(idx >= 0);
  500. int_strsize length = addString.mLength;
  501. int_strsize newLength = mLength + length;
  502. if (newLength >= GetAllocSize())
  503. {
  504. intptr newSize = max((int_strsize)GetAllocSize() * 2, newLength + 1);
  505. Realloc(newSize);
  506. }
  507. auto moveChars = mLength - idx;
  508. auto ptr = GetMutablePtr();
  509. if (moveChars > 0)
  510. memmove(ptr + idx + length, ptr + idx, moveChars);
  511. memcpy(ptr + idx, addString.GetPtr(), length);
  512. mLength = newLength;
  513. ptr[mLength] = 0;
  514. }
  515. void StringImpl::Insert(intptr idx, char c)
  516. {
  517. BF_ASSERT(idx >= 0);
  518. int_strsize newLength = mLength + 1;
  519. if (newLength >= GetAllocSize())
  520. {
  521. int newSize = max((int_strsize)GetAllocSize() * 2, newLength + 1);
  522. Realloc(newSize);
  523. }
  524. auto moveChars = mLength - idx;
  525. auto ptr = GetMutablePtr();
  526. if (moveChars > 0)
  527. memmove(ptr + idx + 1, ptr + idx, moveChars);
  528. ptr[idx] = c;
  529. mLength = newLength;
  530. ptr[mLength] = 0;
  531. }
  532. intptr StringImpl::Compare(const StringImpl & strA, intptr indexA, const StringImpl & strB, intptr indexB, intptr length, bool ignoreCase)
  533. {
  534. intptr lengthA = length;
  535. intptr lengthB = length;
  536. if (strA.GetLength() - indexA < lengthA)
  537. {
  538. lengthA = (strA.GetLength() - indexA);
  539. }
  540. if (strB.GetLength() - indexB < lengthB)
  541. {
  542. lengthB = (strB.GetLength() - indexB);
  543. }
  544. if (ignoreCase)
  545. return CompareOrdinalIgnoreCaseHelper(strA, indexA, lengthA, strB, indexB, lengthB);
  546. return CompareOrdinalHelper(strA, indexA, lengthA, strB, indexB, lengthB);
  547. }
  548. void StringImpl::ReplaceLargerHelper(const StringView& find, const StringView& replace)
  549. {
  550. Array<intptr> replaceEntries;
  551. intptr moveOffset = replace.mLength - find.mLength;
  552. for (intptr startIdx = 0; startIdx <= mLength - find.mLength; startIdx++)
  553. {
  554. if (EqualsHelper(GetPtr() + startIdx, find.mPtr, find.mLength))
  555. {
  556. replaceEntries.Add(startIdx);
  557. startIdx += find.mLength - 1;
  558. }
  559. }
  560. if (replaceEntries.size() == 0)
  561. return;
  562. intptr destLength = mLength + moveOffset * replaceEntries.size();
  563. intptr needSize = destLength + 1;
  564. if (needSize > GetAllocSize())
  565. Realloc((int_strsize)needSize);
  566. auto replacePtr = replace.mPtr;
  567. auto ptr = GetMutablePtr();
  568. intptr lastDestStartIdx = destLength;
  569. for (intptr moveIdx = replaceEntries.size() - 1; moveIdx >= 0; moveIdx--)
  570. {
  571. intptr srcStartIdx = replaceEntries[moveIdx];
  572. intptr srcEndIdx = srcStartIdx + find.mLength;
  573. intptr destStartIdx = srcStartIdx + moveIdx * moveOffset;
  574. intptr destEndIdx = destStartIdx + replace.mLength;
  575. for (intptr i = lastDestStartIdx - destEndIdx - 1; i >= 0; i--)
  576. ptr[destEndIdx + i] = ptr[srcEndIdx + i];
  577. for (intptr i = 0; i < replace.mLength; i++)
  578. ptr[destStartIdx + i] = replacePtr[i];
  579. lastDestStartIdx = destStartIdx;
  580. }
  581. ptr[destLength] = 0;
  582. mLength = (int_strsize)destLength;
  583. }
  584. void StringImpl::Replace(char find, char replace)
  585. {
  586. auto ptr = GetMutablePtr();
  587. for (int i = 0; i < mLength; i++)
  588. {
  589. if (ptr[i] == find)
  590. ptr[i] = replace;
  591. }
  592. }
  593. void StringImpl::Replace(const StringView& find, const StringView & replace)
  594. {
  595. if (replace.mLength > find.mLength)
  596. {
  597. ReplaceLargerHelper(find, replace);
  598. return;
  599. }
  600. auto ptr = GetMutablePtr();
  601. auto findPtr = find.mPtr;
  602. auto replacePtr = replace.mPtr;
  603. intptr inIdx = 0;
  604. intptr outIdx = 0;
  605. while (inIdx <= mLength - find.mLength)
  606. {
  607. if (EqualsHelper(ptr + inIdx, findPtr, find.mLength))
  608. {
  609. for (int_strsize i = 0; i < replace.mLength; i++)
  610. ptr[outIdx++] = replacePtr[i];
  611. inIdx += find.mLength;
  612. }
  613. else if (inIdx == outIdx)
  614. {
  615. ++inIdx;
  616. ++outIdx;
  617. }
  618. else // We need to physically move char8acters once we've found an equal span
  619. {
  620. ptr[outIdx++] = ptr[inIdx++];
  621. }
  622. }
  623. while (inIdx < mLength)
  624. {
  625. if (inIdx == outIdx)
  626. {
  627. ++inIdx;
  628. ++outIdx;
  629. }
  630. else
  631. {
  632. ptr[outIdx++] = ptr[inIdx++];
  633. }
  634. }
  635. ptr[outIdx] = 0;
  636. mLength = (int_strsize)outIdx;
  637. }
  638. void StringImpl::TrimEnd()
  639. {
  640. auto ptr = GetPtr();
  641. for (intptr i = mLength - 1; i >= 0; i--)
  642. {
  643. char c = ptr[i];
  644. if (!iswspace(c))
  645. {
  646. if (i < mLength - 1)
  647. RemoveToEnd(i + 1);
  648. return;
  649. }
  650. }
  651. Clear();
  652. }
  653. void StringImpl::TrimStart()
  654. {
  655. auto ptr = GetPtr();
  656. for (intptr i = 0; i < mLength; i++)
  657. {
  658. char c = ptr[i];
  659. if (!iswspace(c))
  660. {
  661. if (i > 0)
  662. Remove(0, i);
  663. return;
  664. }
  665. }
  666. Clear();
  667. }
  668. void StringImpl::Trim()
  669. {
  670. TrimStart();
  671. TrimEnd();
  672. }
  673. bool StringImpl::IsWhitespace() const
  674. {
  675. auto ptr = GetPtr();
  676. for (intptr i = 0; i < mLength; i++)
  677. if (!iswspace(ptr[i]))
  678. return false;
  679. return true;
  680. }
  681. bool StringImpl::HasMultibyteChars()
  682. {
  683. auto ptr = GetPtr();
  684. for (int i = 0; i < (int)mLength; i++)
  685. if ((uint8)ptr[i] >= (uint8)0x80)
  686. return true;
  687. return false;
  688. }
  689. intptr StringImpl::IndexOf(const StringView& subStr, bool ignoreCase) const
  690. {
  691. for (intptr ofs = 0; ofs <= mLength - subStr.mLength; ofs++)
  692. {
  693. if (Compare(*this, ofs, subStr, 0, subStr.mLength, ignoreCase) == 0)
  694. return ofs;
  695. }
  696. return -1;
  697. }
  698. intptr StringImpl::IndexOf(const StringView& subStr, int32 startIdx) const
  699. {
  700. return IndexOf(subStr, (int64)startIdx);
  701. }
  702. intptr StringImpl::IndexOf(const StringView& subStr, int64 startIdx) const
  703. {
  704. const char* ptr = GetPtr();
  705. const char* subStrPtr = subStr.mPtr;
  706. for (intptr ofs = (intptr)startIdx; ofs <= mLength - subStr.mLength; ofs++)
  707. {
  708. if (strncmp(ptr + ofs, subStrPtr, subStr.mLength) == 0)
  709. return ofs;
  710. }
  711. return -1;
  712. }
  713. intptr StringImpl::IndexOf(char c, int startIdx) const
  714. {
  715. auto ptr = GetPtr();
  716. for (intptr i = startIdx; i < mLength; i++)
  717. if (ptr[i] == c)
  718. return i;
  719. return -1;
  720. }
  721. intptr StringImpl::IndexOf(char c, int64 startIdx) const
  722. {
  723. auto ptr = GetPtr();
  724. for (int64 i = startIdx; i < mLength; i++)
  725. if (ptr[i] == c)
  726. return (intptr)i;
  727. return -1;
  728. }
  729. intptr StringImpl::LastIndexOf(char c) const
  730. {
  731. auto ptr = GetPtr();
  732. for (intptr i = mLength - 1; i >= 0; i--)
  733. if (ptr[i] == c)
  734. return i;
  735. return -1;
  736. }
  737. intptr StringImpl::LastIndexOf(char c, intptr startCheck) const
  738. {
  739. auto ptr = GetPtr();
  740. for (intptr i = startCheck; i >= 0; i--)
  741. if (ptr[i] == c)
  742. return i;
  743. return -1;
  744. }
  745. //////////////////////////////////////////////////////////////////////////
  746. UTF16String::UTF16String()
  747. {
  748. }
  749. UTF16String::UTF16String(const wchar_t* str)
  750. {
  751. Set(str);
  752. }
  753. UTF16String::UTF16String(const wchar_t* str, int len)
  754. {
  755. Set(str, len);
  756. }
  757. void UTF16String::Set(const wchar_t* str, int len)
  758. {
  759. Clear();
  760. ResizeRaw(len + 1);
  761. memcpy(mVals, str, len * 2);
  762. mVals[len] = 0;
  763. }
  764. void UTF16String::Set(const wchar_t* str)
  765. {
  766. return Set(str, (int)wcslen(str));
  767. }
  768. const wchar_t* UTF16String::c_str() const
  769. {
  770. if (mVals == NULL)
  771. return L"";
  772. mVals[mSize - 1] = 0; // Re-terminate in case we modified the string
  773. return (wchar_t*)mVals;
  774. }
  775. size_t UTF16String::length() const
  776. {
  777. if (mSize == 0)
  778. return 0;
  779. return mSize - 1;
  780. }