File gpc-regex.patch of Package gpc
--- p/units/regex.pas 2007-09-04 07:22:55.000000000 +0100
+++ p/units/regex.pas 2011-07-29 15:09:26.492205329 +0100
@@ -221,15 +221,15 @@
implementation
-{$L rx, regexc.c}
+{$L regexc.c}
-procedure CNewRegEx (var RegEx: RegExType; Expression: CString; ExpressionLength: CInteger; ExtendedRegEx, CaseInsensitive, NewLines: Boolean); external name '_p_CNewRegEx';
+procedure CNewRegEx (var RegEx: RegExType; Expression: CString; ExtendedRegEx, CaseInsensitive, NewLines: Boolean); external name '_p_CNewRegEx';
function CMatchRegExFrom (var RegEx: RegExType; aString: CString; StrLength: CInteger; NotBeginningOfLine, NotEndOfLine: Boolean; From: CInteger): Boolean; external name '_p_CMatchRegExFrom';
procedure CGetMatchRegEx (var RegEx: RegExType; n: CInteger; var MatchPosition, MatchLength: CInteger); external name '_p_CGetMatchRegEx';
procedure NewRegEx (var RegEx: RegExType; const Expression: String; ExtendedRegEx, CaseInsensitive, NewLines: Boolean);
begin
- CNewRegEx (RegEx, Expression, Length (Expression), ExtendedRegEx, CaseInsensitive, NewLines);
+ CNewRegEx (RegEx, Expression, ExtendedRegEx, CaseInsensitive, NewLines);
if RegEx.ErrorInternal = nil then
RegEx.Error := nil
else
--- p/units/regexc.c 2007-09-04 07:22:55.000000000 +0100
+++ p/units/regexc.c 2011-08-01 16:39:59.529151945 +0100
@@ -29,7 +29,7 @@
General Public License. */
#include <stdlib.h>
-#include <rxposix.h>
+#include <regex.h>
#define GLOBAL(decl) decl; decl
@@ -44,7 +44,7 @@
void *Error;
} RegExType;
-GLOBAL (void _p_CNewRegEx (RegExType *r, const char *Expression, int ExpressionLength,
+GLOBAL (void _p_CNewRegEx (RegExType *r, const char *Expression,
Boolean ExtendedRegEx, Boolean CaseInsensitive, Boolean NewLines))
{
int result;
@@ -55,7 +55,7 @@
r->SubExpressions = 0;
r->RegMatch = 0;
r->RegEx = (regex_t *) malloc (sizeof (regex_t));
- result = regncomp (r->RegEx, Expression, ExpressionLength,
+ result = regcomp (r->RegEx, Expression,
(ExtendedRegEx ? REG_EXTENDED : 0) |
(CaseInsensitive ? REG_ICASE : 0) |
(NewLines ? REG_NEWLINE : 0));
@@ -77,7 +77,7 @@
}
else
{
- r->SubExpressions = r->RegEx->re_nsub - 1;
+ r->SubExpressions = r->RegEx->re_nsub;
r->RegMatch = (regmatch_t *) malloc ((r->SubExpressions + 1) * sizeof (regmatch_t));
}
}
@@ -113,9 +113,11 @@
{
int i = r->Length = StrLength - From + 1;
r->From = From;
- return !regnexec (r->RegEx, (i >= 0) ? aString + From - 1 : aString, (i >= 0) ? i : 0, r->SubExpressions + 1,
- &r->RegMatch, (NotBeginningOfLine ? REG_NOTBOL : 0) |
- (NotEndOfLine ? REG_NOTEOL : 0));
+ return !regexec(r->RegEx, (i >= 0) ? aString + From - 1 : aString,
+ r->SubExpressions+1,
+ r->RegMatch,
+ (NotBeginningOfLine ? REG_NOTBOL : 0) | (NotEndOfLine ? REG_NOTEOL : 0)
+ );
}
GLOBAL (void _p_CGetMatchRegEx (RegExType *r, int n, int *MatchPosition, int *MatchLength))
--- p/demos/regexdemo.pas 2011-07-30 20:02:15.000000000 +0100
+++ p/demos/regexdemo.pas 2011-07-26 18:18:07.466354736 +0100
@@ -64,25 +64,29 @@
if a = '' then Break;
Write ('Search from position: ');
ReadLn (o);
- WriteLn (a);
if not MatchRegExFrom (r, a, b, e, o) then
WriteLn ('No match.')
- else
- for i := 0 to r.SubExpressions do
- begin
- GetMatchRegEx (r, i, p, l);
- if p = 0 then
- WriteLn ('-')
- else
- begin
- Write ('' : p - 1);
- if l = 0 then
- Write ('0')
- else
- for j := 1 to l do Write ('^');
- WriteLn
+ else begin
+ writeln( ' match, with ', r.Subexpressions, ' subexpressions found' );
+ WriteLn (a);
+ for i := 0 to r.SubExpressions do begin
+ GetMatchRegEx (r, i, p, l);
+ { The first element (index 0) records the part of the string that matched the entire regular expression }
+ { Each other element records the beginning and end of the part that matched a single parenthetical subexpression. }
+ if p = 0 then
+ WriteLn ('--- no match for subexpression ', i)
+ else begin
+ Write ('' : p-1);
+ if l = 0 then
+ WriteLn ('zero length subexpression matches')
+ else begin
+ for j := 1 to l do Write ('^');
+ if i=0 then writeLn( ' ---- this part of the string matches the entire regular expression' )
+ else writeLn( ' ---- this matches parenthesised subexpression ', i );
+ end
end
- end
+ end
+ end
until False;
DisposeRegEx (r)
end.
--- p/test/aregextest.pas 2012-11-10 19:32:37.000000000 +0000
+++ p/test/aregextest.pas 2012-11-10 19:45:15.000000000 +0000
@@ -1,12 +1,13 @@
-{ COMPILE-CMD: regex.cmp }
-
{ Test of the RegEx unit... and maybe also of deeply nested
`for' loops... :-}
program RegExTest;
-uses GPC, RegEx;
+uses GPC, RegEx, Intl;
+const
+ firstChar = Chr(1);
+ lastChar = Chr($ff);
var
sr, s : TString;
r : RegExType;
@@ -16,6 +17,7 @@
c : Char;
Characters : CharSet;
+
{$field-widths}
procedure Error1 (const Msg : String);
@@ -138,6 +140,11 @@
end;
begin
+
+ { some tests require that we set the C locale }
+ s := SetLocale (LC_COLLATE, 'C');
+ s := SetLocale (LC_CTYPE, 'C');
+
Randomize;
TestMain;
TestSet (SpaceCharacters);
@@ -153,7 +160,7 @@
TestSet (['A' .. 'Z', 'a' .. 'z', '_', '0' .. '9']);
TestSet (WildCardChars);
TestSet (FileNameSpecialChars);
- TestSet ([Low (Char) .. High (Char)]);
+ TestSet ([firstChar .. lastChar]);
TestSet (['^']);
TestSet (['-']);
TestSet (['^', '-']);
@@ -169,13 +176,13 @@
TestSet ([']', '[', '^']);
TestSet ([']', '[', '-']);
TestSet ([']', '[', '^', '-']);
- for i := 0 to $100 do
+ for i := Ord(firstChar) to Ord(lastChar) do
begin
Characters := [];
for j := 1 to i do
begin
repeat
- c := Chr (Random (Ord (High (c)) + 1))
+ c := Chr (Random (Ord (lastChar))+1)
until not (c in Characters);
Characters := Characters + [c]
end;
--- p/test/regex.cmp 2007-09-04 07:22:55.000000000 +0100
+++ p/test/regex.cmp 1970-01-01 01:00:00.000000000 +0100
@@ -1,21 +0,0 @@
-#!/bin/sh
-cat << EOF > dummy.c
-#include <stdlib.h>
-#include <rxposix.h>
-int main()
-{
- return 0;
-}
-EOF
-if gcc dummy.c -lrx > /dev/null 2>&1; then
- rm -f "$A_OUT" dummy.c
- $1 $2
- if [ -r "$A_OUT" ] ; then
- ./"$A_OUT"
- else
- echo "failed"
- fi
-else
- rm -f dummy.c
- echo "SKIPPED: librx not installed"
-fi