Version française
Home     About     Download     Resources     Contact us    
Browse thread
[Caml-list] unix.chop_extension
[ Home ] [ Index: by date | by threads ]
[ Search: ]

[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
Date: -- (:)
From: YANG Shouxun <yangsx@f...>
Subject: Re: [Caml-list] unix.chop_extension
On Tuesday 25 May 2004 16:46, Alex Baretta wrote:
> skaller wrote:
> > # Filename.chop_extension "x.y/z";;
> > - : string = "x"
>
> This is a terrible consequence of not having (* functional *) support
> for regexps in the language or standard library. The Filename library
> uses the very weak functions of the String library to find the rightmost
> dot in the filename (* or path *), which is obviously correct only under
> a very stringent precondition, which is not the most general possible
> precondition for this function.

It seems to me that regexp is not necessary to fix this specific problem. 
Hopefully the following patch will get rid of it.

--8<--
--- filename.ml~        2003-12-17 20:23:26.000000000 +0800
+++ filename.ml 2004-05-27 16:11:08.000000000 +0800
@@ -207,10 +207,11 @@
   if n < 0 then invalid_arg "Filename.chop_suffix" else String.sub name 0 n

 let chop_extension name =
-  try
-    String.sub name 0 (String.rindex name '.')
-  with Not_found ->
-    invalid_arg "Filename.chop_extension"
+  let bn = Filename.basename name in
+    try
+      String.sub bn 0 (String.rindex bn '.')
+    with Not_found ->
+      invalid_arg "Filename.chop_extension"

 external open_desc: string -> open_flag list -> int -> int = "sys_open"
 external close_desc: int -> unit = "sys_close"
--8<--

-------------------
To unsubscribe, mail caml-list-request@inria.fr Archives: http://caml.inria.fr
Bug reports: http://caml.inria.fr/bin/caml-bugs FAQ: http://caml.inria.fr/FAQ/
Beginner's list: http://groups.yahoo.com/group/ocaml_beginners