vector_parser.h 699 B

12345678910111213141516171819202122
  1. #pragma once
  2. #include <util/generic/vector.h>
  3. #include <util/string/split.h>
  4. #include <util/string/strip.h>
  5. //! Converts string to vector of type T variables
  6. template <typename T, typename TStringType, typename TDelim = char>
  7. bool TryParseStringToVector(const TStringType& input, TVector<T>& result, const TDelim delim = ',', const bool useEmpty = true) {
  8. T currentValue;
  9. for (const auto& t : StringSplitter(input).Split(delim)) {
  10. auto sb = StripString(t.Token());
  11. if (!useEmpty && !sb) {
  12. continue;
  13. }
  14. if (!TryFromString<T>(sb, currentValue)) {
  15. return false;
  16. }
  17. result.push_back(currentValue);
  18. }
  19. return true;
  20. }