www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - std.algorithm.skipOver broken / misbehaving?

reply "Era Scarecrow" <rtcvb32 yahoo.com> writes:
  Although this likely isn't the most efficient way to do this, 
it's cropped up and here's what I have so far. The idea is to 
drop all the unwanted pathname and only leave the filename (I'm 
sure there's a function there already, just not finding it off 
hand).

  Why is this failing?

[quote]
bool skipOver(alias pred = "a == b", R1, R2)(ref R1 r1, R2 r2);
     If startsWith(r1, r2), consume the corresponding elements off 
r1 and return true. Otherwise, leave r1 unchanged and return 
false.
[/quote]

[code]
import std.algorithm;
import std.stdio;

void main() {
   string filename = r"something/long\or short";
   bool skipped;
     do {
       skipped = false;
       //try to handle either slash in this case.
       skipped |= filename.skipOver('\\');
       skipped |= filename.skipOver('/');
       skipped |= filename.skipOver(r"\");
       skipped |= filename.skipOver(r"/");
     } while (skipped);

   //originally was: do {} while(filename.skipOver('\\'));

   //asserts, filename hasn't changed.
   assert(filename == "or short", filename);
}
[/code]
Oct 05 2012
next sibling parent reply "Era Scarecrow" <rtcvb32 yahoo.com> writes:
  Mmmm glancing at the notes again, maybe I misread it and it only 
skips if the beginning equals, rather than if it contains... 
Kinda annoying since it sounds like what I wanted would work...
Oct 05 2012
parent "Jesse Phillips" <Jessekphillips+D gmail.com> writes:
On Friday, 5 October 2012 at 08:53:22 UTC, Era Scarecrow wrote:
  Mmmm glancing at the notes again, maybe I misread it and it 
 only skips if the beginning equals, rather than if it 
 contains... Kinda annoying since it sounds like what I wanted 
 would work...
Pretty sure the semantics you want are in, findSkip.
Oct 08 2012
prev sibling parent reply =?UTF-8?B?QWxpIMOHZWhyZWxp?= <acehreli yahoo.com> writes:
On 10/05/2012 01:39 AM, Era Scarecrow wrote:
 Although this likely isn't the most efficient way to do this, it's
 cropped up and here's what I have so far. The idea is to drop all the
 unwanted pathname and only leave the filename (I'm sure there's a
 function there already, just not finding it off hand).

 Why is this failing?

 [quote]
 bool skipOver(alias pred = "a == b", R1, R2)(ref R1 r1, R2 r2);
 If startsWith(r1, r2), consume the corresponding elements off r1 and
 return true. Otherwise, leave r1 unchanged and return false.
 [/quote]

 [code]
 import std.algorithm;
 import std.stdio;

 void main() {
 string filename = r"something/long\or short";
 bool skipped;
 do {
 skipped = false;
 //try to handle either slash in this case.
 skipped |= filename.skipOver('\\');
 skipped |= filename.skipOver('/');
 skipped |= filename.skipOver(r"\");
 skipped |= filename.skipOver(r"/");
 } while (skipped);

 //originally was: do {} while(filename.skipOver('\\'));

 //asserts, filename hasn't changed.
 assert(filename == "or short", filename);
 }
 [/code]
There is the std.path module and especially the std.path.baseName function, but it considers either '/' or '\\' depending on the platform. The following range magic works for both '/' and '\\': import std.algorithm; import std.stdio; import std.range; void main() { string filename = r"something/long\or short"; auto firstPart = filename.retro.findAmong("/\\"); filename = filename[firstPart.count .. $]; assert(filename == "or short", filename); } Ali P.S. I too am thinking about abandoning dmd's -property switch. With the -property switch, I would have to write retro() and count() with parentheses.
Oct 06 2012
parent "Era Scarecrow" <rtcvb32 yahoo.com> writes:
On Saturday, 6 October 2012 at 21:57:17 UTC, Ali Çehreli wrote:
 There is the std.path module and especially the 
 std.path.baseName function, but it considers either '/' or '\\' 
 depending on the platform.
Yeah already found that. Easy to find it via grep in the html documentation.
 P.S. I too am thinking about abandoning dmd's -property switch. 
 With the -property switch, I would have to write retro() and 
 count() with parentheses.
I'm not sure what to think regarding the -property switch. I've used it while tinkering with BitArray. I think libraries themselves should adhere to the stricter form, but user code can be more relaxed. If the calling is just user preference then it depends on how they see and understand the call based on the look of it. Maybe it will get thrown away...
Oct 06 2012