WideString array problem
Posted: 21 Jul 2014 11:38
Hi,
Trying to convert a JavaScript script I did to PascalScript and having a problem with defining an array of type WideString. I used to Pascal back in the Turbo Pascal and early Delphi days, but I keep getting the following error: 'BEGIN' expected.
Could someone review the following code and work out what I am doing wrong.
Thanks.
Trying to convert a JavaScript script I did to PascalScript and having a problem with defining an array of type WideString. I used to Pascal back in the Turbo Pascal and early Delphi days, but I keep getting the following error: 'BEGIN' expected.
Could someone review the following code and work out what I am doing wrong.
Thanks.
Code: Select all
procedure formatAssignments;
var
lines, line, s_left, s_right: WideString;
i, n_pos, n_maxlen, n_len: Integer;
//a_lines: Array of WideString;
a_lines: TMyStringArray;
begin
lines := Document.SelText;
a_lines := SplitString(lines, '#13#10');
ShowMessage(Length(a_lines));
n_pos := 0;
n_maxlen := 0;
n_len := 0;
// 1. determine maximum position of = character
// determine max line length - to be used for re-formatting of the line
for i := 0 to Length(a_lines) - 1 do
begin
n_pos := ScriptUtils.WPos('=', a_lines[i]);
line := Copy(a_lines[i], 0, n_pos - 1);
if Length(line) > n_maxlen then
n_maxlen := Length(line);
end;
if n_maxlen = 0 then
exit;
// 2. reformat left side of =
for i := 0 to Length(a_lines) - 1 do
begin
//skip empty lines
if Length(ScriptUtils.RegExReplaceAll(a_lines[i], '\s/g', '')) = 0 then
continue;
n_pos := ScriptUtils.WPos('=', a_lines[i]);
if n_pos = 0 then
continue;
s_left := ScriptUtils.RegExReplaceAll(Copy(a_lines[i], 0, n_pos), '\s+$/', '');
s_right := ScriptUtils.RegExReplaceAll(Copy(a_lines[i], n_pos + 1, Length(a_lines[i])), '\s+/', '');
a_lines[i] := formatLine(s_left, s_right, n_maxlen);
end;
lines := Join(a_lines, '#13#10');
Document.SelText := lines;
end;
function Join(thearray: array of WideString; thedelimiter: WideString) : WideString;
var
thestring: WideString;
i: Integer;
begin
for i := 0 to Length(thearray) - 1 do
begin
thestring := thestring + thedelimiter + thearray[i];
end;
Result := thestring;
end;
function FormatLine(s_left: WideString; s_right: WideString; n_maxlen: integer) : WideString;
var
spaces: WideString;
begin
spaces := ' ';
Result := s_left + Copy(spaces, 0, n_maxlen - Length(s_left)) + ' = ' + s_right;
end;
function SplitString(s_string: WideString; s_delimiter: String) : TMyStringArray;
var
list: TMyStringArray;
item: WideString;
i, cnt, pos, last: Integer;
begin
cnt := 0;
list := [''];
ShowMessage('string='+s_string+',delimiter='+s_delimiter);
//ShowMessage(Length(s_string));
for i := 0 to Length(s_string) - 1 do
begin
//skip empty lines
pos := ScriptUtils.PosFrom(s_delimiter, s_string, i);
if pos > 0 then
begin
item := Copy(s_string, last, pos);
ShowMessage('item='+item);
list[cnt] := item;
last := pos;
end;
end;
Result := list;
end;
type
TMyStringArray = array of WideString;
begin
formatAssignments;
end.