blob: 8be3c63f5d4ef9e72de05cf83953033f954a31ee (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
|
String and char type
--------------------
char = 1..255
i.e. with space for none (=0) in an optional type
This is not necessarilly a whole character. The "real"
characater type is uint32.
string = char#[nullterm]
This also makes it possible to compare strings with character literals safely:
s#[0] == "a" // ok because both are arrays of the same size
s#[0..+1] == "å" // ok because both are arrays of the same size
t = "A"
t#[0..+2] // what should this do? fill with zeros?
Empty string
------------
Some compilers (e.g. FreePascal) implement empty strings as null pointers.
This is probably a quite good optimization, since empty strings are common,
and are also often compared with other strings. But it requires that the
string type is a pointer and is not compatible with C.
A compromise could be to have two string types, one "C-style" null terminated
array (which you usually pass by reference), and one "FreePascal-style"
pointer.
Strings in FreePascal use reference counting, which helps reducing copying and
keeps memory usage down, but could cause problems with multi-threaded code
which would not be expected by the programmer since the sharing happens behind
the scene. AFAIK a String type looks like this:
Index Data
----- ----
-8 Reference count
-4 Length
0... String data
|