Last active
June 2, 2021 13:05
-
-
Save rrooij/7814b4e88e20aad02b08672d79dac062 to your computer and use it in GitHub Desktop.
SWI Prolog wrap string in newlines
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
% wrap_string(+String, +PadLength, -Output) is det. | |
wrap_string(String, PadLength, Output) :- | |
string_length(String, StringLength), | |
wrap_string_(String, 0, PadLength, StringLength, "", Output). | |
wrap_string_(String, PadStart, PadWidth, StrLength, Acc, Output) :- | |
NewPadStart is PadStart + PadWidth, | |
( NewPadStart < StrLength | |
-> sub_string(String, PadStart, PadWidth, _, SubString), | |
format(string(NewAcc), "~s~s~n", [Acc, SubString]), | |
wrap_string_(String, NewPadStart, PadWidth, StrLength, NewAcc, Output) | |
; sub_string(String, PadStart, _, 0, SubString), | |
string_concat(Acc, SubString, Output) | |
). | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment