fork download
  1. program ideone;
  2.  
  3. {$mode objfpc}
  4.  
  5. uses
  6. SysUtils;
  7.  
  8. type
  9. TSection = record
  10. First: PAnsiChar;
  11. Last: PAnsiChar;
  12. end;
  13.  
  14. type
  15. TSectionsArr = array of TSection;
  16.  
  17. var
  18. saLabel: TSectionsArr;
  19.  
  20.  
  21. procedure ShowSectionsContent();
  22. var
  23. intToken: Integer;
  24. strToken: AnsiString;
  25. begin
  26. for intToken := 0 to High(saLabel) do
  27. with saLabel[intToken] do
  28. begin
  29. SetLength(strToken, Last - First + 1);
  30. Move(First^, strToken[1], Last - First + 1);
  31. WriteLn('"', strToken, '"');
  32. end;
  33. end;
  34.  
  35. procedure AddSection(AFirst, ALast: PAnsiChar);
  36. begin
  37. while (AFirst <= ALast) and (AFirst^ = #32) do
  38. Inc(AFirst);
  39.  
  40. while (ALast >= AFirst) and (ALast^ = #32) do
  41. Dec(ALast);
  42.  
  43. if AFirst <= ALast then
  44. begin
  45. SetLength(saLabel, Length(saLabel) + 1);
  46.  
  47. with saLabel[High(saLabel)] do
  48. begin
  49. First := AFirst;
  50. Last := ALast;
  51. end;
  52. end;
  53. end;
  54.  
  55. procedure ExtractSectionsText(ACode: AnsiString);
  56. const
  57. MARKER_HEADER = AnsiChar('''');
  58. MARKER_PARAGRAPH = AnsiChar('"');
  59. var
  60. pchrBegin, pchrEnd, pchrLast: PAnsiChar;
  61. begin
  62. ACode[3] := 'Q';
  63. WriteLn('w ExtractSectionsText ',IntToHex(Integer(@ACode[1]),0));
  64.  
  65.  
  66.  
  67. pchrBegin := @ACode[1];
  68. pchrLast := @ACode[Length(ACode)];
  69.  
  70. while pchrBegin < pchrLast do
  71. begin
  72. while (pchrBegin <= pchrLast) and not (pchrBegin^ in [MARKER_HEADER, MARKER_PARAGRAPH]) do
  73. Inc(pchrBegin);
  74.  
  75. if pchrBegin <= pchrLast then
  76. begin
  77. Inc(pchrBegin);
  78. pchrEnd := pchrBegin;
  79.  
  80. while (pchrEnd <= pchrLast) and not (pchrEnd^ in [MARKER_HEADER, MARKER_PARAGRAPH]) do
  81. Inc(pchrEnd);
  82.  
  83. if pchrEnd <= pchrLast then
  84. begin
  85. AddSection(pchrBegin, pchrEnd - 1);
  86. pchrBegin := pchrEnd + 1;
  87. end;
  88. end;
  89. end;
  90. end;
  91.  
  92. var
  93. Sample2: AnsiString;
  94. begin
  95. Sample2:='x''first header''x"first paragraph"x"second paragraph"x''second header''x"third paragraph"x';
  96. WriteLn('przed ',IntToHex(Integer(@Sample2[1]),0));
  97. SetLength(saLabel, 0);
  98.  
  99. ExtractSectionsText(Sample2);
  100. ShowSectionsContent();
  101. WriteLn('po ',IntToHex(Integer(@Sample2[1]),0));
  102. end.
  103.  
Success #stdin #stdout 0s 388KB
stdin
Standard input is empty
stdout
przed 8081A18
w ExtractSectionsText B778B028
"Qirst header"
"first paragraph"
"second paragraph"
"second header"
"third paragraph"
po 8081A18