[m-rev.] diff: add new predicates to string module

Julien Fischer juliensf at csse.unimelb.edu.au
Mon Feb 11 13:53:16 AEDT 2008


Estimated hours taken: 1
Branches: main

Add some new predicates to the string module.

library/string:
 	Add a new predicate string.is_all_digits/1 that tests whether a string
 	consists entirely of decimal digits.

 	Export the predicate string.all_match/2.

 	Add string.remove_prefix/3 which is a synonym for the string.append(in, uo, in)
 	except that it has a more meaningful name and the argument ordering is more
 	convenient for use with higher-order code.

profiler/demangle.m:
 	Do not define remove_prefix/3 here; use the one from the standard library
 	instead.

NEWS:
 	Announce the new additions.

tests/general/string_test.m:
tests/general/string_test_2.m:
tests/general/string_test_2.exp:
 	Add a test for remove_prefix/3.

 	Update the syntax used in these files, e.g. replace DCGs with
 	state variables etc.

tests/general/.cvsignore:
 	Ignore generated files.

Julien.

Index: NEWS
===================================================================
RCS file: /home/mercury/mercury1/repository/mercury/NEWS,v
retrieving revision 1.485
diff -u -r1.485 NEWS
--- NEWS	22 Jan 2008 15:17:51 -0000	1.485
+++ NEWS	11 Feb 2008 02:48:40 -0000
@@ -96,6 +96,9 @@
  	string.split_at_char/2
  	string.split_at_string/2
  	string.remove_suffix_if_present/2
+	string.is_all_digits/1
+	string.all_match/2
+	string.remove_prefix/3

  * The following functions and predicates have been added to the bag module:
  	bag.count/1
Index: library/string.m
===================================================================
RCS file: /home/mercury/mercury1/repository/mercury/library/string.m,v
retrieving revision 1.269
diff -u -r1.269 string.m
--- library/string.m	30 Dec 2007 05:24:57 -0000	1.269
+++ library/string.m	11 Feb 2008 02:48:40 -0000
@@ -97,10 +97,17 @@
  :- pred string.remove_suffix(string::in, string::in, string::out) is semidet.

      % string.remove_suffix_if_present(Suffix, String) returns `String' minus
-    % `Suffix' if `String' ends with `Suffix', `String' otherwise
+    % `Suffix' if `String' ends with `Suffix', `String' otherwise.
      %
  :- func string.remove_suffix_if_present(string, string) = string.

+    % string.remove_prefix(Prefix, String, Suffix):
+    % This is a synonym for string.append(Prefix, Suffix, String) but with
+    % the arguments in a more convenient order for use with higher-order
+    % code.
+    %
+:- pred string.remove_prefix(string::in, string::in, string::out) is semidet.
+
      % string.prefix(String, Prefix) is true iff Prefix is a prefix of String.
      % Same as string.append(Prefix, _, String).
      %
@@ -351,6 +358,18 @@
      %
  :- pred string.is_all_alnum_or_underscore(string::in) is semidet.

+    % True if the string contains only decimal digits (0-9).
+    %
+:- pred string.is_all_digits(string::in) is semidet.
+
+    % string.all_match(TestPred, String):
+    %
+    % True if TestPred is true when applied to each character in
+    % String or if String is the empty string.
+    % 
+:- pred string.all_match(pred(char)::in(pred(in) is semidet), string::in)
+    is semidet.
+
      % string.pad_left(String0, PadChar, Width, String):
      % Insert `PadChar's at the left of `String0' until it is at least as long
      % as `Width', giving `String'.
@@ -1060,6 +1079,9 @@
      ;
          Out = String
      ).
+ 
+string.remove_prefix(Prefix, String, Suffix) :-
+    string.append(Prefix, Suffix, String).

  :- pragma promise_equivalent_clauses(string.prefix/2).

@@ -1487,9 +1509,6 @@
          S = S0
      ).

-:- pred string.all_match(pred(char)::in(pred(in) is semidet), string::in)
-    is semidet.
-
  string.all_match(P, String) :-
      all_match_2(string.length(String) - 1, P, String).

@@ -1512,6 +1531,9 @@

  string.is_all_alnum_or_underscore(S) :-
      string.all_match(char.is_alnum_or_underscore, S).
+ 
+string.is_all_digits(S) :-
+    string.all_match(char.is_digit, S).

  string.pad_left(String0, PadChar, Width, String) :-
      string.length(String0, Length),
