Strings.IntToStringForm: convert a number to Oberon format
Posted: Mon Sep 13, 2021 5:38 pm
I propose to increase the functionality of the procedure Strings.IntToStringForm.
There is an input parameter MinWidth, which is responsible for the minimum number of output characters of a converted number. Any the number can take up one or more characters. But still, ASSERT inside this procedure also allows the value of MinWidth = 0
Here, when MinWidth = 0, nothing terrible happens, but it will be the same as 1.
I was recently forced to write such code:
This procedure outputs a hexadecimal value in the Oberon format, i.e. if a hexadecimal literal starts with a letter, it is preceded by an insignificant zero.
And this behavior can be demanded quite often, so I suggest that with MinWidth = 0, implement exactly this behavior as encoded in my procedure: a hexadecimal number with an insignificant zero is output if this number starts with a letter. What do you think, colleagues?
There is an input parameter MinWidth, which is responsible for the minimum number of output characters of a converted number. Any the number can take up one or more characters. But still, ASSERT inside this procedure also allows the value of MinWidth = 0
Code: Select all
ASSERT(minWidth >= 0, 22);
I was recently forced to write such code:
Code: Select all
PROCEDURE WriteHex(i: INTEGER);
VAR minWidth: INTEGER;
BEGIN
IF i > 9FFFH THEN minWidth := 5
ELSIF i > 9FFH THEN minWidth := 4
ELSIF i > 9FH THEN minWidth := 3
ELSIF i > 9H THEN minWidth := 2
ELSE minWidth := 1
END;
W.WriteIntForm(i, TextMappers.hexadecimal, minWidth, "0", FALSE)
END WriteHex;
And this behavior can be demanded quite often, so I suggest that with MinWidth = 0, implement exactly this behavior as encoded in my procedure: a hexadecimal number with an insignificant zero is output if this number starts with a letter. What do you think, colleagues?