Browse Source

fcl-json: faster read /u

git-svn-id: trunk@38256 -
Mattias Gaertner 7 years ago
parent
commit
ae3a735e51
1 changed files with 12 additions and 10 deletions
  1. 12 10
      packages/fcl-json/src/jsonscanner.pp

+ 12 - 10
packages/fcl-json/src/jsonscanner.pp

@@ -208,8 +208,8 @@ var
   TokenStart: PChar;
   TokenStart: PChar;
   it : TJSONToken;
   it : TJSONToken;
   I : Integer;
   I : Integer;
-  OldLength, SectionLength,  tstart,tcol: Integer;
-  C : char;
+  OldLength, SectionLength,  tstart,tcol, u: Integer;
+  C , c2: char;
   S : String;
   S : String;
   IsStar,EOC: Boolean;
   IsStar,EOC: Boolean;
 
 
@@ -272,21 +272,24 @@ begin
               '/' : S:='/';
               '/' : S:='/';
               'u' : begin
               'u' : begin
                     S:='0000';
                     S:='0000';
+                    u:=0;
                     For I:=1 to 4 do
                     For I:=1 to 4 do
                       begin
                       begin
                       Inc(TokenStr);
                       Inc(TokenStr);
-                      Case TokenStr[0] of
-                        '0'..'9','A'..'F','a'..'f' :
-                          S[i]:=Upcase(TokenStr[0]);
+                      c2:=TokenStr^;
+                      Case c2 of
+                        '0'..'9': u:=u*16+ord(c2)-ord('0');
+                        'A'..'F': u:=u*16+ord(c2)-ord('A')+10;
+                        'a'..'f': u:=u*16+ord(c2)-ord('a')+10;
                       else
                       else
                         Error(SErrInvalidCharacter, [CurRow,CurColumn,TokenStr[0]]);
                         Error(SErrInvalidCharacter, [CurRow,CurColumn,TokenStr[0]]);
                       end;
                       end;
                       end;
                       end;
-                    // WideChar takes care of conversion...  
-                    if (joUTF8 in Options) then
-                      S:=Utf8Encode(WideString(WideChar(StrToInt('$'+S))))
+                    // ToDo: 4-bytes UTF16
+                    if (joUTF8 in Options) or (DefaultSystemCodePage=CP_UTF8) then
+                      S:=Utf8Encode(WideString(WideChar(u))) // ToDo: use faster function
                     else
                     else
-                      S:=WideChar(StrToInt('$'+S));  
+                      S:=String(WideChar(u)); // WideChar converts the encoding. Should it warn on loss?
                     end;
                     end;
               #0  : Error(SErrOpenString);
               #0  : Error(SErrOpenString);
             else
             else
@@ -298,7 +301,6 @@ begin
             Move(S[1],FCurTokenString[OldLength + SectionLength+1],Length(S));
             Move(S[1],FCurTokenString[OldLength + SectionLength+1],Length(S));
             Inc(OldLength, SectionLength+Length(S));
             Inc(OldLength, SectionLength+Length(S));
             // Next char
             // Next char
-            // Inc(TokenStr);
             TokenStart := TokenStr+1;
             TokenStart := TokenStr+1;
             end;
             end;
           if TokenStr[0] = #0 then
           if TokenStr[0] = #0 then