Index: profiler/demangle.m
===================================================================
RCS file: /home/mercury/mercury1/repository/mercury/profiler/demangle.m,v
retrieving revision 1.25
diff -u -r1.25 demangle.m
--- profiler/demangle.m	1 Dec 2006 15:04:39 -0000	1.25
+++ profiler/demangle.m	11 Feb 2008 02:48:40 -0000
@@ -913,11 +913,6 @@
  maybe_remove_prefix(Prefix) -->
      ( remove_prefix(Prefix) -> [] ; [] ).

-:- pred remove_prefix(string::in, string::in, string::out) is semidet.
-
-remove_prefix(Prefix, Name0, Name) :-
-    string.append(Prefix, Name, Name0).
-
  :- pred m_remove_suffix(string::in, string::in, string::out) is semidet.

  m_remove_suffix(Suffix, Name0, Name) :-
Index: tests/general/.cvsignore
===================================================================
RCS file: /home/mercury/mercury1/repository/tests/general/.cvsignore,v
retrieving revision 1.18
diff -u -r1.18 .cvsignore
--- tests/general/.cvsignore	25 Jul 2007 06:40:15 -0000	1.18
+++ tests/general/.cvsignore	11 Feb 2008 02:48:40 -0000
@@ -16,3 +16,7 @@
  base_string_to_integer
  .mgnuc_copts
  .mgnuc_opts
+*.c_date
+*.d
+*.mih
+*.mh
Index: tests/general/string_test.m
===================================================================
RCS file: /home/mercury/mercury1/repository/tests/general/string_test.m,v
retrieving revision 1.7
diff -u -r1.7 string_test.m
--- tests/general/string_test.m	20 Sep 2006 09:42:27 -0000	1.7
+++ tests/general/string_test.m	11 Feb 2008 02:48:40 -0000
@@ -1,102 +1,99 @@
  :- module string_test.
-
  :- interface.

  :- import_module io.

-:- pred main(io__state, io__state).
-:- mode main(di, uo) is det.
+:- pred main(io::di, io::uo) is det.

  :- implementation.
-:- import_module string, list, require.
-
-main -->
-	test("foo", "bar").

