/system/source/libraries/spoon/support/String.cpp

https://gitlab.com/adam.lukaitis/spoon · C++ · 1101 lines · 716 code · 361 blank · 24 comment · 63 complexity · 4d99aa655abd6abf7968d6c0d09d7e56 MD5 · raw file

  1. #include <types.h>
  2. #include <string.h>
  3. #include <stdlib.h>
  4. #include <stdio.h>
  5. #include <spoon/support/String.h>
  6. String::String()
  7. : buffer(NULL)
  8. {
  9. buffer = strdup("");
  10. length = 0;
  11. max = -1;
  12. }
  13. String::String(const char *string)
  14. : buffer(NULL)
  15. {
  16. SetTo( string );
  17. }
  18. String::String(const char *string, int32 maxLength)
  19. : buffer(NULL)
  20. {
  21. max = maxLength;
  22. SetTo( string );
  23. }
  24. String::String(const String &string)
  25. : buffer(NULL)
  26. {
  27. SetTo( string );
  28. }
  29. String::~String()
  30. {
  31. if ( this->buffer != NULL ) free( this->buffer );
  32. }
  33. String &String::Append(const String &source)
  34. {
  35. int32 new_length;
  36. char *news;
  37. if ( source.length <= 0 ) return *this;
  38. new_length = length + source.length + 1;
  39. news = (char*)malloc( new_length );
  40. strcpy( news, buffer );
  41. strcat( news, source.buffer );
  42. free( buffer );
  43. buffer = news;
  44. length = new_length - 1;
  45. return *this;
  46. }
  47. String &String::Append(const String &source, int32 charCount)
  48. {
  49. int32 new_length;
  50. char *news;
  51. int32 nl;
  52. nl = ((String)source).Length();
  53. if ( charCount < nl ) nl = charCount;
  54. new_length = length + nl + 1;
  55. news = (char*)malloc( new_length );
  56. strcpy( news, buffer );
  57. strncat( news, source.buffer, nl );
  58. free( buffer );
  59. buffer = news;
  60. length = strlen( buffer );
  61. return *this;
  62. }
  63. String &String::Append(const char *source)
  64. {
  65. int32 new_length;
  66. char *news;
  67. new_length = length + strlen( source );
  68. news = (char*)malloc( new_length + 1 );
  69. strcpy( news, buffer );
  70. strcat( news, source );
  71. free( buffer );
  72. buffer = news;
  73. length = strlen( buffer );
  74. return *this;
  75. }
  76. String &String::Append(const char *source, int32 charCount)
  77. {
  78. int32 new_length;
  79. char *news;
  80. int nl;
  81. nl = strlen( source );
  82. if ( charCount < nl ) nl = charCount;
  83. new_length = length + nl;
  84. news = (char*)malloc( new_length + 1 );
  85. strcpy( news, buffer );
  86. strncat( news, source, nl );
  87. free( buffer );
  88. buffer = news;
  89. length = strlen( buffer );
  90. return *this;
  91. }
  92. String &String::Append(char c, int32 charCount)
  93. {
  94. return *this;
  95. }
  96. String &String::Prepend(const String &source)
  97. {
  98. return *this;
  99. }
  100. String &String::Prepend(const String &source, int32 charCount)
  101. {
  102. return *this;
  103. }
  104. String &String::Prepend(const char *source)
  105. {
  106. return *this;
  107. }
  108. String &String::Prepend(const char *source, int32 charCount)
  109. {
  110. return *this;
  111. }
  112. String &String::Prepend(char c, int32 charCount)
  113. {
  114. return *this;
  115. }
  116. String &String::Insert(const String &source, int32 insertAt)
  117. {
  118. return *this;
  119. }
  120. String &String::Insert(const String &source, int32 charCount, int32 insertAt)
  121. {
  122. return *this;
  123. }
  124. String &String::Insert(const String &source, int32 sourceOffset, int32 charCount, int32 insertAt)
  125. {
  126. return *this;
  127. }
  128. String &String::Insert(const char *source, int32 insertAt)
  129. {
  130. if ( insertAt >= Length() ) return Append(source);
  131. int new_len = Length() + strlen(source) + 1;
  132. int src_len = strlen(source);
  133. char *new_buf = (char*)malloc( new_len );
  134. for ( int i = 0; i < insertAt; i++ )
  135. new_buf[i] = buffer[i];
  136. for ( int j = 0; j < src_len; j++ )
  137. new_buf[j + insertAt] = source[j];
  138. for ( int i = 0; i < insertAt; i++ )
  139. new_buf[i + insertAt + src_len] = buffer[i + insertAt];
  140. new_buf[ new_len - 1 ] = 0;
  141. delete buffer;
  142. buffer = new_buf;
  143. return *this;
  144. }
  145. String &String::Insert(const char *source, int32 charCount, int32 insertAt)
  146. {
  147. return *this;
  148. }
  149. String &String::Insert(const char *source, int32 sourceOffset, int32 charCount, int32 insertAt)
  150. {
  151. return *this;
  152. }
  153. String &String::Insert(char c, int32 charCount, int32 insertAt)
  154. {
  155. return *this;
  156. }
  157. String &String::CharacterEscape(const char *original,
  158. const char *setOfCharsToEscape,
  159. char escapeWithChar)
  160. {
  161. return *this;
  162. }
  163. String &String::CharacterEscape(const char *setOfCharsToEscape, char escapeWithChar)
  164. {
  165. return *this;
  166. }
  167. String &String::CharacterDeescape(const char *original, char escapeWithChar)
  168. {
  169. return *this;
  170. }
  171. String &String::CharacterDeescape(char escapeWithChar)
  172. {
  173. return *this;
  174. }
  175. int String::Compare(const String &string)
  176. {
  177. return 0;
  178. }
  179. int String::Compare(const String &string, int32 range)
  180. {
  181. return 0;
  182. }
  183. int String::Compare(const char *string)
  184. {
  185. return 0;
  186. }
  187. int String::Compare(const char *string, int32 range)
  188. {
  189. return 0;
  190. }
  191. int String::Compare(const String &astring, const String &bstring)
  192. {
  193. return 0;
  194. }
  195. int String::Compare(const String *astring, const String *bstring)
  196. {
  197. return 0;
  198. }
  199. int String::ICompare(const String &string)
  200. {
  201. return 0;
  202. }
  203. int String::ICompare(const String &string, int32 range)
  204. {
  205. return 0;
  206. }
  207. int String::ICompare(const char *string)
  208. {
  209. return 0;
  210. }
  211. int String::ICompare(const char *string, int32 range)
  212. {
  213. return 0;
  214. }
  215. int String::ICompare(const String &astring, const String &bstring)
  216. {
  217. return 0;
  218. }
  219. int String::ICompare(const String *astring, const String *bstring)
  220. {
  221. return 0;
  222. }
  223. String &String::CopyInto(String &destination, int32 sourceOffset,
  224. int32 charCount)
  225. {
  226. return *this;
  227. }
  228. void String::CopyInto(char *destination, int32 sourceOffset, int32 charCount)
  229. {
  230. }
  231. String &String::MoveInto(String &destination, int32 sourceOffset, int32 charCount)
  232. {
  233. return *this;
  234. }
  235. void String::MoveInto(char *destination, int32 sourceOffset, int32 charCount)
  236. {
  237. }
  238. int32 String::CountChars()
  239. {
  240. return strlen( buffer );
  241. }
  242. int32 String::Length()
  243. {
  244. return strlen( buffer );
  245. }
  246. int32 String::FindFirst(const String &string)
  247. {
  248. return 0;
  249. }
  250. int32 String::FindFirst(const String &string, int32 offset)
  251. {
  252. return 0;
  253. }
  254. int32 String::FindFirst(const char *string)
  255. {
  256. return 0;
  257. }
  258. int32 String::FindFirst(const char *string, int32 offset)
  259. {
  260. return 0;
  261. }
  262. int32 String::FindFirst(char c)
  263. {
  264. return 0;
  265. }
  266. int32 String::FindFirst(char c, int32 offset)
  267. {
  268. return 0;
  269. }
  270. int32 String::FindLast(const String &string)
  271. {
  272. return 0;
  273. }
  274. int32 String::FindLast(const String &string, int32 offset)
  275. {
  276. return 0;
  277. }
  278. int32 String::FindLast(const char *string)
  279. {
  280. return 0;
  281. }
  282. int32 String::FindLast(const char *string, int32 offset)
  283. {
  284. return 0;
  285. }
  286. int32 String::FindLast(char c)
  287. {
  288. return 0;
  289. }
  290. int32 String::FindLast(char c, int32 offset)
  291. {
  292. return 0;
  293. }
  294. int32 String::IFindFirst(const String &string)
  295. {
  296. return 0;
  297. }
  298. int32 String::IFindFirst(const String &string, int32 offset)
  299. {
  300. return 0;
  301. }
  302. int32 String::IFindFirst(const char *string)
  303. {
  304. return 0;
  305. }
  306. int32 String::IFindFirst(const char *string, int32 offset)
  307. {
  308. return 0;
  309. }
  310. int32 String::IFindLast(const String &string)
  311. {
  312. return 0;
  313. }
  314. int32 String::IFindLast(const String &string, int32 offset)
  315. {
  316. return 0;
  317. }
  318. int32 String::IFindLast(const char *string)
  319. {
  320. return 0;
  321. }
  322. int32 String::IFindLast(const char *string, int32 offset)
  323. {
  324. return 0;
  325. }
  326. char *String::Lockuffer(int32 maxLength)
  327. {
  328. return NULL;
  329. }
  330. String &String::Unlockuffer(int32 length)
  331. {
  332. return *this;
  333. }
  334. String &String::Remove(int32 startingAt, int32 charCount)
  335. {
  336. return *this;
  337. }
  338. String &String::RemoveFirst(const String &string)
  339. {
  340. return *this;
  341. }
  342. String &String::RemoveFirst(const char *string)
  343. {
  344. return *this;
  345. }
  346. String &String::RemoveLast(const String &string)
  347. {
  348. return *this;
  349. }
  350. String &String::RemoveLast(const char *string)
  351. {
  352. return *this;
  353. }
  354. String &String::RemoveAll(const String &string)
  355. {
  356. return *this;
  357. }
  358. String &String::RemoveAll(const char *string)
  359. {
  360. return *this;
  361. }
  362. String &String::RemoveSetTo(const char *charSet)
  363. {
  364. return *this;
  365. }
  366. String &String::Replace(const char *old, const char *nuwe, int32 count, int32 offset)
  367. {
  368. return *this;
  369. }
  370. String &String::Replace(char old, char nuwe, int32 count, int32 offset)
  371. {
  372. return *this;
  373. }
  374. String &String::ReplaceFirst(const char *old, const char *nuwe)
  375. {
  376. return *this;
  377. }
  378. String &String::ReplaceFirst(char old, char nuwe)
  379. {
  380. return *this;
  381. }
  382. String &String::ReplaceLast(const char *old, const char *nuwe)
  383. {
  384. return *this;
  385. }
  386. String &String::ReplaceLast(char old, char nuwe)
  387. {
  388. return *this;
  389. }
  390. String &String::ReplaceAll(const char *old, const char *nuwe, int32 offset)
  391. {
  392. return *this;
  393. }
  394. String &String::ReplaceAll(char old, char nuwe, int32 offset)
  395. {
  396. return *this;
  397. }
  398. String &String::ReplaceSetTo(const char *oldSet, const char *nuwe)
  399. {
  400. return *this;
  401. }
  402. String &String::ReplaceSetTo(const char *oldSet, char nuwe)
  403. {
  404. return *this;
  405. }
  406. String &String::IReplace(const char *old, const char *nuwe, int32 count, int32 offset)
  407. {
  408. return *this;
  409. }
  410. String &String::IReplace(char old, char nuwe, int32 count, int32 offset)
  411. {
  412. return *this;
  413. }
  414. String &String::IReplaceFirst(const char *old, const char *nuwe)
  415. {
  416. return *this;
  417. }
  418. String &String::IReplaceFirst(char old, char nuwe)
  419. {
  420. return *this;
  421. }
  422. String &String::IReplaceLast(const char *old, const char *nuwe)
  423. {
  424. return *this;
  425. }
  426. String &String::IReplaceLast(char old, char nuwe)
  427. {
  428. return *this;
  429. }
  430. String &String::IReplaceAll(const char *old, const char *nuwe, int32 offset)
  431. {
  432. return *this;
  433. }
  434. String &String::IReplaceAll(char old, char nuwe, int32 offset)
  435. {
  436. return *this;
  437. }
  438. String &String::SetTo(const char *source)
  439. {
  440. buffer = strdup( source );
  441. length = strlen( buffer );
  442. max = 0;
  443. return *this;
  444. }
  445. String &String::SetTo(const char *source, int32 charCount)
  446. {
  447. max = charCount;
  448. buffer = strdup( source );
  449. length = strlen( buffer );
  450. return *this;
  451. }
  452. String &String::SetTo(const String &source)
  453. {
  454. max = source.max;
  455. buffer = strdup( source.buffer );
  456. length = source.length;
  457. return *this;
  458. }
  459. String &String::SetTo(const String &source, int32 charCount)
  460. {
  461. max = charCount;
  462. buffer = strdup( source.buffer );
  463. length = source.length;
  464. return *this;
  465. }
  466. String &String::SetTo(char c, int32 charCount)
  467. {
  468. char b[2];
  469. b[0] = c;
  470. b[1] = 0;
  471. SetTo( b, charCount );
  472. return *this;
  473. }
  474. String &String::Adopt(String &source)
  475. {
  476. return *this;
  477. }
  478. String &String::Adopt(String &source, int32 charCount)
  479. {
  480. return *this;
  481. }
  482. const char* String::GetString()
  483. {
  484. return (const char*)buffer;
  485. }
  486. char String::ByteAt(int32 index)
  487. {
  488. if( index >= Length() ) return 0;
  489. return buffer[index];
  490. }
  491. String &String::ToLower()
  492. {
  493. int i;
  494. int len = strlen(buffer);
  495. for ( i = 0; i < len; i++)
  496. {
  497. if ( (buffer[i] <= 'Z') && (buffer[i] >= 'A')) buffer[i] = buffer[i] + 'a' - 'A';
  498. }
  499. return *this;
  500. }
  501. String &String::ToUpper()
  502. {
  503. int i;
  504. int len = strlen(buffer);
  505. for ( i = 0; i < len; i++)
  506. {
  507. if ( (buffer[i] <= 'z') && (buffer[i] >= 'a')) buffer[i] = buffer[i] + 'A' - 'a';
  508. }
  509. return *this;
  510. }
  511. String &String::Capitalize()
  512. {
  513. return *this;
  514. }
  515. String &String::CapitalizeEachWord()
  516. {
  517. return *this;
  518. }
  519. String &String::Truncate(int32 charCount, bool lazy)
  520. {
  521. if ( charCount >= Length() ) return *this;
  522. buffer[charCount] = 0;
  523. return *this;
  524. }
  525. String& String::operator=(const String &string)
  526. {
  527. SetTo( string );
  528. return *this;
  529. }
  530. String& String::operator=(const char *string)
  531. {
  532. SetTo( string );
  533. return *this;
  534. }
  535. String& String::operator=(const char character)
  536. {
  537. char bob[2];
  538. bob[0] = character;
  539. bob[1] = 0;
  540. SetTo( bob );
  541. return *this;
  542. }
  543. String& String::operator+=(const String &string)
  544. {
  545. Append( string );
  546. return *this;
  547. }
  548. String& String::operator+=(const char *string)
  549. {
  550. Append( string );
  551. return *this;
  552. }
  553. String& String::operator+=(const char character)
  554. {
  555. char bob[2];
  556. bob[0] = character;
  557. bob[1] = 0;
  558. Append( bob );
  559. return *this;
  560. }
  561. String &String::operator<<(const char *string)
  562. {
  563. Append( string );
  564. return *this;
  565. }
  566. String &String::operator<<(const String &string)
  567. {
  568. Append( string );
  569. return *this;
  570. }
  571. String &String::operator<<(char c)
  572. {
  573. char bob[2];
  574. bob[0] = c;
  575. bob[1] = 0;
  576. Append( bob );
  577. return *this;
  578. }
  579. String &String::operator<<(uint32 val)
  580. {
  581. char number[32];
  582. sprintf(number, "%u", val );
  583. Append( number );
  584. return *this;
  585. }
  586. String &String::operator<<(int32 val)
  587. {
  588. char number[32];
  589. sprintf(number, "%i", val );
  590. Append( number );
  591. return *this;
  592. }
  593. String &String::operator<<(uint64 val)
  594. {
  595. char number[32];
  596. sprintf(number, "%u", val );
  597. Append( number );
  598. return *this;
  599. }
  600. String &String::operator<<(int64 val)
  601. {
  602. char number[32];
  603. sprintf(number, "%i", val );
  604. Append( number );
  605. return *this;
  606. }
  607. String &String::operator<<(float val)
  608. {
  609. char number[32];
  610. sprintf(number, "%f", val );
  611. Append( number );
  612. return *this;
  613. }
  614. char String::operator[](int32 index)
  615. {
  616. return ByteAt( index );
  617. }
  618. //char &String::operator[](int32 index)
  619. //{
  620. // char *heck;
  621. // heck = &(buffer[index]);
  622. // return *heck;
  623. //}
  624. bool String::operator==(const String &string)
  625. {
  626. if ( string.length != length ) return false;
  627. if ( string.length == 0 ) return true;
  628. if ( strcmp( buffer, string.buffer ) == 0 ) return true;
  629. return false;
  630. }
  631. bool String::operator==(const char *string)
  632. {
  633. if ( (string == NULL) && (buffer == NULL) ) return true;
  634. if ( (string == NULL) || (buffer == NULL) ) return false;
  635. if ( (int)strlen( string ) != length ) return false;
  636. if ( strcmp( buffer, string ) == 0 ) return true;
  637. return false;
  638. }
  639. //bool String::operator==(const char *string, String &string)
  640. //{
  641. //}
  642. bool String::operator!=(const String &string)
  643. {
  644. return ( ! (*this == string) );
  645. }
  646. bool String::operator!=(const char *string)
  647. {
  648. return ( ! (*this == string) );
  649. }
  650. //bool String::operator!=(const char *string, String &string)
  651. //{
  652. //}
  653. bool String::operator<(const String &string)
  654. {
  655. int leftl = length;
  656. int rightl = string.length;
  657. int i = 0;
  658. while ( (i < leftl) && (i < rightl) )
  659. {
  660. if ( buffer[i] < string.buffer[i] ) return true;
  661. if ( buffer[i] > string.buffer[i] ) return false;
  662. i++;
  663. }
  664. if ( leftl < rightl ) return true;
  665. return false;
  666. }
  667. bool String::operator<(const char *string)
  668. {
  669. int leftl = length;
  670. int rightl = strlen( string );
  671. int i = 0;
  672. while ( (i < leftl) && (i < rightl) )
  673. {
  674. if ( buffer[i] < string[i] ) return true;
  675. if ( buffer[i] > string[i] ) return false;
  676. i++;
  677. }
  678. if ( leftl < rightl ) return true;
  679. return false;
  680. }
  681. //bool String::operator<(const char *string, String &string)
  682. //{
  683. //}
  684. bool String::operator<=(const String &string)
  685. {
  686. if ( length != string.length )
  687. return (*this < string);
  688. return (*this == string);
  689. }
  690. bool String::operator<=(const char *string)
  691. {
  692. if ( length != (int)strlen( string ) )
  693. return (*this < string);
  694. return (*this == string);
  695. }
  696. //bool String::operator<=(const char *string, String &string)
  697. //{
  698. //}
  699. bool String::operator>(const String &string)
  700. {
  701. int leftl = length;
  702. int rightl = string.length;
  703. int i = 0;
  704. while ( (i < leftl) && (i < rightl) )
  705. {
  706. if ( buffer[i] < string.buffer[i] ) return false;
  707. if ( buffer[i] > string.buffer[i] ) return true;
  708. i++;
  709. }
  710. if ( leftl > rightl ) return true;
  711. return false;
  712. }
  713. bool String::operator>(const char *string)
  714. {
  715. int leftl = length;
  716. int rightl = strlen( string );
  717. int i = 0;
  718. while ( (i < leftl) && (i < rightl) )
  719. {
  720. if ( buffer[i] < string[i] ) return false;
  721. if ( buffer[i] > string[i] ) return true;
  722. i++;
  723. }
  724. if ( leftl > rightl ) return true;
  725. return false;
  726. }
  727. //bool String::operator>(const char *string, String &string)
  728. //{
  729. //}
  730. bool String::operator>=(const String &string)
  731. {
  732. return (!(*this < string) );
  733. }
  734. bool String::operator>=(const char *string)
  735. {
  736. return (!(*this < string) );
  737. }
  738. //bool String::operator>=(const char *string, String &string)
  739. //{
  740. //}