From 37ae3c0f432e7670848a047ce6fb34628eaa4b29 Mon Sep 17 00:00:00 2001 From: juha Date: Sun, 3 May 2015 10:02:15 +0000 Subject: [PATCH] =?UTF-8?q?LazUtils:=20New=20UTF8WrapText=20function=20for?= =?UTF-8?q?=20LazUTF8.=20Issue=20#28001,=20patch=20from=20Ant=C3=B4nio=20G?= =?UTF-8?q?alv=C3=A3o.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit git-svn-id: trunk@48924 - --- components/lazutils/lazutf8.pas | 70 ++++++++++++++++++++++++++++++++- 1 file changed, 69 insertions(+), 1 deletion(-) diff --git a/components/lazutils/lazutf8.pas b/components/lazutils/lazutf8.pas index e8b91de218..6497f6736b 100644 --- a/components/lazutils/lazutf8.pas +++ b/components/lazutils/lazutf8.pas @@ -34,7 +34,7 @@ uses {$ifdef windows} Windows, {$endif} - Classes, SysUtils; + Classes, SysUtils; // AnsiToUTF8 and UTF8ToAnsi need a widestring manager under Linux, BSD, MacOSX // but normally these OS use UTF-8 as system encoding so the widestringmanager @@ -114,6 +114,9 @@ function UTF8RightStr(const AText: String; const ACount: Integer): String; function UTF8QuotedStr(const S, Quote: string): string; //Utf8 version of MidStr is just Utf8Copy with same parameters, so it is not implemented here +function UTF8WrapText(S, BreakStr :string; BreakChars :TSysCharSet; MaxCol: integer): string; overload; +function UTF8WrapText(S :string; MaxCol :integer) :string; overload; + type TUTF8TrimFlag = ( u8tKeepStart, @@ -2734,6 +2737,71 @@ begin Result+=copy(S,CopyPos-PChar(S)+1,p-CopyPos)+Quote; end; +function UTF8WrapText(S, BreakStr :string; BreakChars :TSysCharSet; MaxCol: integer): string; +var + P :PChar; + CharLen :integer; + RightSpace : Integer = 0; + N :integer = 0; + i : Integer; + j : Integer; + Len :integer = 0; + ResultLen, RP :Integer; +begin + Result := ''; + if (S = '') or (BreakStr = '') or (BreakChars = []) then Exit; + P := PChar(S); + while P^ <> #0 do + begin + CharLen := UTF8CharacterLength(P); + + // Result := Result + Copy(P, 1, CharLen); + i := 1; + j := 0; + ResultLen := Length(Result); + SetLength(Result, ResultLen + CharLen); + while i <= CharLen do + begin + /// Result := Result + (P + j)^; + Result[ResultLen + i] := (P + J)^; + /// + Inc(i); + Inc(j); + end; + // + + Inc(N); + if P^ = {#10} BreakStr[Length(BreakStr)] then + N := 0; + if N > MaxCol then + begin + Len := Length(Result); + + ///// avoiding RPos + RP := Len; + //while Result[RP] <> #32 do + while not (Result[RP] in BreakChars) do + Dec(RP); + ///// + + RightSpace := Len - RP; //RPos(' ', Result); + if RightSpace > 0 then + begin + Dec(P, RightSpace); + SetLength(Result, Len - RightSpace); + end; + Result := Result + BreakStr; + N := 0; + end; + Inc(P, CharLen); + end; +end; + +function UTF8WrapText(S :string; MaxCol: integer): string; +begin + Result := UTF8WrapText(S, LineEnding, [' ', '-', #9], MaxCol); +end; + function UTF8Trim(const s: string; Flags: TUTF8TrimFlags): string; var p: PChar;