We do not need to copy the c++ standard, they do indeed have very diferente requirements than the Arduino framework. But we can learn from it while making na appropriate API that’s closer to String’s API. And while I agree newcomers may have a hard time with ownership and lifetimes we can explicit in the documentation that a StringView should not be stored around, or just not used if you don’t know what you are doing.
We should just make it a null terminated non-owning immutable string with String’s API. Most methods don’t need a String, but a StringView anyway.
With implicit constructors for general string types the regular inexperienced user won’t even notice, and the more experienced ones will make the wildly used libs taking that advantage.
Also a baked type to the Arduino environment is a must, otherwise integration with different libraries won’t be easy.
There is an open PR with an implementation already but it seems abandoned. I also have an implementation we have been using in production for some time that may serve as a start. But SafeString can also serve as one. Honestly to me the most importante thing is having a StringView API upstream Arduino, I’m not stuck in a specific implementation. Whatever works best for Arduino while allowing us to use PROGMEM char*, some other char*, String, or even std::string in the environments that’s available.
But I am indeed prepared to offer a PR to solve it, at least with a minimal implementation.
That improves integration since the user can be from whatever environment, the function doesn’t care who implemented the string they are using, they just have to convert it to a StringView. And builtin strings will do it implicitly.
I don’t see how libraries using StringView in methods can cause trouble to newcomers, they won’t even notice. And a comment can be enough to scare them away from using it wrong. I would argue it’s a safety tool more than a risk. A lot of newcomers have this naive idea that String’s are bad and use char* everywhere, which is even less type-safe without understanding ownership.
Paulo
--
You received this message because you are subscribed to the Google Groups "Developers" group.
To unsubscribe from this group and stop receiving emails from it, send an email to developers+...@arduino.cc.
To view this discussion on the web visit https://groups.google.com/a/arduino.cc/d/msgid/developers/5d7ec37e-5670-76b6-87ad-4e058a55cd0d%40forward.com.au.