Jelajahi Sumber

$ Corrections for widestring use

Michaël Van Canneyt 1 tahun lalu
induk
melakukan
59cb93fcb4
1 mengubah file dengan 48 tambahan dan 9 penghapusan
  1. 48 9
      packages/libpcre/examples/tpcre.pp

+ 48 - 9
packages/libpcre/examples/tpcre.pp

@@ -1,6 +1,8 @@
 program tpcre;
 program tpcre;
 
 
-{$DEFINE USE_WIDESTRING}
+{$mode objfpc}
+{$h+}
+{ $DEFINE USE_WIDESTRING}
 
 
 uses
 uses
    {$IFNDEF USE_WIDESTRING}
    {$IFNDEF USE_WIDESTRING}
@@ -14,18 +16,40 @@ uses
 {$IFNDEF USE_WIDESTRING}
 {$IFNDEF USE_WIDESTRING}
 function GetStrLen(p : PAnsiChar; len : Integer) : AnsiString;
 function GetStrLen(p : PAnsiChar; len : Integer) : AnsiString;
 
 
+var
+   L : Integer;
+
 begin
 begin
-  SetLength(Result,Len);
+  Result:='';
+  L:=StrLen(P);
+  if L>len then
+    L:=Len;
+  SetLength(Result,L);
   if Len>0 then
   if Len>0 then
-    Move(P^,Result[1],Len);
+    Move(P^,Result[1],L);
 end;
 end;
 {$ELSE}
 {$ELSE}
 function GetStrLen(p : PWideChar; len : Integer) : UnicodeString;
 function GetStrLen(p : PWideChar; len : Integer) : UnicodeString;
 
 
+var
+  L : Integer;
+  P2: PWideChar;
+
 begin
 begin
-  SetLength(Result,Len);
+  Result:='';
+  L:=0;
+  P2:=P;
+  // No widestring strlen unless we compile in unicode rtl...
+  While (P2^<>#0) do
+    begin
+    inc(L);
+    inc(P2);
+    end;
+  if L>len then
+    L:=Len;
+  SetLength(Result,L);
   if Len>0 then
   if Len>0 then
-    Move(P^,Result[1],Len*2);
+    Move(P^,Result[1],L*2);
 end;
 end;
 {$ENDIF}
 {$ENDIF}
 
 
@@ -34,9 +58,11 @@ var
  {$IFNDEF USE_WIDESTRING}
  {$IFNDEF USE_WIDESTRING}
  ptrn : AnsiString;
  ptrn : AnsiString;
  subj : AnsiString;
  subj : AnsiString;
+ groupname : AnsiString;
  {$ELSE}
  {$ELSE}
  ptrn : UnicodeString;
  ptrn : UnicodeString;
  subj : UnicodeString;
  subj : UnicodeString;
+ groupname : UnicodeString;
  {$ENDIF}
  {$ENDIF}
 
 
  pattern : PCRE2_SPTR;     (* PCRE2_SPTR is a pointer to unsigned code units of *)
  pattern : PCRE2_SPTR;     (* PCRE2_SPTR is a pointer to unsigned code units of *)
@@ -314,9 +340,16 @@ begin
     tabptr := name_table;
     tabptr := name_table;
     for i:=0 to namecount-1 do
     for i:=0 to namecount-1 do
       begin
       begin
+{$IFDEF USE_WIDESTRING}
+      n:=ord(tabptr[0]);
+      groupname:=GetStrLen((TabPtr+1),name_entry_size-2);
+{$ELSE}
       n:=(ord(tabptr[0]) shl 8) or ord(tabptr[1]);
       n:=(ord(tabptr[0]) shl 8) or ord(tabptr[1]);
+      groupname:=GetStrLen((tabptr + 2),name_entry_size - 3),
+{$ENDIF}
       matchlen:=integer(ovector[2*n+1] - ovector[2*n]);
       matchlen:=integer(ovector[2*n+1] - ovector[2*n]);
-      writeln( '(',n,')', GetStrLen((tabptr + 2),name_entry_size - 3),' : ',
+
+      writeln( '(',n,')', Groupname,' : ',
                           GetStrLen((subject + ovector[2*n]), Matchlen));
                           GetStrLen((subject + ovector[2*n]), Matchlen));
       inc(tabptr, name_entry_size);
       inc(tabptr, name_entry_size);
       end ;
       end ;
@@ -546,9 +579,15 @@ begin
       Writeln('Named substrings');
       Writeln('Named substrings');
       for i:=0 to namecount-1 do
       for i:=0 to namecount-1 do
         begin
         begin
-        n := (Ord(tabptr[0]) shl 8) or ord(tabptr[1]);
-        writeln(n,': ',GetStrLen(tabptr + 2,name_entry_size - 3),'= ',
-                        GetStrLen(subject + ovector[2*n], integer(ovector[2*n+1] - ovector[2*n])));
+        {$IFDEF USE_WIDESTRING}
+        n:=ord(tabptr[0]);
+        groupname:=GetStrLen((TabPtr+1),name_entry_size-2);
+        {$ELSE}
+        n:=(ord(tabptr[0]) shl 8) or ord(tabptr[1]);
+        groupname:=GetStrLen((tabptr + 2),name_entry_size - 3),
+        {$ENDIF}
+        matchlen:=integer(ovector[2*n+1] - ovector[2*n]);
+        writeln( '(',n,')', Groupname,' : ', GetStrLen((subject + ovector[2*n]), Matchlen));
         tabptr := tabptr+name_entry_size;
         tabptr := tabptr+name_entry_size;
         end ;
         end ;
       end;
       end;