If your regular expression library would then provide a new backward
search option? There are only a few regex that can do that. Currently right now,
Java cannot do it . But for example .NET can do it:
Gets a value that indicates whether the regular expression searches from right to left.
https://docs.microsoft.com/en-us/dotnet/api/system.text.regularexpressions.regex.righttoleft?view=net-6.0
Peter Ludemann has already shown how to solve the problem with
left to right search. But right to left seems not to be available:
But this is rather clumsy and not so fast. Whats also a funny idea, is to use
atomic_list_concat/3. I get the following, which is also not extremly efficient
and puts burden on the atom table:
?- atomic_list_concat(L, '/', 'foo/bar/baz.p'), last(L, X).
X = 'baz.p'.
Whats also lacking in the above version is searching for '/'
and '\\'
at the same time. Ok, one could do the following:
dos2unix(A, B) :-
atomic_list_concat(L, '\\', A),
atomic_list_concat(L, '/', B).
?- dos2unix('foo\\bar\\baz.p', B), atomic_list_concat(L, '/', B), last(L, X).
X = 'baz.p'.
Originally I was doing the dos2unix/2
thingy in my new system, before
I had last_sub_atom/5
. Now I am doing it more directly. I donât have issues
with atom table, but issues remain the same with or without an atom table,
the dos2unix/2 thingy creates a lot of objects, making pressure on the
garbage collector of the host programming language, which handles them
in my new system. Maybe a Prolog system allocates the strings in its
environment, but this then gives pressure on environment trimming. But
what ultimately let me dismiss dos2unix, and going for OS Polyglott, is
the problem of retrieving the directory part, without normalizing the file
path into either dos or unix. For this last_sub_atom/5 is ultra handy.