
#include "sto"

namespace tools {
//////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////////////////
inline std::string value::to_string(const value& aThis){
  std::string _s;
  switch(aThis.m_type) {
  case value::NONE:
    sprintf(_s,5,"(nil)");
    return _s;
  case value::INT:
    sprintf(_s,16,"%d",aThis.u.m_int);
    return _s;
  case value::DOUBLE:
    sprintf(_s,16,"%g",aThis.u.m_double);
    return _s;
  case value::VOID_STAR:
    sprintf(_s,32,upointer_format_x(),(upointer)aThis.u.m_void_star);
    return _s;
  case value::UNSIGNED_SHORT:
    sprintf(_s,16,"%u",aThis.u.m_unsigned_short);
    return _s;
  case value::UNSIGNED_INT:
    sprintf(_s,16,"%u",aThis.u.m_unsigned_int);
    return _s;
  case value::BOOL:
    sprintf(_s,5,"%s",aThis.u.m_bool?"true":"false");
    return _s;
  case value::SHORT:
    sprintf(_s,16,"%d",aThis.u.m_short);
    return _s;
  case value::INT64:{
    sprintf(_s,16,int64_format(),aThis.u.m_int64);
   }return _s;
  case value::UNSIGNED_INT64:{
    sprintf(_s,16,int64_format(),aThis.u.m_unsigned_int64);
   }return _s;
  case value::FLOAT:
    sprintf(_s,16,"%g",aThis.u.m_float);
    return _s;
  case value::DOUBLE_STAR:
    sprintf(_s,32,upointer_format_x(),(upointer)aThis.u.m_double_star);
    return _s;
  case value::FLOAT_STAR:
    sprintf(_s,32,upointer_format_x(),(upointer)aThis.u.m_float_star);
    return _s;
  case value::INT_STAR:
    sprintf(_s,32,upointer_format_x(),(upointer)aThis.u.m_int_star);
    return _s;
  case value::STRING:
    return *aThis.u.m_string;

  case value::ARRAY_UNSIGNED_SHORT:
    if(!nums2s<unsigned short>(aThis.u.m_array_unsigned_short->vector(),_s)) {}
    return _s;
  case value::ARRAY_SHORT:
    if(!nums2s<short>(aThis.u.m_array_short->vector(),_s)) {}
    return _s;

  case value::ARRAY_UNSIGNED_INT:
    if(!nums2s<unsigned int>(aThis.u.m_array_unsigned_int->vector(),_s)) {}
    return _s;
  case value::ARRAY_INT:
    if(!nums2s<int>(aThis.u.m_array_int->vector(),_s)) {}
    return _s;

  case value::ARRAY_UNSIGNED_INT64:
    if(!nums2s<uint64>(aThis.u.m_array_unsigned_int64->vector(),_s)) {}
    return _s;
  case value::ARRAY_INT64:
    if(!nums2s<int64>(aThis.u.m_array_int64->vector(),_s)) {}
    return _s;

  case value::ARRAY_FLOAT:
    if(!nums2s<float>(aThis.u.m_array_float->vector(),_s)) {}
    return _s;
  case value::ARRAY_DOUBLE:
    if(!nums2s<double>(aThis.u.m_array_double->vector(),_s)) {}
    return _s;
  case value::ARRAY_BOOL:
    b2s(aThis.u.m_array_bool->vector(),_s);
    return _s;
  case value::ARRAY_STRING:
    if(!nums2s<std::string>(aThis.u.m_array_string->vector(),_s)) {}
    return _s;
  default:
    return "unknown"; //it should not happen.
  }
}

}
