From: Daniel Berger Date: 2005-08-20T08:06:16+09:00 Subject: Re: Refactoring of File.basename nobu.nokada@softhome.net wrote: > Hi, > > At Sat, 20 Aug 2005 00:58:11 +0900, > Daniel Berger wrote in [ruby-talk:152940]: > > After a recent "idiomatic" thread, I decided to try and refactor the > > core File.basename method. I think I did a pretty good job. It's about > > 10 lines short, easier to read (I think), and self contained, i.e. no > > calls to rmext(). > > It doesn't look easier to read for me. I suspect your opinion is biased, but I'll defer to others who may chime in. > > if(strstr(base, ext)){ > > int length = strlen(base) - strlen(ext); > > int span = strcspn(base, ext); > > What does this intend? The strstr() call checks to see if the extension exists in the string. Easy enough. However, say you have this scenario: File.basename("foo.rb.py", ".rb") In that case, we not only have to check that it exists, but that it's the last extension in the string. Hence the need for strcspn() and a length check. If there's an easier way I'm all ears. Regards, Dan