Sign in Join
Wiki copytemporary

Character and string types/ja: what changed

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

Title: Character and string types/ja

OldNewLine
1+Free Pascal は、各種の **[[Char/ja|文字]] と [[String/ja|文字列]]** を扱えます。それらは ANSIキャラクタ から ユニコード文字列、また 各種ポインタも含みます。違いは、encodings と reference counting にも影響します。
2+
3+## AnsiChar
4+
5+A variable of type **AnsiChar** , also referred to as **[[Char|char]]** , is exactly 1 byte in size, and contains one ANSI character.
6+
7+a
8+---
9+
10+#### Reference
11+
12+ * [FPC AnsiChar documentation](<http://www.freepascal.org/docs-html/ref/refsu7.html>)
13+ * [[Char|Usage Char]]
14+
15+## WideChar
16+
17+A variable of type **WideChar** , also referred to as **UnicodeChar** , is exactly 2 bytes in size, and usually contains one [[LCL Unicode Support|Unicode]] code point (normally a character) in UTF-16 encoding. Note: it is impossible to encode all Unicode code points in 2 bytes. Therefore, 2 WideChars may be needed to encode a single code point.
18+
19+| a
20+---|---
21+
22+#### References
23+
24+ * [FPC WideChar documentation](<http://www.freepascal.org/docs-html/ref/refsu8.html>)
25+ * [UTF-16 information on Wikipedia](<https://en.wikipedia.org/wiki/UTF-16>)
26+ * [RTL UnicodeChar documentation](<http://lazarus-ccr.sourceforge.net/docs/rtl/system/unicodechar.html> "doc:rtl/system/unicodechar.html")
27+
28+## Array of Char
29+
30+Early Pascal implementations that were in use before 1978 did not support a string type (with the exception of string constants). The only possibility to store strings in variables was the use of arrays of char. This approach has many disadvantages and is no longer recommended. It is, however, still supported to ensure backward-compatibility with ancient code.
31+
32+### Static Array of Char
33+```
34+type
35+ TOldString4 = array[0..3] of char;
36+ var
37+ aOldString4: TOldString4;
38+ begin
39+ aOldString4[0] := 'a';
40+ aOldString4[1] := 'b';
41+ aOldString4[2] := 'c';
42+ aOldString4[3] := 'd';
43+ end;
44+```
45+
46+The static array of char has now the content:
47+
48+a | b | c | d
49+---|---|---|---
50+
51+[![Note-icon.png](/wiki-images/b/be/Note-icon.png)](</File:Note-icon.png>)
52+
53+**Note:** Unassigned chars can have any content, depending on what was just in memory when the memory for the array was made available.
54+
55+### Dynamic Array of Char
56+```
57+var
58+ aOldString: Array of Char;
59+ begin
60+ SetLength(aOldString, 5);
61+ aOldString[0] := 'a';
62+ aOldString[1] := 'b';
63+ aOldString[2] := 'c';
64+ aOldString[3] := 'd';
65+ end;
66+```
67+
68+The dynamic array of char has now the content:
69+
70+a | b | c | d | #0
71+---|---|---|---|---
72+
73+[![Note-icon.png](/wiki-images/b/be/Note-icon.png)](</File:Note-icon.png>)
74+
75+**Note:** Unassigned chars in dynamic arrays have a content #0, cause empty positions of all dynamic arrays are initially initialised with 0 (or #0, or nil, or ...)
76+
77+## PChar
78+
79+A variable of type **[[PChar]]** is basically a pointer to a **Char** type, but allows additional operations. PChars can be used to access C-style [null-terminated strings](<http://en.wikipedia.org/wiki/Null-terminated_string>), e.g. in interaction with certain OS libraries or third-party software.
80+
81+a | b | c | #0
82+---|---|---|---
83+^
84+
85+#### Reference
86+
87+ * [FPC PChar documentation](<http://www.freepascal.org/docs-html/ref/refsu13.html>)
88+ * [PChar related functions](<http://lazarus-ccr.sourceforge.net/docs/rtl/sysutils/pcharfunctions.html> "doc:rtl/sysutils/pcharfunctions.html")
89+
90+## PWideChar
91+
92+A variable of type **PWideChar** is a pointer to a WideChar variable.
93+
94+| a | | b | | c | #0 | #0
95+---|---|---|---|---|---|---|---
96+^
97+
98+#### Reference
99+
100+ * [RTL PWideChar documentation](<http://lazarus-ccr.sourceforge.net/docs/rtl/system/pwidechar.html> "doc:rtl/system/pwidechar.html")
101+
102+## String
103+
104+The type **String** may refer to **ShortString** or **AnsiString** , depending from the [{$H} switch](<http://www.freepascal.org/docs-html/prog/progsu25.html#x32-310001.2.25>). If the switch is off ({$H-}) then any string declaration will define a **ShortString**. It size will be 255 chars, if not otherwise specified. If it is on ({$H+}) **string** without length specifier will define an **AnsiString** , otherwise a **ShortString** with specified length. In **mode delphiunicode'**_String_ is **UnicodeString**.
105+
106+#### Reference
107+
108+ * [[String|Usage String]]
109+ * [String functions](<http://lazarus-ccr.sourceforge.net/docs/rtl/sysutils/stringfunctions.html> "doc:rtl/sysutils/stringfunctions.html")
110+ * [Reference for unit 'strutils': Procedures and functions](<http://lazarus-ccr.sourceforge.net/docs/rtl/strutils/index-5.html> "doc:rtl/strutils/index-5.html")
111+
112+## ShortString
113+
114+Short strings have a maximum length of 255 characters with the implicit [[FPC Unicode support|codepage]] CP_ACP. The length is stored in the character at index 0.
115+
116+#3 | a | b | c
117+---|---|---|---
118+
119+#### Reference
120+
121+ * [FPC AnsiString documentation](<http://www.freepascal.org/docs-html/ref/refsu10.html>)
122+
123+## AnsiString
124+
125+Ansistrings are strings that have no length limit. They are [reference counted](<http://en.wikipedia.org/wiki/Reference_counting>) and are guaranteed to be [null terminated](<http://en.wikipedia.org/wiki/Null-terminated_string>). Internally, a variable of type **AnsiString** is treated as a pointer: the actual content of the string is stored on the heap, as much memory as needed to store the string content is allocated.
126+
127+| | | | | | | | a | b | c | #0
128+---|---|---|---|---|---|---|---|---|---|---|---
129+RefCount | Length
130+
131+#### Reference
132+
133+ * [FPC AnsiString documentation](<http://www.freepascal.org/docs-html/ref/refsu10.html>)
134+
135+## UnicodeString
136+
137+Like **AnsiStrings** , **UnicodeStrings** are reference counted, null-terminated arrays, but they are implemented as arrays of **WideChars** instead of regular **Chars**.
138+
139+[![Note-icon.png](/wiki-images/b/be/Note-icon.png)](</File:Note-icon.png>)
140+
141+**Note:** The UnicodeString naming is a bit ambiguous but probably due to its use in Delphi on Windows, where the OS uses UTF16 encoding; it's not the only string type that can hold Unicode string data (see also UTF8String)...
142+
143+| | | | | | | | | a | | b | | c | #0 | #0
144+---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---
145+RefCount | Length
146+
147+#### Reference
148+
149+ * [FPC UnicodeString documentation](<http://www.freepascal.org/docs-html/ref/refsu11.html>)
150+
151+## UTF8String
152+
153+In FPC 2.6.5 and below the type **UTF8String** was an alias to the type **AnsiString**. In FPC 2.7.1 and above it is defined as
154+```
155+UTF8String = type AnsiString(CP_UTF8);
156+```
157+
158+It is meant to contain UTF-8 encoded strings (i.e. unicode data) ranging from 1..4 bytes per character. Note that **String** can also contain UTF-8 encoded characters.
159+
160+#### Reference
161+
162+ * [FPC UTF8String documentation](<http://www.freepascal.org/docs-html/ref/refsu10.html#x33-410003.2.4>)
163+
164+## UTF16String
165+
166+The type **UTF16String** is an alias to the type **WideString**. In the LCL unit _lclproc_ it is an alias to **UnicodeString**.
167+
168+#### Reference
169+
170+ * [LCL UTF16String documentation](<http://lazarus-ccr.sourceforge.net/docs/lcl/lclproc/utf16string.html> "doc:lcl/lclproc/utf16string.html")
171+
172+## WideString
173+
174+Variables of type **[[Widestrings|WideString]]** (used to represent unicode character strings in COM applications) resemble those of type **UnicodeString** , but unlike them they are not reference-counted. On Windows they are allocated with a special windows function which allows them to be used for OLE automation.
175+
176+WideStrings consist of COM compatible UTF16 encoded bytes on Windows machines (UCS2 on Windows 2000), and they are encoded as plain UTF16 on Linux, Mac OS X and iOS.
177+
178+| | | | | a | | b | | c | #0 | #0
179+---|---|---|---|---|---|---|---|---|---|---|---
180+Length
181+
182+#### Reference
183+
184+ * [FPC WideString documentation](<http://www.freepascal.org/docs-html/ref/refsu11.html#x34-440003.2.5>)
185+
186+## PShortString
187+
188+A variable of type **PShortString** is a pointer that points to the first byte of a **ShortString** -type variable (which defines the length of the ShortString).
189+
190+#3 | a | b | c
191+---|---|---|---
192+^
193+
194+#### Reference
195+
196+ * [RTL PShortString documentation](<http://lazarus-ccr.sourceforge.net/docs/rtl/system/pshortstring.html> "doc:rtl/system/pshortstring.html")
197+
198+## PAnsiString
199+
200+Variables of type **PAnsiString** are pointers to **AnsiString** -type variables. However, unlike **PShortString** -type variables they don't point to the first byte of the header, but to the first **char** of the **AnsiString**.
201+
202+| | | | | | | | a | b | c | #0
203+---|---|---|---|---|---|---|---|---|---|---|---
204+RefCount | Length | ^
205+
206+#### Reference
207+
208+ * [RTL PAnsiString documentation](<http://lazarus-ccr.sourceforge.net/docs/rtl/system/pansistring.html> "doc:rtl/system/pansistring.html")
209+
210+## PUnicodeString
211+
212+Variables of type **PUnicodeString** are pointers to variables of type **UnicodeString**.
213+
214+| | | | | | | | | a | | b | | c | #0 | #0
215+---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---
216+RefCount | Length | ^
217+
218+#### Reference
219+
220+ * [RTL PUnicodeString documentation](<http://lazarus-ccr.sourceforge.net/docs/rtl/system/punicodestring.html> "doc:rtl/system/punicodestring.html")
221+
222+## PWideString
223+
224+Variables of type **PWideString** are pointers. They point to the first char of a **WideString** -typed variable.
225+
226+| | | | | a | | b | | c | #0 | #0
227+---|---|---|---|---|---|---|---|---|---|---|---
228+Length | ^
229+
230+#### Reference
231+
232+ * [RTL PWideString documentation](<http://lazarus-ccr.sourceforge.net/docs/rtl/system/pwidestring.html> "doc:rtl/system/pwidestring.html")
233+
234+## String constants
235+
236+If you use only English constants your strings work the same with all types, on all platforms and all compiler versions. Non English strings can be loaded via resourcestrings or from files. If you want to use non English strings in code then you should read further.
237+
238+There are various encodings for non English strings. By default Lazarus saves Pascal files as **UTF-8 without BOM**. UTF-8 supports the full Unicode range. That means all string constants are stored in UTF-8. Lazarus also supports to change the encoding of a file to other encoding, for example under Windows your local codepage. The Windows codepage is limited to your current language group.
239+
240+String Type, UTF-8 Source | With or without {$codepage utf8} | FPC 2.6.5 and below | FPC 2.7.1 and above | FPC 2.7.1+ with UTF8 as default CodePage
241+---|---|---|---|---
242+AnAnsiString:='ãü'; | Without | Needs UTF8ToAnsi in RTL/WinAPI. Ok in LCL | Needs UTF8ToAnsi in RTL/WinAPI. Ok in LCL | Ok in RTL/W-WinAPI/LCL. Needs UTF8ToWinCP in A-WinAPI.
243+AnAnsiString:='ãü'; | With | System cp ok in RTL/WinAPI. Needs SysToUTF8 in LCL | Ok in RTL/WinAPI/LCL. Mixing with other strings converts to system cp | Ok in RTL/W-WinAPI/LCL. Needs UTF8ToWinCP in A-WinAPI
244+AnUnicodeString:='ãü'; | Without | Wrong everywhere | Wrong everywhere | Wrong everywhere
245+AnUnicodeString:='ãü'; | With | System cp ok in RTL/WinAPI. Needs UTF8Encode in LCL | Ok in RTL/WinAPI/LCL. Mixing with other strings converts to system cp | Ok in RTL/W-WinAPI/LCL. Needs UTF8ToWinCP in A-WinAPI
246+AnUTF8String:='ãü'; | Without | Same as AnsiString | Wrong everywhere | Wrong everywhere
247+AnUTF8String:='ãü'; | With | Same as AnsiString | Ok in RTL/WinAPI/LCL. Mixing with other strings converts to system cp | Ok in RTL/W-WinAPI/LCL. Needs UTF8ToWinCP in A-WinAPI.
248+
249+ * W-WinAPI = Windows API "W" functions, UTF-16
250+ * A-WinAPI = Windows API non "W" functions, 8bit system code page
251+ * System CP = The 8bit system code page of the OS. For example code page [1252](<http://en.wikipedia.org/wiki/Windows-1252>).
252+
253+```
254+const
255+ c='ãü';
256+ cstring: string = 'ãü'; // see AnAnsiString:='ãü';
257+ var
258+ s: string;
259+ u: UnicodeString;
260+ begin
261+ s:=c; // same as s:='ãü';
262+ s:=cstring; // does not change encoding
263+ u:=c; // same as u:='ãü';
264+ u:=cstring; // fpc 2.6.1: converts from system cp to UTF-16, fpc 2.7.1+: depends on encoding of cstring
265+ end;
266+```
267+
268+## See also
269+
270+ * [[FPC Unicode support]]
271+ * [[LCL Unicode Support]]
272+ * [[TStringList-TStrings Tutorial]]
273+ * [A Brief History of Strings](<http://web.archive.org/web/20151125114234/http://www.codexterity.com/delphistrings.htm>)
274+
275+---
276+*From the Free Pascal wiki, page "Character and string types/ja", as the Internet Archive kept it on 2021-05-13, last changed on 2020-02-11. 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).*

Back to history