www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Why isn't skipOver(string, string) nothrow?

reply Per =?UTF-8?B?Tm9yZGzDtnc=?= <per.nordlow gmail.com> writes:
Why isn't a call to

     skipOver(string, string)

nothrow?

I see no reason why it shouldn't be.

Further, this test should be qualifyable as nothrow:

 safe pure /* TODO nothrow  nogc */ unittest
{
     import std.algorithm.searching : skipOver;
     auto x = "beta version";
     assert(x.skipOver("beta"));
     assert(x == " version");
}
Oct 22 2019
next sibling parent reply Adam D. Ruppe <destructionator gmail.com> writes:
On Tuesday, 22 October 2019 at 15:33:05 UTC, Per Nordlöw wrote:
 Why isn't a call to

     skipOver(string, string)

 nothrow?
without really looking, probably because of invalid utf sequences potentially throwing. Using the .representation thingy might help if im right about this. A good way to try this is to edit your copy of the Phobos file and add nothrow to it. That should give an error inside that is more descriptive. (I would love if the compiler would do this automatically, we were talking about maybe making that mod on irc yesterday).
Oct 22 2019
parent reply Per =?UTF-8?B?Tm9yZGzDtnc=?= <per.nordlow gmail.com> writes:
On Tuesday, 22 October 2019 at 15:39:17 UTC, Adam D. Ruppe wrote:
 On Tuesday, 22 October 2019 at 15:33:05 UTC, Per Nordlöw wrote:
 Why isn't a call to

     skipOver(string, string)

 nothrow?
without really looking, probably because of invalid utf sequences potentially throwing. Using the .representation thingy might help if im right about this. A good way to try this is to edit your copy of the Phobos file and add nothrow to it. That should give an error inside that is more descriptive. (I would love if the compiler would do this automatically, we were talking about maybe making that mod on irc yesterday).
But startsWith(string, string) is nothrow so skipOver should be that too.
Oct 22 2019
parent Jonathan M Davis <newsgroup.d jmdavisprog.com> writes:
On Tuesday, October 22, 2019 4:27:59 PM MDT Per Nordlw via Digitalmars-d-
learn wrote:
 On Tuesday, 22 October 2019 at 15:39:17 UTC, Adam D. Ruppe wrote:
 On Tuesday, 22 October 2019 at 15:33:05 UTC, Per Nordlw wrote:
 Why isn't a call to

     skipOver(string, string)

 nothrow?
without really looking, probably because of invalid utf sequences potentially throwing. Using the .representation thingy might help if im right about this. A good way to try this is to edit your copy of the Phobos file and add nothrow to it. That should give an error inside that is more descriptive. (I would love if the compiler would do this automatically, we were talking about maybe making that mod on irc yesterday).
But startsWith(string, string) is nothrow so skipOver should be that too.
That's only true with startsWith, because it avoids decoding in the case where the two strings have the same encoding (e.g. it's not nothrow if you compare a dstring and a string). Presumably, skipOver could be made to do the same, but no one has done so. - Jonathan M Davis
Oct 22 2019
prev sibling parent Jonathan M Davis <newsgroup.d jmdavisprog.com> writes:
On Tuesday, October 22, 2019 9:33:05 AM MDT Per Nordlw via Digitalmars-d-
learn wrote:
 Why isn't a call to

      skipOver(string, string)

 nothrow?

 I see no reason why it shouldn't be.

 Further, this test should be qualifyable as nothrow:

  safe pure /* TODO nothrow  nogc */ unittest
 {
      import std.algorithm.searching : skipOver;
      auto x = "beta version";
      assert(x.skipOver("beta"));
      assert(x == " version");
 }
Almost anything involving strings isn't going to be nothrow, because front and popFront throw on invalid UTF. To really fix that, we'd need to get rid of auto-decoding. You can use std.utf.byDchar to wrap the string in a range of dchar which replaces invalid Unicode with the replacement character instead, which means that no exception gets thrown, but it also means that if you hadn't previously validated the Unicode, you could end up processing invalid Unicode without realizing it. How much that matters depends on what you're doing. Ideally, all strings would just be validated when they were created, and then it wouldn't be an issue, but any code that decodes the code points would still have to deal with invalid Unicode in some manner (though if we decided that it was the responsibility of the caller to always validate the Unicode first, then we could use assertions). For better or worse, the chosen solution when ranges were first put together was to throw on invalid Unicode, which basically makes it impossible for functions that process strings to be nothrow unless they go to the extra effort working around auto-decoding. If we're ever able to remove auto-decoding, then that's no longer an issue for all string processing functions, but it's still going to be an issue for any code that calls functions like decode or stride. They're either going to throw or replace invalid Unicode with the replacement character. Which approach is better depends on the code. In any case, as long as auto-decoding is a thing, you'll have to use wrappers like byDchar or byCodeUnit if you want much of anything involving strings to be nothrow. - Jonathan M Davis
Oct 22 2019