/*------------------------------------------------------------------------------
 src/lib/dj/djStr_.h
 
 Copyright (c) 2004-2006 Darby Johnston.
 All rights reserved.
 Use is subject to license terms.
------------------------------------------------------------------------------*/

namespace dj {

inline List<char>::List() :
  _List<char>(),
  _ascii(0)
{}

inline List<char>::List(const List<char> & in) :
  _List<char>(in),
  _ascii(0)
{}

inline List<char>::List(char in) :
  _ascii(0)
{
  add(&in, 1);
}

inline List<char>::List(const char * in, int size) :
  _ascii(0)
{
  if (! in) return;

  const char * p = in;
  if (-1 == size)
    while (*p++) ;
  else
    while (*p++ && size-- > 0) ;
  size = p - in - 1;

  add(in, size);
}

inline List<char>::~List()
{
  if (_ascii)
    delete [] _ascii;
}

inline List<char> & List<char>::operator = (const List<char> & in)
{
  _List<char>::operator = (in);
  return *this;
}

inline const char * List<char>::toAscii() const
{
  List<char> * that = const_cast<List<char> *>(this);
  if (_ascii) delete [] that->_ascii;
  that->_ascii = dj::toAscii(*this);
  return _ascii;
}

inline const Str List<char>::arg(int in, int width, char fill) const
{
  return arg(toStr(in), width, fill);
}

inline const Str List<char>::arg(
  double in, int width, int precision, char fill
) const {
  return arg(toStr(in, precision), width, fill);
}

inline const Str List<char>::arg(const StrList & in, int width, char fill) const
{
  return arg(join(in, ' '), width, fill);
}

inline bool compare(const Str & a, const Str & b, int size)
  { return compareSort(a, b, size) == 0; }
inline bool compareCase(const Str & a, const Str & b, int size)
  { return compareCaseSort(a, b, size) == 0; }

inline bool operator < (const Str & a, const Str & b)
  { return compareSort(a, b) < 0; }
inline bool operator > (const Str & a, const Str & b)
  { return compareSort(a, b) > 0; }

inline char * toAscii(const Str & in)
{
  char * out = new char [in.size() + 1];
  char * p = out;
  for (ConstStrIt it = in; it.isValid(); ++it)
    *p++ = it();
  *p = 0;
  return out;
}

inline int toAscii(const Str & in, char * out, int size, bool terminate)
{
  if (terminate) size--;
  int i = 0;
  for (ConstStrIt it = in; it.isValid() && i < size; ++it, ++i)
    out[i] = it();
  if (terminate) out[i++] = 0;
  return i;
}

template <class T>
inline const StrList toStrList(const List<T> & in)
{
  StrList out;
  for (ConstListIt<T> it = in; it.isValid(); ++it)
    out += toStr(it());
  return out;
}

inline const List<int> toIntList(const StrList & in)
{
  List<int> out;
  for (ConstStrListIt it = in; it.isValid(); ++it)
    out += toInt(it());
  return out;
}

}
