-- unique.e v0.2 by CChris -- exports the unique_filename() function sequence alphabet alphabet="abcdefghijklmnopqrstuvwxyz0123456789" integer lmax lmax=0 function separate(sequence s1,sequence s2) --converts s1 and s2 to intermediate form and checks whether they are consecutive --Returns "" if so, else the pair of intermediate forms sequence S1,S2 integer p S2=repeat(0,lmax) S1=repeat(0,lmax) for i=1 to length(s1) do S1[i]=find(s1[i],alphabet) end for for i=1 to length(s2) do S2[i]=find(s2[i],alphabet) end for p=find(0,S1=S2) if length(s1)=lmax and length(s2)=lmax and p=lmax and S2[p]=S1[p]+1 then return {} else return {S1,S2,p,s1,s2} end if end function function intermediate(sequence s) -- s is not empty and returned from separate integer p sequence S1,S2 S1=s[1] S2=s[2] p=s[3] if p<=length(s[4]) and p<=length(s[5]) and S1[p]length(s[4]) then -- 2nd file name starts with 1st one if length(s[5])>p then -- second filename is longer by 2 or more chars, return a slice in between return s[5][1..p] elsif S2[p]>1 then -- replace last letter with 'a' return s[4]&'a' elsif S2[p-1]p then -- first name is longer by 2 chars or more return s[4][1..p] elsif S1[length(s[5])]lmax then lmax=length(s) end if for j=1 to length(s) do if not find(s[j],alphabet) then alphabet&=s[j] end if end for end for filenames=sort(filenames[1..k]) for i=1 to length(filenames)-1 do -- find first pair of non consecutive names s=separate(filenames[i],filenames[i+1]) if length(s) then return intermediate(s) end if end for -- all filenames are consecutive - a rare case ! if length(filenames[1])>1 then return filenames[1][1..length(filenames[1])-1] elsif compare(filenames[1],"a") then return "a" elsif length(filenames[length(filenames)])