A
WString is a fixed-size array of wide-chars that never overflows if the size is known at compile-time. It has no descriptor, and does never resize unless it's a pointer and
Allocate/
Reallocate/
Deallocate are used directly. When the variable has a fixed
size (numeric constant, or expression that can be evaluated at compile time), FreeBASIC avoids any overflow that could occur on assignment, by truncating the contents to a length of
size - 1.
The end of the string is marked by the character 0 automatically added by the FreeBASIC string handling functions, so that character must never be part of a
WString or the content will be truncated. The character 0 will be appended when the string is created, and the length will be calculated by scanning the string for the first null character.
In a
WString,
Len returns the size of the contained string and
SizeOf returns the space allocated to the
WString.
SizeOf only works if the size is known by the compiler, i.e. a fixed-size
WString variable is passed directly, not as a dereferenced pointer or a
ByRef function argument.
This type is provided for support non-Latin based alphabets. Any intrinsic string function like
Left will work with
WStrings too, as will any string operator.
When processing source files, FreeBASIC can parse ASCII files with Unicode escape sequences (\u), or UTF-8, UTF-16LE, UTF-16BE, UTF-32LE and UTF-32BE files, as long as they were saved with Byte Order Mark (BOM).
Note: The most reliable cross-platform code is obtained by encoding without BOM in ASCII/UTF-8 characters.
The FreeBASIC text file functions can read and write Unicode files in different encodings, provided the
Encoding is specified when the file is opened. The text is automatically converted to the internal encoding at read and converted back to the file encoding at write.
SizeOf(
WString ) returns the number of bytes used by a
WString character in the current platform.
When allocating dynamic memory for a
WString, the safest is to use
CAllocate (or at worst, to use
Allocate followed by an immediate assignment of the string data, as in the second example), in order to avoid creating string data without any null character (the terminal character for a
WString).
Note : When any operand of a binary operator (as assignment, equal, +, *, ...) consists in dereferencing a '
Wstring Ptr' pointer ('
pw'), this can give a '
Wstring' string or a '
Numeric' variable, depending on the other operand. If the other operand is numeric, so the dereferenced '
Wstring Ptr' pointer ('
*pw') will be treated as a '
Numeric' reference to the one character pointed. If a '
Wstring' pointer indexing '
[]' operator is used as dereferencing syntax ('
pw[n]'), it is basically a short-cut version of the '
String' indexing '
[]' operator ('
(*pw)[n]').