WStringArray.hpp 2.0 KB

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