[m-rev.] diff: use integer constant functions in string formatting

Julien Fischer jfischer at opturion.com
Tue May 26 18:27:22 AEST 2020


Use integer constant functions in string formatting.

library/string.format.m:
     Use the constant functions exported by the integer module
     for small integers where possible; this avoids having to
     construct those integers at runtime.

library/integer.m:
     Add constant functions returning 8 and 16 as arbitrary
     precision integers.

NEWS:
     Announce the above additions.

Julien.

diff --git a/NEWS b/NEWS
index 426b733..90d1a44 100644
--- a/NEWS
+++ b/NEWS
@@ -94,6 +94,13 @@ Changes to the Mercury standard library

      - pred `hash/2`

+### Changes to the `integer` modules
+
+* The following functions have been added:
+
+   - func `eight/0`
+   - func `sixteen/0`
+
  ### Changes to the `hash_table` module

  * The following predicates have been deprecated and will be removed in a future
diff --git a/library/integer.m b/library/integer.m
index 8caa1ea..c063d5a 100644
--- a/library/integer.m
+++ b/library/integer.m
@@ -51,10 +51,18 @@
      %
  :- func two = integer.

+    % Equivalent to integer(8).
+    %
+:- func eight = integer.
+
      % Equivalent to integer(10).
      %
  :- func ten = integer.

+    % Equivalent to integer(16).
+    %
+:- func sixteen = integer.
+
  %---------------------------------------------------------------------------%

      % X < Y: Succeed if and only if X is less than Y.
@@ -614,8 +622,12 @@ one = i(1, [1]).

  two = i(1, [2]).

+eight = i(1, [8]).
+
  ten = i(1, [10]).

+sixteen = i(1, [16]).
+
  %---------------------------------------------------------------------------%

  '<'(X, Y) :-
diff --git a/library/string.format.m b/library/string.format.m
index d8f958a..236a104 100644
--- a/library/string.format.m
+++ b/library/string.format.m
@@ -868,7 +868,7 @@ format_unsigned_int(Flags, MaybeWidth, MaybePrec, Base, Int) = String :-
                  AbsIntStr = abs_int_to_hex_uc(Int)
              )
          else
-            Div = integer.pow(integer(2), integer(int.bits_per_int)),
+            Div = integer.pow(integer.two, integer(int.bits_per_int)),
              UnsignedInteger = integer(Int) mod Div,
              (
                  Base = base_octal,
@@ -1219,8 +1219,8 @@ justify_string(Flags, MaybeWidth, Str) = JustifiedStr :-
  :- func abs_int_to_octal(int) = string.

  abs_integer_to_octal(Num) = NumStr :-
-    ( if Num > integer(0) then
-        Integer8 = integer(8),
+    ( if Num > integer.zero then
+        Integer8 = integer.eight,
          FrontDigitsStr = abs_int_to_octal(det_to_int(Num // Integer8)),
          LastDigitStr = get_octal_digit(det_to_int(Num rem Integer8)),
          NumStr = append(FrontDigitsStr, LastDigitStr)
@@ -1243,8 +1243,8 @@ abs_int_to_octal(Num) = NumStr :-
  :- func abs_int_to_decimal(int) = string.

  abs_integer_to_decimal(Num) = NumStr :-
-    ( if Num > integer(0) then
-        Integer10 = integer(10),
+    ( if Num > integer.zero then
+        Integer10 = integer.ten,
          FrontDigitsStr = abs_int_to_decimal(det_to_int(Num // Integer10)),
          LastDigitStr = get_decimal_digit(det_to_int(Num rem Integer10)),
          NumStr = append(FrontDigitsStr, LastDigitStr)
@@ -1270,8 +1270,8 @@ abs_int_to_decimal(Num) = NumStr :-
  :- func abs_int_to_hex_uc(int) = string.

  abs_integer_to_hex_lc(Num) = NumStr :-
-    ( if Num > integer(0) then
-        Integer16 = integer(16),
+    ( if Num > integer.zero then
+        Integer16 = integer.sixteen,
          FrontDigitsStr = abs_int_to_hex_lc(det_to_int(Num // Integer16)),
          LastDigitStr = get_hex_digit_lc(det_to_int(Num rem Integer16)),
          NumStr = append(FrontDigitsStr, LastDigitStr)
@@ -1280,8 +1280,8 @@ abs_integer_to_hex_lc(Num) = NumStr :-
      ).

  abs_integer_to_hex_uc(Num) = NumStr :-
-    ( if Num > integer(0) then
-        Integer16 = integer(16),
+    ( if Num > integer.zero then
+        Integer16 = integer.sixteen,
          FrontDigitsStr = abs_int_to_hex_uc(det_to_int(Num // Integer16)),
          LastDigitStr = get_hex_digit_uc(det_to_int(Num rem Integer16)),
          NumStr = append(FrontDigitsStr, LastDigitStr)


More information about the reviews mailing list