Browse Source

+ add a AnsiString based overload of GetToken

Sven/Sarah Barth 3 years ago
parent
commit
5562e3e440
1 changed files with 55 additions and 0 deletions
  1. 55 0
      compiler/cutils.pas

+ 55 - 0
compiler/cutils.pas

@@ -100,6 +100,7 @@ interface
     function PadSpace(const s:string;len:longint):string;
     function PadSpace(const s:AnsiString;len:longint):AnsiString;
     function GetToken(var s:string;endchar:char):string;
+    function GetToken(var s:ansistring;endchar:char):ansistring;
     procedure uppervar(var s : string);
     function realtostr(e:extended):string;{$ifdef USEINLINE}inline;{$endif}
     function tostr(i : qword) : string;{$ifdef USEINLINE}inline;{$endif}overload;
@@ -887,6 +888,60 @@ implementation
       end;
 
 
+    function GetToken(var s:ansistring;endchar:char):ansistring;
+      var
+        i : longint;
+        quote : char;
+      begin
+        GetToken:='';
+        s:=TrimSpace(s);
+        if (length(s)>0) and
+           (s[1] in ['''','"']) then
+         begin
+           quote:=s[1];
+           i:=1;
+           while (i<length(s)) do
+            begin
+              inc(i);
+              if s[i]=quote then
+               begin
+                 { Remove double quote }
+                 if (i<length(s)) and
+                    (s[i+1]=quote) then
+                  begin
+                    Delete(s,i,1);
+                    inc(i);
+                  end
+                 else
+                  begin
+                    GetToken:=Copy(s,2,i-2);
+                    Delete(s,1,i);
+                    exit;
+                  end;
+               end;
+            end;
+           GetToken:=s;
+           s:='';
+         end
+        else
+         begin
+           i:=pos(EndChar,s);
+           if i=0 then
+            begin
+              GetToken:=s;
+              s:='';
+              exit;
+            end
+           else
+            begin
+              GetToken:=Copy(s,1,i-1);
+              Delete(s,1,i);
+              exit;
+            end;
+         end;
+      end;
+
+
    function realtostr(e:extended):string;{$ifdef USEINLINE}inline;{$endif}
      begin
         str(e,result);