Permalink
Browse files

Add get_extension predicate.

  • Loading branch information...
1 parent ff11834 commit 1f2a5e9e66046a825d034e2f548dbb0117e493ec @wangp committed Mar 27, 2012
Showing with 23 additions and 0 deletions.
  1. +23 −0 string_util.m
View
@@ -20,6 +20,8 @@
:- pred unsafe_strstr(string::in, string::in, int::in, int::out) is semidet.
+:- pred get_extension(string::in, string::out) is semidet.
+
:- pred fix_utf8(string::in, string::out) is det.
:- type pieces
@@ -117,6 +119,27 @@
%-----------------------------------------------------------------------------%
+get_extension(FileName, Ext) :-
+ string.length(FileName, End),
+ get_extension_2(FileName, End, Start),
+ string.between(FileName, Start, End, Ext).
+
+:- pred get_extension_2(string::in, int::in, int::out) is semidet.
+
+get_extension_2(FileName, !Index) :-
+ string.unsafe_prev_index(FileName, !Index, C),
+ ( C = ('.') ->
+ true
+ ; C = ('/') ->
+ fail
+ ; C = ('\\') ->
+ fail
+ ;
+ get_extension_2(FileName, !Index)
+ ).
+
+%-----------------------------------------------------------------------------%
+
fix_utf8(String0, String) :-
fix_utf8_loop(String0, length(String0), 0, 0, empty, Pieces),
( Pieces = substring(_, _, _, empty) ->

0 comments on commit 1f2a5e9

Please sign in to comment.