StringViewUtils.h 1.2 KB

1234567891011121314151617181920212223242526272829303132333435
  1. #pragma once
  2. #include "il2cpp-config.h"
  3. #include <string>
  4. #include "StringView.h"
  5. #define STRING_TO_STRINGVIEW(sv) il2cpp::utils::StringViewUtils::StringToStringView(sv)
  6. namespace il2cpp
  7. {
  8. namespace utils
  9. {
  10. class StringViewUtils
  11. {
  12. public:
  13. template<typename CharType, typename CharTraits, typename StringAlloc>
  14. static StringView<CharType> StringToStringView(const std::basic_string<CharType, CharTraits, StringAlloc>& str)
  15. {
  16. return StringView<CharType>(str.c_str(), str.length());
  17. }
  18. // This will prevent accidentally assigning temporary values (like function return values)
  19. // to a string view. While this protection will only be enabled on C++11 compiles, even those
  20. // are enough to catch the bug in our runtime
  21. #if IL2CPP_HAS_DELETED_FUNCTIONS
  22. template<typename CharType, typename CharTraits, typename StringAlloc>
  23. static StringView<CharType> StringToStringView(const std::basic_string<CharType, CharTraits, StringAlloc>&& str)
  24. {
  25. IL2CPP_ASSERT(0 && "Cannot create stringview into R-value reference");
  26. return StringView<CharType>::Empty();
  27. }
  28. #endif
  29. };
  30. }
  31. }