Hi,

At Sat, 20 Aug 2005 08:06:16 +0900,
Daniel Berger wrote in [ruby-talk:153002]:
> 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.

It feels a roundabout way, and also it has a bug.

Give a try the example in [ruby-talk:152997]:
>          assert_equal("cat", Base.basename("cat.c", ".c"))

> If there's an easier way I'm all ears.

Why not check if the result of strstr() is at the end of base?

  char *ext_in_base = strstr(base, ext);
  if (ext_in_base && strlen(ext_in_base) == strlen(ext)) {
      rbBase = rb_str_new(base, ext_in_base - base);
  }

Or better, check if the last portion matches to ext.

  int extlen = strlen(ext);
  int length = strlen(base) - extlen;

  if (length > 0 && !strcmp(base + length, ext)) {
      rbBase = rb_str_new(base, length);
  }

-- 
Nobu Nakada