장현덕 님이 쓰신 글 :
: 스트링을 파싱해서 트리구조를 만드는 걸 테스트 하는 겁니다.
:
: dll 에서 스트링을 받아 프로그램에서 파싱을 하는 구조로 되어있구스트링을 파싱해서 트리구조를 만드는 걸 테스트 하는 겁니다.
:
: dll 에서 스트링을 받아 프로그램에서 파싱을 하는 구조로 되어있구요.
:
: 스트링이 데이터( 그래픽 좌표, 정보 ) 여서 크거든요.
:
: 그래서 속도를 높이고 싶어요. ( 코드개선, 획기적 방법등 ) 을 문의 드립니다.
:
:
:
:
: procedure TForm1.Button1Click(Sender: TObject);
: var
: s: string;
: paircount: integer;
: totalpair: integer;
: token: String;
: c:Pchar;
: IsPairInc: boolean;
:
: procedure AddOne;
: begin
: token := token + c^;
: Inc(c);
: end;
:
: procedure Skip;
: begin
: inc(c);
: end;
: begin
: s:= '[';
: s:= s + '{root1,{child1, child2, {child3,{c1,c2}}, child4}},';
: s:= s + '{root2,{child1, child2, {child3,{a1,a2,a3}}}},';
: s:= s + '{root3,{{child1,{m1,m2,m3}}, child2, {child3,{a1,a2,a3}}}';
: s:= s + ']';
:
: c:= Pchar(s);
: paircount := 0;
: token:='';
: Memo1.Lines.Clear;
: IsPairInc := true;
:
: while c^ <> ']' do
: begin
: case c^ of
: '{': begin
: token := '';
: inc(paircount);
: inc(c);
: IsPairInc := True;
: end;
: '0'..'9', 'a'..'z', 'A'..'Z':
: begin
: AddOne;
: end;
: ',': begin
: if (IsPairInc and (token <> '')) then
: if (c+1)^ = '{' then
: begin
: Memo1.Lines.Add(inttostr((paircount-1) div 2) + ':' + token); // add
: end else
: Memo1.Lines.Add(inttostr(paircount div 2) + ':' + token); // add
: token:='';
: inc(c);
: end;
: '}': begin
: if paircount <> 0 then IsPairInc := false;
: if ((c+1)^ = '}') and ((c+2)^ <> '}') and (token <> '') then
: Memo1.Lines.Add(inttostr(paircount div 2) + ':' + token); // add
:
: if ((c+1)^ = '}') and ((c+2)^ = '}') and (token <> '') then
: begin
: Memo1.Lines.Add(inttostr(paircount div 2) + ':' + token); // add
: token := '';
: IsPairInc:= True;
: end;
:
: dec(paircount);
: if paircount = 0 then
: begin
: IsPairInc:=true;
: token := '';
: Memo1.Lines.Add('');
: end;
:
: inc(c);
: end;
: ' ', '[':
: begin
: Skip;
: end;
: end;
: end;
:
: end;
:
:
:
: 결과
:
: 0:root1
: 1:child1
: 1:child2
: 1:child3
: 2:c1
: 2:c2
: 1:child4
: --------------------------------
: 0:root2
: 1:child1
: 1:child2
: 1:child3
: 2:a1
: 2:a2
: 2:a3
: --------------------------------
: 0:root3
: 1:child1
: 2:m1
: 2:m2
: 2:m3
: 1:child3
: 2:a1
: 2:a2
: 2:a3
:
:
다시보니 이전 답변이 틀려서 다시 내용을 수정했습니다. (_ _);
짝수째 괄호에서만 아이템의 레벨을 신경써주면 될 것 같습니다.
const
datastr='['+#13#10+
'{root1,{child1, child2, {child3,{c1,c2}}, child4}},'+#13#10+
'{root2,{child1, child2, {child3,{a1,a2,a3}}}},'+#13#10+
'{root3,{{child1,{m1,m2,m3}}, child2, {child3,{a1,a2,a3}}}'+#13#10+
']';
{ TForm1 }
procedure TForm1.Button1Click(Sender: TObject);
var
token:string;
level,Treelvl:Integer;
c:char;
dataptr:pchar;
begin
dataptr:=pchar(datastr);
level:=0;
Treelvl:=0;
token:='';
while dataptr^<>#0 do begin
c:=dataptr^;
case c of
'[': ;
']': break;
'{': begin
Inc(level);
if (level and 1)=0 then
Inc(Treelvl);
end;
',','}':
begin
if token<>'' then begin
Memo1.Lines.Add(format('%d : %s',[Treelvl,token]));
token:='';
end;
if c='}' then begin
Dec(level);
if (level and 1)=0 then
Dec(Treelvl);
end;
end;
#1..#32: ;
else
token:=token+c;
end;
Inc(dataptr);
end;
end;