-:- pred test(string, string, io__state, io__state).
-:- mode test(in, in, di, uo) is det.
-
-test(X, Y) -->
-	write_message("X: ", X),
-	write_message("Y: ", Y),
-	{ string__append(X, Y, Z) },
-	write_message("X append Y: ", Z),
-	{ string__capitalize_first(X, CapX) },
-	write_message("capitalize_first X: ", CapX),
-	{ string__uncapitalize_first(CapX, UnCapX) },
-	write_message("uncapitalize_first CapX: ", UnCapX),
-	{ string__int_to_string(1234, Num) },
-	write_message("int_to_string 1234: ", Num),
-	{ string__int_to_base_string(1234, 8, Num8) },
-	write_message("octal 1234: ", Num8),
-	{ string__int_to_base_string(1234, 16, Num16) },
-	write_message("hexadecimal 1234: ", Num16),
-	{ NumG1 = string__int_to_base_string_group(1234, 10, 3, ",") },
-	write_message("Grouped 1234: ", NumG1),
-	{ NumG2 = string__int_to_base_string_group(113, 2, 1, "--") },
-	write_message("Grouped 113: ", NumG2),
-	{ NumG3 = string__int_to_string_thousands(1300000) },
-	write_message("Grouped 1300000: ", NumG3),
-	{ NumG4 = string__int_to_base_string_group(45999, 10, 0, ",") },
-	write_message("Non Grouped 45999: ", NumG4),
-	{ string__duplicate_char('f', 5, FiveFs) },
-	( { string__to_int("5678", Num5678) } ->
-		io__write_string("string_to_int 5678: "),
-		io__write_int(Num5678),
-		io__write_string("\n")
+:- import_module list.
+:- import_module require.
+:- import_module string.
+
+main(!IO) :-
+	test("foo", "bar", !IO).
+
+:- pred test(string::in, string::in, io::di, io::uo) is det.
+
+test(X, Y, !IO) :-
+	write_message("X: ", X, !IO),
+	write_message("Y: ", Y, !IO),
+	string.append(X, Y, Z),
+	write_message("X append Y: ", Z, !IO),
+	string.capitalize_first(X, CapX),
+	write_message("capitalize_first X: ", CapX, !IO),
+	string.uncapitalize_first(CapX, UnCapX),
+	write_message("uncapitalize_first CapX: ", UnCapX, !IO),
+	string.int_to_string(1234, Num),
+	write_message("int_to_string 1234: ", Num, !IO),
+	string.int_to_base_string(1234, 8, Num8),
+	write_message("octal 1234: ", Num8, !IO),
+	string.int_to_base_string(1234, 16, Num16),
+	write_message("hexadecimal 1234: ", Num16, !IO),
+	NumG1 = string.int_to_base_string_group(1234, 10, 3, ","),
+	write_message("Grouped 1234: ", NumG1, !IO),
+	NumG2 = string.int_to_base_string_group(113, 2, 1, "--"),
+	write_message("Grouped 113: ", NumG2, !IO),
+	NumG3 = string.int_to_string_thousands(1300000),
+	write_message("Grouped 1300000: ", NumG3, !IO),
+	NumG4 = string.int_to_base_string_group(45999, 10, 0, ","),
+	write_message("Non Grouped 45999: ", NumG4, !IO),
+	string.duplicate_char('f', 5, FiveFs),
+	( string.to_int("5678", Num5678) ->
+		io.write_string("string_to_int 5678: ", !IO),
+		io.write_int(Num5678, !IO),
+		io.write_string("\n", !IO)
  	;
-		{ error("string__to_int(""5678"", _) failed") }
+		error("string.to_int(""5678"", _) failed")
  	),
-	{ string__to_int("asdf", _) ->
-		error("string__to_int(""asdf"", _) succeeded")
+	( string.to_int("asdf", _) ->
+		error("string.to_int(""asdf"", _) succeeded")
  	;
  		true
-	},
-	write_message("Five f's: ", FiveFs),
-	{ string__pad_right(FiveFs, '.', 10, FsAndDots) },
-	write_message("Five f's and five dots: ", FsAndDots),
-	{ string__pad_left(FsAndDots, '-', 15, DashesFsAndDots) },
+	),
+	write_message("Five f's: ", FiveFs, !IO),
+	string.pad_right(FiveFs, '.', 10, FsAndDots),
+	write_message("Five f's and five dots: ", FsAndDots, !IO),
+	string.pad_left(FsAndDots, '-', 15, DashesFsAndDots),
  	write_message("Five dashes, five f's and five dots: ", 
-		DashesFsAndDots),
-	{ Table = string__format_table([left(["aaa", "b", "cc"]), 
+		DashesFsAndDots, !IO),
+	Table = string.format_table([left(["aaa", "b", "cc"]),
  		right(["1111111", "", "333"]), right(["1,300,000.00", 
-		"9,999.00", "123,456,789.99"])], "|") ++ "\n" },
-	write_string(Table),
-	{ Wrapped = string.word_wrap("*aaaaaaaaaaaaaaaaaaaa*  bbbbb bbb  b\t"
+		"9,999.00", "123,456,789.99"])], "|") ++ "\n",
+	io.write_string(Table, !IO),
+	Wrapped = string.word_wrap("*aaaaaaaaaaaaaaaaaaaa*  bbbbb bbb  b\t"
  		++ " ccccc c c c   cccc c c c c ccccc ccc cccc c  ccc ccc ccc "
  		++ "*dddddddddddddddddddddddddddddddddddddddddddddddddddddd*"
  		++ "                                                    eee",
-		10) },
-	{ WrappedHyphen = 
+		10),
+	WrappedHyphen =
  		string.word_wrap_separator(
  		"*aaaaaaaaaaaaaaaaaaaa*  bbbbb bbb  b\t"
  		++ " ccccc c c c   cccc c c c c ccccc ccc cccc c  ccc ccc ccc "
  		++ "*dddddddddddddddddddddddddddddddddddddddddddddddddddddd*"
  		++ "                                                    eee",
-		10, "-") },
-	{ WrappedDots = 
+		10, "-"),
+	WrappedDots =
  		string.word_wrap_separator("*aaaaaa*  bbbbb bbb  b\t"
  		++ " ccccc c c c   cccc c c c c ccccc ccc cccc c  ccc ccc ccc "
  		++ "*dddddddddddddd*"
  		++ "                                                    eee",
-		5, "...") },
-	{ SepTooLong = 
-		string.word_wrap_separator("whatever", 2, "...") },
-	write_string("\nWrapped string:\n"),
-	write_string(Wrapped),
-	write_string("\nWrapped string with hyphens:\n"),
-	write_string(WrappedHyphen),
-	write_string("\nWrapped string with dots:\n"),
-	write_string(WrappedDots),
-	write_string("\nWrapped string where seperator is too long:\n"),
-	write_string(SepTooLong),
-	[].
-
-:- pred write_message(string, string, io__state, io__state).
-:- mode write_message(in, in, di, uo) is det.
-
-write_message(Message, String) -->
-	io__write_string(Message),
-	io__write_string(String),
-	io__write_string("\n").
+		5, "..."),
+	SepTooLong = string.word_wrap_separator("whatever", 2, "..."),
+	io.write_string("\nWrapped string:\n", !IO),
+	io.write_string(Wrapped, !IO),
+	io.write_string("\nWrapped string with hyphens:\n", !IO),
+	io.write_string(WrappedHyphen, !IO),
+	io.write_string("\nWrapped string with dots:\n", !IO),
+	io.write_string(WrappedDots, !IO),
+	io.write_string("\nWrapped string where seperator is too long:\n", !IO),
+	io.write_string(SepTooLong, !IO).
+
+:- pred write_message(string::in, string::in, io::di, io::uo) is det.
+
+write_message(Message, String, !IO) :-
+	io.write_string(Message, !IO),
+	io.write_string(String, !IO),
+	io.write_string("\n", !IO).

