1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322
|
{
*****************************************************************************
This file is part of LazUtils.
See the file COPYING.modifiedLGPL.txt, included in this distribution,
for details about the license.
*****************************************************************************
This unit provides encoding agnostic unicode string manipulation functions and
an enumerator. It works transparently with UTF-8 and UTF-16 encodings,
thus allowing one source code to work between :
1. Lazarus with its UTF-8 solution.
2. Future FPC and Lazarus with Delphi compatible UTF-16 solution.
3. Delphi, where String = UnicodeString.
}
unit LazUnicode;
{$IFDEF FPC}
{$mode objfpc}{$H+}{$inline on}
{$ENDIF}
// For testing the UTF16 version.
{$IF DEFINED(FPC) and DEFINED(UseUTF16)}
{$ModeSwitch UnicodeStrings} // Sets also FPC_UNICODESTRINGS.
{$ENDIF}
{$IF DEFINED(FPC_UNICODESTRINGS) or not DEFINED(FPC)}
{$DEFINE ReallyUseUTF16} // FPC with UTF-16 or Delphi
{$ENDIF}
interface
uses
Classes, SysUtils
{$IFDEF ReallyUseUTF16}
,character, LazUTF16
{$ENDIF}
{$IFDEF FPC}
,LazUTF8
{$ENDIF}
;
// Helper functions for codepoints. They change behavior depending on ModeSwitch.
function CodePointCopy(const s: string; StartCharIndex, CharCount: NativeInt): string; inline;
function CodePointLength(const s: string): NativeInt; inline;
function CodePointPos(const SearchForText, SearchInText: string; StartPos: NativeInt = 1): NativeInt; inline;
function CodePointSize(p: PChar): integer; inline;
function IsCombining(const AChar: PChar): Boolean; {$IFDEF FPC}inline;{$ENDIF}
function UnicodeToWinCP(const s: string): AnsiString;
function WinCPToUnicode(const s: AnsiString): string;
function StringOfCodePoint(ACodePoint: String; N: Integer): String;
type
// Base class for CodePoint and Character enumerators.
{ TUnicodeEnumeratorBase }
TUnicodeEnumeratorBase = class
private
fSrcPos, fEndPos: PChar; // Pointers to source string.
// Preset variables for different codepoint/character lengths.
// Current will be assigned to one of them.
fCurOne, fCurTwo, fCurThree, fCurLong: String;
fCurrent: String; // Current separated codepoint/character.
fCurrentCodeUnitCount: Integer; // Number of CodeUnits (Pascal Char) in Current.
procedure UpdateCurrent(aCount: integer);
public
constructor Create(const A: String);
property Current: String read fCurrent;
property CurrentCodeUnitCount: Integer read fCurrentCodeUnitCount;
end;
{ TCodePointEnumerator }
// Traverse Unicode codepoints. Uses UTF-8 or UTF-16 depending on $ModeSwitch.
TCodePointEnumerator = class(TUnicodeEnumeratorBase)
public
function MoveNext: Boolean;
end;
{ TUnicodeCharacterEnumerator }
// Traverse Unicode (user perceived) characters, including accented characters
// with combined codepoints. Uses UTF-8 or UTF-16 depending on $ModeSwitch.
TUnicodeCharacterEnumerator = class(TUnicodeEnumeratorBase)
private
fCurrentCodePointCount: Integer; // Number of CodePoints in Current.
public
property CurrentCodePointCount: Integer read fCurrentCodePointCount;
function MoveNext: Boolean;
end;
{$IFDEF FPC}
// Enumerator for CodePoints could be used for the for-in loop.
//operator Enumerator(A: String): TCodePointEnumerator;
// This enumerator combines diacritical marks.
// It is used by default although there are more rules for combining codepoints.
// Diacritical marks cover rules for most western languages.
operator Enumerator(A: String): TUnicodeCharacterEnumerator;
{$ENDIF}
implementation
{$IFDEF ReallyUseUTF16}
function UTF16IsCombining(const AChar: PWideChar): Boolean;
var
ch: WideChar;
begin
ch := AChar[0];
Result := // Combining Diacritical Marks (belongs to previos char)
( (ch >= #$300) and (ch <= #$36F) ) or // 0300-036F
( (ch >= #$610) and (ch <= #$61A) ) or // Arabic 0610..061A
( (ch >= #$64B) and (ch <= #$65F) ) or // Arabic 064B..065F
( ch = #$670) or // Arabic 0670
( (ch >= #$6D6) and (ch <= #$6DC) ) or // Arabic 06D6..06DC
( (ch >= #$6DF) and (ch <= #$6E4) ) or // Arabic 06DF..06E4
( (ch >= #$6E7) and (ch <= #$6E8) ) or // Arabic 06E7..06E8
( (ch >= #$6EA) and (ch <= #$6ED) ) or // Arabic 06EA..06ED
( (ch >= #$8E4) and (ch <= #$8FE) ) or // Arabic 08E4..08FE
( (ch >= #$1DC0) and (ch <= #$1DFF) ) or // Combining Diacritical Marks Supplement 1DC0-1DFF
( (ch >= #$20D0) and (ch <= #$20FF) ) or // Combining Diacritical Marks for Symbols 20D0-20FF
( (ch >= #$FE20) and (ch <= #$FE2F) ); // Combining half Marks FE20-FE2F
end;
{$ELSE}
function UTF8IsCombining(const AChar: PChar): Boolean;
begin
Result :=
( (AChar[0] = #$CC) ) or // Combining Diacritical Marks (belongs to previos char) 0300-036F
( (AChar[0] = #$CD) and (AChar[1] in [#$80..#$AF]) ) or // Combining Diacritical Marks
( (AChar[0] = #$D8) and (AChar[1] in [#$90..#$9A]) ) or // Arabic 0610 (d890)..061A (d89a)
( (AChar[0] = #$D9) and (AChar[1] in [#$8b..#$9f, #$B0]) ) or // Arabic 064B (d98b)..065F (d99f) // 0670 (d9b0)
( (AChar[0] = #$DB) and (AChar[1] in [#$96..#$9C, #$9F..#$A4, #$A7..#$A8, #$AA..#$AD]) ) or // Arabic 06D6 (db96).. .. ..06ED (dbad)
( (AChar[0] = #$E0) and (AChar[1] = #$A3) and (AChar[2] in [#$A4..#$BE]) ) or // Arabic 08E4 (e0a3a4) ..08FE (e0a3be)
( (AChar[0] = #$E1) and (AChar[1] = #$B7) ) or // Combining Diacritical Marks Supplement 1DC0-1DFF (e1b780)
( (AChar[0] = #$E2) and (AChar[1] = #$83) and (AChar[2] in [#$90..#$FF]) ) or // Combining Diacritical Marks for Symbols 20D0-20FF
( (AChar[0] = #$EF) and (AChar[1] = #$B8) and (AChar[2] in [#$A0..#$AF]) ); // Combining half Marks FE20-FE2F
end;
{$ENDIF}
//---
function CodePointCopy(const s: string; StartCharIndex, CharCount: NativeInt): string;
// Copy CharCount CodePoints from s, starting from StartCharIndex'th CodePoints.
begin
{$IFDEF ReallyUseUTF16}
Result := UTF16Copy(s, StartCharIndex, CharCount);
{$ELSE}
Result := UTF8Copy(s, StartCharIndex, CharCount);
{$ENDIF}
end;
function CodePointLength(const s: string): NativeInt;
// Number of CodePoints in s.
begin
{$IFDEF ReallyUseUTF16}
Result := UTF16Length(s);
{$ELSE}
Result := UTF8LengthFast(s);
{$ENDIF}
end;
function CodePointPos(const SearchForText, SearchInText: string; StartPos: NativeInt = 1): NativeInt;
// Position of SearchForText in CodePoints.
begin
{$IFDEF ReallyUseUTF16}
Result := UTF16Pos(SearchForText, SearchInText, StartPos);
{$ELSE}
Result := UTF8Pos(SearchForText, SearchInText, StartPos);
{$ENDIF}
end;
function CodePointSize(p: PChar): integer;
// Returns the number of CodeUnits in one CodePoint pointed by p.
begin
{$IFDEF ReallyUseUTF16}
if TCharacter.IsHighSurrogate(p^) then
Result := 2
else
Result := 1
{$ELSE}
Result := UTF8CodepointSizeFast(p);
{$ENDIF}
end;
function IsCombining(const AChar: PChar): Boolean;
// Note: there are many more rules for combining codepoints.
// The diacritical marks here are only a subset.
begin
{$IFDEF ReallyUseUTF16}
Result := UTF16IsCombining(AChar);
{$ELSE}
Result := UTF8IsCombining(AChar);
{$ENDIF}
end;
function UnicodeToWinCP(const s: string): AnsiString;
// Convert s to Windows system codepage. The Unicode encoding of s depends on mode.
begin
{$IFDEF ReallyUseUTF16}
{$IFDEF FPC}
// ToDo: Don't convert through UTF-8.
Result := UTF8ToWinCP(UTF16ToUTF8(s));
{$ELSE}
Result := s; // s is UnicodeString in Delphi. Conversion may be lossy.
{$ENDIF}
{$ELSE}
Result := UTF8ToWinCP(s);
{$ENDIF}
end;
function WinCPToUnicode(const s: AnsiString): string;
// Convert Windows system codepage s to Unicode (encoding depends on mode).
begin
{$IFDEF ReallyUseUTF16}
{$IFDEF FPC}
// ToDo: Don't convert through UTF-8.
Result := UTF8ToUTF16(WinCPToUTF8(s));
{$ELSE}
Result := s; // Result is UnicodeString in Delphi.
{$ENDIF}
{$ELSE}
Result := WinCPToUTF8(s);
{$ENDIF}
end;
function StringOfCodePoint(ACodePoint: String; N: Integer): String;
// Like StringOfChar
{$IFDEF ReallyUseUTF16}
var
i: Integer;
{$ENDIF}
begin
{$IFDEF ReallyUseUTF16}
Result := '';
for i := 1 to N do
Result := Result + ACodePoint;
{$ELSE}
Result := Utf8StringOfChar(ACodePoint, N);
{$ENDIF}
end;
{ TUnicodeEnumeratorBase }
constructor TUnicodeEnumeratorBase.Create(const A: String);
begin
fSrcPos := PChar(A); // Note: if A='' then PChar(A) returns a pointer to a #0 string
fEndPos := fSrcPos + length(A);
SetLength(fCurOne, 1); // Space for the most common codepoint/character lengths.
SetLength(fCurTwo, 2);
SetLength(fCurThree, 3);
end;
procedure TUnicodeEnumeratorBase.UpdateCurrent(aCount: integer);
// Copy the needed bytes to fCurrent which then holds a codepoint or "character".
begin
fCurrentCodeUnitCount := aCount;
Assert(aCount<>0, 'TUnicodeEnumeratorBase.UpdateCurrent: aCount=0.');
case aCount of
1: fCurrent := fCurOne; // Assignment does not copy but reference count changes.
2: fCurrent := fCurTwo;
3: fCurrent := fCurThree;
else begin
SetLength(fCurLong, aCount);
fCurrent := fCurLong;
end;
end;
Move(fSrcPos^, fCurrent[1], aCount*SizeOf(Char));
Assert(Length(fCurrent)=aCount, 'TUnicodeEnumeratorBase.UpdateCurrent: Length(fCurrent)<>aCount.');
inc(fSrcPos, aCount);
end;
{ TCodePointEnumerator }
function TCodePointEnumerator.MoveNext: Boolean;
begin
Result := fSrcPos < fEndPos;
if Result then
UpdateCurrent(CodePointSize(fSrcPos));
end;
{ TUnicodeCharacterEnumerator }
function TUnicodeCharacterEnumerator.MoveNext: Boolean;
var
NextCP: PChar;
NextCUCount: Integer;
begin
Result := fSrcPos < fEndPos;
if Result then
begin
fCurrentCodePointCount := 0;
NextCP := fSrcPos;
repeat
NextCUCount := CodePointSize(NextCP);
Inc(NextCP, NextCUCount); // Prepare for combining diacritical marks.
Inc(fCurrentCodePointCount);
until not IsCombining(NextCP);
UpdateCurrent(NextCP - fSrcPos); // Pointer arithmetics.
end;
end;
//---
// Enumerator
//---
{$IFDEF FPC}
operator Enumerator(A: String): TUnicodeCharacterEnumerator;
begin
Result := TUnicodeCharacterEnumerator.Create(A);
end;
{$ENDIF}
end.
|