PDA

View Full Version : fstring()



HKbotz
01-19-2015, 11:48 AM
Was trying to make a pretty proggy and needed something to format either side of a string so the ends would be even regardless of the info presented. I have no idea how to do this in lape or if you even can, so I made a function to format strings so they're either aligned to the left, right, or center. It fills the sides of the string with whatever character you choose depending on how you want it aligned.

If that didn't make sense it's cause I'm terrible at explaining things :p, so I will let the code do the explaining.

fstring():
type
alignType = (left, right, center);
(*
fstring()
by: HKbotz

Formats the string so it is either aligned to the left, right, or center.
Fills sides based on align with whatever character you choose for specified
length.

-str: the string to format
-len: the length of the formatted string
-align: the way you wish to align the text compared to the fill char
-fill: the character you wish to fill the sides with

usage:
fstring('Some text', 30);
fstring('Some text', 30, center, '-');

example:
writeln('// ' + fstring('SomeStat', 20) + '\\');
writeln('// ' + fstring('SomeOtherStat', 20) + '\\');
Output:
// SomeStat \\
// SomeOtherStat \\
*)
function fstring(str: string; len: integer; align: alignType = left; fill: char = ' '): string;
var
i, c, n, strLen: integer;
begin
setLength(result, len);
strLen := length(str);
if (align = left) then
begin
for (i := 1) to (len) do
begin
if (i <= strLen) then
result[i] := str[i]
else
result[i] := fill;
end;
end else if (align = right) then
begin
n := len - strLen;
c := 1;
for (i := 1) to (len) do
begin
if (i <= n) then
result[i] := fill
else
begin
result[i] := str[c];
inc(c);
end;
end;
end else if (align = center) then
begin
n := round((len - strLen) / 2);
c := 1;
for (i := 1) to (len) do
begin
if (i <= n) then
result[i] := fill
else if (c <= strLen) then
begin
result[i] := str[c];
inc(c);
end else
result[i] := fill;
end;
end else
begin
writeln('Passed incorrect align param. Use right, left, or center.');
writeln('Returning your original string...');
setLength(result, strLen);
result := str;
end;
end;


Example:
procedure exampleProggy();
var
logType: string = 'Willow';
begin
writeln('//' + fstring('', 30, , '-') + '\\');
writeln('//' + fstring('Fake log chopping script', 30, center) + '\\');
writeln('//' + fstring('', 30) + '\\');
writeln('//' + fstring(' Log type: ' + logType, 30) + '\\');
writeln('//' + fstring(' Logs chopped: ' + intToStr(230), 30) + '\\');
writeln('//' + fstring(' Runs done: ' + intToStr(8), 30) + '\\');
writeln('//' + fstring(' Time running: ' + timeRunning(TIME_SHORT), 30) + '\\');
writeln('//' + fstring('', 30, , '-') + '\\');
end;

begin
exampleProggy();
end.
Output from exampleProggy():

//------------------------------\\
// Fake log chopping script \\
// \\
// Log type: Willow \\
// Logs chopped: 230 \\
// Runs done: 8 \\
// Time running: 02s \\
//------------------------------\\

If the string you supply is longer than len, it will just return the first len charaters.
So if you have a string that is 'This is a really long string', but only supply a len of 10, it will return 'This is a '.

Hope that makes sense and that someone can get some use out of this(so far I'm just using for proggies).
If there's a better way to do the formatting thing please let me know :).

Daniel
01-19-2015, 12:36 PM
Nice work! Also check out the Pad* (http://docs.villavu.com/simba/scriptref/string.html#padl) functions that Simba provides. :)

HKbotz
01-19-2015, 01:13 PM
Nice work! Also check out the Pad* (http://docs.villavu.com/simba/scriptref/string.html#padl) functions that Simba provides. :)
I knew there had to be something like that, just didn't know what lol. I still like to be able to pad with whatever character I want though, so I still prefer mine ;)

akarigar
01-19-2015, 08:59 PM
I knew there had to be something like that, just didn't know what lol. I still like to be able to pad with whatever character I want though, so I still prefer mine ;)

You can use any character with the PadL / PadR functions.


begin
WriteLn(PadL('Test', 10, '~'));
end.



~~~~~~Test
Successfully executed.


Regardless, good work. Sometimes reinventing the wheel can teach you a lot.

HKbotz
01-19-2015, 10:06 PM
You can use any character with the PadL / PadR functions.


begin
WriteLn(PadL('Test', 10, '~'));
end.



~~~~~~Test
Successfully executed.


Regardless, good work. Sometimes reinventing the wheel can teach you a lot.

Well damn. Good to know for future reference. I guess my last attempt to justify mine is I can center text lol. I didn't notice a pad function that does that, however I won't be surprised if there is one lol.