Index: tests/general/string_test_2.exp
===================================================================
RCS file: /home/mercury/mercury1/repository/tests/general/string_test_2.exp,v
retrieving revision 1.1
diff -u -r1.1 string_test_2.exp
--- tests/general/string_test_2.exp	4 Nov 1996 07:08:49 -0000	1.1
+++ tests/general/string_test_2.exp	11 Feb 2008 02:48:40 -0000
@@ -3,3 +3,4 @@
  abcdef
  abcdef
  def
+def
Index: tests/general/string_test_2.m
===================================================================
RCS file: /home/mercury/mercury1/repository/tests/general/string_test_2.m,v
retrieving revision 1.2
diff -u -r1.2 string_test_2.m
--- tests/general/string_test_2.m	27 Jul 1995 18:14:26 -0000	1.2
+++ tests/general/string_test_2.m	11 Feb 2008 02:48:40 -0000
@@ -2,36 +2,40 @@
  :- interface.
  :- import_module io.

-:- pred main(io__state::di, io__state::uo) is det.
+:- pred main(io::di, io::uo) is det.

  :- implementation.
  :- import_module string.

-main -->
-	{ string__append("abc", "def", S1) },
-	io__write_string(S1),
-	io__write_string("\n"),
-	{ string__append("", "abcdef", S2) },
-	io__write_string(S2),
-	io__write_string("\n"),
-	{ string__append("abcdef", "", S3) },
-	io__write_string(S3),
-	io__write_string("\n"),
-	( { string__append("", S4, "abcdef") } ->
-		io__write_string(S4),
-		io__write_string("\n")
+main(!IO) :-
+	string.append("abc", "def", S1),
+	io.write_string(S1, !IO),
+	io.write_string("\n", !IO),
+	string.append("", "abcdef", S2),
+	io.write_string(S2, !IO),
+	io.write_string("\n", !IO),
+	string.append("abcdef", "", S3),
+	io.write_string(S3, !IO),
+	io.write_string("\n", !IO),
+	( string.append("", S4, "abcdef") ->
+		io.write_string(S4, !IO),
+		io.write_string("\n", !IO)
  	;
-		io__write_string("failed\n")
+		io.write_string("failed\n", !IO)
  	),
-	( { string__append("abc", S5, "abcdef") } ->
-		io__write_string(S5),
-		io__write_string("\n")
+	( string.append("abc", S5, "abcdef") ->
+		io.write_string(S5, !IO),
+		io.write_string("\n", !IO)
  	;
-		io__write_string("failed\n")
+		io.write_string("failed\n", !IO)
+	),
+	( string.remove_prefix("abc", "abcdef", S6) ->
+		io.write_string(S6, !IO),
+		io.nl(!IO)
+	;
+		io.write_string("failed\n", !IO)
  	).

-
-
  /*
  	( { string__append(S6, "", "abcdef") } ->
  		io__write_string(S6),


--------------------------------------------------------------------------
mercury-reviews mailing list
Post messages to:       mercury-reviews at csse.unimelb.edu.au
Administrative Queries: owner-mercury-reviews at csse.unimelb.edu.au
Subscriptions:          mercury-reviews-request at csse.unimelb.edu.au
--------------------------------------------------------------------------



More information about the reviews mailing list