summaryrefslogtreecommitdiff
path: root/src/corelib
diff options
context:
space:
mode:
authorgraemeg <graemeg@ae50a9b5-8222-0410-bf8d-8a13f76226bf>2008-07-24 10:13:21 +0000
committergraemeg <graemeg@ae50a9b5-8222-0410-bf8d-8a13f76226bf>2008-07-24 10:13:21 +0000
commiteaf3100e8d682ac3bc28725c004e4836169d7654 (patch)
treea8c5af8978053b5a24ca4a06937ef5f6b79a5a5a /src/corelib
parentd446ec27e140081d470dd2f1e6fda5f7c5212444 (diff)
downloadfpGUI-eaf3100e8d682ac3bc28725c004e4836169d7654.tar.xz
* Added some short form utf-8 methods to gfx_utf8utils unit.
Diffstat (limited to 'src/corelib')
-rw-r--r--src/corelib/gfx_utf8utils.pas189
1 files changed, 112 insertions, 77 deletions
diff --git a/src/corelib/gfx_utf8utils.pas b/src/corelib/gfx_utf8utils.pas
index 01028fe6..c3787d76 100644
--- a/src/corelib/gfx_utf8utils.pas
+++ b/src/corelib/gfx_utf8utils.pas
@@ -1,6 +1,6 @@
{
- Some handly UTF8 function copied from the Lazarus LCL. Comes from the LCLProc
- unit. Surely we can move this into FPC?
+ Some handly UTF8 functions copied from the Lazarus LCL. Comes from the
+ LCLProc unit. Surely we can move this into FPC?
}
unit gfx_UTF8utils;
@@ -24,18 +24,77 @@ procedure UTF8Insert(const Source: string; var S: string; Index: integer);
function UTF8CharAtByte(const s: string; BytePos: integer; var aChar: string): integer;
+// short form (alias or convenience) functions for the UTF8 ones above
+function Copy8(const s: string; StartCharIndex, CharCount: integer): string;
+function Length8(const s: string): integer;
+function Pos8(const SearchForText, SearchInText: string): integer;
+procedure Delete8(var S: string; Index, Size: integer);
+procedure Insert8(const Source: string; var S: string; Index: integer);
+
+
implementation
-// returns the character index, where the SearchForText starts in SearchInText
-function UTF8Pos(const SearchForText, SearchInText: string): integer;
-var
- p: LongInt;
+
+function UTF8CharacterLength(p: PChar): integer;
begin
- p := System.Pos(SearchForText, SearchInText);
- if p > 0 then
- Result := UTF8Length(PChar(SearchInText), p-1) + 1
+ if p <> nil then
+ begin
+ if ord(p^) < %11000000 then
+ begin
+ // regular single byte character (#0 is a character, this is pascal ;)
+ Result:=1;
+ end
+ else if ((ord(p^) and %11100000) = %11000000) then
+ begin
+ // could be 2 byte character
+ if (ord(p[1]) and %11000000) = %10000000 then
+ Result := 2
+ else
+ Result := 1;
+ end
+ else if ((ord(p^) and %11110000) = %11100000) then
+ begin
+ // could be 3 byte character
+ if ((ord(p[1]) and %11000000) = %10000000)
+ and ((ord(p[2]) and %11000000) = %10000000) then
+ Result:=3
+ else
+ Result:=1;
+ end
+ else if ((ord(p^) and %11111000) = %11110000) then
+ begin
+ // could be 4 byte character
+ if ((ord(p[1]) and %11000000) = %10000000)
+ and ((ord(p[2]) and %11000000) = %10000000)
+ and ((ord(p[3]) and %11000000) = %10000000) then
+ Result:=4
+ else
+ Result:=1;
+ end
+ else
+ Result:=1;
+ end
else
- Result := 0;
+ Result:=0;
+end;
+
+function UTF8CharStart(UTF8Str: PChar; Len, Index: integer): PChar;
+var
+ CharLen: LongInt;
+begin
+ Result := UTF8Str;
+ if Result <> nil then
+ begin
+ while (Index > 0) and (Len > 0) do
+ begin
+ CharLen := UTF8CharacterLength(Result);
+ dec(Len, CharLen);
+ dec(Index);
+ inc(Result, CharLen);
+ end;
+ if (Index > 0) or (Len < 0) then
+ Result := nil;
+ end;
end;
// returns substring
@@ -135,7 +194,7 @@ end;
function UTF8Length(const s: string): integer;
begin
if s = '' then
- Result := length(s)
+ Result := 0
else
Result := UTF8Length(PChar(s),length(s));
end;
@@ -154,66 +213,30 @@ begin
end;
end;
-function UTF8CharStart(UTF8Str: PChar; Len, Index: integer): PChar;
+// returns the character index, where the SearchForText starts in SearchInText
+function UTF8Pos(const SearchForText, SearchInText: string): integer;
var
- CharLen: LongInt;
+ p: LongInt;
begin
- Result := UTF8Str;
- if Result <> nil then
- begin
- while (Index > 0) and (Len > 0) do
- begin
- CharLen := UTF8CharacterLength(Result);
- dec(Len, CharLen);
- dec(Index);
- inc(Result, CharLen);
- end;
- if (Index > 0) or (Len < 0) then
- Result := nil;
- end;
+ p := System.Pos(SearchForText, SearchInText);
+ if p > 0 then
+ Result := UTF8Length(PChar(SearchInText), p-1) + 1
+ else
+ Result := 0;
end;
-function UTF8CharacterLength(p: PChar): integer;
+procedure UTF8Delete(var S: string; Index, Size: integer);
+var
+ ls: integer;
+ b: string;
+ e: string;
begin
- if p <> nil then
- begin
- if ord(p^) < %11000000 then
- begin
- // regular single byte character (#0 is a character, this is pascal ;)
- Result:=1;
- end
- else if ((ord(p^) and %11100000) = %11000000) then
- begin
- // could be 2 byte character
- if (ord(p[1]) and %11000000) = %10000000 then
- Result := 2
- else
- Result := 1;
- end
- else if ((ord(p^) and %11110000) = %11100000) then
- begin
- // could be 3 byte character
- if ((ord(p[1]) and %11000000) = %10000000)
- and ((ord(p[2]) and %11000000) = %10000000) then
- Result:=3
- else
- Result:=1;
- end
- else if ((ord(p^) and %11111000) = %11110000) then
- begin
- // could be 4 byte character
- if ((ord(p[1]) and %11000000) = %10000000)
- and ((ord(p[2]) and %11000000) = %10000000)
- and ((ord(p[3]) and %11000000) = %10000000) then
- Result:=4
- else
- Result:=1;
- end
- else
- Result:=1;
- end
- else
- Result:=0;
+ ls := UTF8Length(S);
+ if (Index > ls) or (Index <= 0) or (Size <= 0) then
+ Exit; //==>
+ b := UTF8Copy(S, 1, Index-1); // beginning string
+ e := UTF8Copy(S, Index+Size, UTF8Length(S)-(Index+Size-1)); // ending string
+ S := b + e;
end;
procedure UTF8Insert(const Source: string; var S: string; Index: integer);
@@ -245,19 +268,31 @@ begin
end;
end;
-procedure UTF8Delete(var S: string; Index, Size: integer);
-var
- ls: integer;
- b: string;
- e: string;
+function Copy8(const s: string; StartCharIndex, CharCount: integer): string;
begin
- ls := UTF8Length(S);
- if (Index > ls) or (Index <= 0) or (Size <= 0) then
- Exit; //==>
- b := UTF8Copy(S, 1, Index-1); // beginning string
- e := UTF8Copy(S, Index+Size, UTF8Length(S)-(Index+Size-1)); // ending string
- S := b + e;
+ Result := UTF8Copy(s, StartCharIndex, CharCount);
+end;
+
+function Length8(const s: string): integer;
+begin
+ Result := UTF8Length(s);
end;
+function Pos8(const SearchForText, SearchInText: string): integer;
+begin
+ Result := UTF8Pos(SearchForText, SearchInText);
+end;
+
+procedure Delete8(var S: string; Index, Size: integer);
+begin
+ UTF8Delete(S, Index, Size);
+end;
+
+procedure Insert8(const Source: string; var S: string; Index: integer);
+begin
+ UTF8Insert(Source, S, Index);
+end;
+
+
end.