TbUtf8: what changed
Revision 0 to 1. Removed lines have a minus sign; added lines have a plus sign.
Title: TbUtf8
| Old | New | Line | |
|---|---|---|---|
| 1 | + | ## About | |
| 2 | + | ||
| 3 | + | **TbUtf8** is a library to easily change UTF-8 encoded strings. | |
| 4 | + | ||
| 5 | + | ### Problem | |
| 6 | + | ||
| 7 | + | With 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. | |
| 8 | + | ||
| 9 | + | ### Solution | |
| 10 | + | ||
| 11 | + | With 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). | |
| 12 | + | ||
| 13 | + | ### Benefits | |
| 14 | + | ||
| 15 | + | - TIbUtf8 is a class type of TInterfacedObject and does not need to be cleaned up with free. | |
| 16 | + | - All indexes are character based. | |
| 17 | + | - All returned characters are of type String. | |
| 18 | + | - Returns the number of characters in the string. | |
| 19 | + | - Returns the number of bytes in the string. | |
| 20 | + | - Delete characters or character groups. | |
| 21 | + | - Insertion of characters and character groups. | |
| 22 | + | - Appending characters and character groups. | |
| 23 | + | - Reading / writing of characters and character groups. | |
| 24 | + | - Read from file / write to a file. | |
| 25 | + | - Read from stream / write to a stream. | |
| 26 | + | ||
| 27 | + | ### Disadvantage | |
| 28 | + | ||
| 29 | + | - 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) | |
| 30 | + | - Slightly more memory is required. | |
| 31 | + | ||
| 32 | + | ### Example | |
| 33 | + | ||
| 34 | + | ``` pascal | |
| 35 | + | var | |
| 36 | + | u: IbUtf8; | |
| 37 | + | i: Integer; | |
| 38 | + | begin | |
| 39 | + | u:= TIbUtf8.Create('Thömäß'); | |
| 40 | + | for i:= 1 to u.NumberOfChars do begin | |
| 41 | + | case u.Chars[i] of | |
| 42 | + | 'ö': u.Chars[i]:= 'o'; | |
| 43 | + | 'ä': u.Chars[i]:= 'a'; | |
| 44 | + | 'ß': u.Chars[i]:= 's'; | |
| 45 | + | end; | |
| 46 | + | end; | |
| 47 | + | if u.Text = 'Thomas' then begin | |
| 48 | + | WriteLn('That''s right!'); | |
| 49 | + | end; | |
| 50 | + | end. | |
| 51 | + | ``` | |
| 52 | + | ||
| 53 | + | ## Download | |
| 54 | + | ||
| 55 | + | - GitLab repository: [FpTuxe/TbUtf8](https://gitlab.com/FpTuxe/tbutf8) | |
| 56 | + | - Git clone: | |
| 57 | + | ||
| 58 | + | ``` bash | |
| 59 | + | git clone https://gitlab.com/FpTuxe/tbutf8.git | |
| 60 | + | ``` | |
| 61 | + | ||
| 62 | + | ## Installation | |
| 63 | + | ||
| 64 | + | Variant 1 | |
| 65 | + | Start Lazarus and open your project. | |
| 66 | + | ||
| 67 | + | Lazarus-\>File-\>Open your workspace/tbutf8/src/tb_utf8.pas | |
| 68 | + | ||
| 69 | + | Lazarus-\>Project-\>Add Editor File to Project | |
| 70 | + | ||
| 71 | + | Variant 2 | |
| 72 | + | Start Lazarus and open your project. | |
| 73 | + | ||
| 74 | + | Lazarus-\>Package-\>Open Package File (.lpk) your workspace/tbutf8/src/tbutf8.lpk | |
| 75 | + | ||
| 76 | + | Now, click Use-\>Add to Project | |
| 77 | + | ||
| 78 | + | Close then Package window. | |
| 79 | + | ||
| 80 | + | ## Functional Description | |
| 81 | + | ||
| 82 | + | The functional description, you can found under the project folder "doc/". | |
| 83 | + | ||
| 84 | + | --- | |
| 85 | + | *From the Free Pascal wiki, page "TbUtf8", as it stood on 2022-04-15 after a change by Alextpp, 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).* |
