#zstring_view

1 messages ยท Page 1 of 1 (latest)

tribal yew
gusty hawk
tribal yew
#

The order makes some sense I think, special member functions then other constructors

gusty hawk
#

interesting

shut summit
#

so it's just the smf grouped together I suppose

gusty hawk
waxen marlin
#

(npos - sizeof(size_type) - sizeof(void*)) / sizeof(value_type) / 4?

shut summit
#

heh, I've just been thinking about that as well

tribal yew
#

libstdc++'s string_view

waxen marlin
#

why is it dividing by 4

waxen marlin
#

even has some bugs that let you create a std::basic_string which has a size greater than max_size

tribal yew
#

Hmm I see

#

Well, one thing I could do is just return basic_string_view<charT, traits>::max_size() and not have to worry about it

waxen marlin
#

if you're doing it just for consistency with string_view there should probably be a -1 added

tribal yew
#

Maybe add a -1 yeah

waxen marlin
#

for the zero byte at the end which must be there

waxen marlin
shut summit
#

I dislike that your zstring_view has substr. You've already gotten rid of remove_suffix, which would also break the zero-termination premise - imho you should do the same to substr

#

that just seems to bug prone. If you really want a substr of a zstring_view, you should convert to string_view first and not magically return a string_view out of a sudden

#

all operations on zstring_view should preserve the zero-termination guarantee

gusty hawk
#

make a different substr that only allows removing the prefix?

shut summit
#

it's called remove_prefix

gusty hawk
#

that function returns void though right

shut summit
#

as it should, it mutates the string_view it's called on

sacred rock
#

more of a dumb question, how does the implemention differ from string_view? how does it solve the null terminator issue?

gusty hawk
#

so i'm saying there should be a substr that returns a zstring_view but only allows removing the prefix

sacred rock
#

I see

tribal yew
shut summit
#

another thing to note: this is missing a ctor from a range and iterator pair - in both cases the input may be zero terminated, you just have to check it

#

also this precondition should probably be expressed using contracts since you're targeting new C++ anyway

tribal yew
#

Not including a range or iterator constructor was intentional, I think they're bug-prone and the char*+size was already a concession. But I'm persuadable, I was talking with Peter about this (who is the other main author). We may also leave this to LEWG as an open question.

tribal yew
shut summit
shut summit
#

oh wow, hana co-authors?

tribal yew
#

Indeed ๐Ÿ™‚

shut summit
#

nice, she's done a few really great proposals ๐Ÿ™‚

waxen marlin
#

also is it intended to allow null terminators within the zstring_view, not just at the end

#

zstring_view s("a\0b",3); for example

spark helm
#

why no remove_prefix/_suffix?

waxen marlin
#

remove_suffix would generally break the null termination though

spark helm
#

it should return a normal string_view

waxen marlin
limber sedgeBOT
waxen marlin
#

they return void

spark helm
#

ah true

#

I always forget how unfortunate the string view api is

waxen marlin
#

there is substr which returns a normal string_view

waxen marlin
#

and the existence of char_traits

spark helm
#

btw @tribal yew I would humbly suggest calling it cstring_view or cstr_view, for both consistency with .c_str(), and the fact that it's not a zero string.

tribal yew
spark helm
#

I guess I'll add proposing the addition of .without_prefix/_suffix to my list ^^

spark helm
tribal yew
waxen marlin
#

should ping <@&331718482485837825> to make sure they know about it

waxen marlin
#

csv file literal suffix trolol

spark helm
#

lol

waxen marlin
#

confuse people by having a literal like "1,2,3"csv

spark helm
#

probably should just be "1,2,3"v

#

I'll also submit this one to your bikeshed section @tribal yew ๐Ÿ˜›

#

"bla" is already a C string, no need to repeat that fact ๐Ÿ˜›

waxen marlin
#

would "..."sv literals already be null terminated

spark helm
#

ideally they would have been

waxen marlin
#

if you just made string_view implicitly convertible to zstring_view/cstring_view then no need for new literals trolol

tribal yew
#

So the funny thing is I actually started drafting a proposal for this back in august and sent it on the mailing list and people were like "this has already been proposed don't bother"

#

and I called it cstring_view with operator""csv ๐Ÿ™‚

#

But Peter and others think times have changed so it's worth re-proposing

spark helm
#

hm there should probably be a way to decay a cstring_view to a normal string_view

waxen marlin
#

implicit conversion

tribal yew
#

We have operator basic_string_view

spark helm
waxen marlin
tribal yew
#

