wasm.regexp.shared.pas 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. {
  2. This file is part of the Free Component Library
  3. Webassembly RegExp API - Shared constants & functions
  4. Copyright (c) 2024 by Michael Van Canneyt [email protected]
  5. See the file COPYING.FPC, included in this distribution,
  6. for details about the copyright.
  7. This program is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  10. **********************************************************************}
  11. unit wasm.regexp.shared;
  12. {$mode ObjFPC}{$H+}
  13. interface
  14. Type
  15. TWasmRegexpID = Longint;
  16. TWasmRegexpResult = Longint;
  17. {$IFNDEF PAS2JS}
  18. TWasmPointer = Pointer;
  19. PWasmRegexpID = ^TWasmRegexpID;
  20. {$ELSE}
  21. TWasmPointer = Longint;
  22. PWasmRegexpID = TWasmPointer;
  23. {$ENDIF}
  24. Const
  25. WASMRE_RESULT_SUCCESS = 0;
  26. WASMRE_RESULT_ERROR = -1;
  27. WASMRE_RESULT_INVALIDID = -2;
  28. WASMRE_RESULT_NO_MEM = -3;
  29. WASMRE_RESULT_NO_REGEXP = -4;
  30. WASMRE_RESULT_INVALIDIDX = -5;
  31. WASMRE_RESULT_NOINDEXES = -6;
  32. WASMRE_FLAG_DOTALL = 1;
  33. WASMRE_FLAG_GLOBAL = 2;
  34. WASMRE_FLAG_INDICES = 4;
  35. WASMRE_FLAG_IGNORECASE = 8;
  36. WASMRE_FLAG_MULTILINE = 16;
  37. WASMRE_FLAG_STICKY = 32;
  38. WASMRE_FLAG_UNICODE = 64;
  39. WASMRE_FLAG_UNICODESETS = 128;
  40. // Aliases that correspond to the letters used when creating a regexp
  41. WASMRE_FLAG_S = WASMRE_FLAG_DOTALL;
  42. WASMRE_FLAG_G = WASMRE_FLAG_GLOBAL;
  43. WASMRE_FLAG_D = WASMRE_FLAG_INDICES;
  44. WASMRE_FLAG_I = WASMRE_FLAG_IGNORECASE;
  45. WASMRE_FLAG_M = WASMRE_FLAG_MULTILINE;
  46. WASMRE_FLAG_Y = WASMRE_FLAG_STICKY;
  47. WASMRE_FLAG_U = WASMRE_FLAG_UNICODE;
  48. WASMRE_FLAG_V = WASMRE_FLAG_UNICODESETS;
  49. regexpExportName = 'regexp';
  50. regexpFN_Allocate = 'allocate';
  51. regexpFN_DeAllocate = 'deallocate';
  52. regexpFN_Exec = 'exec';
  53. regexpFN_Test = 'test';
  54. regexpFN_GetFlags = 'get_flags';
  55. regexpFN_GetExpression = 'get_expression';
  56. regexpFN_GetLastIndex = 'get_last_index';
  57. regexpFN_SetLastIndex = 'set_last_index';
  58. regexpFN_GetResultMatch = 'get_result_match';
  59. regexpFN_GetGroupCount = 'get_group_count';
  60. regexpFN_GetGroupName = 'get_group_name';
  61. regexpFN_GetNamedGroup = 'get_named_group';
  62. regexpFN_GetIndexes = 'get_indexes';
  63. regexpFN_GetNamedGroupIndexes = 'get_named_group_indexes';
  64. Function StringToRegexpFlags(const S : String; IgnoreUnknown : Boolean = True) : Longint;
  65. Function RegexpFlagsToString(S : Longint) : String;
  66. implementation
  67. uses
  68. {$IFDEF FPC_DOTTEDUNITS}
  69. System.SysUtils;
  70. {$ELSE}
  71. SysUtils;
  72. {$ENDIF}
  73. Function StringToRegexpFlags(const S : String; IgnoreUnknown : Boolean = True) : Longint;
  74. var
  75. C : Char;
  76. Flag : Longint;
  77. begin
  78. Result:=0;
  79. for C in S do
  80. begin
  81. case C of
  82. 's': Flag:=WASMRE_FLAG_S;
  83. 'g': Flag:=WASMRE_FLAG_G;
  84. 'd': Flag:=WASMRE_FLAG_D;
  85. 'i': Flag:=WASMRE_FLAG_I;
  86. 'm': Flag:=WASMRE_FLAG_M;
  87. 'y': Flag:=WASMRE_FLAG_Y;
  88. 'u': Flag:=WASMRE_FLAG_U;
  89. 'v': Flag:=WASMRE_FLAG_V;
  90. else
  91. if not IgnoreUnknown then
  92. Raise EConvertError.CreateFmt('Unknown regexp flag: %s',[C]);
  93. Flag:=0;
  94. end;
  95. Result:=Result or Flag;
  96. end;
  97. end;
  98. Function RegexpFlagsToString(S : Longint) : String;
  99. var
  100. C,I : Longint;
  101. Flag : Char;
  102. begin
  103. Result:='';
  104. for I:=0 to 7 do
  105. begin
  106. C:=S and (1 shl i);
  107. case C of
  108. WASMRE_FLAG_S : Flag:='s';
  109. WASMRE_FLAG_G : Flag:='g';
  110. WASMRE_FLAG_D : Flag:='d';
  111. WASMRE_FLAG_I : Flag:='i';
  112. WASMRE_FLAG_M : Flag:='m';
  113. WASMRE_FLAG_Y : Flag:='y';
  114. WASMRE_FLAG_U : Flag:='u';
  115. WASMRE_FLAG_V : Flag:='v';
  116. else
  117. Flag:='0';
  118. end;
  119. if Flag<>'0' then
  120. Result:=Result + Flag;
  121. end;
  122. end;
  123. end.