Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 30 additions & 0 deletions modules/IOUtils.tla
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@

LOCAL INSTANCE TLC
LOCAL INSTANCE Integers
LOCAL INSTANCE Sequences
LOCAL INSTANCE SequencesExt
(*************************************************************************)
(* Imports the definitions from the modules, but doesn't export them. *)
(*************************************************************************)
Expand Down Expand Up @@ -126,4 +128,32 @@ IOEnv ==
atoi(str) ==
CHOOSE i \in Int : ToString(i) = str

(***************************************************************************)
(* Pads a natural number with leading zeros to achieve a specified width. *)
(* *)
(* This function converts a natural number to a string and pads it with *)
(* leading zeros if necessary to reach the specified width. If the *)
(* number's string representation is already equal to or longer than the *)
(* specified width, no padding is added. *)
(* *)
(* Parameters: *)
(* - n: The natural number to be padded (must be >= 0) *)
(* - width: The desired total width of the resulting string *)
(* *)
(* Examples: *)
(* zeroPadN(42, 5) = "00042" *)
(* zeroPadN(123, 3) = "123" *)
(* zeroPadN(7, 1) = "7" *)
(* zeroPadN(0, 4) = "0000" *)
(* *)
(* Note: Negative numbers are not supported and may produce unexpected *)
(* results. *)
(* *)
(* Returns a sequence of characters representing the zero-padded number. *)
(***************************************************************************)
zeroPadN(n, width) ==
LET s == ToString(n)
padLen == width - Len(s)
zeros == [i \in 1..padLen |-> "0"]
IN FoldLeft(LAMBDA acc, z: acc \o z, "", zeros) \o s
============================================================================
8 changes: 5 additions & 3 deletions modules/SVG.tla
Original file line number Diff line number Diff line change
Expand Up @@ -230,14 +230,16 @@ PointOnLine(from, to, segment) ==
(* SVGSerialize(SVGDoc(myElements, 0, 0, 800, 600, <<>>), *)
(* "svg_frame_", TLCGet("level")) *)
(* *)
(* This creates files like: svg_frame_1.svg, *)
(* svg_frame_2.svg, etc. *)
(* This creates files like: svg_frame_01.svg, *)
(* svg_frame_02.svg, etc. *)
(**************************************************************************)
SVGSerialize(svg, frameNamePrefix, frameNumber) ==
LET IO == INSTANCE IOUtils IN
IO!Serialize(
SVGElemToString(svg),
frameNamePrefix \o ToString(frameNumber) \o ".svg",
\* Construct the filename using the prefix and a zero-padded frame
\* number so that files sort correctly lexicographically.
frameNamePrefix \o IO!zeroPadN(frameNumber, 2) \o ".svg",
[format |-> "TXT", charset |-> "UTF-8",
openOptions |-> <<"WRITE", "CREATE", "TRUNCATE_EXISTING">>])

Expand Down
9 changes: 9 additions & 0 deletions tests/IOUtilsTests.tla
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,15 @@ ASSUME AssertError(
"The argument of atoi should be a string, but instead it is:\n\"foo\"",
atoi("foo"))

\* Test zeroPadN function with various inputs
ASSUME(AssertEq(zeroPadN(42, 5), "00042"))
ASSUME(AssertEq(zeroPadN(123, 3), "123"))
ASSUME(AssertEq(zeroPadN(7, 1), "7"))
ASSUME(AssertEq(zeroPadN(0, 4), "0000"))
ASSUME(AssertEq(zeroPadN(999, 2), "999"))
ASSUME(AssertEq(zeroPadN(1000, 6), "001000"))
ASSUME(AssertEq(zeroPadN(0, 1), "0"))

---------------------------------------------------------------------------------------------------------------------------

ASSUME PrintT("IOUtilsTests!C")
Expand Down
Loading