瀏覽代碼

* part of r16533, forgot to commit
* also fixes CharLengthPChar() for non-BeOS by initialising the mbstate
parameter passed to mbrlen() to 0 (otherwise its contents are interpreted
as already valid, containing information about the current shiftstate etc)

git-svn-id: trunk@16552 -

Jonas Maebe 14 年之前
父節點
當前提交
a2e8134b7c
共有 1 個文件被更改,包括 24 次插入0 次删除
  1. 24 0
      rtl/unix/cwstring.pp

+ 24 - 0
rtl/unix/cwstring.pp

@@ -566,6 +566,9 @@ function CharLengthPChar(const Str: PChar): PtrInt;
   begin
     result:=0;
     s:=str;
+{$ifndef beos}
+    fillchar(mbstate,sizeof(mbstate),0);
+{$endif not beos}
     repeat
 {$ifdef beos}
       nextlen:=ptrint(mblen(str,MB_CUR_MAX));
@@ -581,6 +584,26 @@ function CharLengthPChar(const Str: PChar): PtrInt;
   end;
 
 
+function CodePointLength(const Str: PChar; maxlookahead: ptrint): PtrInt;
+  var
+    nextlen: ptrint;
+{$ifndef beos}
+    mbstate: mbstate_t;
+{$endif not beos}
+  begin
+{$ifdef beos}
+    result:=ptrint(mblen(str,maxlookahead));
+{$else beos}
+    fillchar(mbstate,sizeof(mbstate),0);
+    result:=ptrint(mbrlen(str,maxlookahead,@mbstate));
+    { mbrlen can also return -2 for "incomplete but potially valid character
+      and data has been processed" }
+    if result<0 then
+      result:=-1;
+{$endif beos}
+  end;
+
+
 function StrCompAnsiIntern(s1,s2 : PChar; len1, len2: PtrInt; canmodifys1, canmodifys2: boolean): PtrInt;
   var
     a,b: pchar;
@@ -758,6 +781,7 @@ begin
       CompareTextWideStringProc:=@CompareTextWideString;
 
       CharLengthPCharProc:=@CharLengthPChar;
+      CodePointLengthProc:=@CodePointLength;
 
       UpperAnsiStringProc:=@UpperAnsiString;
       LowerAnsiStringProc:=@LowerAnsiString;