[m-rev.] diff: change integer.from_string/1

Julien Fischer juliensf at cs.mu.OZ.AU
Wed Nov 10 18:54:41 AEDT 2004


Estimated hours taken: 0.5
Branches: main

Allow strings of digits prefixed by a single '+' to be converted
into arbitrary precision integers.

library/integer.m:
	Modify integer.from_string/1 and integer.det_from_string/1
	so that they accept strings that match the regexp [+-]?[0-9]+.
	Previously they would fail to correctly convert a string of
	digits if it was prefixed by a single '+'.

	This now corresponds to the behaviour of string.to_int/1
	and string.det_to_int/1.

tests/hard_coded/integer_test.m:
	Check that strings of digits prefixed with '+' are converted
	correctly.

Julien.

Index: library/integer.m
===================================================================
RCS file: /home/mercury1/repository/mercury/library/integer.m,v
retrieving revision 1.16
diff -u -r1.16 integer.m
--- library/integer.m	10 Nov 2004 07:12:37 -0000	1.16
+++ library/integer.m	10 Nov 2004 07:34:21 -0000
@@ -996,11 +996,12 @@

 :- func string_to_integer(list(char)::in) = (integer::out) is semidet.

-string_to_integer(CCs @ [C | Cs]) =
+string_to_integer(CCs0 @ [C | Cs]) = Integer :-
 	( C = ('-') ->
-		big_sign(-1, string_to_integer(Cs))
+		Integer = big_sign(-1, string_to_integer(Cs))
 	;
-		string_to_integer_acc(CCs, integer__zero)
+		CCs = ( C = ('+') -> Cs ; CCs0),
+		Integer = string_to_integer_acc(CCs, integer__zero)
 	).

 :- func string_to_integer_acc(list(char)::in, integer::in) = (integer::out)
Index: tests/hard_coded/integer_test.m
===================================================================
RCS file: /home/mercury1/repository/tests/hard_coded/integer_test.m,v
retrieving revision 1.3
diff -u -r1.3 integer_test.m
--- tests/hard_coded/integer_test.m	10 Nov 2004 07:12:38 -0000	1.3
+++ tests/hard_coded/integer_test.m	10 Nov 2004 07:37:48 -0000
@@ -11,7 +11,7 @@
 :- import_module integer, string, list, int, require.

 main -->
-	{ X = integer.det_from_string("1234567890987654321") },
+	{ X = integer.det_from_string("+1234567890987654321") },
 	{ Y = integer.det_from_string(
 		"98765432101234567890123400000009999111") },
 	{ Z = integer(200) },

--------------------------------------------------------------------------
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