1 % (c) 2009-2026 Lehrstuhl fuer Softwaretechnik und Programmiersprachen,
2 % Heinrich Heine Universitaet Duesseldorf
3 % This software is licenced under EPL 1.0 (http://www.eclipse.org/org/documents/epl-v10.html)
4
5
6 :- module(tools_strings,
7 [string_concatenate/3,
8 ajoin/2, ajoin_with_limit/3, ajoin_with_sep/3,
9 ajoin_path/3,
10 safe_name/2,
11 convert_to_atom/2,
12 predicate_functor/3,
13 atom_codes_with_limit/2, atom_codes_with_limit/3,
14 truncate_atom/3,
15 atom_prefix/2, atom_suffix/2, atom_split/4,
16 match_atom/2, match_atom/3,
17 convert_cli_arg/2,
18 convert_to_number/2,
19 convert_atom_to_number/2,
20 get_hex_codes/2,
21 get_hex_bytes/2,
22 number_codes_min_length/3,
23 is_digit_code/1,
24 is_alphabetical_ascii_code/1, is_alphabetical_ascii_codes/1,
25 is_ascii_id_codes/1,
26 is_simple_classical_b_identifier/1, is_simple_classical_b_identifier_codes/1,
27 is_composed_classical_b_identifier/1,
28 strip_newlines/2,
29 atom_tail_to_lower_case/2,
30 format_modified_info/2, format_datime_to_codes/2
31 ]).
32
33 :- use_module(module_information).
34
35 :- module_info(group,infrastructure).
36 :- module_info(description,'A few utilities on strings/atoms seperated out from tools.pl to avoid cyclic module dependencies.').
37
38 :- set_prolog_flag(double_quotes, codes).
39
40
41 %! string_concatenate(+X,+Y,-XY)
42 string_concatenate(X,Y,XY) :- atom(X),atom(Y),!, atom_concat(X,Y,XY).
43 string_concatenate(X,Y,XY) :- atom(X),number(Y),!,
44 % convert Y to atom, atom_concat is much faster than converting to codes list and back
45 number_codes(Y,YC), atom_codes(X,XC),
46 append(XC,YC,XYC),
47 atom_codes(XY,XYC).
48 string_concatenate(X,Y,XY) :-
49 safe_name(X,Xs),safe_name(Y,Ys),append(Xs,Ys,XYs),atom_codes(XY,XYs).
50
51 % tests are stored in tools.pl to avoid cyclic module dependencies
52 %:- assert_must_succeed((tools_strings: ajoin_with_sep([link,a,xa],'.',Text), Text == 'link.a.xa')).
53 %:- assert_must_succeed((tools_strings: ajoin_with_sep([link],'.',Text), Text == 'link')).
54
55 :- use_module(library(types), [illarg/3]).
56 ajoin_with_sep(List,Sep,Text) :- var(List),!,
57 illarg(var, ajoin_with_sep(List,Sep,Text), 1).
58 ajoin_with_sep(List,Sep,Text) :-
59 insert_sep(List,Sep,L2),
60 ajoin(L2,Text).
61
62 insert_sep([H1,H2|T],Sep,Res) :- !, Res= [H1,Sep|RT], insert_sep([H2|T],Sep,RT).
63 insert_sep(L,_Sep,L).
64
65
66 /* Concats a list of atoms, but checks if numbers or compound terms are
67 involved, which are converted to simple atoms */
68 ajoin(List,Text) :- var(List),!,
69 illarg(var, ajoin(List,Text), 1).
70 ajoin([Atom],Res) :- !, Res=Atom.
71 ajoin(ListOfAtoms,Atom) :- ajoin_with_limit(ListOfAtoms,10000000,Atom).
72 % atom_codes with list of 100000000 takes very long, with 10000000 a few seconds
73
74 % concats a directory with a tail file path
75 % checks if directory ends in slash and adds one if necessary and ensures only one / is present
76 % sicstus can deal paths with multiple /, so this is more for aesthetic reasons
77 ajoin_path(Directory,File,FullPath) :- \+ atom(Directory),!,
78 illarg(force_type(atom), ajoin_path(Directory,File,FullPath) , 1).
79 ajoin_path(Directory,File,FullPath) :- \+ atom(File),!,
80 illarg(force_type(atom), ajoin_path(Directory,File,FullPath) , 2).
81 ajoin_path(Directory,File,FullPath) :-
82 atom_codes(Directory,DC),
83 atom_codes(File,FC),
84 Sep = 0'/,
85 (FC=[Sep|FC2] -> FC2=FC ; FC2=[Sep|FC]),
86 (append(DC2,[Sep],DC) % directory ends in /
87 -> append(DC2,FC2,FullCodes)
88 ; append(DC,FC2,FullCodes)),
89 atom_codes(FullPath,FullCodes).
90
91
92 %safe_atom_concat(A,B,C,ExceptionOcc) :-
93 % catch(atom_concat(A,B,C),
94 % error(representation_error(max_atom_length),_),
95 % (print(exception(max_atom_length)),nl,A=C,ExceptionOcc=true)).
96
97 :- use_module(library(codesio),[write_to_codes/2]).
98
99 % convert an atom, number or other term into an atom
100 convert_to_atom(X,A) :- atom(X),!, A=X.
101 convert_to_atom(X,A) :- number(X),!, number_codes(X,C), atom_codes(A,C).
102 convert_to_atom(X,A) :- write_to_codes(X,C), atom_codes(A,C).
103
104 %toAtom(Number,Atom) :- number(Number),!,number_chars(Number,C),atom_chars(Atom,C).
105 %toAtom(Atom,Atom) :- atomic(Atom),!.
106 %toAtom(Term,Atom) :- write_to_codes(Term,Codes),safe_atom_codes(Atom,Codes).
107
108 toCodes(Number,Codes) :- number(Number),!,number_codes(Number,Codes).
109 toCodes(Atom,Codes) :- atomic(Atom),!,atom_codes(Atom,Codes).
110 toCodes(Term,Codes) :- write_to_codes(Term,Codes).
111
112
113 % match an atom with a term
114 % we want to match the atom 's(0)' with the term s(0)
115 match_atom(Atom,Term) :- atom(Term),!,Term=Atom.
116 match_atom(Atom,Term) :- atom_codes(Atom,AtomCodes), write_to_codes(Term,Codes),Codes=AtomCodes.
117
118 % version where atom_codes is precomputed for efficiency:
119 match_atom(Atom,_,Term) :- atom(Term),!,Term=Atom.
120 match_atom(_Atom,AtomCodes,Term) :- write_to_codes(Term,Codes),Codes=AtomCodes.
121
122
123 % a version of number codes with 0 padding at the left to achieve minimum length.
124 number_codes_min_length(Nr,Min,Codes) :- number_codes(Nr,C1), length(C1,Len),
125 pad_nrc(Len,Min,C1,Codes).
126 pad_nrc(L,Min,C,R) :- L >= Min,!, R=C.
127 pad_nrc(L,Min,C,[0'0 | RT]) :- L1 is L+1,
128 pad_nrc(L1,Min,C,RT).
129
130 % a copy of safe_atom_codes/2 from tools to avoid module dependency on error_manager
131 safe_atom_codes(V,C) :- var(V),var(C),!,
132 print_error('Variables in call: '),print_error(safe_atom_codes(V,C)),
133 C='$VARIABLE$'.
134 safe_atom_codes(A,C) :-
135 catch(atom_codes(A,C),
136 error(representation_error(max_atom_length),_),
137 (print(exception(max_atom_length)),nl,atom_codes_with_limit(A,1000,C))).
138
139 % will concatenate until the Limit is reached or exceeded; it may produce atoms longer than Limit
140 % (if first atom already longer than limit + it adds ...
141 %:- assert_must_succeed((tools: ajoin_with_limit(['A','B','C','D'],100,Text), Text == 'ABCD')).
142 %:- assert_must_succeed((tools: ajoin_with_limit(['A','B','C','D'],2,Text), Text == 'AB...')).
143
144
145 ajoin_with_limit(Atoms,Limit,Result) :-
146 ajoin_codes_with_limit(Atoms,Limit,Codes),
147 safe_atom_codes(Atom,Codes), Result=Atom.
148
149 %:- assert_must_succeed((tools: ajoin_codes_with_limit(['A','B','C','D'],100,Text), Text == "ABCD")).
150 ajoin_codes_with_limit([],_,[]).
151 ajoin_codes_with_limit([Atom|TAtoms],Limit,Res) :-
152 toCodes(Atom,AtomCodes),
153 add_codes(AtomCodes,TAtoms,Limit,Res).
154
155 add_codes([],TAtoms,Limit,Res) :- !, ajoin_codes_with_limit(TAtoms,Limit,Res).
156 add_codes(_,_,Limit,Res) :- Limit < 1, !, Res = "...".
157 add_codes([H|T],TAtoms,Limit,[H|TR]) :- L1 is Limit-1,
158 add_codes(T,TAtoms,L1,TR).
159
160
161
162 :- use_module(tools_printing,[print_error/1]).
163 safe_name([], "[]") :- !. % [] is not an atom on SWI-Prolog!
164 safe_name(X,N) :- atom(X),!, atom_codes(X,N).
165 safe_name(X,N) :- number(X),!, number_codes(X,N).
166 safe_name(X,N) :- var(X),!, N="var".
167 safe_name(lambda_res(X),[114,101,115,95|N]) :- !, atom_codes(X,N).
168 safe_name(X,N) :- functor(X,F,_),atom_codes(F,N), print_error(non_atomic_in_safe_name(X)).
169
170
171 predicate_functor(X,F,N) :- var(X),!, print_error(var_in_predicate_functor),F='$VAR',N=0.
172 predicate_functor(_Module:Pred,F,N) :- !,predicate_functor(Pred,F,N).
173 predicate_functor(P,F,N) :- functor(P,F,N).
174
175
176
177 atom_codes_with_limit(A,C) :-
178 catch(
179 atom_codes(A,C),
180 error(representation_error(max_atom_length),_),
181 (print(exception(max_atom_length)),nl,atom_codes_with_limit(A,1000,C))).
182
183
184 atom_codes_with_limit(A,Limit,Codes) :- var(A), Limit >= 0, !,
185 truncate_codes(Codes,Limit,TCodes,_),
186 atom_codes(A,TCodes).
187 %atom_codes_with_limit(A,Limit,Codes) :- compound(A),!, % should we catch this error?
188 atom_codes_with_limit(A,Limit,Codes) :- Limit < 1, !, atom_codes(A,Codes).
189 atom_codes_with_limit(A,Limit,Codes) :- atom_codes(A,Codes1),
190 truncate_codes(Codes,Limit,Codes1,_).
191
192
193 truncate_codes([],_,[],false).
194 truncate_codes([H|T],Count,Res,Trunc) :-
195 Count<1 -> Res = [46,46,46],Trunc=true /* '...' */
196 ; Res = [H|TT], C1 is Count-1, truncate_codes(T,C1,TT,Trunc).
197
198 %:- assert_must_succeed((tools_strings:truncate_atom(abcd,100,Text), Text == 'abcd')).
199 %:- assert_must_succeed((tools_strings:truncate_atom(abcd,2,Text), Text == 'ab...')).
200 %:- assert_must_succeed((tools_strings:truncate_atom(abcd,0,Text), Text == '...')).
201 % TO DO: could be made more efficient by using something like sub_atom(Atom,0,Limit,_,NewAtom)
202 truncate_atom(Atom,Limit,NewAtom) :-
203 atom_codes(Atom,Codes),
204 truncate_codes(Codes,Limit,TCodes,Trunc),
205 (Trunc=true -> atom_codes(NewAtom,TCodes) ; NewAtom=Atom).
206
207
208 %atom_prefix(_,Atom) :- \+ atom(Atom),!, trace,fail.
209 atom_prefix(Prefix,Atom) :-
210 sub_atom(Atom,0,_,_,Prefix). % instead of atom_concat(Prefix,_,Atom)
211
212 %atom_suffix(_,Atom) :- \+ atom(Atom),!, trace,fail.
213 atom_suffix(Suffix,Atom) :-
214 sub_atom(Atom,_,_,0,Suffix). % instead of atom_concat(_,Suffix,Atom)
215
216 % Atom can be split into Prefix.Sep.Suffix
217 atom_split(Prefix,Sep,Suffix,Atom) :-
218 atom_concat(Prefix,Suffix1,Atom),
219 atom_concat(Sep,Suffix,Suffix1).
220
221 convert_cli_arg(PrefVal,Value) :- compound(PrefVal),!,Value=PrefVal.
222 convert_cli_arg(Atom,Value) :-
223 convert_atom_to_number(Atom,Nr),!, /* convert '12' to 12 */
224 Value=Nr.
225 convert_cli_arg(V,V).
226
227 convert_to_number(Nr,Res) :- number(Nr),!,Res=Nr.
228 convert_to_number(Atom,Nr) :- convert_atom_to_number(Atom,Nr).
229
230 convert_atom_to_number(Atom,Nr) :-
231 atom(Atom), atom_codes(Atom,C),
232 catch(number_codes(Nr,C),
233 error(syntax_error(_N),_),
234 % in this case safe_number_codes fails ; we cannot convert the codes into a number
235 fail).
236
237
238 % detect simple ASCII classical B identifiers accepted by the parser
239 is_simple_classical_b_identifier(Atom) :- atom_codes(Atom,Codes),
240 is_simple_classical_b_identifier_codes(Codes).
241 is_simple_classical_b_identifier_codes(Codes) :-
242 Codes = [Code|T],
243 ? is_valid_id_letter_code(Code),!,
244 legal_id_aux(T).
245
246 legal_id_aux([]).
247 legal_id_aux([0'$,0'0]) :- !. % only $0 allowed
248 ?legal_id_aux([Code|T]) :- legal_id_code(Code),!,legal_id_aux(T).
249
250 legal_id_code(0'_).
251 % FIXME Primes should only be allowed at the end of the identifier, not in the middle!
252 legal_id_code(0'\').
253 legal_id_code(0x2032). % Unicode prime
254 legal_id_code(C) :- is_digit_code(C).
255 ?legal_id_code(C) :- is_valid_id_letter_code(C).
256
257 is_digit_code(Code) :- Code >= 48, Code =< 57. % 0-9
258
259 is_alphabetical_ascii_code(Code) :- Code >= 65,
260 ( Code =< 90 -> true % A-Z
261 ; Code >= 97, Code =< 122). % a-z
262 is_alphabetical_ascii_codes([]).
263 is_alphabetical_ascii_codes([H|T]) :- is_alphabetical_ascii_code(H), is_alphabetical_ascii_codes(T).
264
265 % the codes list represents a classical ASCII identifier starting with a letter and then digits/letters
266 is_ascii_id_codes([H|T]) :- is_alphabetical_ascii_code(H),
267 is_alphabetical_ascii_or_digit_codes(T).
268 is_alphabetical_ascii_or_digit_codes([]).
269 is_alphabetical_ascii_or_digit_codes([H|T]) :-
270 (is_alphabetical_ascii_code(H) -> true ; is_digit_code(H) -> true),
271 is_alphabetical_ascii_or_digit_codes(T).
272
273 is_greek_lower_case(Code) :- Code >= 945, % alpha
274 Code =< 1017. % omega
275 is_greek_upper_case(Code) :- Code >= 916, % Alpha
276 Code =< 937. % Omega
277
278 % partially taken from BParser.scc: unicode_letter definition from java-1.7.sablecc
279 is_umlaut_code(Code) :- Code < 0xc0, !, fail.
280 is_umlaut_code(Code) :- Code >= 0xc0, Code =< 0xd6. % [0x00c0..0x00d6] in BParser.scc )
281 is_umlaut_code(Code) :- Code >= 0xd8, Code =< 0xf6. % [0x00c0..0x00d6] in BParser.scc
282 is_umlaut_code(Code) :- Code >= 0xf8, Code =< 0x01f5. % [0x00f8..0x01f5] in BParser.scc
283 is_umlaut_code(Code) :- Code >= 0x01fa, Code =< 0x0217.
284 is_umlaut_code(Code) :- Code >= 0x0401, Code =< 0x040c. % Cyrillic 0x0401..0x040c] + [0x040e..0x044f]
285 is_umlaut_code(Code) :- Code >= 0x040e, Code =< 0x044f.
286 is_umlaut_code(Code) :- Code >= 0x0451, Code =< 0x045c. % [0x0451..0x045c] + [0x045e..0x0481]
287 is_umlaut_code(Code) :- Code >= 0x045e, Code =< 0x0481.
288 % TO DO: add more
289
290 is_greek_lambda_code(955). % special treatment as it is used as an operator
291
292 is_valid_id_letter_code(Code) :- is_alphabetical_ascii_code(Code).
293 is_valid_id_letter_code(Code) :- is_greek_lower_case(Code), % accepted by ProB, but not by Atelier-B
294 \+ is_greek_lambda_code(Code).
295 is_valid_id_letter_code(Code) :- is_greek_upper_case(Code). % accepted by ProB, but not by Atelier-B
296 is_valid_id_letter_code(Code) :- is_umlaut_code(Code). % accepted by ProB, but not by Atelier-B
297
298 is_composed_classical_b_identifier(Atom) :- atom_codes(Atom,Codes),
299 Codes = [Code|T],
300 is_valid_id_letter_code(Code), !,
301 legal_comp_id_aux(T).
302
303 legal_comp_id_aux([]).
304 legal_comp_id_aux([0'$,0'0]). % only $0 allowed
305 legal_comp_id_aux([0'. , Code |T]) :- !, % a dot which must be followed by a new legal id
306 is_valid_id_letter_code(Code), !, legal_comp_id_aux(T).
307 legal_comp_id_aux([Code|T]) :- legal_id_code(Code), !, legal_comp_id_aux(T).
308
309 % strip newlines and replace by space
310 strip_newlines(Atom,SAtom) :- atom_codes(Atom,Codes), strip_aux(Codes,SC), atom_codes(SAtom,SC).
311
312 strip_aux([],R) :- !,R=[].
313 strip_aux([10|T],R) :- !, strip_aux(T,R).
314 strip_aux([13|T],[32|R]) :- !, strip_aux(T,R).
315 strip_aux([H|T],[H|R]) :- !,strip_aux(T,R).
316
317 :- use_module(library(lists),[maplist/3]).
318
319 % transform upper case to lower case, except for first letter
320 atom_tail_to_lower_case(ATOM_uc,Atom_lc) :-
321 atom_codes(ATOM_uc,[First|TC]),
322 maplist(simple_lowcase,TC,TC2),
323 atom_codes(Atom_lc,[First|TC2]).
324
325 simple_lowcase(H,R) :- 0'A =< H, H =< 0'Z, !, R is H+0'a-0'A.
326 simple_lowcase(Code,Code).
327
328
329 % ---------- hex utilities: to do move to tools ?
330
331
332 %:- assert_must_succeed(tools_strings: get_hex_codes(255,"ff").
333 get_hex_codes(0,Chars) :- !, Chars="0".
334 get_hex_codes(Nr,Chars) :- get_hex_codes(Nr,[],Chars).
335
336 get_hex_codes(0,Acc,R) :- !, R=Acc.
337 get_hex_codes(Nr,Acc,R) :-
338 DigNr is Nr mod 16,
339 get_hex_code(DigNr,Char),
340 N1 is Nr // 16,
341 get_hex_codes(N1,[Char|Acc],R).
342
343 %:- assert_must_succeed(tools_strings: get_hex_bytes([255,3],"ff03")).
344 % use for converting output of sha hash library:
345 get_hex_bytes([],[]).
346 get_hex_bytes([Byte|T],[C1,C2|HT]) :- get_hex_byte(Byte,C1,C2), get_hex_bytes(T,HT).
347
348 get_hex_byte(Byte,C1,C2) :-
349 N1 is Byte // 16, N2 is Byte mod 16,
350 get_hex_code(N1,C1), get_hex_code(N2,C2).
351 get_hex_code(Nr,Digit) :- Nr<10,!, Digit is Nr+48. % "0" = [48]
352 get_hex_code(Nr,Digit) :- Nr<16,!, Digit is Nr+87. % "A" = [65], "a" = [97]
353
354 % ---------------------- date utilities
355
356 % a way to print modify_localtime
357 format_modified_info(Stream,ModLocTime) :-
358 format(Stream,' (modified on ',[]),
359 format_datime(Stream,ModLocTime),
360 format(Stream,')~n',[]).
361
362 format_datime(Stream,datime(Yr,Mon,Day,Hr,Min,_Sec)) :-
363 number_codes_min_length(Min,2,MC),!,
364 format(Stream,'~w/~w/~w at ~w:~s',[Day,Mon,Yr,Hr,MC]).
365 format_datime(Stream,Time) :-
366 format(Stream,'<<?? ~w ??>>',Time).
367
368 :- use_module(library(codesio),[format_to_codes/3]).
369
370 format_datime_to_codes(datime(Yr,Mon,Day,Hr,Min,_Sec),Codes) :-
371 number_codes_min_length(Min,2,MC),!,
372 format_to_codes('~w/~w/~w at ~w:~s',[Day,Mon,Yr,Hr,MC],Codes).
373 format_datime_to_codes(Time,Codes) :-
374 format('<<?? ~w ??>>',Time,Codes).
375