mirror of
https://github.com/Mercury-Language/mercury.git
synced 2026-04-17 02:13:54 +00:00
library/io.m:
Add two new predicates.
The new predicate read_named_file_as_string reads in a named file,
and returns its contents as a single giant string. This is effectively
a combination of open_input, read_file_as_string, and close_input.
The new predicate read_named_file_as_lines reads in a named file,
and returns its contents as a list of strings, each containing
the contents one line (minus the newline char at the end, if any).
This is effectively a combination of open_input, read_file_as_string,
split_into_lines (see below), and close_input.
library/string.m:
Add a new function, split_into_lines, which breaks up a string into
its constituent lines, returning each line minus its newline.
This differs from the old split_at_char('\n', ...) in that it
expects the input string to end with a newline, and does not return
an empty string after a final newline. (If there *are* characters
after the final newline, it does return *them* as the final line.)
NEWS:
Announce the new predicates and function.
compiler/source_file_map.m:
Use the new functionality to simplify the code that reads in
Mercury.modules files.
tests/hard_coded/string_split.{m,exp}:
Add tests for split_into_lines.
58 lines
1.6 KiB
Mathematica
58 lines
1.6 KiB
Mathematica
%---------------------------------------------------------------------------%
|
|
% vim: ts=4 sw=4 et ft=mercury
|
|
%---------------------------------------------------------------------------%
|
|
|
|
:- module string_split.
|
|
:- interface.
|
|
:- import_module io.
|
|
|
|
:- pred main(io::di, io::uo) is det.
|
|
|
|
:- implementation.
|
|
|
|
:- import_module char.
|
|
:- import_module list.
|
|
:- import_module string.
|
|
|
|
main(!IO) :-
|
|
io.write_list(
|
|
split_at_separator(char.is_upper, ""),
|
|
":", io.write_string, !IO),
|
|
io.nl(!IO),
|
|
io.write_list(
|
|
split_at_separator(char.is_upper, "!"),
|
|
":", io.write_string, !IO),
|
|
io.nl(!IO),
|
|
io.write_list(
|
|
split_at_separator(char.is_upper, "helloXworldXhowXareYyou!"),
|
|
":", io.write_string, !IO),
|
|
io.nl(!IO),
|
|
io.write_list(
|
|
split_at_separator(char.is_whitespace, "hello world\thow are\t\tyou!"),
|
|
"<tab>", io.write_string, !IO),
|
|
io.nl(!IO),
|
|
io.write_list(
|
|
split_at_char(':', "user:group:id1:id2"),
|
|
"<tab>", io.write_string, !IO),
|
|
io.nl(!IO),
|
|
io.write_list(
|
|
split_at_string("aa", "xaaayaaaz"),
|
|
"<tab>", io.write_string, !IO),
|
|
io.nl(!IO),
|
|
io.write_list(
|
|
split_at_string("aaa", "xaaaa aaaaax aaa x"),
|
|
"<tab>", io.write_string, !IO),
|
|
io.nl(!IO),
|
|
io.write_list(
|
|
split_at_string(":::", "col1:::col2:val2:::col3:::"),
|
|
"<tab>", io.write_string, !IO),
|
|
io.nl(!IO),
|
|
|
|
io.nl(!IO),
|
|
list.foldl(io.write_line,
|
|
split_into_lines("line1\nline2\nline3\nline4\n"), !IO),
|
|
io.nl(!IO),
|
|
list.foldl(io.write_line,
|
|
split_into_lines("line1\nline2\nline3\nline4nonl"), !IO),
|
|
true.
|