And yeah, following the model of basic_string's operator basic_string_view

spark helm
#

k ^^

waxen marlin
tribal yew
#

I would like that, yes

#

We haven't written anything in the paper for it yet

waxen marlin
#

ok

spark helm
tribal yew
#

This is a long list withered

spark helm
#

btw @tribal yew, will the proposal also add a conversion from/to basic_string?

#

didn't see it upon first glance

waxen marlin
#

like a constructor in std::string?

spark helm
#

both

#

like with string_view

#

a basic_string does store a C string after all

waxen marlin
#

for string -> view

spark helm
#

ah good

tribal yew
#

basic_string has a template constructor taking a StringViewLike which zstring_view should match

tribal yew
#

I realized the conversion operators I want introduce ambiguity... shit ๐Ÿ™‚

#

oh wait nvm this was already present between std::string and std::string_view yamikek

gusty hawk
#

lol

tribal yew
#

Most subsets of these four will have ambiguity issues

waxen marlin
#

what expression is that ambiguous with

#

foo("...")?

tribal yew
#

For one yes

waxen marlin
#

;compile cpp void foo(const char*); void foo(const std::string&); void foo(std::string_view); foo("...");

lucid flickerBOT
#
Compiler Output
/opt/compiler-explorer/gcc-14.2.0/bin/../lib/gcc/x86_64-linux-gnu/14.2.0/../../../../x86_64-linux-gnu/bin/ld: /tmp/ccRBpe9v.o: in function โ€‹`main':
<source>:9:(.text+0xa): undefined reference to โ€‹`foo(char const*)'
collect2: error: ld returned 1 exit status
Build failed
waxen marlin
tribal yew
#

Remove the char* overload for this one

shut summit
#

ie

#

;compile

#include <string>
#include <string_view>
#include <cstdio>

// void foo(const char*) { puts("char const*"); }
void foo(const std::string&) { puts("std::string const&");}
void foo(std::string_view) { puts("std::string_view");}
void foo(bool) { puts("bool"); }

int main(){
    foo("bar");
}
lucid flickerBOT
#
Program Output
bool
shut summit
#

tbh I hadn't yet realized

void foo(const std::string&);
void foo(std::string_view);

is ambiguous when passed a string literal. It's not exactly surprising but oh well

#

doesn't really seem all that avoidable

waxen marlin
#

;compile cpp #include<string> #include<iostream> #include<string_view> template<int=0>void foo(const std::string&){ std::cout<<"A\n"; } void foo(std::string_view){ std::cout<<"B\n"; } int main(){ foo("..."); }

lucid flickerBOT
#
Program Output
B
waxen marlin
waxen marlin
gusty hawk
#

not confused

#

neat trick

waxen marlin
#

templates get a worse ranking compared to non-templates

#

if you need further ordering you can do fun stuff like requires true

gusty hawk
#

i might use this

shut summit
wheat bay
#

I'd totally use this if this was in some namespace other than std, and compiled with Clang without warnings ๐Ÿ˜›

#

I need one for my project...

#

Have you thought about making this a full library?

waxen marlin
#
#pragma GCC diagnostic ignored "-Wreserved-user-defined-literal"
#pragma GCC diagnostic ignored "-Wuser-defined-literals"```instead of```cpp
#pragma GCC diagnostic ignored "-Wliteral-suffix"```seems to work for clang
wheat bay
#

Yeah, but I have to go in and fix the pragmas and the namespace, and I was looking for something I can use as is

tribal yew
tribal yew
wheat bay
tribal yew
spark helm
tribal yew
#

But are polls normative nooo

spark helm
#

motion to formally recognize the supreme authority of TCCPP community opinion polls.

waxen marlin
spark helm
#

indeed

tribal yew
#

Indeed

#

Fortunately I didnโ€™t write that sentence

waxen marlin
#

seems kind of strange to have s.substr(1) and s.substr(1,1) return different types if that is the intent though

tribal yew
#

and yes, it is strange but it's the only way

#

Unless we want to force users to std::string_view(zsv).substr(...)

waxen marlin
tribal yew
#

The whole point of this paper is not losing type info though

sacred rock
#

would love std::string to have view() and zview() functions

#

i think there was proposal for std::string::view but can't remember how far it made

#

considering this is almost in c++26, I'd add zsubview() too

sacred rock
#

I thought this type made some magic to allow cutting and still have a null terminator, ofcourse not the case. only possible way to do this is copy the string to another buffer :(

tribal yew
#

subzview would be weird because the view has the be only at the end of the string