123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141 |
- #pragma once
- #include <vector>
- #include "DString.hpp"
- #include "Iterator.tlh"
- #include "ReverseIterator.tlh"
- //-----------------------------------------------------------------------------
- // DStringArray
- //-----------------------------------------------------------------------------
- class DStringArray : public std::vector <DString>
- {
- using inherited = std::vector <DString>;
- public:
- // using Iterator = Iterator <std::vector <DString>>;
- public:
- // Nothing different with Array <DString> but add some search functions
- // find string starting at left, -1 if not found
- int Find (const DString & str) const
- {
- for (auto Iter = Iterator::From (*this); Iter; Iter++)
- {
- const auto & s = Iter ();
- if (s == str)
- return Iter.Index ();
- }
- return -1;
- }
- // find string starting at left, -1 if not found
- int NCFind (const DString & str) const
- {
- for (auto Iter = Iterator::From (*this); Iter; Iter++)
- {
- const auto & s = Iter ();
- if (!s.CompareNoCase (str))
- return Iter.Index ();
- }
- return -1;
- }
- int ReverseFind (const DString & str) const
- {
- for (auto Iter = ReverseIterator::From (*this); Iter; Iter++)
- {
- const auto & s = Iter ();
- if (s == str)
- return Iter.Index ();
- }
- return -1;
- }
- bool IsExist (const DString & str) const
- {
- return (Find (str) >= 0);
- }
- bool IsEmpty () const
- {
- return empty ();
- }
- void Add (const DString & str)
- {
- push_back (str);
- }
- void Add (DString && str)
- {
- push_back (std::move (str));
- }
- int GetSize () const
- {
- return (int) size ();
- }
- void Reset ()
- {
- clear ();
- }
- int GetStringLength (void) const
- {
- int Length = 0;
- for (auto Iter = Iterator::From (*this); Iter; Iter++)
- {
- const auto & s = Iter ();
- Length += s.GetLength ();
- }
- return Length;
- }
- virtual DString ToString (void) const
- {
- return ToString (NULL);
- }
- virtual DString ToString (const char * sep) const
- {
- int seplen = (int) strlen (sep);
- int length = 0;
- for (auto Iter = Iterator::From (*this); Iter; Iter++)
- {
- const auto & s = Iter ();
- length += s.GetLength () + 2;
- if (sep)
- length += seplen + 2;
- }
- DString rc;
- rc.GetBuffer (length);
- for (auto Iter = Iterator::From (*this); Iter; Iter++)
- {
- const auto & s = Iter ();
- rc += s;
- if (sep)
- rc += sep;
- }
- return rc;
- }
- };
|