fields/ library predicate, to return empty fields.

This commit is contained in:
Costa Vitor 2009-07-15 17:30:27 -05:00
parent 0b85333018
commit a69a34f3a5
2 changed files with 55 additions and 0 deletions

View File

@ -8854,6 +8854,37 @@ S = ["Hello","I","am","free"] ?
no
@end example
@item split(+@var{Line},-@var{Split})
@findex split/2
@snindex split/2
@cnindex split/2
Unify @var{Words} with a set of strings obtained from @var{Line} by
using the blank characters as separators.
@item fields(+@var{Line},+@var{Separators},-@var{Split})
@findex fields/3
@snindex fields/3
@cnindex fields/3
Unify @var{Words} with a set of strings obtained from @var{Line} by
using the character codes in @var{Separators} as separators for
fields. If two separators occur in a row, the field is considered
empty. As an example, consider:
@example
?- fields("Hello I am free"," *",S).
S = ["Hello","","I","am","","free"] ?
@end example
@item fields(+@var{Line},-@var{Split})
@findex fields/2
@snindex fields/2
@cnindex fields/2
Unify @var{Words} with a set of strings obtained from @var{Line} by
using the blank characters as field separators.
@item glue(+@var{Words},+@var{Separator},-@var{Line})
@findex glue/3
@snindex glue/3

View File

@ -5,6 +5,8 @@
scan_integer/3,
split/2,
split/3,
fields/2,
fields/3,
glue/3,
copy_line/2,
filter/3,
@ -69,6 +71,28 @@ split(SplitCodes, [C|New], Set) -->
split(SplitCodes, New, Set).
split(_, [], []) --> [].
fields(String, Strings) :-
fields(" ", Strings, String, []).
fields(String, FieldsCodes, Strings) :-
dofields(FieldsCodes, First, More, String, []),
(
First = [], More = []
->
Strings = []
;
Strings = [First|More]
).
dofields(FieldsCodes, [], New.More) -->
[C],
{ member(C, FieldsCodes) }, !,
dofields(FieldsCodes, New, More).
dofields(FieldsCodes, [C|New], Set) -->
[C], !,
dofields(FieldsCodes, New, Set).
dofields(_, [], []) --> [].
glue([], _, []).
glue([A], _, A) :- !.
glue([H|T], [B|_], Merged) :-