Browse Source

* fixed TryStringToGUID: this function throwed an exceptions

git-svn-id: trunk@14059 -
ivost 16 years ago
parent
commit
537825dee9
1 changed files with 40 additions and 20 deletions
  1. 40 20
      rtl/objpas/sysutils/sysuintf.inc

+ 40 - 20
rtl/objpas/sysutils/sysuintf.inc

@@ -71,39 +71,59 @@ end;
 
 
 
 
 function TryStringToGUID(const S: string; out Guid: TGUID): Boolean;
 function TryStringToGUID(const S: string; out Guid: TGUID): Boolean;
+var
+  e: Boolean;
+  p: PChar;
 
 
-  function HexChar(c: Char): Byte;
+  function rb: Byte;
   begin
   begin
-    case c of
-      '0'..'9':
-        Result:=Byte(c) - Byte('0');
-      'a'..'f':
-        Result:=(Byte(c) - Byte('a')) + 10;
-      'A'..'F':
-        Result:=(Byte(c) - Byte('A')) + 10;
-      else
-        raise EConvertError.CreateFmt(SInvalidGUID, [s]);
-        Result:=0;
+    case p^ of
+      '0'..'9': Result := Byte(p^) - Byte('0');
+      'a'..'f': Result := Byte(p^) - Byte('a') + 10;
+      'A'..'F': Result := Byte(p^) - Byte('A') + 10;
+      else e := False;
     end;
     end;
+    Inc(p);
   end;
   end;
 
 
-  function HexByte(p: PChar): Byte;
+  procedure nextChar(c: Char); inline;
   begin
   begin
-    Result:=(HexChar(p[0]) shl 4) + HexChar(p[1]);
+    if p^ <> c then
+      e := False;
+    Inc(p);
   end;
   end;
 
 
-var
-  i: integer;
-  src: PChar;
-  dest: PByte;
 begin
 begin
-  if ((Length(S)<>38) or (s[1]<>'{')) then
+  if Length(S)<>38 then Exit(False);
+  e := True;
+  p := PChar(S);
+  nextChar('{');
+  Guid.D1 := rb shl 28 or rb shl 24 or rb shl 20 or rb shl 16 or rb shl 12 or rb shl 8 or rb shl 4 or rb;
+  nextChar('-');
+  Guid.D2 := rb shl 12 or rb shl 8 or rb shl 4 or rb;
+  nextChar('-');
+  Guid.D3 := rb shl 12 or rb shl 8 or rb shl 4 or rb;
+  nextChar('-');
+  Guid.D4[0] := rb shl 4 or rb;
+  Guid.D4[1] := rb shl 4 or rb;
+  nextChar('-');
+  Guid.D4[2] := rb shl 4 or rb;
+  Guid.D4[3] := rb shl 4 or rb;
+  Guid.D4[4] := rb shl 4 or rb;
+  Guid.D4[5] := rb shl 4 or rb;
+  Guid.D4[6] := rb shl 4 or rb;
+  Guid.D4[7] := rb shl 4 or rb;
+  nextChar('}');
+  Result := e;
+end;
+(*
+   if ((Length(S)<>38) or (s[1]<>'{')) then
     Exit(False);
     Exit(False);
   dest:=PByte(@Guid);
   dest:=PByte(@Guid);
   src:=PChar(s);
   src:=PChar(s);
   inc(src);
   inc(src);
   for i:=0 to 3 do
   for i:=0 to 3 do
-    dest[i]:=HexByte(src+(3-i)*2);
+    if not HexByte(src+(3-i)*2, dest[i]) then Exit(False);
   inc(src, 8);
   inc(src, 8);
   inc(dest, 4);
   inc(dest, 4);
   if src[0]<>'-' then
   if src[0]<>'-' then
@@ -136,7 +156,7 @@ begin
      inc(src, 2);
      inc(src, 2);
    end;
    end;
    Result := True;
    Result := True;
-end;
+end;*)
 
 
 
 
 function IsEqualGUID(const guid1, guid2: TGUID): Boolean;
 function IsEqualGUID(const guid1, guid2: TGUID): Boolean;