[m-rev.] Minor addition to string.m
Ralph Becket
rafe at cs.mu.OZ.AU
Thu Feb 13 12:35:57 AEDT 2003
I'm going to check this one in now:
Estimated hours taken: 6
Branches: main
Addition of various stripping operators to string.m and elem and unsafe_elem.
library/string.m:
Added functions chomp/1, lstrip/1, rstrip/1, strip/1, lstrip/2,
rstrip/2, prefix_length/2, suffix_length/2, elem/2, unsafe_elem/2.
tests/hard_coded/Mmakefile:
tests/hard_coded/string_strip.m:
tests/hard_coded/string_strip.exp:
Added test cases.
Index: NEWS
===================================================================
RCS file: /home/mercury1/repository/mercury/NEWS,v
retrieving revision 1.299
diff -u -r1.299 NEWS
--- NEWS 30 Jan 2003 05:59:16 -0000 1.299
+++ NEWS 12 Feb 2003 05:55:56 -0000
@@ -16,7 +16,9 @@
with `mmc --make'.
Changes to the Mercury standard library:
-* Nothing yet.
+* Several new functions have been added to the string module, namely
+ elem/2, unsafe_elem/2, chomp/1, lstrip/1, lstrip/2, rstrip/1, rstrip/2,
+ strip/1, prefix_length/2, and suffix_length/2.
Portability improvements:
* Nothing yet.
@@ -60,6 +62,10 @@
chapter of the Mercury Language Reference Manual.
Changes to the Mercury standard library:
+
+* Several new functions have been added to the string module, namely
+ elem/2, unsafe_elem/2, chomp/1, lstrip/1, lstrip/2, rstrip/1, rstrip/2,
+ strip/1, prefix_length/2, and suffix_length/2.
* We've added a new library module, `array2d'.
Index: library/string.m
===================================================================
RCS file: /home/mercury1/repository/mercury/library/string.m,v
retrieving revision 1.190
diff -u -r1.190 string.m
--- library/string.m 29 Nov 2002 13:25:56 -0000 1.190
+++ library/string.m 13 Feb 2003 01:28:44 -0000
@@ -263,6 +263,10 @@
% Calls error/1 if `Index' is out of range (negative, or greater than or
% equal to the length of `String').
+:- func string ^ elem(int) = char.
+% A synonym for index_dex/2:
+% String ^ elem(Index) = string__index_det(String, Index).
+
:- func string__unsafe_index(string, int) = char.
:- pred string__unsafe_index(string, int, char).
:- mode string__unsafe_index(in, in, out) is det.
@@ -274,6 +278,50 @@
% may be linear in the length of the string.
% Use with care!
+:- func string ^ unsafe_elem(int) = char.
+% A synonym for index_dex/2:
+% String ^ unsafe_elem(Index) = string__unsafe_index(String, Index).
+
+:- func string__chomp(string) = string.
+% string__chomp(String):
+% `String' minus any single trailing newline character.
+
+:- func string__lstrip(string) = string.
+% string__lstrip(String):
+% `String' minus any initial whitespace characters.
+
+:- func string__rstrip(string) = string.
+% string__rstrip(String):
+% `String' minus any trailing whitespace characters.
+
+:- func string__strip(string) = string.
+% string__strip(String):
+% `String' minus any initial and trailing whitespace characters.
+
+:- func string__lstrip(pred(char), string) = string.
+:- mode string__lstrip(pred(in) is semidet, in ) = out is det.
+% string__lstrip(Pred, String):
+% `String' minus the maximal prefix consisting entirely of
+% chars satisfying `Pred'.
+
+:- func string__rstrip(pred(char), string) = string.
+:- mode string__rstrip(pred(in) is semidet, in ) = out is det.
+% string__rstrip(Pred, String):
+% `String' minus the maximal suffix consisting entirely of
+% chars satisfying `Pred'.
+
+:- func string__prefix_length(pred(char), string) = int.
+:- mode string__prefix_length(pred(in ) is semidet, in) = out is det.
+% string__prefix_length(Pred, String):
+% The length of the maximal prefix of `String' consisting entirely of
+% chars satisfying Pred.
+
+:- func suffix_length(pred(char), string) = int.
+:- mode suffix_length(pred(in ) is semidet, in) = out is det.
+% string__suffix_length(Pred, String):
+% The length of the maximal suffix of `String' consisting entirely of
+% chars satisfying Pred.
+
:- pred string__set_char(char, int, string, string).
:- mode string__set_char(in, in, in, out) is semidet.
% XXX This mode is disabled because the compiler puts constant
@@ -653,6 +701,8 @@
error("string__index_det: index out of range")
).
+String ^ elem(Index) = index_det(String, Index).
+
string__set_char_det(Char, Int, String0, String) :-
( string__set_char(Char, Int, String0, String1) ->
String = String1
@@ -3009,6 +3059,8 @@
error("string__unsafe_index: out of bounds")
).
+String ^ unsafe_elem(Index) = unsafe_index(String, Index).
+
/*-----------------------------------------------------------------------*/
:- pragma c_header_code("
@@ -3799,6 +3851,70 @@
else
error("string__det_base_string_to_int/2: conversion failed")
).
+
+%-----------------------------------------------------------------------------%
+
+chomp(S) =
+ ( if index(S, length(S) - 1, '\n')
+ then left(S, length(S) - 1)
+ else S
+ ).
+
+%-----------------------------------------------------------------------------%
+
+rstrip(S) = rstrip(is_whitespace, S).
+
+%-----------------------------------------------------------------------------%
+
+lstrip(S) = lstrip(is_whitespace, S).
+
+%-----------------------------------------------------------------------------%
+
+strip(S0) = S :-
+ L = prefix_length(is_whitespace, S0),
+ R = suffix_length(is_whitespace, S0),
+ S = substring(S0, L, length(S0) - L - R).
+
+%-----------------------------------------------------------------------------%
+
+rstrip(P, S) = left(S, length(S) - suffix_length(P, S)).
+
+%-----------------------------------------------------------------------------%
+
+lstrip(P, S) = right(S, length(S) - prefix_length(P, S)).
+
+%-----------------------------------------------------------------------------%
+
+prefix_length(P, S) = prefix_length_2(0, length(S), P, S).
+
+
+:- func prefix_length_2(int, int, pred(char), string) = int.
+:- mode prefix_length_2(in, in, pred(in ) is semidet, in) = out is det.
+
+prefix_length_2(I, N, P, S) =
+ ( if I < N % XXX We need ordered conjunction.
+ then ( if P(S ^ unsafe_elem(I)) then prefix_length_2(I + 1, N, P, S)
+ else I
+ )
+ else I
+ ).
+
+%-----------------------------------------------------------------------------%
+
+suffix_length(P, S) = suffix_length_2(length(S) - 1, length(S), P, S).
+
+
+:- func suffix_length_2(int, int, pred(char), string) = int.
+:- mode suffix_length_2(in, in, pred(in ) is semidet, in) = out is det.
+
+suffix_length_2(I, N, P, S) =
+ ( if 0 =< I % XXX We need ordered conjunction.
+ then ( if P(S ^ unsafe_elem(I)) then suffix_length_2(I - 1, N, P, S)
+ else N - (I + 1)
+ )
+ else N - (I + 1)
+ ).
+
%------------------------------------------------------------------------------%
Index: tests/hard_coded/Mmakefile
===================================================================
RCS file: /home/mercury1/repository/tests/hard_coded/Mmakefile,v
retrieving revision 1.185
diff -u -r1.185 Mmakefile
--- tests/hard_coded/Mmakefile 7 Feb 2003 03:55:36 -0000 1.185
+++ tests/hard_coded/Mmakefile 13 Feb 2003 01:33:35 -0000
@@ -138,6 +138,7 @@
string_alignment \
string_alignment_bug \
string_loop \
+ string_strip \
target_mlobjs \
term_io_test \
term_to_univ_test \
Index: tests/hard_coded/string_strip.exp
===================================================================
RCS file: tests/hard_coded/string_strip.exp
diff -N tests/hard_coded/string_strip.exp
--- /dev/null 1 Jan 1970 00:00:00 -0000
+++ tests/hard_coded/string_strip.exp 13 Feb 2003 01:32:57 -0000
@@ -0,0 +1,384 @@
+chomp("foo") = "foo"
+chomp("\tfoo") = "\tfoo"
+chomp("foo\t") = "foo\t"
+chomp("\tfoo\t") = "\tfoo\t"
+chomp("foo") = "foo"
+chomp("\nfoo") = "\nfoo"
+chomp("foo\n") = "foo"
+chomp("\nfoo\n") = "\nfoo"
+chomp("foo") = "foo"
+chomp("\ foo") = "\ foo"
+chomp("foo\ ") = "foo\ "
+chomp("\ foo\ ") = "\ foo\ "
+chomp("foo") = "foo"
+chomp("\t\tfoo") = "\t\tfoo"
+chomp("foo\t\t") = "foo\t\t"
+chomp("\t\tfoo\t\t") = "\t\tfoo\t\t"
+chomp("foo") = "foo"
+chomp("\t\nfoo") = "\t\nfoo"
+chomp("foo\t\n") = "foo\t"
+chomp("\t\nfoo\t\n") = "\t\nfoo\t"
+chomp("foo") = "foo"
+chomp("\t\ foo") = "\t\ foo"
+chomp("foo\t\ ") = "foo\t\ "
+chomp("\t\ foo\t\ ") = "\t\ foo\t\ "
+chomp("foo") = "foo"
+chomp("\n\tfoo") = "\n\tfoo"
+chomp("foo\n\t") = "foo\n\t"
+chomp("\n\tfoo\n\t") = "\n\tfoo\n\t"
+chomp("foo") = "foo"
+chomp("\n\nfoo") = "\n\nfoo"
+chomp("foo\n\n") = "foo\n"
+chomp("\n\nfoo\n\n") = "\n\nfoo\n"
+chomp("foo") = "foo"
+chomp("\n\ foo") = "\n\ foo"
+chomp("foo\n\ ") = "foo\n\ "
+chomp("\n\ foo\n\ ") = "\n\ foo\n\ "
+chomp("foo") = "foo"
+chomp("\ \tfoo") = "\ \tfoo"
+chomp("foo\ \t") = "foo\ \t"
+chomp("\ \tfoo\ \t") = "\ \tfoo\ \t"
+chomp("foo") = "foo"
+chomp("\ \nfoo") = "\ \nfoo"
+chomp("foo\ \n") = "foo\ "
+chomp("\ \nfoo\ \n") = "\ \nfoo\ "
+chomp("foo") = "foo"
+chomp("\ \ foo") = "\ \ foo"
+chomp("foo\ \ ") = "foo\ \ "
+chomp("\ \ foo\ \ ") = "\ \ foo\ \ "
+lstrip("foo") = "foo"
+lstrip("\tfoo") = "foo"
+lstrip("foo\t") = "foo\t"
+lstrip("\tfoo\t") = "foo\t"
+lstrip("foo") = "foo"
+lstrip("\nfoo") = "foo"
+lstrip("foo\n") = "foo\n"
+lstrip("\nfoo\n") = "foo\n"
+lstrip("foo") = "foo"
+lstrip("\ foo") = "foo"
+lstrip("foo\ ") = "foo\ "
+lstrip("\ foo\ ") = "foo\ "
+lstrip("foo") = "foo"
+lstrip("\t\tfoo") = "foo"
+lstrip("foo\t\t") = "foo\t\t"
+lstrip("\t\tfoo\t\t") = "foo\t\t"
+lstrip("foo") = "foo"
+lstrip("\t\nfoo") = "foo"
+lstrip("foo\t\n") = "foo\t\n"
+lstrip("\t\nfoo\t\n") = "foo\t\n"
+lstrip("foo") = "foo"
+lstrip("\t\ foo") = "foo"
+lstrip("foo\t\ ") = "foo\t\ "
+lstrip("\t\ foo\t\ ") = "foo\t\ "
+lstrip("foo") = "foo"
+lstrip("\n\tfoo") = "foo"
+lstrip("foo\n\t") = "foo\n\t"
+lstrip("\n\tfoo\n\t") = "foo\n\t"
+lstrip("foo") = "foo"
+lstrip("\n\nfoo") = "foo"
+lstrip("foo\n\n") = "foo\n\n"
+lstrip("\n\nfoo\n\n") = "foo\n\n"
+lstrip("foo") = "foo"
+lstrip("\n\ foo") = "foo"
+lstrip("foo\n\ ") = "foo\n\ "
+lstrip("\n\ foo\n\ ") = "foo\n\ "
+lstrip("foo") = "foo"
+lstrip("\ \tfoo") = "foo"
+lstrip("foo\ \t") = "foo\ \t"
+lstrip("\ \tfoo\ \t") = "foo\ \t"
+lstrip("foo") = "foo"
+lstrip("\ \nfoo") = "foo"
+lstrip("foo\ \n") = "foo\ \n"
+lstrip("\ \nfoo\ \n") = "foo\ \n"
+lstrip("foo") = "foo"
+lstrip("\ \ foo") = "foo"
+lstrip("foo\ \ ") = "foo\ \ "
+lstrip("\ \ foo\ \ ") = "foo\ \ "
+rstrip("foo") = "foo"
+rstrip("\tfoo") = "\tfoo"
+rstrip("foo\t") = "foo"
+rstrip("\tfoo\t") = "\tfoo"
+rstrip("foo") = "foo"
+rstrip("\nfoo") = "\nfoo"
+rstrip("foo\n") = "foo"
+rstrip("\nfoo\n") = "\nfoo"
+rstrip("foo") = "foo"
+rstrip("\ foo") = "\ foo"
+rstrip("foo\ ") = "foo"
+rstrip("\ foo\ ") = "\ foo"
+rstrip("foo") = "foo"
+rstrip("\t\tfoo") = "\t\tfoo"
+rstrip("foo\t\t") = "foo"
+rstrip("\t\tfoo\t\t") = "\t\tfoo"
+rstrip("foo") = "foo"
+rstrip("\t\nfoo") = "\t\nfoo"
+rstrip("foo\t\n") = "foo"
+rstrip("\t\nfoo\t\n") = "\t\nfoo"
+rstrip("foo") = "foo"
+rstrip("\t\ foo") = "\t\ foo"
+rstrip("foo\t\ ") = "foo"
+rstrip("\t\ foo\t\ ") = "\t\ foo"
+rstrip("foo") = "foo"
+rstrip("\n\tfoo") = "\n\tfoo"
+rstrip("foo\n\t") = "foo"
+rstrip("\n\tfoo\n\t") = "\n\tfoo"
+rstrip("foo") = "foo"
+rstrip("\n\nfoo") = "\n\nfoo"
+rstrip("foo\n\n") = "foo"
+rstrip("\n\nfoo\n\n") = "\n\nfoo"
+rstrip("foo") = "foo"
+rstrip("\n\ foo") = "\n\ foo"
+rstrip("foo\n\ ") = "foo"
+rstrip("\n\ foo\n\ ") = "\n\ foo"
+rstrip("foo") = "foo"
+rstrip("\ \tfoo") = "\ \tfoo"
+rstrip("foo\ \t") = "foo"
+rstrip("\ \tfoo\ \t") = "\ \tfoo"
+rstrip("foo") = "foo"
+rstrip("\ \nfoo") = "\ \nfoo"
+rstrip("foo\ \n") = "foo"
+rstrip("\ \nfoo\ \n") = "\ \nfoo"
+rstrip("foo") = "foo"
+rstrip("\ \ foo") = "\ \ foo"
+rstrip("foo\ \ ") = "foo"
+rstrip("\ \ foo\ \ ") = "\ \ foo"
+strip("foo") = "foo"
+strip("\tfoo") = "foo"
+strip("foo\t") = "foo"
+strip("\tfoo\t") = "foo"
+strip("foo") = "foo"
+strip("\nfoo") = "foo"
+strip("foo\n") = "foo"
+strip("\nfoo\n") = "foo"
+strip("foo") = "foo"
+strip("\ foo") = "foo"
+strip("foo\ ") = "foo"
+strip("\ foo\ ") = "foo"
+strip("foo") = "foo"
+strip("\t\tfoo") = "foo"
+strip("foo\t\t") = "foo"
+strip("\t\tfoo\t\t") = "foo"
+strip("foo") = "foo"
+strip("\t\nfoo") = "foo"
+strip("foo\t\n") = "foo"
+strip("\t\nfoo\t\n") = "foo"
+strip("foo") = "foo"
+strip("\t\ foo") = "foo"
+strip("foo\t\ ") = "foo"
+strip("\t\ foo\t\ ") = "foo"
+strip("foo") = "foo"
+strip("\n\tfoo") = "foo"
+strip("foo\n\t") = "foo"
+strip("\n\tfoo\n\t") = "foo"
+strip("foo") = "foo"
+strip("\n\nfoo") = "foo"
+strip("foo\n\n") = "foo"
+strip("\n\nfoo\n\n") = "foo"
+strip("foo") = "foo"
+strip("\n\ foo") = "foo"
+strip("foo\n\ ") = "foo"
+strip("\n\ foo\n\ ") = "foo"
+strip("foo") = "foo"
+strip("\ \tfoo") = "foo"
+strip("foo\ \t") = "foo"
+strip("\ \tfoo\ \t") = "foo"
+strip("foo") = "foo"
+strip("\ \nfoo") = "foo"
+strip("foo\ \n") = "foo"
+strip("\ \nfoo\ \n") = "foo"
+strip("foo") = "foo"
+strip("\ \ foo") = "foo"
+strip("foo\ \ ") = "foo"
+strip("\ \ foo\ \ ") = "foo"
+lstrip(is_alpha)("foo") = ""
+lstrip(is_alpha)("\tfoo") = "\tfoo"
+lstrip(is_alpha)("foo\t") = "\t"
+lstrip(is_alpha)("\tfoo\t") = "\tfoo\t"
+lstrip(is_alpha)("foo") = ""
+lstrip(is_alpha)("\nfoo") = "\nfoo"
+lstrip(is_alpha)("foo\n") = "\n"
+lstrip(is_alpha)("\nfoo\n") = "\nfoo\n"
+lstrip(is_alpha)("foo") = ""
+lstrip(is_alpha)("\ foo") = "\ foo"
+lstrip(is_alpha)("foo\ ") = "\ "
+lstrip(is_alpha)("\ foo\ ") = "\ foo\ "
+lstrip(is_alpha)("foo") = ""
+lstrip(is_alpha)("\t\tfoo") = "\t\tfoo"
+lstrip(is_alpha)("foo\t\t") = "\t\t"
+lstrip(is_alpha)("\t\tfoo\t\t") = "\t\tfoo\t\t"
+lstrip(is_alpha)("foo") = ""
+lstrip(is_alpha)("\t\nfoo") = "\t\nfoo"
+lstrip(is_alpha)("foo\t\n") = "\t\n"
+lstrip(is_alpha)("\t\nfoo\t\n") = "\t\nfoo\t\n"
+lstrip(is_alpha)("foo") = ""
+lstrip(is_alpha)("\t\ foo") = "\t\ foo"
+lstrip(is_alpha)("foo\t\ ") = "\t\ "
+lstrip(is_alpha)("\t\ foo\t\ ") = "\t\ foo\t\ "
+lstrip(is_alpha)("foo") = ""
+lstrip(is_alpha)("\n\tfoo") = "\n\tfoo"
+lstrip(is_alpha)("foo\n\t") = "\n\t"
+lstrip(is_alpha)("\n\tfoo\n\t") = "\n\tfoo\n\t"
+lstrip(is_alpha)("foo") = ""
+lstrip(is_alpha)("\n\nfoo") = "\n\nfoo"
+lstrip(is_alpha)("foo\n\n") = "\n\n"
+lstrip(is_alpha)("\n\nfoo\n\n") = "\n\nfoo\n\n"
+lstrip(is_alpha)("foo") = ""
+lstrip(is_alpha)("\n\ foo") = "\n\ foo"
+lstrip(is_alpha)("foo\n\ ") = "\n\ "
+lstrip(is_alpha)("\n\ foo\n\ ") = "\n\ foo\n\ "
+lstrip(is_alpha)("foo") = ""
+lstrip(is_alpha)("\ \tfoo") = "\ \tfoo"
+lstrip(is_alpha)("foo\ \t") = "\ \t"
+lstrip(is_alpha)("\ \tfoo\ \t") = "\ \tfoo\ \t"
+lstrip(is_alpha)("foo") = ""
+lstrip(is_alpha)("\ \nfoo") = "\ \nfoo"
+lstrip(is_alpha)("foo\ \n") = "\ \n"
+lstrip(is_alpha)("\ \nfoo\ \n") = "\ \nfoo\ \n"
+lstrip(is_alpha)("foo") = ""
+lstrip(is_alpha)("\ \ foo") = "\ \ foo"
+lstrip(is_alpha)("foo\ \ ") = "\ \ "
+lstrip(is_alpha)("\ \ foo\ \ ") = "\ \ foo\ \ "
+rstrip(is_alpha)("foo") = ""
+rstrip(is_alpha)("\tfoo") = "\t"
+rstrip(is_alpha)("foo\t") = "foo\t"
+rstrip(is_alpha)("\tfoo\t") = "\tfoo\t"
+rstrip(is_alpha)("foo") = ""
+rstrip(is_alpha)("\nfoo") = "\n"
+rstrip(is_alpha)("foo\n") = "foo\n"
+rstrip(is_alpha)("\nfoo\n") = "\nfoo\n"
+rstrip(is_alpha)("foo") = ""
+rstrip(is_alpha)("\ foo") = "\ "
+rstrip(is_alpha)("foo\ ") = "foo\ "
+rstrip(is_alpha)("\ foo\ ") = "\ foo\ "
+rstrip(is_alpha)("foo") = ""
+rstrip(is_alpha)("\t\tfoo") = "\t\t"
+rstrip(is_alpha)("foo\t\t") = "foo\t\t"
+rstrip(is_alpha)("\t\tfoo\t\t") = "\t\tfoo\t\t"
+rstrip(is_alpha)("foo") = ""
+rstrip(is_alpha)("\t\nfoo") = "\t\n"
+rstrip(is_alpha)("foo\t\n") = "foo\t\n"
+rstrip(is_alpha)("\t\nfoo\t\n") = "\t\nfoo\t\n"
+rstrip(is_alpha)("foo") = ""
+rstrip(is_alpha)("\t\ foo") = "\t\ "
+rstrip(is_alpha)("foo\t\ ") = "foo\t\ "
+rstrip(is_alpha)("\t\ foo\t\ ") = "\t\ foo\t\ "
+rstrip(is_alpha)("foo") = ""
+rstrip(is_alpha)("\n\tfoo") = "\n\t"
+rstrip(is_alpha)("foo\n\t") = "foo\n\t"
+rstrip(is_alpha)("\n\tfoo\n\t") = "\n\tfoo\n\t"
+rstrip(is_alpha)("foo") = ""
+rstrip(is_alpha)("\n\nfoo") = "\n\n"
+rstrip(is_alpha)("foo\n\n") = "foo\n\n"
+rstrip(is_alpha)("\n\nfoo\n\n") = "\n\nfoo\n\n"
+rstrip(is_alpha)("foo") = ""
+rstrip(is_alpha)("\n\ foo") = "\n\ "
+rstrip(is_alpha)("foo\n\ ") = "foo\n\ "
+rstrip(is_alpha)("\n\ foo\n\ ") = "\n\ foo\n\ "
+rstrip(is_alpha)("foo") = ""
+rstrip(is_alpha)("\ \tfoo") = "\ \t"
+rstrip(is_alpha)("foo\ \t") = "foo\ \t"
+rstrip(is_alpha)("\ \tfoo\ \t") = "\ \tfoo\ \t"
+rstrip(is_alpha)("foo") = ""
+rstrip(is_alpha)("\ \nfoo") = "\ \n"
+rstrip(is_alpha)("foo\ \n") = "foo\ \n"
+rstrip(is_alpha)("\ \nfoo\ \n") = "\ \nfoo\ \n"
+rstrip(is_alpha)("foo") = ""
+rstrip(is_alpha)("\ \ foo") = "\ \ "
+rstrip(is_alpha)("foo\ \ ") = "foo\ \ "
+rstrip(is_alpha)("\ \ foo\ \ ") = "\ \ foo\ \ "
+prefix_length(is_whitespace)("foo") = "0"
+prefix_length(is_whitespace)("\tfoo") = "1"
+prefix_length(is_whitespace)("foo\t") = "0"
+prefix_length(is_whitespace)("\tfoo\t") = "1"
+prefix_length(is_whitespace)("foo") = "0"
+prefix_length(is_whitespace)("\nfoo") = "1"
+prefix_length(is_whitespace)("foo\n") = "0"
+prefix_length(is_whitespace)("\nfoo\n") = "1"
+prefix_length(is_whitespace)("foo") = "0"
+prefix_length(is_whitespace)("\ foo") = "1"
+prefix_length(is_whitespace)("foo\ ") = "0"
+prefix_length(is_whitespace)("\ foo\ ") = "1"
+prefix_length(is_whitespace)("foo") = "0"
+prefix_length(is_whitespace)("\t\tfoo") = "2"
+prefix_length(is_whitespace)("foo\t\t") = "0"
+prefix_length(is_whitespace)("\t\tfoo\t\t") = "2"
+prefix_length(is_whitespace)("foo") = "0"
+prefix_length(is_whitespace)("\t\nfoo") = "2"
+prefix_length(is_whitespace)("foo\t\n") = "0"
+prefix_length(is_whitespace)("\t\nfoo\t\n") = "2"
+prefix_length(is_whitespace)("foo") = "0"
+prefix_length(is_whitespace)("\t\ foo") = "2"
+prefix_length(is_whitespace)("foo\t\ ") = "0"
+prefix_length(is_whitespace)("\t\ foo\t\ ") = "2"
+prefix_length(is_whitespace)("foo") = "0"
+prefix_length(is_whitespace)("\n\tfoo") = "2"
+prefix_length(is_whitespace)("foo\n\t") = "0"
+prefix_length(is_whitespace)("\n\tfoo\n\t") = "2"
+prefix_length(is_whitespace)("foo") = "0"
+prefix_length(is_whitespace)("\n\nfoo") = "2"
+prefix_length(is_whitespace)("foo\n\n") = "0"
+prefix_length(is_whitespace)("\n\nfoo\n\n") = "2"
+prefix_length(is_whitespace)("foo") = "0"
+prefix_length(is_whitespace)("\n\ foo") = "2"
+prefix_length(is_whitespace)("foo\n\ ") = "0"
+prefix_length(is_whitespace)("\n\ foo\n\ ") = "2"
+prefix_length(is_whitespace)("foo") = "0"
+prefix_length(is_whitespace)("\ \tfoo") = "2"
+prefix_length(is_whitespace)("foo\ \t") = "0"
+prefix_length(is_whitespace)("\ \tfoo\ \t") = "2"
+prefix_length(is_whitespace)("foo") = "0"
+prefix_length(is_whitespace)("\ \nfoo") = "2"
+prefix_length(is_whitespace)("foo\ \n") = "0"
+prefix_length(is_whitespace)("\ \nfoo\ \n") = "2"
+prefix_length(is_whitespace)("foo") = "0"
+prefix_length(is_whitespace)("\ \ foo") = "2"
+prefix_length(is_whitespace)("foo\ \ ") = "0"
+prefix_length(is_whitespace)("\ \ foo\ \ ") = "2"
+suffix_length(is_whitespace)("foo") = "0"
+suffix_length(is_whitespace)("\tfoo") = "0"
+suffix_length(is_whitespace)("foo\t") = "1"
+suffix_length(is_whitespace)("\tfoo\t") = "1"
+suffix_length(is_whitespace)("foo") = "0"
+suffix_length(is_whitespace)("\nfoo") = "0"
+suffix_length(is_whitespace)("foo\n") = "1"
+suffix_length(is_whitespace)("\nfoo\n") = "1"
+suffix_length(is_whitespace)("foo") = "0"
+suffix_length(is_whitespace)("\ foo") = "0"
+suffix_length(is_whitespace)("foo\ ") = "1"
+suffix_length(is_whitespace)("\ foo\ ") = "1"
+suffix_length(is_whitespace)("foo") = "0"
+suffix_length(is_whitespace)("\t\tfoo") = "0"
+suffix_length(is_whitespace)("foo\t\t") = "2"
+suffix_length(is_whitespace)("\t\tfoo\t\t") = "2"
+suffix_length(is_whitespace)("foo") = "0"
+suffix_length(is_whitespace)("\t\nfoo") = "0"
+suffix_length(is_whitespace)("foo\t\n") = "2"
+suffix_length(is_whitespace)("\t\nfoo\t\n") = "2"
+suffix_length(is_whitespace)("foo") = "0"
+suffix_length(is_whitespace)("\t\ foo") = "0"
+suffix_length(is_whitespace)("foo\t\ ") = "2"
+suffix_length(is_whitespace)("\t\ foo\t\ ") = "2"
+suffix_length(is_whitespace)("foo") = "0"
+suffix_length(is_whitespace)("\n\tfoo") = "0"
+suffix_length(is_whitespace)("foo\n\t") = "2"
+suffix_length(is_whitespace)("\n\tfoo\n\t") = "2"
+suffix_length(is_whitespace)("foo") = "0"
+suffix_length(is_whitespace)("\n\nfoo") = "0"
+suffix_length(is_whitespace)("foo\n\n") = "2"
+suffix_length(is_whitespace)("\n\nfoo\n\n") = "2"
+suffix_length(is_whitespace)("foo") = "0"
+suffix_length(is_whitespace)("\n\ foo") = "0"
+suffix_length(is_whitespace)("foo\n\ ") = "2"
+suffix_length(is_whitespace)("\n\ foo\n\ ") = "2"
+suffix_length(is_whitespace)("foo") = "0"
+suffix_length(is_whitespace)("\ \tfoo") = "0"
+suffix_length(is_whitespace)("foo\ \t") = "2"
+suffix_length(is_whitespace)("\ \tfoo\ \t") = "2"
+suffix_length(is_whitespace)("foo") = "0"
+suffix_length(is_whitespace)("\ \nfoo") = "0"
+suffix_length(is_whitespace)("foo\ \n") = "2"
+suffix_length(is_whitespace)("\ \nfoo\ \n") = "2"
+suffix_length(is_whitespace)("foo") = "0"
+suffix_length(is_whitespace)("\ \ foo") = "0"
+suffix_length(is_whitespace)("foo\ \ ") = "2"
+suffix_length(is_whitespace)("\ \ foo\ \ ") = "2"
\ No newline at end of file
Index: tests/hard_coded/string_strip.m
===================================================================
RCS file: tests/hard_coded/string_strip.m
diff -N tests/hard_coded/string_strip.m
--- /dev/null 1 Jan 1970 00:00:00 -0000
+++ tests/hard_coded/string_strip.m 13 Feb 2003 01:27:37 -0000
@@ -0,0 +1,119 @@
+%-----------------------------------------------------------------------------%
+% string_strip.m
+% Ralph Becket <rafe at cs.mu.oz.au>
+% Wed Feb 12 17:10:49 EST 2003
+% vim: ft=mercury ts=4 sw=4 et wm=0 tw=0
+%
+% Test the string strip type functions.
+%
+%-----------------------------------------------------------------------------%
+
+:- module string_strip.
+
+:- interface.
+
+:- import_module io.
+
+
+
+:- pred main(io::di, io::uo) is det.
+
+%-----------------------------------------------------------------------------%
+%-----------------------------------------------------------------------------%
+
+:- implementation.
+
+:- import_module string, int, list, std_util, char.
+
+
+
+:- pred space(char, char).
+:- mode space(in, out ) is semidet.
+:- mode space(out, out ) is multi.
+
+space(' ', ' ').
+space('\t', 't').
+space('\n', 'n').
+
+%-----------------------------------------------------------------------------%
+
+main(!IO) :-
+
+ Strings =
+ condense(
+ map(func(Spaces) = ["foo", Spaces ++ "foo", "foo" ++ Spaces,
+ Spaces ++ "foo" ++ Spaces],
+ condense(
+ map(func(Length) = solutions(mk_spaces(Length)),
+ 1`..`2
+ )
+ )
+ )
+ ),
+
+ Tests = [
+
+ "chomp" -
+ chomp,
+
+ "lstrip" -
+ lstrip,
+
+ "rstrip" -
+ rstrip,
+
+ "strip" -
+ strip,
+
+ "lstrip(is_alpha)" -
+ lstrip(char.is_alpha),
+
+ "rstrip(is_alpha)" -
+ rstrip(char.is_alpha),
+
+ "prefix_length(is_whitespace)" -
+ ( func(S) = format("%d", [i(prefix_length(char.is_whitespace, S))]) ),
+
+ "suffix_length(is_whitespace)" -
+ ( func(S) = format("%d", [i(suffix_length(char.is_whitespace, S))]) )
+
+ ],
+
+ Results =
+ condense(
+ map(func(Name - Test) =
+ map(func(String) =
+ format("%s(\"%s\") = \"%s\"",
+ [s(Name), s(quote(String)), s(quote(Test(String)))]),
+ Strings
+ ),
+ Tests
+ )
+ ),
+
+ io.write_list(Results, "\n", io.write_string, !IO).
+
+%-----------------------------------------------------------------------------%
+
+:- func quote(string) = string.
+
+quote(S) =
+ string.from_char_list(
+ foldr(
+ func(C, Cs) = ( if space(C, X) then ['\\', X | Cs] else [C | Cs] ),
+ string.to_char_list(S),
+ []
+ )
+ ).
+
+%-----------------------------------------------------------------------------%
+
+:- pred mk_spaces(int, string).
+:- mode mk_spaces(in, out ) is multi.
+
+mk_spaces(N, S) :-
+ map((pred(_::in, C::out) is multi :- space(C, _)), 1`..`N, Cs),
+ S = string.from_char_list(Cs).
+
+%-----------------------------------------------------------------------------%
+%-----------------------------------------------------------------------------%
--------------------------------------------------------------------------
mercury-reviews mailing list
post: mercury-reviews at cs.mu.oz.au
administrative address: owner-mercury-reviews at cs.mu.oz.au
unsubscribe: Address: mercury-reviews-request at cs.mu.oz.au Message: unsubscribe
subscribe: Address: mercury-reviews-request at cs.mu.oz.au Message: subscribe
--------------------------------------------------------------------------
More information about the reviews
mailing list