修改 Source目录下的ppUtils.pas
以下仅供参考:
{------------------------------------------------------------------------------}
{ ppGetWord }
function ppGetWord(Text: PChar;
var CharPos: Longint): String;
const
lcSpace = ' ';
lcLF = #10;
lcNull = #0;
lcTab = #9;
var
lStartText: PChar;
lFindText: PChar;
lFindNull: PChar;
lFindSpace: PChar;
lFindCRLF: PChar;
lFindTab: PChar;
liLength: Integer;
lsWord: String;
lWord: PChar;
liNewCharPos: Integer;
liWordLength: Integer;
lbEndOfText: Boolean;
liBytePos:integer;
begin
lbEndOfText := False;
lsWord := '';
lFindText := nil;
{create a PChar of text to be searched}
liLength := StrLen(Text) + 1;
{move to begin
ning of words not yet wrapped}
lStartText := Text;
lStartText := lStartText + CharPos;
{find occurance of a space}
lFindSpace := lStartText;
lFindSpace := StrScan(lFindSpace, lcSpace);
{find occurance of a carriage return}
lFindCRLF := lStartText;
lFindCRLF := StrScan(lFindCRLF, lcLF);
lFindNull := lStartText;
lFindNull := StrScan(lFindNull, lcNull);
lFindTab := lStartText;
lFindTab := StrScan(lFindTab, lcTab);
if lFindSpace <> nil then
lFindText := lFindSpace;
if (lFindTab <> nil) and ( (lFindText = nil) or (lFindTab < lFindText) ) then
lFindText := lFindTab;
if (lFindCRLF <> nil) and ( (lFindText = nil) or (lFindCRLF < lFindText) ) then
lFindText := lFindCRLF;
if (lFindText = nil) and (lFindNull <> nil) then
begin
lFindText := lFindNull;
lbEndOfText := True;
end;
{was a space or carriage return found?}
if lFindText <> nil then
begin
{calculate new character position}
if lbEndOfText then
liNewCharPos := liLength
else
liNewCharPos := liLength - Integer(StrLen(lFindText));
liWordLength := liNewCharPos - CharPos;
(******************************************)
{添加正确处理双字节字符(如汉字)的代码}
{Lighttop ,2000.4.10}
{用StrByteType 函数检查第一个字节的类型}
//如为mbSingleByte ,则截取到下一个不为mbSingleByte的字节(如果存在的话)
//如为mbLeadByte ,则截取到下一个为mbTrailByte的字节
//如为mbTrailByte (正常情况下不会出现这种情况)
(******************************************)
liBytePos:=0;
if StrByteType(lStartText,0) = mbSingleByte then
begin
liBytePos:=liBytePos + 1;
while (liBytePos < liWordLength) and (StrByteType(lStartText,liBytePos) = mbSingleByte)do
Inc(liBytePos);
end
else
if StrByteType(lStartText,0) = mbLeadByte then
begin
liBytePos:= 2;
//双字节字符.
end;
if liBytePos <> 0 then
begin
liNewCharPos := liNewCharPos - (LiWordLength - liBytePos);
liWordLength := liBytePos;
end;
{添加的代码结束}
(******************************************)
lWord := StrAlloc(liWordLength + 1);
lWord := StrLCopy(lWord, lStartText, liWordLength);
lsWord := StrPas(lWord);
if Assigned(lWord) then
StrDispose(lWord);
{return new char pos to calling procedure}
if lbEndOfText then
CharPos := -1
else
CharPos := liNewCharPos;
end;
Result := lsWord;
end;
{function, ppGetWord}