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