2
0

Template1.dpr 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. program Template1;
  2. {$APPTYPE CONSOLE}
  3. {$R *.res}
  4. uses
  5. System.SysUtils,
  6. System.Generics.Collections,
  7. Quick.Commons,
  8. Quick.Console,
  9. Quick.Chrono,
  10. Quick.Template;
  11. var
  12. crono : TChronometer;
  13. dict : TDictionary<string,string>;
  14. template : TStringTemplate;
  15. mytemplate : string;
  16. str : string;
  17. str2 : string;
  18. //i : Integer;
  19. begin
  20. try
  21. mytemplate := 'User {{User}} {{SurName}} are {{Age}} years old.' + sLineBreak +
  22. '{{SurName}} is {{Height}} tall and has {{Money}} euro(s) bank account and access level {{Level}}.' + sLineBreak +
  23. '{{User}} is from {{State}} ({{Country}}).';
  24. // for i := 1 to 15 do
  25. // begin
  26. // mytemplate := mytemplate + mytemplate + sLineBreak;
  27. // end;
  28. cout('String Lenght = %s',[NumberToStr(mytemplate.Length)],etInfo);
  29. crono := TChronometer.Create(False);
  30. dict := TDictionary<string,string>.Create;
  31. dict.Add('User','John');
  32. dict.Add('Age','20');
  33. dict.Add('SurName','Peterson');
  34. dict.Add('Money','100.000');
  35. dict.Add('Country','Spain');
  36. dict.Add('State','Barcelona');
  37. dict.Add('Level','1');
  38. dict.Add('Height','175');
  39. //Test StrinTemplate
  40. template := TStringTemplate.Create('{{','}}',dict);
  41. cout('Test with StringTemplate',ccGreen);
  42. crono.Start;
  43. str := template.Replace(mytemplate);
  44. crono.Stop;
  45. cout('Time: %s',[crono.ElapsedTime(False)],etInfo);
  46. //cout(str,ccLightGreen);
  47. //Test standard StringReplace
  48. cout('Test with standard StringReplace',ccGreen);
  49. crono.Start;
  50. str2 := StringReplace(mytemplate,'{{User}}',dict['User'],[rfReplaceAll]);
  51. str2 := StringReplace(str2,'{{SurName}}',dict['SurName'],[rfReplaceAll]);
  52. str2 := StringReplace(str2,'{{Age}}',dict['Age'],[rfReplaceAll]);
  53. str2 := StringReplace(str2,'{{Money}}',dict['Money'],[rfReplaceAll]);
  54. str2 := StringReplace(str2,'{{Country}}',dict['Country'],[rfReplaceAll]);
  55. str2 := StringReplace(str2,'{{State}}',dict['State'],[rfReplaceAll]);
  56. str2 := StringReplace(str2,'{{Level}}',dict['Level'],[rfReplaceAll]);
  57. str2 := StringReplace(str2,'{{Height}}',dict['Height'],[rfReplaceAll]);
  58. crono.Stop;
  59. cout('Time: %s',[crono.ElapsedTime(False)],etInfo);
  60. cout(str,ccLightGreen);
  61. cout('Press <ENTER> to Exit',ccYellow);
  62. ConsoleWaitForEnterKey;
  63. dict.Free;
  64. except
  65. on E: Exception do
  66. Writeln(E.ClassName, ': ', E.Message);
  67. end;
  68. end.