Debian Codegraph @

src/doc/manual/standards/strings.txi

raw
1 @node Strings
2 @section Strings
3
4 @menu
5 * Strings - String types & Unicode::
6 * Strings - C reference::
7 @end menu
8
9 @node Strings - String types & Unicode
10 @subsection String types & Unicode
11 The ECL implementation of strings is ANSI Common-Lisp compliant. There are basically four string types as shown in @ref{tab:cl-str-types}. As explained in @ref{Characters}, when Unicode support is disabled, @code{character} and @code{base-character} are the same type and the last two string types are equivalent to the first two.
12
13 @float Table, tab:cl-str-types
14 @caption{Common Lisp string types}
15 @multitable @columnfractions .25 .25 .5
16 @headitem Abbreviation @tab Expanded type @tab Remarks
17 @item string @tab (array character (*)) @tab 8 or 32 bits per character, adjustable.
18 @item simple-string @tab (simple-array character (*)) @tab 8 or 32 bits per character, not adjustable nor displaced.
19 @item base-string @tab (array base-char (*)) @tab 8 bits per character, adjustable.
20 @item simple-base-string @tab (simple-array base-char (*)) @tab 8 bits per character, not adjustable nor displaced.
21 @end multitable
22 @end float
23
24 It is important to remember that strings with unicode characters can only be printed readably when the external format supports those characters. If this is not the case, ECL will signal a @code{serious-condition}. This condition will abort your program if not properly handled.
25
26 @node Strings - C reference
27 @subsection C reference
28 @subsubsection Base string constructors
29 @cppdef ecl_alloc_adjustable_base_string
30 @cppdef ecl_alloc_simple_base_string
31 @cppdef ecl_make_simple_base_string
32 @cppdef ecl_make_constant_base_string
33
34 Building strings of C data
35
36 @subsubheading Functions
37
38 @deftypefun cl_object ecl_alloc_adjustable_base_string (cl_index length);
39 @deftypefunx cl_object ecl_alloc_simple_base_string (cl_index length);
40 @deftypefunx cl_object ecl_make_simple_base_string (const char* data, cl_fixnum length);
41 @deftypefunx cl_object ecl_make_constant_base_string (const char* data, cl_fixnum length);
42
43 @paragraph Description
44 These are different ways to create a base string, which is a string that holds a small subset of characters, the @code{base-char}, with codes ranging from 0 to 255.
45
46 @coderef{ecl_alloc_simple_base_string} creates an empty string with that much space for characters and a fixed length. The string does not have a fill pointer and cannot be resized, and the initial data is unspecified
47
48 @coderef{ecl_alloc_adjustable_base_string} is similar to the previous function, but creates an adjustable string with a fill pointer. This means that the length of the string can be changed and the string itself can be resized to accommodate more data.
49
50 The other constructors create strings but use some preexisting data. @coderef{ecl_make_simple_base_string} creates a string copying the data that the user supplies, and using freshly allocated memory. @coderef{ecl_make_constant_base_string} on the other hand, does not allocate memory, but simply uses the supplied pointer as buffer for the string. This last function should be used with care, ensuring that the supplied buffer is not deallocated. If the @var{length} argument of these functions is -1, the length is determined by @code{strlen}.
51 @end deftypefun
52
53 @subsubsection String accessors
54 @cppdef ecl_char
55 @cppdef ecl_char_set
56 Reading and writing characters into a string
57
58 @subsubheading Functions
59 @deftypefun ecl_character ecl_char (cl_object string, cl_index index);
60 @deftypefunx ecl_character ecl_char_set (cl_object string, cl_index index, ecl_character c);
61
62 @paragraph Description
63 Access to string information should be done using these two functions. The first one implements the equivalent of the @code{char} function from Common Lisp, returning the character that is at position @var{index} in the string @var{string}.
64
65 The counterpart of the previous function is @coderef{ecl_char_set}, which implements @code{(setf char)} and stores character @var{c} at the position @var{index} in the given string.
66
67 Both functions check the type of their arguments and verify that the indices do not exceed the string boundaries. Otherwise they signal a @code{serious-condition}.
68 @end deftypefun
69
70 @subsubsection Converting Unicode strings
71 Converting between different encodings. See @ref{Streams - External formats} for a list of supported encodings (external formats).
72
73 @subsubheading Functions
74 @cppdef si_octets_to_string
75 @lspdef ext:octets-to-string
76 @defun ext:octets-to-string octets &key (external-format :default) (start 0) (end nil)
77 Decode a sequence of octets (i.e. 8-bit bytes) into a string according
78 to the given external format. @var{octets} must be a vector whose
79 elements have a size of 8-bit. The bounding index designators
80 @var{start} and @var{end} optionally denote a subsequence to be decoded.
81 Signals an @coderef{ext:character-decoding-error} if the decoding fails.
82 @end defun
83
84 @cppdef si_string_to_octets
85 @lspdef ext:string-to-octets
86 @defun ext:string-to-octets string &key (external-format :default) (start 0) (end nil) (null-terminate nil)
87 Encode a string into a sequence of octets according to the given
88 external format. The bounding index designators @var{start} and
89 @var{end} optionally denote a subsequence to be encoded. If
90 @var{null-terminate} is true, add a terminating null byte. Signals an
91 @coderef{ext:character-encoding-error} if the encoding fails.
92 @end defun
93
94 @cppdef ecl_decode_from_cstring
95 @deftypefun cl_object ecl_decode_from_cstring (const char *string, cl_fixnum length, cl_object external_format)
96 Decode a C string of the given length into a Lisp string using the
97 specified external format. If @var{length} is -1, the length is
98 determined by @code{strlen}. Returns @code{NULL} if the decoding fails.
99 @end deftypefun
100
101 @cppdef ecl_encode_to_cstring
102 @deftypefun cl_fixnum ecl_encode_to_cstring (char *output, cl_fixnum output_length, cl_object input, cl_object external_format)
103 Encode the Lisp string @var{input} into a C string of the given length
104 using the specified external format. Returns the number of characters
105 necessary to encode the Lisp string (including the null terminator). If
106 this is larger than @var{output_length}, @var{output} is unchanged.
107 Returns -1 if the encoding fails.
108 @end deftypefun
109
110 @cppdef ecl_decode_from_unicode_wstring
111 @cppdef ecl_encode_to_unicode_wstring
112 @deftypefun cl_object ecl_decode_from_unicode_wstring (const wchar_t *string, cl_fixnum length)
113 @deftypefunx cl_fixnum ecl_encode_to_unicode_wstring (wchar_t *output, cl_fixnum output_length, cl_object input)
114 These functions work the same as @coderef{ecl_decode_from_cstring},
115 @coderef{ecl_encode_to_cstring}, except that the external format used is
116 either utf-8, utf-16 or utf-32 depending on whether
117 @code{sizeof(wchar_t)} is 1, 2, or 4 respectively.
118 @end deftypefun
119
120 @subsubsection ANSI dictionary
121 Common Lisp and C equivalence
122
123 @multitable @columnfractions .22 .78
124 @headitem Lisp symbol @tab C function
125 @item @clhs{f_smp-st.htm,simple-string-p} @tab cl_object cl_simple_string_p(cl_object string)
126 @item @clhs{f_char_.htm,char} @tab cl_object cl_char(cl_object string, cl_object index)
127 @item @clhs{f_char_.htm,(setf char)} @tab cl_object si_char_set(cl_object string, cl_object index, cl_object char)
128 @item @clhs{f_char_.htm,schar} @tab cl_object cl_schar(cl_object string, cl_object index)
129 @item @clhs{f_char_.htm,(setf schar)} @tab cl_object si_char_set(cl_object string, cl_object index, cl_object char)
130 @item @clhs{f_string.htm,string} @tab cl_object cl_string(cl_object x)
131 @item @clhs{f_stg_up.htm,string-upcase} @tab cl_object cl_string_upcase(cl_narg narg, cl_obejct string, ...)
132 @item @clhs{f_stg_up.htm,string-downcase} @tab cl_object cl_string_downcase(cl_narg narg, cl_obejct string, ...)
133 @item @clhs{f_stg_up.htm,string-capitalize} @tab cl_object cl_string_capitalize(cl_narg narg, cl_obejct string, ...)
134 @item @clhs{f_stg_up.htm,nstring-upcase} @tab cl_object cl_nstring_upcase(cl_narg narg, cl_obejct string, ...)
135 @item @clhs{f_stg_up.htm,nstring-downcase} @tab cl_object cl_nstring_downcase(cl_narg narg, cl_obejct string, ...)
136 @item @clhs{f_stg_up.htm,nstring-capitalize} @tab cl_object cl_nstring_capitalize(cl_narg narg, cl_obejct string, ...)
137 @item @clhs{f_stg_tr.htm,string-trim} @tab cl_object cl_string_trim(cl_object character_bag, cl_object string)
138 @item @clhs{f_stg_tr.htm,string-left-trim} @tab cl_object cl_string_left_trim(cl_object character_bag, cl_object string)
139 @item @clhs{f_stg_tr.htm,string-right-trim} @tab cl_object cl_string_right_trim(cl_object character_bag, cl_object string)
140 @item @clhs{f_string.htm,string} @tab cl_object cl_string(cl_object x)
141 @item @clhs{f_stgeq_.htm,string=} @tab cl_object cl_stringE(cl_narg narg, cl_object string1, cl_object string2, ...)
142 @item @clhs{f_stgeq_.htm,string/=} @tab cl_object cl_stringNE(cl_narg narg, cl_object string1, cl_object string2, ...)
143 @item @clhs{f_stgeq_.htm,string<} @tab cl_object cl_stringL(cl_narg narg, cl_object string1, cl_object string2, ...)
144 @item @clhs{f_stgeq_.htm,string>} @tab cl_object cl_stringG(cl_narg narg, cl_object string1, cl_object string2, ...)
145 @item @clhs{f_stgeq_.htm,string<=} @tab cl_object cl_stringLE(cl_narg narg, cl_object string1, cl_object string2, ...)
146 @item @clhs{f_stgeq_.htm,string>=} @tab cl_object cl_stringGE(cl_narg narg, cl_object string1, cl_object string2, ...)
147 @item @clhs{f_stgeq_.htm,string-equal} @tab cl_object cl_string_equal(cl_narg narg, cl_object string1, cl_object string2, ...)
148 @item @clhs{f_stgeq_.htm,string-not-equal} @tab cl_object cl_string_not_equal(cl_narg narg, cl_object string1, cl_object string2, ...)
149 @item @clhs{f_stgeq_.htm,string-lessp} @tab cl_object cl_string_lessp(cl_narg narg, cl_object string1, cl_object string2, ...)
150 @item @clhs{f_stgeq_.htm,string-greaterp} @tab cl_object cl_string_greaterp(cl_narg narg, cl_object string1, cl_object string2, ...)
151 @item @clhs{f_stgeq_.htm,string-not-greaterp} @tab cl_object cl_string_not_greaterp(cl_narg narg, cl_object string1, cl_object string2, ...)
152 @item @clhs{f_stgeq_.htm,string-not-lessp} @tab cl_object cl_string_not_lessp(cl_narg narg, cl_object string1, cl_object string2, ...)
153 @item @clhs{f_stgp.htm,stringp} @tab cl_object cl_stringp(cl_object x)
154 @item @clhs{f_mk_stg.htm,make-string} @tab cl_object cl_make_string(cl_narg narg, cl_object size, ...)
155 @end multitable