AnsiString: what changed
Revision 0 to 1. Removed lines have a minus sign; added lines have a plus sign.
Title: AnsiString
| Old | New | Line | |
|---|---|---|---|
| 1 | + | **`AnsiString`** is a variable-length string [[Data type|data type]]. It can store characters that have a size of one Byte. | |
| 2 | + | ||
| 3 | + | ## implementation | |
| 4 | + | ||
| 5 | + | In [[FPC]] an `AnsiString` is implemented as a [[Pointer|pointer]]. It is a managed data type. As such it is initialized with [[Nil|nil]] as soon as it enters the scope. Memory for the character sequence is dynamically allocated and freed. | |
| 6 | + | ||
| 7 | + | An `AnsiString` points to the *first* character. This facilitates interfacing to libraries or foreign functions expecting [[PChar|pChar strings]]. For that, an `AnsiString` always concludes with a null [[Byte]]. *In [[Pascal]]*, this terminating null Byte has no significance as to the string’s value (including its length). An `AnsiString` always entails some management data *before* the first character. These are | |
| 8 | + | ||
| 9 | + | - a code page | |
| 10 | + | - the size of a character | |
| 11 | + | - a reference count | |
| 12 | + | - the length of the string. | |
| 13 | + | ||
| 14 | + | <table> | |
| 15 | + | <tbody> | |
| 16 | + | <tr> | |
| 17 | + | <td><code>253</code></td> | |
| 18 | + | <td><code>233</code></td> | |
| 19 | + | <td> <code>0</code> </td> | |
| 20 | + | <td> <code>1</code> </td> | |
| 21 | + | <td> <code>0</code> </td> | |
| 22 | + | <td> <code>0</code> </td> | |
| 23 | + | <td> <code>0</code> </td> | |
| 24 | + | <td> <code>1</code> </td> | |
| 25 | + | <td> <code>0</code> </td> | |
| 26 | + | <td> <code>0</code> </td> | |
| 27 | + | <td> <code>0</code> </td> | |
| 28 | + | <td> <code>3</code> </td> | |
| 29 | + | <td><code>'F'</code></td> | |
| 30 | + | <td><code>'o'</code></td> | |
| 31 | + | <td><code>'o'</code></td> | |
| 32 | + | <td> <code>#0</code></td> | |
| 33 | + | </tr> | |
| 34 | + | <tr> | |
| 35 | + | <td colspan="2">code page</td> | |
| 36 | + | <td colspan="2">maximum character size</td> | |
| 37 | + | <td colspan="4">reference count</td> | |
| 38 | + | <td colspan="4">length</td> | |
| 39 | + | <td colspan="3">payload</td> | |
| 40 | + | <td>complimentary Null</td> | |
| 41 | + | </tr> | |
| 42 | + | <tr> | |
| 43 | + | <td colspan="13" style="text-align: right;">pointer points here ⤴ </td> | |
| 44 | + | <td colspan="3"></td> | |
| 45 | + | </tr> | |
| 46 | + | </tbody> | |
| 47 | + | </table> | |
| 48 | + | ||
| 49 | + | Only the length field has significance in Pascal. In Pascal, an `AnsiString` may contain `#0` characters. | |
| 50 | + | ||
| 51 | + | An `AnsiString` can furthermore be associated with a code page (since [[FPC New Features 3.0.0|3.0.0]]). | |
| 52 | + | ||
| 53 | + | ## application | |
| 54 | + | ||
| 55 | + | The data type `AnsiString` can be used like any other string data type. You may [[Becomes|assign]] string literals to an `AnsiString` variable as normal. String values can be compared ([[Equal|=]]) just as usual. The entire pointer-characteristic is transparent. | |
| 56 | + | ||
| 57 | + | Characters in `AnsiString` have a 1-based index. `myAnsiString[1]` refers to the first character. | |
| 58 | + | ||
| 59 | + | > **Note:** The linear character index is only guaranteed to work for strings that have a maximum character size of `1`. That means, using an integer index for example on an UTF-8 encoded string (not exclusively containing ASCII characters) will produce erroneous results. | |
| 60 | + | ||
| 61 | + | The [`length` function](https://www.freepascal.org/docs-html/rtl/system/length.html), and for that matter also `high`, will return a string’s length by examining the length data field. | |
| 62 | + | ||
| 63 | + | Because an `AnsiString` is essentially a pointer, *copying* strings of this type is fast, since only the reference is copied and the reference count increased. Modifications may trigger a <abbr title="copy-on-write">COW</abbr>. | |
| 64 | + | ||
| 65 | + | ## caveats | |
| 66 | + | ||
| 67 | + | - The compiler directive [[$H|{$longStrings on}]] (or `{$H+}`) [[Defensive programming techniques|aliases string]] (without a specified length) to `AnsiString`. | |
| 68 | + | - `AnsiString` as a *managed* data type introduces a certain overhead. See [[Avoiding implicit try finally section]] for more explanations. | |
| 69 | + | - The [[SizeOf|sizeOf]] value of an `AnsiString` variable is merely the size of a pointer. | |
| 70 | + | - Assigning an empty string <code>*</code> to an `AnsiString` variable will in fact assign `nil` to the variable and, if the reference count hit zero, release underlying memory (if any was previously allocated at all). Empty strings are*not'' stored as described above. | |
| 71 | + | ||
| 72 | + | ## see also | |
| 73 | + | ||
| 74 | + | - [[Character and string types]] | |
| 75 | + | - [Ansistrings](https://www.freepascal.org/docs-html/current/ref/refsu9.html#x32-370003.2.4) in the reference guide | |
| 76 | + | - [Ansistrings](https://www.freepascal.org/docs-html/current/prog/progsu161.html#x205-2160008.2.7) in the programmer’s guide | |
| 77 | + | ||
| 78 | + | --- | |
| 79 | + | *From the Free Pascal wiki, page "AnsiString", as it stood on 2023-12-22 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).* | |
| 80 | + | Category: Data types |
