220 8649 <3379bbae-dd54-461c-bf90-228e476ca7db@isocpp.org> article
Path: news.gmane.org!not-for-mail
From: Peter Bigot <bigotp@acm.org>
Newsgroups: gmane.comp.lang.c++.isocpp.proposals
Subject: Re: string_view::is_null()
Date: Fri, 17 Jan 2014 10:34:37 -0800 (PST)
Lines: 217
Approved: news@gmane.org
Message-ID: <3379bbae-dd54-461c-bf90-228e476ca7db@isocpp.org>
References: <8045a4d2-721d-4725-8bb7-7a91b6f53ec8@isocpp.org> <186E0927-C962-4E34-AED0-E7509DD1F0A0@gmail.com> <7aad44d8-6b4e-438a-b309-bd898b994b34@isocpp.org>
 <9C2C439F-7983-4224-B272-53CAD0E2E456@gmail.com>
Reply-To: std-proposals@isocpp.org
NNTP-Posting-Host: plane.gmane.org
Mime-Version: 1.0
Content-Type: multipart/alternative; 
	boundary="----=_Part_6_3650826.1389983677618"
X-Trace: ger.gmane.org 1389983675 28616 80.91.229.3 (17 Jan 2014 18:34:35 GMT)
X-Complaints-To: usenet@ger.gmane.org
NNTP-Posting-Date: Fri, 17 Jan 2014 18:34:35 +0000 (UTC)
To: std-proposals@isocpp.org
Original-X-From: std-proposals+bncBDA3LUEAQACBBPXP4WLAKGQEXG3PAJI@isocpp.org Fri Jan 17 19:34:43 2014
Return-path: <std-proposals+bncBDA3LUEAQACBBPXP4WLAKGQEXG3PAJI@isocpp.org>
Envelope-to: gclcip-std-proposals@m.gmane.org
Original-Received: from mail-ob0-f197.google.com ([209.85.214.197])
	by plane.gmane.org with esmtp (Exim 4.69)
	(envelope-from <std-proposals+bncBDA3LUEAQACBBPXP4WLAKGQEXG3PAJI@isocpp.org>)
	id 1W4EFc-0001Ci-0X
	for gclcip-std-proposals@m.gmane.org; Fri, 17 Jan 2014 19:34:40 +0100
Original-Received: by mail-ob0-f197.google.com with SMTP id gq1sf8882183obb.4
        for <gclcip-std-proposals@m.gmane.org>; Fri, 17 Jan 2014 10:34:39 -0800 (PST)
X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed;
        d=1e100.net; s=20130820;
        h=x-gm-message-state:date:from:to:message-id:in-reply-to:references
         :subject:mime-version:x-original-sender:reply-to:precedence
         :mailing-list:list-id:list-post:list-help:list-archive
         :list-subscribe:list-unsubscribe:content-type;
        bh=C7VPy1V56dj1r1ljNlYQHq2C4b/gQ4gfRw13veP0VUc=;
        b=ZgHQ1xT/QH1mXSxeiQ6Q61i5kN5FVQ71kPwRwoH8EEfhqjWyEzN96z6rSYd02aAnoq
         WR6ncQ9oXMu/mNwpbEgQWprASf/H7HfYWNIh9peQoFh4Uy7G2DMQqAzlVLai2kjTvMeU
         +kV/iWssdRF/k0aIrixu5S3T0gcePUc/bciGJUVmCmxQhent3+HBiRu8zT2oGiN6Va1e
         QYp5VDPQ/BDE+wojMAZC7kp2E+J0sCX4wClxWMrSSN3LLCJZcdsHudgdJV2/UzQrYjfa
         TuEU9g9JFOVeMfFWHRi5P/hoQsm1RxxF+L7xLvEbTWPQqiw+Meh1nuxjzOx7VWQ3LxSb
         A4Jg==
X-Gm-Message-State: ALoCoQnnUqdQ320+W2zTCv8PX326KqUHiRbgd9dUfDLAZJI1soMRb/aW1NqirD2kS2JPP6yvyzdU
X-Received: by 10.182.186.73 with SMTP id fi9mr1305132obc.48.1389983678995;
        Fri, 17 Jan 2014 10:34:38 -0800 (PST)
