MyGUI / mygui

Fast, flexible and simple GUI.
http://mygui.info/
Other
713 stars 205 forks source link

Add support for std::string_view #241

Closed Assumeru closed 1 year ago

Assumeru commented 2 years ago

This PR changes UString to have a string_view constructor when compiling with C++17. The change to CMakeLists is because of https://docs.microsoft.com/en-us/cpp/build/reference/zc-cplusplus?view=msvc-170

To make the string_view constructor worthwhile, I changed assign to not allocate a temporary std::string and to operate on const char* + size_t instead. I then also added a constructor for string literals so the const char* constructor doesn't get used for those, this prevents a call to strlen.

I also prevented out of bound accesses that would be caused by invalid UTF-8 sequences.

If MyGUI wants to switch to C++17, the const char*, const std::string&, and const char*, size_t constructors could straight up be replaced by a single string_view constructor.