StringList.inl.h 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. // Copyright (C) 2009-2016, Panagiotis Christopoulos Charitos.
  2. // All rights reserved.
  3. // Code licensed under the BSD License.
  4. // http://www.anki3d.org/LICENSE
  5. #include <anki/util/StringList.h>
  6. #include <cstring>
  7. namespace anki
  8. {
  9. //==============================================================================
  10. template<typename TAllocator>
  11. inline void StringList::destroy(TAllocator alloc)
  12. {
  13. auto it = Base::getBegin();
  14. auto endit = Base::getEnd();
  15. for(; it != endit; ++it)
  16. {
  17. (*it).destroy(alloc);
  18. }
  19. Base::destroy(alloc);
  20. }
  21. //==============================================================================
  22. template<typename TAllocator>
  23. inline void StringList::join(
  24. TAllocator alloc, const CString& separator, String& out) const
  25. {
  26. if(Base::isEmpty())
  27. {
  28. return;
  29. }
  30. // Count the characters
  31. const I sepLen = separator.getLength();
  32. I charCount = 0;
  33. for(const String& str : *this)
  34. {
  35. charCount += str.getLength() + sepLen;
  36. }
  37. charCount -= sepLen; // Remove last separator
  38. ANKI_ASSERT(charCount > 0);
  39. // Allocate
  40. out.create(alloc, '?', charCount);
  41. // Append to output
  42. Char* to = &out[0];
  43. typename Base::ConstIterator it = Base::getBegin();
  44. for(; it != Base::getEnd(); it++)
  45. {
  46. const String& from = *it;
  47. std::memcpy(to, &from[0], from.getLength() * sizeof(Char));
  48. to += from.getLength();
  49. if(it != Base::end() - 1)
  50. {
  51. std::memcpy(to, &separator[0], sepLen * sizeof(Char));
  52. to += sepLen;
  53. }
  54. }
  55. }
  56. //==============================================================================
  57. template<typename TAllocator>
  58. inline void StringList::splitString(
  59. TAllocator alloc, const CString& s, const Char separator)
  60. {
  61. ANKI_ASSERT(Base::isEmpty());
  62. const Char* begin = &s[0];
  63. const Char* end = begin;
  64. while(1)
  65. {
  66. if(*end == '\0')
  67. {
  68. if(begin < end)
  69. {
  70. Base::emplaceBack(alloc);
  71. String str;
  72. str.create(alloc, begin, end);
  73. Base::getBack() = std::move(str);
  74. }
  75. break;
  76. }
  77. else if(*end == separator)
  78. {
  79. if(begin < end)
  80. {
  81. Base::emplaceBack(alloc);
  82. String str;
  83. str.create(alloc, begin, end);
  84. Base::getBack() = std::move(str);
  85. begin = end + 1;
  86. }
  87. else
  88. {
  89. ++begin;
  90. }
  91. }
  92. ++end;
  93. }
  94. }
  95. //==============================================================================
  96. template<typename TAllocator, typename... TArgs>
  97. inline void StringList::pushBackSprintf(TAllocator alloc, const TArgs&... args)
  98. {
  99. String str;
  100. str.sprintf(alloc, args...);
  101. Base::emplaceBack(alloc);
  102. Base::getBack() = std::move(str);
  103. }
  104. } // end namespace anki