emscripten icon indicating copy to clipboard operation
emscripten copied to clipboard

std::string_view initialization support in val.h

Open kkimdev opened this issue 7 years ago • 7 comments

Currently val class has a few constructors for different basic types. It would be nice to have std::string_view type constructor too. The only difference with const char* is that std::string_view is not necessarily null-terminated, instead, it holds the length information.

I guess it's a very low priority feature request. But is this something acceptable if someone implements and opens a PR?

kkimdev avatar Jun 01 '18 21:06 kkimdev

An alternative API would be a val constructor with two arguments. 1. const char* p and 2. std::size_t length for non-null terminated strings in general. Maybe this makes more sense.

kkimdev avatar Jun 01 '18 21:06 kkimdev

Sounds reasonable to me; string_views would be super useful in parsing markup kinds of cases, so not having to copy to a C string first will be a plus if you have to push a lot of data around!

Existing constructor for null-terminated strings ends up calling a JS function that is specific to null-terminated strings (readLatin1String in embind.js/emval.js) but it would be easy to add a variant that takes a length and call through to that.

bvibber avatar Jun 02 '18 02:06 bvibber

This issue has been automatically marked as stale because there has been no activity in the past year. It will be closed automatically if no further activity occurs in the next 7 days. Feel free to re-open at any time if this issue is still relevant.

stale[bot] avatar Sep 18 '19 23:09 stale[bot]

If there are no objections I'd like to pick up implementing this.

reillyeon avatar Apr 09 '25 04:04 reillyeon

@brendandahl what do you think?

kripken avatar Apr 09 '25 18:04 kripken

Seems reasonable. Out of curiosity, what's the use case you're interested in?

brendandahl avatar Apr 09 '25 18:04 brendandahl

Pragmatically, my use case is getting reviewers to stop linking me to https://abseil.io/tips/1 when I'm using emscripten::val however the reasons behind that tip apply: There are more efficient operations available with string views (e.g. zero-copy splits) which are unavailable if in the end the value needs to be converted to an emscripten::val.

reillyeon avatar Apr 11 '25 20:04 reillyeon