X-BeenThere: std-proposals@isocpp.org
Original-Received: by 10.182.47.4 with SMTP id z4ls655455obm.93.gmail; Fri, 17 Jan 2014
 10:34:38 -0800 (PST)
X-Received: by 10.182.154.38 with SMTP id vl6mr18454obb.40.1389983678103;
        Fri, 17 Jan 2014 10:34:38 -0800 (PST)
In-Reply-To: <9C2C439F-7983-4224-B272-53CAD0E2E456@gmail.com>
X-Original-Sender: bigotp@acm.org
Precedence: list
Mailing-list: list std-proposals@isocpp.org; contact std-proposals+owners@isocpp.org
List-ID: <std-proposals.isocpp.org>
X-Google-Group-Id: 399137483710
List-Post: <http://groups.google.com/a/isocpp.org/group/std-proposals/post>, <mailto:std-proposals@isocpp.org>
List-Help: <http://support.google.com/a/isocpp.org/bin/topic.py?topic=25838>, <mailto:std-proposals+help@isocpp.org>
List-Archive: <http://groups.google.com/a/isocpp.org/group/std-proposals/>
List-Subscribe: <http://groups.google.com/a/isocpp.org/group/std-proposals/subscribe>,
 <mailto:std-proposals+subscribe@isocpp.org>
List-Unsubscribe: <http://groups.google.com/a/isocpp.org/group/std-proposals/subscribe>,
 <mailto:googlegroups-manage+399137483710+unsubscribe@googlegroups.com>
Xref: news.gmane.org gmane.comp.lang.c++.isocpp.proposals:8649
Archived-At: <http://permalink.gmane.org/gmane.comp.lang.c++.isocpp.proposals/8649>

------=_Part_6_3650826.1389983677618
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: quoted-printable



On Friday, January 17, 2014 10:26:50 AM UTC-6, Marshall wrote:
>
> On Jan 15, 2014, at 4:28 AM, Olaf van der Spek <olafv...@gmail.com<javasc=
ript:>>=20
> wrote:
>
> On Saturday, December 28, 2013 6:01:34 AM UTC+1, Marshall wrote:
>>
>> if sv.size () =3D=3D 0 or sv.empty () [same thing], there is nothing tha=
t you=20
>> can do with sv.data()
>> (except compare it to null, and that doesn=E2=80=99t tell you anything u=
seful).
>>
>
> Are you sure?
> IMO an empty string_view could still point into a string and I should be=
=20
> able to use that pointer to for example construct a new string_view.
>
>
> A string view references a half-open contiguous range of characters; [=20
> data(), data()+size())
>
> if size() =3D=3D 0, then the range of characters referenced by the string=
 view=20
> is empty, and accessing them (either via dereferencing begin() or data())=
=20
> is (or should be) undefined behavior. (the same as any other container)
>
> If in your program, you =E2=80=9Cknow=E2=80=9D that the pointer returned =
by data() points=20
> to something valid, well, that=E2=80=99s on you to determine.
>
> Code example:
>
> char deref ( string_view sv ) { return *sv.data(); }
>
> string_view sv1;
> string_view sv2 { =E2=80=9CABCDE=E2=80=9D };
> sv2.remove_prefix(5);
> assert ( sv1 =3D=3D sv2 );
> deref ( sv1 ); // may or may not blow up in your face
> deref ( sv2 ); // this will probably =E2=80=9Cwork=E2=80=9D ; but I don=
=E2=80=99t know what it=20
> will return.
>
> =E2=80=94 Marshall
>
>
While the half-open range argument is true in isolation, it's missing the=
=20
point that the value of data() is well-known based on how the string_view=
=20
was created and what operations have been performed on it. Adjusting the=20
string_view to the point where size() is zero does not invalidate data().

Based on how sv2 is constructed in that example, I believe it's guaranteed=
=20
that deref(sv2) will return the '\0' that terminates the string literal=20
used to initialize sv2, and that doing so does not invoke undefined=20
behavior.  This follows from how sv2 is created and the definition of=20
remove_prefix.

Here's a more relevant example.  There is no need for scare quotes when=20
stating that the programmer knows delim.data() always points to something=
=20
valid in the test code.

Peter

string_view
comma_delimiter (string_view sv)
{
  string_view::size_type p{sv.find(',')};
  if (string_view::npos =3D=3D p) {
    return sv.substr(sv.size());
  }
  return sv.substr(p, 1);
}

