Sign in Join
Wiki copytemporary

UTF8 string class/en: what changed

Revision 1 to 2. Removed lines have a minus sign; added lines have a plus sign.

OldNewLine
11## What is TbUtf8?
22
33With the library TbUtf8 you can easily change UTF8 Strings.
44
55### Problem
66
77With Lazarus (Free Pascal) the string UTF8 encoded. However, the "String" type is nothing more than a dynamic byte array. Length returns the number of bytes in the array but not the number of characters. With UTF8, a character can be 4 bytes long and even 7 bytes with combined characters. An example should illustrate this. 'Thomas' 6 characters, 6 bytes in size. 'Thömäs' 6 characters, 8 bytes in size.
88
99### Solution
1010
1111With TbUtf8 you can now easily change and search UTF8 strings with special and combined characters, such as "üäößẶặǺǻǼǽǞǟǍǎḂḃÞþÇçĆćĊċ...". Essentially, the library consists of a UTF8 string class (TIbUtf8).
1212
1313### Benefits
1414
1515- TIbUtf8 is a class type of TInterfacedObject and does not need to be cleaned up with free.
1616- All indexes are character based.
1717- All returned characters are of type String.
1818- Returns the number of characters in the string.
1919- Returns the number of bytes in the string.
2020- Delete characters or character groups.
2121- Insertion of characters and character groups.
2222- Appending characters and character groups.
2323- Reading / writing of characters and character groups.
2424- Read from file / write to a file.
2525- Read from stream / write to a stream.
2626
2727### Disadvantage
2828
2929- Since UTF8 does not have a constant offset from character to character, searching for characters is much more complex. Iterating over the characters is about 20 times slower than with the string. (Comfort has its price)
3030- Slightly more memory is required.
3131
3232### Example
3333
3434``` pascal
3535proceudre Demo01: Boolean;
3636var
3737 u: IbUtf8;
3838 i: Integer;
3939begin
4040 u:= TIbUtf8.Create('Thömäß');
4141 for i:= 1 to u.NumberOfChars do begin
4242 case u.Chars[i] of
4343 'ö': u.Chars[i]:= 'o';
4444 'ä': u.Chars[i]:= 'a';
4545 'ß': u.Chars[i]:= 's';
4646 end;
4747 end;
4848 if u.Text = 'Thomas' then begin
4949 WriteLn('That's right!');
5050 end;
5151end;
5252```
5353
5454## DownLoad
5555
5656- GitLab FpTuxe/TbUtf8 repository
5757
58-[`FpTuxe/TbUtf8`](https://gitlab.com/FpTuxe/tbutf8)
58+```text
59+FpTuxe/TbUtf8
60+```
5961
6062- Git clone
6163
6264``` bash
6365git clone https://gitlab.com/FpTuxe/tbutf8.git
6466```
6567
6668## Installation
6769
6870Variant 1
6971Start Lazarus and open your project.
7072
7173Lazarus-\>File-\>Open your workspace/tbutf8/src/tb_utf8.pas
7274
7375Lazarus-\>Project-\>Add Editor File to Project
7476
7577Variant 2
7678Start Lazarus and open your project.
7779
7880Lazarus-\>Package-\>Open Package File (.lpk) your workspace/tbutf8/src/tbutf8.lpk
7981
8082Now, click Use-\>Add to Project
8183
8284Close then Package window.
8385
8486## Functional Description
8587
8688The functional description, you can found under the project folder "doc/".
8789
8890---
8991*From the Free Pascal wiki, page "UTF8 string class/en", as it stood on 2022-01-23 after a change by FpTuxe, out of the wiki's dump of 2024-05-26. Copyright stays with the author who wrote it, according to the old wiki's [[Licensing]] page. If you have concerns about this material, [message the admin](/wiki-contact).*
9092Category: Unicode

Back to history