uparse.pas 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. // SPDX-License-Identifier: GPL-3.0-only
  2. unit UParse;
  3. {$mode objfpc}
  4. interface
  5. uses
  6. Classes, SysUtils;
  7. type
  8. arrayOfString = array of string;
  9. function RectToStr(rect: TRect): string;
  10. function StrToRect(str: string): TRect;
  11. function SimpleParseFuncParam(str: string): arrayOfString;
  12. implementation
  13. function SimpleParseFuncParam(str: string): arrayOfString;
  14. var idxOpen,start,cur,bracketDepth: integer;
  15. begin
  16. result := nil;
  17. idxOpen := pos('(',str);
  18. if idxOpen = 0 then
  19. start := 1
  20. else
  21. start := idxOpen+1;
  22. cur := start;
  23. bracketDepth := 0;
  24. while cur <= length(str) do
  25. begin
  26. if str[cur] = '(' then inc(bracketDepth) else
  27. if (str[cur] = ')') and (bracketDepth > 0) then dec(bracketDepth) else
  28. if str[cur] in[',',')'] then
  29. begin
  30. setlength(result,length(result)+1);
  31. result[high(result)] := copy(str,start,cur-start);
  32. start := cur+1;
  33. if str[cur]=')' then break;
  34. end;
  35. inc(cur);
  36. end;
  37. if start <= length(str) then
  38. begin
  39. setlength(result,length(result)+1);
  40. result[high(result)] := copy(str,start,length(str)-start+1);
  41. end;
  42. end;
  43. function RectToStr(rect: TRect): string;
  44. begin
  45. result := 'Rect('+intToStr(rect.left)+','+intToStr(rect.Top)+','+intToStr(rect.Right)+','+intToStr(rect.Bottom)+')';
  46. end;
  47. {$hints off}
  48. {$notes off}
  49. function StrToRect(str: string): TRect;
  50. var param: arrayOfString;
  51. errPos: integer;
  52. begin
  53. if lowercase(copy(str,1,5))='rect(' then
  54. begin
  55. param := SimpleParseFuncParam(str);
  56. if length(param)=4 then
  57. begin
  58. val(param[0],result.left,errPos);
  59. val(param[1],result.top,errPos);
  60. val(param[2],result.right,errPos);
  61. val(param[3],result.bottom,errPos);
  62. exit;
  63. end;
  64. end;
  65. fillchar(result,sizeof(result),0);
  66. end;
  67. {$notes on}
  68. {$hints on}
  69. end.