string_view
case_change_delimiter (string_view sv)
{
  for (string_view::size_type i =3D 1; i < sv.size(); ++i) {
    string_view::value_type c1 =3D sv[i-1];
    string_view::value_type c2 =3D sv[i];
    if (isalpha(c1) && isalpha(c2) && (islower(c1) !=3D islower(c2))) {
      return sv.substr(i, 0);
    }
  }
  return sv.substr(sv.size());
}

TEST(Delimiter, Example)
{
  const std::string base("A,Bcde");
  string_view sv(base);
  ASSERT_EQ(base.data(), sv.data());

  /* Locate the comma as a single-character delimiter */
  string_view delim(comma_delimiter(sv));
  ASSERT_EQ(1, delim.size());
  ASSERT_EQ(1, (delim.data() - sv.data()));
  ASSERT_EQ(",", delim);

  /* Locate the case change as zero-width delimiter */
  delim =3D case_change_delimiter(sv);
  ASSERT_EQ(0, delim.size());
  ASSERT_EQ(3, (delim.data() - sv.data()));
  ASSERT_EQ("cde", sv.substr(delim.data()-sv.data()));
}

--=20

---=20
You received this message because you are subscribed to the Google Groups "=
ISO C++ Standard - Future Proposals" group.
To unsubscribe from this group and stop receiving emails from it, send an e=
mail to std-proposals+unsubscribe@isocpp.org.
To post to this group, send email to std-proposals@isocpp.org.
Visit this group at http://groups.google.com/a/isocpp.org/group/std-proposa=
ls/.

------=_Part_6_3650826.1389983677618
Content-Type: text/html; charset=UTF-8
Content-Transfer-Encoding: quoted-printable

<div dir=3D"ltr"><br><br>On Friday, January 17, 2014 10:26:50 AM UTC-6, Mar=
shall wrote:<blockquote class=3D"gmail_quote" style=3D"margin: 0;margin-lef=
t: 0.8ex;border-left: 1px #ccc solid;padding-left: 1ex;"><div style=3D"word=
-wrap:break-word">On Jan 15, 2014, at 4:28 AM, Olaf van der Spek &lt;<a hre=
f=3D"javascript:" target=3D"_blank" gdf-obfuscated-mailto=3D"MEVbJLx2UbAJ" =
onmousedown=3D"this.href=3D'javascript:';return true;" onclick=3D"this.href=
=3D'javascript:';return true;">olafv...@gmail.com</a>&gt; wrote:<br><div><b=
r><blockquote type=3D"cite"><div dir=3D"ltr">On Saturday, December 28, 2013=
 6:01:34 AM UTC+1, Marshall wrote:<blockquote class=3D"gmail_quote" style=
=3D"margin:0;margin-left:0.8ex;border-left:1px #ccc solid;padding-left:1ex"=
><div style=3D"word-wrap:break-word"><div>if sv.size () =3D=3D 0 or sv.empt=
y () [same thing], there is nothing that you can do with sv.data()</div><di=
v>(except compare it to null, and that doesn=E2=80=99t tell you anything us=
eful).</div></div></blockquote><div><br></div><div>Are you sure?</div><div>=
IMO an empty string_view could still point into a string and I should be ab=
le to use that pointer to for example construct a new string_view.</div></d=
iv></blockquote></div><div><br></div><div>A string view references a half-o=
pen contiguous range of characters; [ data(), data()+size())</div><div><br>=
</div><div>if size() =3D=3D 0, then the range of characters referenced by t=
he string view is empty, and accessing them (either via dereferencing begin=
() or data()) is (or should be) undefined behavior. (the same as any other =
container)</div><div><br></div><div>If in your program, you =E2=80=9Cknow=
=E2=80=9D that the pointer returned by data() points to something valid, we=
ll, that=E2=80=99s on you to determine.</div><div><br></div><div>Code examp=
le:</div><div><br></div><div><span style=3D"white-space:pre">	</span>char d=
eref ( string_view sv ) { return *sv.data(); }</div><div><br></div><div><sp=
an style=3D"white-space:pre">	</span>string_view sv1;</div><div><span style=
=3D"white-space:pre">	</span>string_view sv2 { =E2=80=9CABCDE=E2=80=9D };</=
div><div><span style=3D"white-space:pre">	</span>sv2.remove_prefix(5);</div=
><div><span style=3D"white-space:pre">	</span>assert ( sv1 =3D=3D sv2 );</d=
iv><div><span style=3D"white-space:pre">	</span>deref ( sv1 );<span style=
=3D"white-space:pre">	</span>// may or may not blow up in your face</div><d=
iv><span style=3D"white-space:pre">	</span>deref ( sv2 );<span style=3D"whi=
te-space:pre">	</span>// this will probably =E2=80=9Cwork=E2=80=9D ; but I =
don=E2=80=99t know what it will return.</div><div><br></div><div>=E2=80=94 =
Marshall</div><div><br></div></div></blockquote><div><br>While the half-ope=
n range argument is true in isolation, it's missing the point that the valu=
e of data() is well-known based on how the string_view was created and what=
 operations have been performed on it. Adjusting the string_view to the poi=
