DStringArray.hpp 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. #pragma once
  2. #include <vector>
  3. #include "DString.hpp"
  4. #include "Iterator.tlh"
  5. #include "ReverseIterator.tlh"
  6. //-----------------------------------------------------------------------------
  7. // DStringArray
  8. //-----------------------------------------------------------------------------
  9. class DStringArray : public std::vector <DString>
  10. {
  11. using inherited = std::vector <DString>;
  12. public:
  13. // using Iterator = Iterator <std::vector <DString>>;
  14. public:
  15. // Nothing different with Array <DString> but add some search functions
  16. // find string starting at left, -1 if not found
  17. int Find (const DString & str) const
  18. {
  19. for (auto Iter = Iterator::From (*this); Iter; Iter++)
  20. {
  21. const auto & s = Iter ();
  22. if (s == str)
  23. return Iter.Index ();
  24. }
  25. return -1;
  26. }
  27. // find string starting at left, -1 if not found
  28. int NCFind (const DString & str) const
  29. {
  30. for (auto Iter = Iterator::From (*this); Iter; Iter++)
  31. {
  32. const auto & s = Iter ();
  33. if (!s.CompareNoCase (str))
  34. return Iter.Index ();
  35. }
  36. return -1;
  37. }
  38. int ReverseFind (const DString & str) const
  39. {
  40. for (auto Iter = ReverseIterator::From (*this); Iter; Iter++)
  41. {
  42. const auto & s = Iter ();
  43. if (s == str)
  44. return Iter.Index ();
  45. }
  46. return -1;
  47. }
  48. bool IsExist (const DString & str) const
  49. {
  50. return (Find (str) >= 0);
  51. }
  52. bool IsEmpty () const
  53. {
  54. return empty ();
  55. }
  56. void Add (const DString & str)
  57. {
  58. push_back (str);
  59. }
  60. void Add (DString && str)
  61. {
  62. push_back (std::move (str));
  63. }
  64. int GetSize () const
  65. {
  66. return (int) size ();
  67. }
  68. void Reset ()
  69. {
  70. clear ();
  71. }
  72. int GetStringLength (void) const
  73. {
  74. int Length = 0;
  75. for (auto Iter = Iterator::From (*this); Iter; Iter++)
  76. {
  77. const auto & s = Iter ();
  78. Length += s.GetLength ();
  79. }
  80. return Length;
  81. }
  82. virtual DString ToString (void) const
  83. {
  84. return ToString (NULL);
  85. }
  86. virtual DString ToString (const char * sep) const
  87. {
  88. int seplen = (int) strlen (sep);
  89. int length = 0;
  90. for (auto Iter = Iterator::From (*this); Iter; Iter++)
  91. {
  92. const auto & s = Iter ();
  93. length += s.GetLength () + 2;
  94. if (sep)
  95. length += seplen + 2;
  96. }
  97. DString rc;
  98. rc.GetBuffer (length);
  99. for (auto Iter = Iterator::From (*this); Iter; Iter++)
  100. {
  101. const auto & s = Iter ();
  102. rc += s;
  103. if (sep)
  104. rc += sep;
  105. }
  106. return rc;
  107. }
  108. };