[mercury-users] Reading a word from a string and/or list of char
Robert Ernst Johann JESCHOFNIK
rejj at cat.cs.mu.OZ.AU
Sat Oct 16 14:51:38 AEST 1999
Hello again, everybody.
I have just made some predicates to handle reading the reading in of
whitespace delimited words from either a string, or a list of characters.
They do not mimic io__streams, but apart from that they operate in
basically the same way as io__read_word.
This is all very basic stuff, but I think it is useful. Please feel free
to use this, or comment on my code, or add functionality, or whatever.
I guess I will add in more functionality once I get a bit more time (read:
semester is over), like analogues of io__read_char, io__read_line, etc
etc.
Rob
-------------- next part --------------
%-----------------------------------------------------------------------------%
%
% Read a whitespace delimited word from a string or list of characters.
%
%-----------------------------------------------------------------------------%
:- module word.
:- interface.
:- import_module io, string, char, list.
% reads a whitespace delimited word from the specified string.
% read_word_from_string(String, Word, Rest) will read the first word from
% `String' and bind it as `Word', and give back the remainder of the string
% as `Rest'.
%
% NOTE: the returned io__result will never be `error(_)'
:- pred read_word_from_string(string, io__result(list(char)), string).
:- mode read_word_from_string(in, out, out) is det.
% reads a whitespace delimited word from the specified list of chars.
% read_word_from_list(List, Word, Rest) will read the first word from
% `List' and bind it as `Word', and give back the remainder of the list
% as `Rest'.
%
% NOTE: the returned io__result will never be `error(_)'
:- pred read_word_from_list(list(char), io__result(list(char)), list(char)).
:- mode read_word_from_list(in, out, out) is det.
:- pred main(io__state::di, io__state::uo) is det.
%-----------------------------------------------------------------------------%
:- implementation.
read_word_from_string(String, Result, Rest) :-
string__to_char_list(String, List0),
ignore_leading_whitespace(List0, List),
read_word_from_list2(List, Word, ListRest),
string__from_char_list(ListRest, Rest),
(
Word = [],
Result = eof
;
Word = [_ | _],
Result = ok(Word)
).
read_word_from_list(List, Result, Rest) :-
ignore_leading_whitespace(List, List0),
read_word_from_list2(List0, Word, Rest),
(
Word = [],
Result = eof
;
Word = [_ | _],
Result = ok(Word)
).
% Ignore any leading whitespace chararters in the given list of chars.
:- pred ignore_leading_whitespace(list(char), list(char)).
:- mode ignore_leading_whitespace(in, out) is det.
ignore_leading_whitespace([], []).
ignore_leading_whitespace([Char | Chars], List) :-
(
char__is_whitespace(Char)
->
ignore_leading_whitespace(Chars, List)
;
List = [Char | Chars]
).
:- pred read_word_from_list2(list(char), list(char), list(char)).
:- mode read_word_from_list2(in, out, out) is det.
read_word_from_list2([], [], []).
read_word_from_list2([Char | Chars], Word, Rest) :-
(
char__is_whitespace(Char)
->
Word = [],
Rest = [Char | Chars]
;
read_word_from_list2(Chars, Word0, Rest),
Word = [Char | Word0]
).
More information about the users
mailing list