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