nt where size() is zero does not invalidate data().<br><br>Based on how sv2=
 is constructed in that example, I believe it's guaranteed that deref(sv2) =
will return the '\0' that terminates the string literal used to initialize =
sv2, and that doing so does not invoke undefined behavior.&nbsp; This follo=
ws from how sv2 is created and the definition of remove_prefix.<br><br>Here=
's a more relevant example.&nbsp; There is no need for scare quotes when st=
ating that the programmer knows delim.data() always points to something val=
id in the test code.<br><br>Peter<br><br>string_view<br>comma_delimiter (st=
ring_view sv)<br>{<br>&nbsp; string_view::size_type p{sv.find(',')};<br>&nb=
sp; if (string_view::npos =3D=3D p) {<br>&nbsp;&nbsp;&nbsp; return sv.subst=
r(sv.size());<br>&nbsp; }<br>&nbsp; return sv.substr(p, 1);<br>}<br><br>str=
ing_view<br>case_change_delimiter (string_view sv)<br>{<br>&nbsp; for (stri=
ng_view::size_type i =3D 1; i &lt; sv.size(); ++i) {<br>&nbsp;&nbsp;&nbsp; =
string_view::value_type c1 =3D sv[i-1];<br>&nbsp;&nbsp;&nbsp; string_view::=
value_type c2 =3D sv[i];<br>&nbsp;&nbsp;&nbsp; if (isalpha(c1) &amp;&amp; i=
salpha(c2) &amp;&amp; (islower(c1) !=3D islower(c2))) {<br>&nbsp;&nbsp;&nbs=
p;&nbsp;&nbsp; return sv.substr(i, 0);<br>&nbsp;&nbsp;&nbsp; }<br>&nbsp; }<=
br>&nbsp; return sv.substr(sv.size());<br>}<br><br>TEST(Delimiter, Example)=
<br>{<br>&nbsp; const std::string base("A,Bcde");<br>&nbsp; string_view sv(=
base);<br>&nbsp; ASSERT_EQ(base.data(), sv.data());<br><br>&nbsp; /* Locate=
 the comma as a single-character delimiter */<br>&nbsp; string_view delim(c=
omma_delimiter(sv));<br>&nbsp; ASSERT_EQ(1, delim.size());<br>&nbsp; ASSERT=
_EQ(1, (delim.data() - sv.data()));<br>&nbsp; ASSERT_EQ(",", delim);<br><br=
>&nbsp; /* Locate the case change as zero-width delimiter */<br>&nbsp; deli=
m =3D case_change_delimiter(sv);<br>&nbsp; ASSERT_EQ(0, delim.size());<br>&=
nbsp; ASSERT_EQ(3, (delim.data() - sv.data()));<br>&nbsp; ASSERT_EQ("cde", =
sv.substr(delim.data()-sv.data()));<br>}<br><br></div></div>

<p></p>

-- <br />
&nbsp;<br />
--- <br />
You received this message because you are subscribed to the Google Groups &=
quot;ISO C++ Standard - Future Proposals&quot; group.<br />
To unsubscribe from this group and stop receiving emails from it, send an e=
mail to std-proposals+unsubscribe@isocpp.org.<br />
To post to this group, send email to std-proposals@isocpp.org.<br />
Visit this group at <a href=3D"http://groups.google.com/a/isocpp.org/group/=
std-proposals/">http://groups.google.com/a/isocpp.org/group/std-proposals/<=
/a>.<br />

------=_Part_6_3650826.1389983677618--

.
