Mercurial > hg > xemacs-beta
annotate man/lispref/strings.texi @ 5917:ffb5abc8dc4e
Fix a bug in the #'equalp compiler macro.
lisp/ChangeLog addition:
2015-06-11 Aidan Kehoe <kehoea@parhasard.net>
* cl-macs.el (equalp):
Fix a bug in this compiler macro when passed constants in a
certain order.
tests/ChangeLog addition:
2015-06-11 Aidan Kehoe <kehoea@parhasard.net>
* automated/lisp-tests.el:
Add a test looking for a bug just fixed in the equalp compiler
macro.
author | Aidan Kehoe <kehoea@parhasard.net> |
---|---|
date | Thu, 11 Jun 2015 16:09:11 +0100 |
parents | e0f1dfaa821e |
children |
rev | line source |
---|---|
428 | 1 @c -*-texinfo-*- |
2 @c This is part of the XEmacs Lisp Reference Manual. | |
444 | 3 @c Copyright (C) 1990, 1991, 1992, 1993, 1994 Free Software Foundation, Inc. |
428 | 4 @c See the file lispref.texi for copying conditions. |
5 @setfilename ../../info/strings.info | |
6 @node Strings and Characters, Lists, Numbers, Top | |
7 @chapter Strings and Characters | |
8 @cindex strings | |
9 @cindex character arrays | |
10 @cindex characters | |
11 @cindex bytes | |
12 | |
13 A string in XEmacs Lisp is an array that contains an ordered sequence | |
14 of characters. Strings are used as names of symbols, buffers, and | |
15 files, to send messages to users, to hold text being copied between | |
16 buffers, and for many other purposes. Because strings are so important, | |
17 XEmacs Lisp has many functions expressly for manipulating them. XEmacs | |
18 Lisp programs use strings more often than individual characters. | |
19 | |
20 @menu | |
440 | 21 * String Basics:: Basic properties of strings and characters. |
428 | 22 * Predicates for Strings:: Testing whether an object is a string or char. |
23 * Creating Strings:: Functions to allocate new strings. | |
24 * Predicates for Characters:: Testing whether an object is a character. | |
25 * Character Codes:: Each character has an equivalent integer. | |
26 * Text Comparison:: Comparing characters or strings. | |
27 * String Conversion:: Converting characters or strings and vice versa. | |
28 * Modifying Strings:: Changing characters in a string. | |
29 * String Properties:: Additional information attached to strings. | |
30 * Formatting Strings:: @code{format}: XEmacs's analog of @code{printf}. | |
31 * Character Case:: Case conversion functions. | |
32 * Case Tables:: Customizing case conversion. | |
33 * Char Tables:: Mapping from characters to Lisp objects. | |
34 @end menu | |
35 | |
5791
9fae6227ede5
Silence texinfo 5.2 warnings, primarily by adding next, prev, and up
Jerry James <james@xemacs.org>
parents:
5384
diff
changeset
|
36 @node String Basics, Predicates for Strings, Strings and Characters, Strings and Characters |
428 | 37 @section String and Character Basics |
38 | |
39 Strings in XEmacs Lisp are arrays that contain an ordered sequence of | |
40 characters. Characters are their own primitive object type in XEmacs | |
41 20. However, in XEmacs 19, characters are represented in XEmacs Lisp as | |
42 integers; whether an integer was intended as a character or not is | |
43 determined only by how it is used. @xref{Character Type}. | |
44 | |
45 The length of a string (like any array) is fixed and independent of | |
46 the string contents, and cannot be altered. Strings in Lisp are | |
47 @emph{not} terminated by a distinguished character code. (By contrast, | |
48 strings in C are terminated by a character with @sc{ascii} code 0.) | |
49 This means that any character, including the null character (@sc{ascii} | |
50 code 0), is a valid element of a string.@refill | |
51 | |
52 Since strings are considered arrays, you can operate on them with the | |
53 general array functions. (@xref{Sequences Arrays Vectors}.) For | |
54 example, you can access or change individual characters in a string | |
55 using the functions @code{aref} and @code{aset} (@pxref{Array | |
56 Functions}). | |
57 | |
58 Strings use an efficient representation for storing the characters | |
59 in them, and thus take up much less memory than a vector of the same | |
60 length. | |
61 | |
62 Sometimes you will see strings used to hold key sequences. This | |
63 exists for backward compatibility with Emacs 18, but should @emph{not} | |
64 be used in new code, since many key chords can't be represented at | |
65 all and others (in particular meta key chords) are confused with | |
66 accented characters. | |
67 | |
68 @ignore @c Not accurate any more | |
69 Each character in a string is stored in a single byte. Therefore, | |
70 numbers not in the range 0 to 255 are truncated when stored into a | |
71 string. This means that a string takes up much less memory than a | |
72 vector of the same length. | |
73 | |
74 Sometimes key sequences are represented as strings. When a string is | |
75 a key sequence, string elements in the range 128 to 255 represent meta | |
76 characters (which are extremely large integers) rather than keyboard | |
77 events in the range 128 to 255. | |
78 | |
79 Strings cannot hold characters that have the hyper, super or alt | |
80 modifiers; they can hold @sc{ASCII} control characters, but no other | |
81 control characters. They do not distinguish case in @sc{ASCII} control | |
82 characters. @xref{Character Type}, for more information about | |
83 representation of meta and other modifiers for keyboard input | |
84 characters. | |
85 @end ignore | |
86 | |
87 Strings are useful for holding regular expressions. You can also | |
88 match regular expressions against strings (@pxref{Regexp Search}). The | |
89 functions @code{match-string} (@pxref{Simple Match Data}) and | |
90 @code{replace-match} (@pxref{Replacing Match}) are useful for | |
91 decomposing and modifying strings based on regular expression matching. | |
92 | |
93 Like a buffer, a string can contain extents in it. These extents are | |
94 created when a function such as @code{buffer-substring} is called on a | |
95 region with duplicable extents in it. When the string is inserted into | |
96 a buffer, the extents are inserted along with it. @xref{Duplicable | |
97 Extents}. | |
98 | |
99 @xref{Text}, for information about functions that display strings or | |
100 copy them into buffers. @xref{Character Type}, and @ref{String Type}, | |
101 for information about the syntax of characters and strings. | |
102 | |
5791
9fae6227ede5
Silence texinfo 5.2 warnings, primarily by adding next, prev, and up
Jerry James <james@xemacs.org>
parents:
5384
diff
changeset
|
103 @node Predicates for Strings, Creating Strings, String Basics, Strings and Characters |
428 | 104 @section The Predicates for Strings |
105 | |
106 For more information about general sequence and array predicates, | |
107 see @ref{Sequences Arrays Vectors}, and @ref{Arrays}. | |
108 | |
109 @defun stringp object | |
110 This function returns @code{t} if @var{object} is a string, @code{nil} | |
111 otherwise. | |
112 @end defun | |
113 | |
114 @defun char-or-string-p object | |
115 This function returns @code{t} if @var{object} is a string or a | |
116 character, @code{nil} otherwise. | |
117 | |
118 In XEmacs addition, this function also returns @code{t} if @var{object} | |
119 is an integer that can be represented as a character. This is because | |
120 of compatibility with previous XEmacs and should not be depended on. | |
121 @end defun | |
122 | |
5791
9fae6227ede5
Silence texinfo 5.2 warnings, primarily by adding next, prev, and up
Jerry James <james@xemacs.org>
parents:
5384
diff
changeset
|
123 @node Creating Strings, Predicates for Characters, Predicates for Strings, Strings and Characters |
428 | 124 @section Creating Strings |
125 | |
126 The following functions create strings, either from scratch, or by | |
127 putting strings together, or by taking them apart. | |
128 | |
129 @defun string &rest characters | |
130 This function returns a new string made up of @var{characters}. | |
131 | |
132 @example | |
133 (string ?X ?E ?m ?a ?c ?s) | |
134 @result{} "XEmacs" | |
135 (string) | |
136 @result{} "" | |
137 @end example | |
138 | |
139 Analogous functions operating on other data types include @code{list}, | |
140 @code{cons} (@pxref{Building Lists}), @code{vector} (@pxref{Vectors}) | |
444 | 141 and @code{bit-vector} (@pxref{Bit Vectors}). This function has not been |
428 | 142 available in XEmacs prior to 21.0 and FSF Emacs prior to 20.3. |
143 @end defun | |
144 | |
444 | 145 @defun make-string length character |
146 This function returns a new string consisting entirely of @var{length} | |
147 successive copies of @var{character}. @var{length} must be a | |
4885
6772ce4d982b
Fix hash tables, #'member*, #'assoc*, #'eql compiler macros if bignums
Aidan Kehoe <kehoea@parhasard.net>
parents:
4329
diff
changeset
|
148 non-negative fixnum. |
428 | 149 |
150 @example | |
151 (make-string 5 ?x) | |
152 @result{} "xxxxx" | |
153 (make-string 0 ?x) | |
154 @result{} "" | |
155 @end example | |
156 | |
157 Other functions to compare with this one include @code{char-to-string} | |
158 (@pxref{String Conversion}), @code{make-vector} (@pxref{Vectors}), and | |
159 @code{make-list} (@pxref{Building Lists}). | |
160 @end defun | |
161 | |
162 @defun substring string start &optional end | |
163 This function returns a new string which consists of those characters | |
164 from @var{string} in the range from (and including) the character at the | |
165 index @var{start} up to (but excluding) the character at the index | |
166 @var{end}. The first character is at index zero. | |
167 | |
5089
99f8ebc082d9
Make #'substring an alias of #'subseq; give the latter the byte code.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4885
diff
changeset
|
168 In this implementation, @code{substring} is an alias for @code{subseq}, |
99f8ebc082d9
Make #'substring an alias of #'subseq; give the latter the byte code.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4885
diff
changeset
|
169 so @var{string} can be any sequence. In GNU Emacs, @var{string} can be |
99f8ebc082d9
Make #'substring an alias of #'subseq; give the latter the byte code.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4885
diff
changeset
|
170 a string or a vector, and in older XEmacs it can only be a string. |
99f8ebc082d9
Make #'substring an alias of #'subseq; give the latter the byte code.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4885
diff
changeset
|
171 |
428 | 172 @example |
173 @group | |
174 (substring "abcdefg" 0 3) | |
175 @result{} "abc" | |
176 @end group | |
177 @end example | |
178 | |
179 @noindent | |
180 Here the index for @samp{a} is 0, the index for @samp{b} is 1, and the | |
181 index for @samp{c} is 2. Thus, three letters, @samp{abc}, are copied | |
182 from the string @code{"abcdefg"}. The index 3 marks the character | |
183 position up to which the substring is copied. The character whose index | |
184 is 3 is actually the fourth character in the string. | |
185 | |
186 A negative number counts from the end of the string, so that @minus{}1 | |
444 | 187 signifies the index of the last character of the string. For example: |
428 | 188 |
189 @example | |
190 @group | |
191 (substring "abcdefg" -3 -1) | |
192 @result{} "ef" | |
193 @end group | |
194 @end example | |
195 | |
196 @noindent | |
197 In this example, the index for @samp{e} is @minus{}3, the index for | |
198 @samp{f} is @minus{}2, and the index for @samp{g} is @minus{}1. | |
199 Therefore, @samp{e} and @samp{f} are included, and @samp{g} is excluded. | |
200 | |
201 When @code{nil} is used as an index, it stands for the length of the | |
202 string. Thus, | |
203 | |
204 @example | |
205 @group | |
206 (substring "abcdefg" -3 nil) | |
207 @result{} "efg" | |
208 @end group | |
209 @end example | |
210 | |
211 Omitting the argument @var{end} is equivalent to specifying @code{nil}. | |
212 It follows that @code{(substring @var{string} 0)} returns a copy of all | |
213 of @var{string}. | |
214 | |
215 @example | |
216 @group | |
217 (substring "abcdefg" 0) | |
218 @result{} "abcdefg" | |
219 @end group | |
220 @end example | |
221 | |
222 @noindent | |
223 But we recommend @code{copy-sequence} for this purpose (@pxref{Sequence | |
224 Functions}). | |
225 | |
226 If the characters copied from @var{string} have duplicable extents or | |
227 text properties, those are copied into the new string also. | |
228 @xref{Duplicable Extents}. | |
229 | |
230 A @code{wrong-type-argument} error is signaled if either @var{start} or | |
4885
6772ce4d982b
Fix hash tables, #'member*, #'assoc*, #'eql compiler macros if bignums
Aidan Kehoe <kehoea@parhasard.net>
parents:
4329
diff
changeset
|
231 @var{end} is not a fixnum or @code{nil}. An @code{args-out-of-range} |
428 | 232 error is signaled if @var{start} indicates a character following |
233 @var{end}, or if either integer is out of range for @var{string}. | |
234 | |
235 Contrast this function with @code{buffer-substring} (@pxref{Buffer | |
236 Contents}), which returns a string containing a portion of the text in | |
237 the current buffer. The beginning of a string is at index 0, but the | |
238 beginning of a buffer is at index 1. | |
239 @end defun | |
240 | |
241 @defun concat &rest sequences | |
242 @cindex copying strings | |
243 @cindex concatenating strings | |
244 This function returns a new string consisting of the characters in the | |
245 arguments passed to it (along with their text properties, if any). The | |
246 arguments may be strings, lists of numbers, or vectors of numbers; they | |
247 are not themselves changed. If @code{concat} receives no arguments, it | |
248 returns an empty string. | |
249 | |
250 @example | |
251 (concat "abc" "-def") | |
252 @result{} "abc-def" | |
4885
6772ce4d982b
Fix hash tables, #'member*, #'assoc*, #'eql compiler macros if bignums
Aidan Kehoe <kehoea@parhasard.net>
parents:
4329
diff
changeset
|
253 (equal (concat "abc" (list 120 (+ 256 121)) [122]) (format "abcx%cz" 377)) |
6772ce4d982b
Fix hash tables, #'member*, #'assoc*, #'eql compiler macros if bignums
Aidan Kehoe <kehoea@parhasard.net>
parents:
4329
diff
changeset
|
254 @result{} t |
428 | 255 ;; @r{@code{nil} is an empty sequence.} |
256 (concat "abc" nil "-def") | |
257 @result{} "abc-def" | |
258 (concat "The " "quick brown " "fox.") | |
259 @result{} "The quick brown fox." | |
260 (concat) | |
261 @result{} "" | |
262 @end example | |
263 | |
264 @noindent | |
265 The @code{concat} function always constructs a new string that is | |
266 not @code{eq} to any existing string. | |
267 | |
268 For information about other concatenation functions, see the description | |
269 of @code{mapconcat} in @ref{Mapping Functions}, @code{vconcat} in | |
270 @ref{Vectors}, @code{bvconcat} in @ref{Bit Vectors}, and @code{append} | |
271 in @ref{Building Lists}. | |
272 @end defun | |
273 | |
1495 | 274 The function @code{split-string}, in @ref{Regexp Search}, generates a |
5384
3889ef128488
Fix misspelled words, and some grammar, across the entire source tree.
Jerry James <james@xemacs.org>
parents:
5089
diff
changeset
|
275 list of strings by splitting a string on occurrences of a regular |
1495 | 276 expression. |
277 | |
5791
9fae6227ede5
Silence texinfo 5.2 warnings, primarily by adding next, prev, and up
Jerry James <james@xemacs.org>
parents:
5384
diff
changeset
|
278 @node Predicates for Characters, Character Codes, Creating Strings, Strings and Characters |
428 | 279 @section The Predicates for Characters |
280 | |
281 @defun characterp object | |
282 This function returns @code{t} if @var{object} is a character. | |
283 | |
284 Some functions that work on integers (e.g. the comparison functions | |
285 <, <=, =, /=, etc. and the arithmetic functions +, -, *, etc.) | |
286 accept characters and implicitly convert them into integers. In | |
287 general, functions that work on characters also accept char-ints and | |
288 implicitly convert them into characters. WARNING: Neither of these | |
289 behaviors is very desirable, and they are maintained for backward | |
290 compatibility with old E-Lisp programs that confounded characters and | |
291 integers willy-nilly. These behaviors may change in the future; therefore, | |
292 do not rely on them. Instead, convert the characters explicitly | |
293 using @code{char-int}. | |
294 @end defun | |
295 | |
296 @defun integer-or-char-p object | |
297 This function returns @code{t} if @var{object} is an integer or character. | |
298 @end defun | |
299 | |
5867
e0f1dfaa821e
Add the non-ASCII digit support, now #'parse-integer can handle it.
Aidan Kehoe <kehoea@parhasard.net>
parents:
5863
diff
changeset
|
300 @defun digit-char-p character &optional (radix 10) radix-table |
e0f1dfaa821e
Add the non-ASCII digit support, now #'parse-integer can handle it.
Aidan Kehoe <kehoea@parhasard.net>
parents:
5863
diff
changeset
|
301 This function returns non-nil if @var{character} represents a digit in base |
e0f1dfaa821e
Add the non-ASCII digit support, now #'parse-integer can handle it.
Aidan Kehoe <kehoea@parhasard.net>
parents:
5863
diff
changeset
|
302 @var{radix}. The non-nil value returned is the integer value associated with |
e0f1dfaa821e
Add the non-ASCII digit support, now #'parse-integer can handle it.
Aidan Kehoe <kehoea@parhasard.net>
parents:
5863
diff
changeset
|
303 @var{character}. @var{radix} defaults to @code{10}. |
e0f1dfaa821e
Add the non-ASCII digit support, now #'parse-integer can handle it.
Aidan Kehoe <kehoea@parhasard.net>
parents:
5863
diff
changeset
|
304 |
e0f1dfaa821e
Add the non-ASCII digit support, now #'parse-integer can handle it.
Aidan Kehoe <kehoea@parhasard.net>
parents:
5863
diff
changeset
|
305 @var{radix-table}, if non-nil, specifies a character table mapping from |
e0f1dfaa821e
Add the non-ASCII digit support, now #'parse-integer can handle it.
Aidan Kehoe <kehoea@parhasard.net>
parents:
5863
diff
changeset
|
306 characters to their digit values. See the function @code{parse-integer} in |
e0f1dfaa821e
Add the non-ASCII digit support, now #'parse-integer can handle it.
Aidan Kehoe <kehoea@parhasard.net>
parents:
5863
diff
changeset
|
307 @ref{String Conversion}. |
e0f1dfaa821e
Add the non-ASCII digit support, now #'parse-integer can handle it.
Aidan Kehoe <kehoea@parhasard.net>
parents:
5863
diff
changeset
|
308 @end defun |
e0f1dfaa821e
Add the non-ASCII digit support, now #'parse-integer can handle it.
Aidan Kehoe <kehoea@parhasard.net>
parents:
5863
diff
changeset
|
309 |
e0f1dfaa821e
Add the non-ASCII digit support, now #'parse-integer can handle it.
Aidan Kehoe <kehoea@parhasard.net>
parents:
5863
diff
changeset
|
310 @defun digit-char weight &optional (radix 10) radix-table |
e0f1dfaa821e
Add the non-ASCII digit support, now #'parse-integer can handle it.
Aidan Kehoe <kehoea@parhasard.net>
parents:
5863
diff
changeset
|
311 This function is the inverse of @code{digit-char-p}. Given an integer |
e0f1dfaa821e
Add the non-ASCII digit support, now #'parse-integer can handle it.
Aidan Kehoe <kehoea@parhasard.net>
parents:
5863
diff
changeset
|
312 @var{weight}, it returns a character representing @var{weight} when read with |
e0f1dfaa821e
Add the non-ASCII digit support, now #'parse-integer can handle it.
Aidan Kehoe <kehoea@parhasard.net>
parents:
5863
diff
changeset
|
313 base @var{radix}. If no such character exists, it returns @code{nil}. |
e0f1dfaa821e
Add the non-ASCII digit support, now #'parse-integer can handle it.
Aidan Kehoe <kehoea@parhasard.net>
parents:
5863
diff
changeset
|
314 @end defun |
e0f1dfaa821e
Add the non-ASCII digit support, now #'parse-integer can handle it.
Aidan Kehoe <kehoea@parhasard.net>
parents:
5863
diff
changeset
|
315 |
5791
9fae6227ede5
Silence texinfo 5.2 warnings, primarily by adding next, prev, and up
Jerry James <james@xemacs.org>
parents:
5384
diff
changeset
|
316 @node Character Codes, Text Comparison, Predicates for Characters, Strings and Characters |
428 | 317 @section Character Codes |
318 | |
444 | 319 @defun char-int character |
428 | 320 This function converts a character into an equivalent integer. |
321 The resulting integer will always be non-negative. The integers in | |
322 the range 0 - 255 map to characters as follows: | |
323 | |
324 @table @asis | |
325 @item 0 - 31 | |
326 Control set 0 | |
327 @item 32 - 127 | |
328 @sc{ascii} | |
329 @item 128 - 159 | |
330 Control set 1 | |
331 @item 160 - 255 | |
332 Right half of ISO-8859-1 | |
333 @end table | |
334 | |
335 If support for @sc{mule} does not exist, these are the only valid | |
336 character values. When @sc{mule} support exists, the values assigned to | |
337 other characters may vary depending on the particular version of XEmacs, | |
338 the order in which character sets were loaded, etc., and you should not | |
339 depend on them. | |
340 @end defun | |
341 | |
342 @defun int-char integer | |
343 This function converts an integer into the equivalent character. Not | |
344 all integers correspond to valid characters; use @code{char-int-p} to | |
345 determine whether this is the case. If the integer cannot be converted, | |
346 @code{nil} is returned. | |
347 @end defun | |
348 | |
5863
15041705c196
Provide `char-code-limit', implement the GNU equivalent in terms of it.
Aidan Kehoe <kehoea@parhasard.net>
parents:
5791
diff
changeset
|
349 @defvar char-code-limit |
15041705c196
Provide `char-code-limit', implement the GNU equivalent in terms of it.
Aidan Kehoe <kehoea@parhasard.net>
parents:
5791
diff
changeset
|
350 This is a constant integer describing an exclusive upper bound on the |
15041705c196
Provide `char-code-limit', implement the GNU equivalent in terms of it.
Aidan Kehoe <kehoea@parhasard.net>
parents:
5791
diff
changeset
|
351 results return by @code{char-int} and that set of integers (fixnums) for |
15041705c196
Provide `char-code-limit', implement the GNU equivalent in terms of it.
Aidan Kehoe <kehoea@parhasard.net>
parents:
5791
diff
changeset
|
352 which @code{int-char} will give non-nil. Without @sc{mule} |
15041705c196
Provide `char-code-limit', implement the GNU equivalent in terms of it.
Aidan Kehoe <kehoea@parhasard.net>
parents:
5791
diff
changeset
|
353 (internationalization) support this will be @code{#x100}, as described |
15041705c196
Provide `char-code-limit', implement the GNU equivalent in terms of it.
Aidan Kehoe <kehoea@parhasard.net>
parents:
5791
diff
changeset
|
354 under @code{char-int}, but with @sc{mule} support the range of values is |
15041705c196
Provide `char-code-limit', implement the GNU equivalent in terms of it.
Aidan Kehoe <kehoea@parhasard.net>
parents:
5791
diff
changeset
|
355 much bigger, at least 21 bits' worth. If an integer is less than |
15041705c196
Provide `char-code-limit', implement the GNU equivalent in terms of it.
Aidan Kehoe <kehoea@parhasard.net>
parents:
5791
diff
changeset
|
356 @var{char-code-limit}, it may still not have an associated character, it |
15041705c196
Provide `char-code-limit', implement the GNU equivalent in terms of it.
Aidan Kehoe <kehoea@parhasard.net>
parents:
5791
diff
changeset
|
357 is still necessary to check with the next function, @code{char-int-p}. |
15041705c196
Provide `char-code-limit', implement the GNU equivalent in terms of it.
Aidan Kehoe <kehoea@parhasard.net>
parents:
5791
diff
changeset
|
358 @end defvar |
15041705c196
Provide `char-code-limit', implement the GNU equivalent in terms of it.
Aidan Kehoe <kehoea@parhasard.net>
parents:
5791
diff
changeset
|
359 |
428 | 360 @defun char-int-p object |
361 This function returns @code{t} if @var{object} is an integer that can be | |
362 converted into a character. | |
363 @end defun | |
364 | |
365 @defun char-or-char-int-p object | |
366 This function returns @code{t} if @var{object} is a character or an | |
367 integer that can be converted into one. | |
368 @end defun | |
369 | |
370 @need 2000 | |
5791
9fae6227ede5
Silence texinfo 5.2 warnings, primarily by adding next, prev, and up
Jerry James <james@xemacs.org>
parents:
5384
diff
changeset
|
371 @node Text Comparison, String Conversion, Character Codes, Strings and Characters |
428 | 372 @section Comparison of Characters and Strings |
373 @cindex string equality | |
374 | |
444 | 375 @defun char-equal character1 character2 &optional buffer |
428 | 376 This function returns @code{t} if the arguments represent the same |
377 character, @code{nil} otherwise. This function ignores differences | |
444 | 378 in case if the value of @code{case-fold-search} is non-@code{nil} in |
379 @var{buffer}, which defaults to the current buffer. | |
428 | 380 |
381 @example | |
382 (char-equal ?x ?x) | |
383 @result{} t | |
384 (let ((case-fold-search t)) | |
385 (char-equal ?x ?X)) | |
386 @result{} t | |
387 (let ((case-fold-search nil)) | |
388 (char-equal ?x ?X)) | |
389 @result{} nil | |
390 @end example | |
391 @end defun | |
392 | |
393 @defun char= character1 character2 | |
394 This function returns @code{t} if the arguments represent the same | |
395 character, @code{nil} otherwise. Case is significant. | |
396 | |
397 @example | |
398 (char= ?x ?x) | |
399 @result{} t | |
400 (char= ?x ?X) | |
401 @result{} nil | |
402 (let ((case-fold-search t)) | |
403 (char-equal ?x ?X)) | |
404 @result{} nil | |
405 (let ((case-fold-search nil)) | |
406 (char-equal ?x ?X)) | |
407 @result{} nil | |
408 @end example | |
409 @end defun | |
410 | |
411 @defun string= string1 string2 | |
412 This function returns @code{t} if the characters of the two strings | |
413 match exactly; case is significant. | |
414 | |
415 @example | |
416 (string= "abc" "abc") | |
417 @result{} t | |
418 (string= "abc" "ABC") | |
419 @result{} nil | |
420 (string= "ab" "ABC") | |
421 @result{} nil | |
422 @end example | |
423 | |
424 @ignore @c `equal' in XEmacs does not compare text properties | |
425 The function @code{string=} ignores the text properties of the | |
426 two strings. To compare strings in a way that compares their text | |
427 properties also, use @code{equal} (@pxref{Equality Predicates}). | |
428 @end ignore | |
429 @end defun | |
430 | |
431 @defun string-equal string1 string2 | |
432 @code{string-equal} is another name for @code{string=}. | |
433 @end defun | |
434 | |
435 @cindex lexical comparison | |
436 @defun string< string1 string2 | |
437 @c (findex string< causes problems for permuted index!!) | |
438 This function compares two strings a character at a time. First it | |
439 scans both the strings at once to find the first pair of corresponding | |
440 characters that do not match. If the lesser character of those two is | |
441 the character from @var{string1}, then @var{string1} is less, and this | |
442 function returns @code{t}. If the lesser character is the one from | |
443 @var{string2}, then @var{string1} is greater, and this function returns | |
444 @code{nil}. If the two strings match entirely, the value is @code{nil}. | |
445 | |
446 Pairs of characters are compared by their @sc{ascii} codes. Keep in | |
447 mind that lower case letters have higher numeric values in the | |
448 @sc{ascii} character set than their upper case counterparts; numbers and | |
449 many punctuation characters have a lower numeric value than upper case | |
450 letters. | |
451 | |
452 @example | |
453 @group | |
454 (string< "abc" "abd") | |
455 @result{} t | |
456 (string< "abd" "abc") | |
457 @result{} nil | |
458 (string< "123" "abc") | |
459 @result{} t | |
460 @end group | |
461 @end example | |
462 | |
463 When the strings have different lengths, and they match up to the | |
464 length of @var{string1}, then the result is @code{t}. If they match up | |
465 to the length of @var{string2}, the result is @code{nil}. A string of | |
466 no characters is less than any other string. | |
467 | |
468 @example | |
469 @group | |
470 (string< "" "abc") | |
471 @result{} t | |
472 (string< "ab" "abc") | |
473 @result{} t | |
474 (string< "abc" "") | |
475 @result{} nil | |
476 (string< "abc" "ab") | |
477 @result{} nil | |
478 (string< "" "") | |
444 | 479 @result{} nil |
428 | 480 @end group |
481 @end example | |
482 @end defun | |
483 | |
484 @defun string-lessp string1 string2 | |
485 @code{string-lessp} is another name for @code{string<}. | |
486 @end defun | |
487 | |
488 See also @code{compare-buffer-substrings} in @ref{Comparing Text}, for | |
489 a way to compare text in buffers. The function @code{string-match}, | |
490 which matches a regular expression against a string, can be used | |
491 for a kind of string comparison; see @ref{Regexp Search}. | |
492 | |
5791
9fae6227ede5
Silence texinfo 5.2 warnings, primarily by adding next, prev, and up
Jerry James <james@xemacs.org>
parents:
5384
diff
changeset
|
493 @node String Conversion, Modifying Strings, Text Comparison, Strings and Characters |
428 | 494 @section Conversion of Characters and Strings |
495 @cindex conversion of strings | |
496 | |
497 This section describes functions for conversions between characters, | |
498 strings and integers. @code{format} and @code{prin1-to-string} | |
499 (@pxref{Output Functions}) can also convert Lisp objects into strings. | |
500 @code{read-from-string} (@pxref{Input Functions}) can ``convert'' a | |
501 string representation of a Lisp object into an object. | |
502 | |
503 @xref{Documentation}, for functions that produce textual descriptions | |
504 of text characters and general input events | |
505 (@code{single-key-description} and @code{text-char-description}). These | |
506 functions are used primarily for making help messages. | |
507 | |
508 @defun char-to-string character | |
509 @cindex character to string | |
510 This function returns a new string with a length of one character. | |
511 The value of @var{character}, modulo 256, is used to initialize the | |
512 element of the string. | |
513 | |
514 This function is similar to @code{make-string} with an integer argument | |
515 of 1. (@xref{Creating Strings}.) This conversion can also be done with | |
516 @code{format} using the @samp{%c} format specification. | |
517 (@xref{Formatting Strings}.) | |
518 | |
519 @example | |
520 (char-to-string ?x) | |
521 @result{} "x" | |
522 (char-to-string (+ 256 ?x)) | |
523 @result{} "x" | |
524 (make-string 1 ?x) | |
525 @result{} "x" | |
526 @end example | |
527 @end defun | |
528 | |
529 @defun string-to-char string | |
530 @cindex string to character | |
531 This function returns the first character in @var{string}. If the | |
532 string is empty, the function returns 0. (Under XEmacs 19, the value is | |
533 also 0 when the first character of @var{string} is the null character, | |
534 @sc{ascii} code 0.) | |
535 | |
536 @example | |
537 (string-to-char "ABC") | |
538 @result{} ?A ;; @r{Under XEmacs 20.} | |
539 @result{} 65 ;; @r{Under XEmacs 19.} | |
540 (string-to-char "xyz") | |
541 @result{} ?x ;; @r{Under XEmacs 20.} | |
542 @result{} 120 ;; @r{Under XEmacs 19.} | |
543 (string-to-char "") | |
544 @result{} 0 | |
545 (string-to-char "\000") | |
546 @result{} ?\^@ ;; @r{Under XEmacs 20.} | |
547 @result{} 0 ;; @r{Under XEmacs 20.} | |
548 @end example | |
549 | |
550 This function may be eliminated in the future if it does not seem useful | |
551 enough to retain. | |
552 @end defun | |
553 | |
554 @defun number-to-string number | |
555 @cindex integer to string | |
556 @cindex integer to decimal | |
557 This function returns a string consisting of the printed | |
558 representation of @var{number}, which may be an integer or a floating | |
559 point number. The value starts with a sign if the argument is | |
560 negative. | |
561 | |
562 @example | |
563 (number-to-string 256) | |
564 @result{} "256" | |
565 (number-to-string -23) | |
566 @result{} "-23" | |
567 (number-to-string -23.5) | |
568 @result{} "-23.5" | |
569 @end example | |
570 | |
571 @cindex int-to-string | |
572 @code{int-to-string} is a semi-obsolete alias for this function. | |
573 | |
574 See also the function @code{format} in @ref{Formatting Strings}. | |
575 @end defun | |
576 | |
5867
e0f1dfaa821e
Add the non-ASCII digit support, now #'parse-integer can handle it.
Aidan Kehoe <kehoea@parhasard.net>
parents:
5863
diff
changeset
|
577 @defun parse-integer string &key (start 0) end (radix 10) junk-allowed radix-table) |
e0f1dfaa821e
Add the non-ASCII digit support, now #'parse-integer can handle it.
Aidan Kehoe <kehoea@parhasard.net>
parents:
5863
diff
changeset
|
578 @cindex parse-integer |
e0f1dfaa821e
Add the non-ASCII digit support, now #'parse-integer can handle it.
Aidan Kehoe <kehoea@parhasard.net>
parents:
5863
diff
changeset
|
579 This function returns the fixnum or bignum value represented by @var{string} |
e0f1dfaa821e
Add the non-ASCII digit support, now #'parse-integer can handle it.
Aidan Kehoe <kehoea@parhasard.net>
parents:
5863
diff
changeset
|
580 in base @var{radix}. If @var{start} or @var{end} are specified, they describe |
e0f1dfaa821e
Add the non-ASCII digit support, now #'parse-integer can handle it.
Aidan Kehoe <kehoea@parhasard.net>
parents:
5863
diff
changeset
|
581 a subsequence of @var{string} to examine. @xref{Sequence Basics, Sequence |
e0f1dfaa821e
Add the non-ASCII digit support, now #'parse-integer can handle it.
Aidan Kehoe <kehoea@parhasard.net>
parents:
5863
diff
changeset
|
582 Basics, Sequences, cl}. It skips whitespace at the beginning and end of the |
e0f1dfaa821e
Add the non-ASCII digit support, now #'parse-integer can handle it.
Aidan Kehoe <kehoea@parhasard.net>
parents:
5863
diff
changeset
|
583 digits seen. Leading @code{?+} and @code{?-} are accepted and have the usual |
e0f1dfaa821e
Add the non-ASCII digit support, now #'parse-integer can handle it.
Aidan Kehoe <kehoea@parhasard.net>
parents:
5863
diff
changeset
|
584 meaning; there is no provision for recognition of a leading @code{#x} or |
e0f1dfaa821e
Add the non-ASCII digit support, now #'parse-integer can handle it.
Aidan Kehoe <kehoea@parhasard.net>
parents:
5863
diff
changeset
|
585 @code{#o} to indicate a non-default @var{radix}, and there is no special |
e0f1dfaa821e
Add the non-ASCII digit support, now #'parse-integer can handle it.
Aidan Kehoe <kehoea@parhasard.net>
parents:
5863
diff
changeset
|
586 provision for a trailing @code{.}. |
e0f1dfaa821e
Add the non-ASCII digit support, now #'parse-integer can handle it.
Aidan Kehoe <kehoea@parhasard.net>
parents:
5863
diff
changeset
|
587 |
e0f1dfaa821e
Add the non-ASCII digit support, now #'parse-integer can handle it.
Aidan Kehoe <kehoea@parhasard.net>
parents:
5863
diff
changeset
|
588 If @var{junk-allowed} is non-nil, @code{parse-integer} allows non-digit, |
e0f1dfaa821e
Add the non-ASCII digit support, now #'parse-integer can handle it.
Aidan Kehoe <kehoea@parhasard.net>
parents:
5863
diff
changeset
|
589 non-whitespace characters before or after the number to be parsed. Otherwise, |
e0f1dfaa821e
Add the non-ASCII digit support, now #'parse-integer can handle it.
Aidan Kehoe <kehoea@parhasard.net>
parents:
5863
diff
changeset
|
590 it errors when presented with anything other than an optional series of |
e0f1dfaa821e
Add the non-ASCII digit support, now #'parse-integer can handle it.
Aidan Kehoe <kehoea@parhasard.net>
parents:
5863
diff
changeset
|
591 whitespace characters, followed by a non-optional series of digit characters, |
e0f1dfaa821e
Add the non-ASCII digit support, now #'parse-integer can handle it.
Aidan Kehoe <kehoea@parhasard.net>
parents:
5863
diff
changeset
|
592 followed by an optional series of whitespace characters. |
e0f1dfaa821e
Add the non-ASCII digit support, now #'parse-integer can handle it.
Aidan Kehoe <kehoea@parhasard.net>
parents:
5863
diff
changeset
|
593 |
e0f1dfaa821e
Add the non-ASCII digit support, now #'parse-integer can handle it.
Aidan Kehoe <kehoea@parhasard.net>
parents:
5863
diff
changeset
|
594 @code{parse-integer} returns two values, the second of which can be accessed |
e0f1dfaa821e
Add the non-ASCII digit support, now #'parse-integer can handle it.
Aidan Kehoe <kehoea@parhasard.net>
parents:
5863
diff
changeset
|
595 by, e.g. @xref{Multiple values, multiple-value-bind, ,}. If |
e0f1dfaa821e
Add the non-ASCII digit support, now #'parse-integer can handle it.
Aidan Kehoe <kehoea@parhasard.net>
parents:
5863
diff
changeset
|
596 @var{junk-allowed} is nil, the second value is always the length of that |
e0f1dfaa821e
Add the non-ASCII digit support, now #'parse-integer can handle it.
Aidan Kehoe <kehoea@parhasard.net>
parents:
5863
diff
changeset
|
597 subsequence of @var{string} specified. Otherwise, it is the index of the |
e0f1dfaa821e
Add the non-ASCII digit support, now #'parse-integer can handle it.
Aidan Kehoe <kehoea@parhasard.net>
parents:
5863
diff
changeset
|
598 first non-digit trailing character encountered. |
e0f1dfaa821e
Add the non-ASCII digit support, now #'parse-integer can handle it.
Aidan Kehoe <kehoea@parhasard.net>
parents:
5863
diff
changeset
|
599 |
e0f1dfaa821e
Add the non-ASCII digit support, now #'parse-integer can handle it.
Aidan Kehoe <kehoea@parhasard.net>
parents:
5863
diff
changeset
|
600 Normally, the digit values for each character encountered are those returned |
e0f1dfaa821e
Add the non-ASCII digit support, now #'parse-integer can handle it.
Aidan Kehoe <kehoea@parhasard.net>
parents:
5863
diff
changeset
|
601 by @code{digit-char-p}. Specifying @var{radix-table} allows this to be |
e0f1dfaa821e
Add the non-ASCII digit support, now #'parse-integer can handle it.
Aidan Kehoe <kehoea@parhasard.net>
parents:
5863
diff
changeset
|
602 overridden; if it is specified, it is a character table (@pxref{Char Tables}) |
e0f1dfaa821e
Add the non-ASCII digit support, now #'parse-integer can handle it.
Aidan Kehoe <kehoea@parhasard.net>
parents:
5863
diff
changeset
|
603 mapping from characters to fixnum digit values. If a given character has both |
e0f1dfaa821e
Add the non-ASCII digit support, now #'parse-integer can handle it.
Aidan Kehoe <kehoea@parhasard.net>
parents:
5863
diff
changeset
|
604 a digit value and a whitespace value, the digit value overrides. However, if a |
e0f1dfaa821e
Add the non-ASCII digit support, now #'parse-integer can handle it.
Aidan Kehoe <kehoea@parhasard.net>
parents:
5863
diff
changeset
|
605 @code{?-} both has a digit value and is the first character examined, its |
e0f1dfaa821e
Add the non-ASCII digit support, now #'parse-integer can handle it.
Aidan Kehoe <kehoea@parhasard.net>
parents:
5863
diff
changeset
|
606 digit value is ignored, it is treated as indicating the number is negative. |
e0f1dfaa821e
Add the non-ASCII digit support, now #'parse-integer can handle it.
Aidan Kehoe <kehoea@parhasard.net>
parents:
5863
diff
changeset
|
607 A work-around is to prepend a character having digit value zero to positive |
e0f1dfaa821e
Add the non-ASCII digit support, now #'parse-integer can handle it.
Aidan Kehoe <kehoea@parhasard.net>
parents:
5863
diff
changeset
|
608 integers and to double the leading @code{?-} for negative integers. |
e0f1dfaa821e
Add the non-ASCII digit support, now #'parse-integer can handle it.
Aidan Kehoe <kehoea@parhasard.net>
parents:
5863
diff
changeset
|
609 @end defun |
e0f1dfaa821e
Add the non-ASCII digit support, now #'parse-integer can handle it.
Aidan Kehoe <kehoea@parhasard.net>
parents:
5863
diff
changeset
|
610 |
428 | 611 @defun string-to-number string &optional base |
612 @cindex string to number | |
444 | 613 This function returns the numeric value represented by @var{string}, |
614 read in @var{base}. It skips spaces and tabs at the beginning of | |
615 @var{string}, then reads as much of @var{string} as it can interpret as | |
616 a number. (On some systems it ignores other whitespace at the | |
617 beginning, not just spaces and tabs.) If the first character after the | |
618 ignored whitespace is not a digit or a minus sign, this function returns | |
619 0. | |
428 | 620 |
621 If @var{base} is not specified, it defaults to ten. With @var{base} | |
622 other than ten, only integers can be read. | |
623 | |
624 @example | |
625 (string-to-number "256") | |
626 @result{} 256 | |
627 (string-to-number "25 is a perfect square.") | |
628 @result{} 25 | |
629 (string-to-number "X256") | |
630 @result{} 0 | |
631 (string-to-number "-4.5") | |
632 @result{} -4.5 | |
633 (string-to-number "ffff" 16) | |
634 @result{} 65535 | |
635 @end example | |
636 | |
637 @findex string-to-int | |
638 @code{string-to-int} is an obsolete alias for this function. | |
639 @end defun | |
640 | |
5791
9fae6227ede5
Silence texinfo 5.2 warnings, primarily by adding next, prev, and up
Jerry James <james@xemacs.org>
parents:
5384
diff
changeset
|
641 @node Modifying Strings, String Properties, String Conversion, Strings and Characters |
428 | 642 @section Modifying Strings |
643 @cindex strings, modifying | |
644 | |
645 You can modify a string using the general array-modifying primitives. | |
646 @xref{Arrays}. The function @code{aset} modifies a single character; | |
647 the function @code{fillarray} sets all characters in the string to | |
648 a specified character. | |
649 | |
650 Each string has a tick counter that starts out at zero (when the string | |
651 is created) and is incremented each time a change is made to that | |
652 string. | |
653 | |
654 @defun string-modified-tick string | |
655 This function returns the tick counter for @samp{string}. | |
656 @end defun | |
657 | |
5791
9fae6227ede5
Silence texinfo 5.2 warnings, primarily by adding next, prev, and up
Jerry James <james@xemacs.org>
parents:
5384
diff
changeset
|
658 @node String Properties, Formatting Strings, Modifying Strings, Strings and Characters |
428 | 659 @section String Properties |
660 @cindex string properties | |
661 @cindex properties of strings | |
662 | |
442 | 663 Just as with symbols, extents, faces, and glyphs, you can attach |
428 | 664 additional information to strings in the form of @dfn{string |
665 properties}. These differ from text properties, which are logically | |
666 attached to particular characters in the string. | |
667 | |
668 To attach a property to a string, use @code{put}. To retrieve a property | |
669 from a string, use @code{get}. You can also use @code{remprop} to remove | |
442 | 670 a property from a string and @code{object-plist} to retrieve a list of |
428 | 671 all the properties in a string. |
672 | |
5791
9fae6227ede5
Silence texinfo 5.2 warnings, primarily by adding next, prev, and up
Jerry James <james@xemacs.org>
parents:
5384
diff
changeset
|
673 @node Formatting Strings, Character Case, String Properties, Strings and Characters |
428 | 674 @section Formatting Strings |
675 @cindex formatting strings | |
676 @cindex strings, formatting them | |
677 | |
678 @dfn{Formatting} means constructing a string by substitution of | |
679 computed values at various places in a constant string. This string | |
680 controls how the other values are printed as well as where they appear; | |
681 it is called a @dfn{format string}. | |
682 | |
683 Formatting is often useful for computing messages to be displayed. In | |
684 fact, the functions @code{message} and @code{error} provide the same | |
685 formatting feature described here; they differ from @code{format} only | |
686 in how they use the result of formatting. | |
687 | |
688 @defun format string &rest objects | |
689 This function returns a new string that is made by copying | |
444 | 690 @var{string} and then replacing any format specification |
428 | 691 in the copy with encodings of the corresponding @var{objects}. The |
692 arguments @var{objects} are the computed values to be formatted. | |
693 @end defun | |
694 | |
695 @cindex @samp{%} in format | |
696 @cindex format specification | |
697 A format specification is a sequence of characters beginning with a | |
698 @samp{%}. Thus, if there is a @samp{%d} in @var{string}, the | |
699 @code{format} function replaces it with the printed representation of | |
700 one of the values to be formatted (one of the arguments @var{objects}). | |
701 For example: | |
702 | |
703 @example | |
704 @group | |
705 (format "The value of fill-column is %d." fill-column) | |
706 @result{} "The value of fill-column is 72." | |
707 @end group | |
708 @end example | |
709 | |
710 If @var{string} contains more than one format specification, the | |
711 format specifications correspond with successive values from | |
712 @var{objects}. Thus, the first format specification in @var{string} | |
713 uses the first such value, the second format specification uses the | |
714 second such value, and so on. Any extra format specifications (those | |
715 for which there are no corresponding values) cause unpredictable | |
716 behavior. Any extra values to be formatted are ignored. | |
717 | |
718 Certain format specifications require values of particular types. | |
719 However, no error is signaled if the value actually supplied fails to | |
720 have the expected type. Instead, the output is likely to be | |
721 meaningless. | |
722 | |
723 Here is a table of valid format specifications: | |
724 | |
725 @table @samp | |
726 @item %s | |
727 Replace the specification with the printed representation of the object, | |
728 made without quoting. Thus, strings are represented by their contents | |
729 alone, with no @samp{"} characters, and symbols appear without @samp{\} | |
730 characters. This is equivalent to printing the object with @code{princ}. | |
731 | |
732 If there is no corresponding object, the empty string is used. | |
733 | |
734 @item %S | |
735 Replace the specification with the printed representation of the object, | |
736 made with quoting. Thus, strings are enclosed in @samp{"} characters, | |
737 and @samp{\} characters appear where necessary before special characters. | |
738 This is equivalent to printing the object with @code{prin1}. | |
739 | |
740 If there is no corresponding object, the empty string is used. | |
741 | |
742 @item %o | |
743 @cindex integer to octal | |
744 Replace the specification with the base-eight representation of an | |
745 integer. | |
746 | |
747 @item %d | |
748 @itemx %i | |
749 Replace the specification with the base-ten representation of an | |
750 integer. | |
751 | |
752 @item %x | |
753 @cindex integer to hexadecimal | |
754 Replace the specification with the base-sixteen representation of an | |
755 integer, using lowercase letters. | |
756 | |
757 @item %X | |
758 @cindex integer to hexadecimal | |
759 Replace the specification with the base-sixteen representation of an | |
760 integer, using uppercase letters. | |
761 | |
4329
d9eb5ea14f65
Provide %b in #'format; use it for converting between ints and bit vectors.
Aidan Kehoe <kehoea@parhasard.net>
parents:
1495
diff
changeset
|
762 @item %b |
d9eb5ea14f65
Provide %b in #'format; use it for converting between ints and bit vectors.
Aidan Kehoe <kehoea@parhasard.net>
parents:
1495
diff
changeset
|
763 @cindex integer to binary |
d9eb5ea14f65
Provide %b in #'format; use it for converting between ints and bit vectors.
Aidan Kehoe <kehoea@parhasard.net>
parents:
1495
diff
changeset
|
764 Replace the specification with the base-two representation of an |
d9eb5ea14f65
Provide %b in #'format; use it for converting between ints and bit vectors.
Aidan Kehoe <kehoea@parhasard.net>
parents:
1495
diff
changeset
|
765 integer. |
d9eb5ea14f65
Provide %b in #'format; use it for converting between ints and bit vectors.
Aidan Kehoe <kehoea@parhasard.net>
parents:
1495
diff
changeset
|
766 |
428 | 767 @item %c |
768 Replace the specification with the character which is the value given. | |
769 | |
770 @item %e | |
771 Replace the specification with the exponential notation for a floating | |
772 point number (e.g. @samp{7.85200e+03}). | |
773 | |
774 @item %f | |
775 Replace the specification with the decimal-point notation for a floating | |
776 point number. | |
777 | |
778 @item %g | |
779 Replace the specification with notation for a floating point number, | |
780 using a ``pretty format''. Either exponential notation or decimal-point | |
781 notation will be used (usually whichever is shorter), and trailing | |
782 zeroes are removed from the fractional part. | |
783 | |
784 @item %% | |
785 A single @samp{%} is placed in the string. This format specification is | |
786 unusual in that it does not use a value. For example, @code{(format "%% | |
787 %d" 30)} returns @code{"% 30"}. | |
788 @end table | |
789 | |
790 Any other format character results in an @samp{Invalid format | |
791 operation} error. | |
792 | |
793 Here are several examples: | |
794 | |
795 @example | |
796 @group | |
797 (format "The name of this buffer is %s." (buffer-name)) | |
798 @result{} "The name of this buffer is strings.texi." | |
799 | |
800 (format "The buffer object prints as %s." (current-buffer)) | |
801 @result{} "The buffer object prints as #<buffer strings.texi>." | |
802 | |
444 | 803 (format "The octal value of %d is %o, |
428 | 804 and the hex value is %x." 18 18 18) |
444 | 805 @result{} "The octal value of 18 is 22, |
428 | 806 and the hex value is 12." |
807 @end group | |
808 @end example | |
809 | |
810 There are many additional flags and specifications that can occur | |
811 between the @samp{%} and the format character, in the following order: | |
812 | |
813 @enumerate | |
814 @item | |
815 An optional repositioning specification, which is a positive | |
816 integer followed by a @samp{$}. | |
817 | |
818 @item | |
819 Zero or more of the optional flag characters @samp{-}, @samp{+}, | |
820 @samp{ }, @samp{0}, and @samp{#}. | |
821 | |
822 @item | |
823 An asterisk (@samp{*}, meaning that the field width is now assumed to | |
824 have been specified as an argument. | |
825 | |
826 @item | |
827 An optional minimum field width. | |
828 | |
829 @item | |
830 An optional precision, preceded by a @samp{.} character. | |
831 @end enumerate | |
832 | |
833 @cindex repositioning format arguments | |
834 @cindex multilingual string formatting | |
835 A @dfn{repositioning} specification changes which argument to | |
836 @code{format} is used by the current and all following format | |
837 specifications. Normally the first specification uses the first | |
838 argument, the second specification uses the second argument, etc. Using | |
839 a repositioning specification, you can change this. By placing a number | |
444 | 840 @var{n} followed by a @samp{$} between the @samp{%} and the format |
841 character, you cause the specification to use the @var{n}th argument. | |
842 The next specification will use the @var{n}+1'th argument, etc. | |
428 | 843 |
844 For example: | |
845 | |
846 @example | |
847 @group | |
848 (format "Can't find file `%s' in directory `%s'." | |
849 "ignatius.c" "loyola/") | |
850 @result{} "Can't find file `ignatius.c' in directory `loyola/'." | |
851 | |
852 (format "In directory `%2$s', the file `%1$s' was not found." | |
853 "ignatius.c" "loyola/") | |
854 @result{} "In directory `loyola/', the file `ignatius.c' was not found." | |
855 | |
856 (format | |
857 "The numbers %d and %d are %1$x and %x in hex and %1$o and %o in octal." | |
858 37 12) | |
859 @result{} "The numbers 37 and 12 are 25 and c in hex and 45 and 14 in octal." | |
860 @end group | |
861 @end example | |
862 | |
863 As you can see, this lets you reprocess arguments more than once or | |
864 reword a format specification (thereby moving the arguments around) | |
865 without having to actually reorder the arguments. This is especially | |
866 useful in translating messages from one language to another: Different | |
867 languages use different word orders, and this sometimes entails changing | |
868 the order of the arguments. By using repositioning specifications, | |
869 this can be accomplished without having to embed knowledge of particular | |
870 languages into the location in the program's code where the message is | |
871 displayed. | |
872 | |
873 @cindex numeric prefix | |
874 @cindex field width | |
875 @cindex padding | |
876 All the specification characters allow an optional numeric prefix | |
877 between the @samp{%} and the character, and following any repositioning | |
878 specification or flag. The optional numeric prefix defines the minimum | |
879 width for the object. If the printed representation of the object | |
880 contains fewer characters than this, then it is padded. The padding is | |
881 normally on the left, but will be on the right if the @samp{-} flag | |
882 character is given. The padding character is normally a space, but if | |
883 the @samp{0} flag character is given, zeros are used for padding. | |
884 | |
885 @example | |
886 (format "%06d is padded on the left with zeros" 123) | |
887 @result{} "000123 is padded on the left with zeros" | |
888 | |
889 (format "%-6d is padded on the right" 123) | |
890 @result{} "123 is padded on the right" | |
891 @end example | |
892 | |
893 @code{format} never truncates an object's printed representation, no | |
894 matter what width you specify. Thus, you can use a numeric prefix to | |
895 specify a minimum spacing between columns with no risk of losing | |
896 information. | |
897 | |
898 In the following three examples, @samp{%7s} specifies a minimum width | |
899 of 7. In the first case, the string inserted in place of @samp{%7s} has | |
900 only 3 letters, so 4 blank spaces are inserted for padding. In the | |
901 second case, the string @code{"specification"} is 13 letters wide but is | |
902 not truncated. In the third case, the padding is on the right. | |
903 | |
444 | 904 @smallexample |
428 | 905 @group |
906 (format "The word `%7s' actually has %d letters in it." | |
907 "foo" (length "foo")) | |
444 | 908 @result{} "The word ` foo' actually has 3 letters in it." |
428 | 909 @end group |
910 | |
911 @group | |
912 (format "The word `%7s' actually has %d letters in it." | |
444 | 913 "specification" (length "specification")) |
914 @result{} "The word `specification' actually has 13 letters in it." | |
428 | 915 @end group |
916 | |
917 @group | |
918 (format "The word `%-7s' actually has %d letters in it." | |
919 "foo" (length "foo")) | |
444 | 920 @result{} "The word `foo ' actually has 3 letters in it." |
428 | 921 @end group |
922 @end smallexample | |
923 | |
924 @cindex format precision | |
925 @cindex precision of formatted numbers | |
926 After any minimum field width, a precision may be specified by | |
927 preceding it with a @samp{.} character. The precision specifies the | |
928 minimum number of digits to appear in @samp{%d}, @samp{%i}, @samp{%o}, | |
929 @samp{%x}, and @samp{%X} conversions (the number is padded on the left | |
930 with zeroes as necessary); the number of digits printed after the | |
931 decimal point for @samp{%f}, @samp{%e}, and @samp{%E} conversions; the | |
932 number of significant digits printed in @samp{%g} and @samp{%G} | |
933 conversions; and the maximum number of non-padding characters printed in | |
934 @samp{%s} and @samp{%S} conversions. The default precision for | |
935 floating-point conversions is six. | |
936 | |
937 The other flag characters have the following meanings: | |
938 | |
939 @itemize @bullet | |
940 @item | |
941 The @samp{ } flag means prefix non-negative numbers with a space. | |
942 | |
943 @item | |
944 The @samp{+} flag means prefix non-negative numbers with a plus sign. | |
945 | |
946 @item | |
947 The @samp{#} flag means print numbers in an alternate, more verbose | |
948 format: octal numbers begin with zero; hex numbers begin with a | |
949 @samp{0x} or @samp{0X}; a decimal point is printed in @samp{%f}, | |
950 @samp{%e}, and @samp{%E} conversions even if no numbers are printed | |
951 after it; and trailing zeroes are not omitted in @samp{%g} and @samp{%G} | |
952 conversions. | |
953 @end itemize | |
954 | |
5791
9fae6227ede5
Silence texinfo 5.2 warnings, primarily by adding next, prev, and up
Jerry James <james@xemacs.org>
parents:
5384
diff
changeset
|
955 @node Character Case, Case Tables, Formatting Strings, Strings and Characters |
428 | 956 @section Character Case |
444 | 957 @cindex upper case |
958 @cindex lower case | |
959 @cindex character case | |
428 | 960 |
961 The character case functions change the case of single characters or | |
962 of the contents of strings. The functions convert only alphabetic | |
963 characters (the letters @samp{A} through @samp{Z} and @samp{a} through | |
964 @samp{z}); other characters are not altered. The functions do not | |
965 modify the strings that are passed to them as arguments. | |
966 | |
967 The examples below use the characters @samp{X} and @samp{x} which have | |
968 @sc{ascii} codes 88 and 120 respectively. | |
969 | |
444 | 970 @defun downcase string-or-char &optional buffer |
428 | 971 This function converts a character or a string to lower case. |
972 | |
973 When the argument to @code{downcase} is a string, the function creates | |
974 and returns a new string in which each letter in the argument that is | |
975 upper case is converted to lower case. When the argument to | |
976 @code{downcase} is a character, @code{downcase} returns the | |
977 corresponding lower case character. (This value is actually an integer | |
978 under XEmacs 19.) If the original character is lower case, or is not a | |
979 letter, then the value equals the original character. | |
980 | |
444 | 981 Optional second arg @var{buffer} specifies which buffer's case tables to |
982 use, and defaults to the current buffer. | |
983 | |
428 | 984 @example |
985 (downcase "The cat in the hat") | |
986 @result{} "the cat in the hat" | |
987 | |
988 (downcase ?X) | |
989 @result{} ?x ;; @r{Under XEmacs 20.} | |
990 @result{} 120 ;; @r{Under XEmacs 19.} | |
991 | |
992 @end example | |
993 @end defun | |
994 | |
444 | 995 @defun upcase string-or-char &optional buffer |
428 | 996 This function converts a character or a string to upper case. |
997 | |
998 When the argument to @code{upcase} is a string, the function creates | |
999 and returns a new string in which each letter in the argument that is | |
1000 lower case is converted to upper case. | |
1001 | |
1002 When the argument to @code{upcase} is a character, @code{upcase} returns | |
1003 the corresponding upper case character. (This value is actually an | |
1004 integer under XEmacs 19.) If the original character is upper case, or | |
1005 is not a letter, then the value equals the original character. | |
1006 | |
444 | 1007 Optional second arg @var{buffer} specifies which buffer's case tables to |
1008 use, and defaults to the current buffer. | |
1009 | |
428 | 1010 @example |
1011 (upcase "The cat in the hat") | |
1012 @result{} "THE CAT IN THE HAT" | |
1013 | |
1014 (upcase ?x) | |
1015 @result{} ?X ;; @r{Under XEmacs 20.} | |
1016 @result{} 88 ;; @r{Under XEmacs 19.} | |
1017 @end example | |
1018 @end defun | |
1019 | |
444 | 1020 @defun capitalize string-or-char &optional buffer |
428 | 1021 @cindex capitalization |
1022 This function capitalizes strings or characters. If | |
1023 @var{string-or-char} is a string, the function creates and returns a new | |
1024 string, whose contents are a copy of @var{string-or-char} in which each | |
1025 word has been capitalized. This means that the first character of each | |
1026 word is converted to upper case, and the rest are converted to lower | |
1027 case. | |
1028 | |
1029 The definition of a word is any sequence of consecutive characters that | |
1030 are assigned to the word constituent syntax class in the current syntax | |
1031 table (@pxref{Syntax Class Table}). | |
1032 | |
1033 When the argument to @code{capitalize} is a character, @code{capitalize} | |
1034 has the same result as @code{upcase}. | |
1035 | |
444 | 1036 Optional second arg @var{buffer} specifies which buffer's case tables to |
1037 use, and defaults to the current buffer. | |
1038 | |
428 | 1039 @example |
1040 (capitalize "The cat in the hat") | |
1041 @result{} "The Cat In The Hat" | |
1042 | |
1043 (capitalize "THE 77TH-HATTED CAT") | |
1044 @result{} "The 77th-Hatted Cat" | |
1045 | |
1046 @group | |
1047 (capitalize ?x) | |
1048 @result{} ?X ;; @r{Under XEmacs 20.} | |
1049 @result{} 88 ;; @r{Under XEmacs 19.} | |
1050 @end group | |
1051 @end example | |
1052 @end defun | |
1053 | |
5791
9fae6227ede5
Silence texinfo 5.2 warnings, primarily by adding next, prev, and up
Jerry James <james@xemacs.org>
parents:
5384
diff
changeset
|
1054 @node Case Tables, Char Tables, Character Case, Strings and Characters |
428 | 1055 @section The Case Table |
1056 | |
1057 You can customize case conversion by installing a special @dfn{case | |
1058 table}. A case table specifies the mapping between upper case and lower | |
1059 case letters. It affects both the string and character case conversion | |
1060 functions (see the previous section) and those that apply to text in the | |
1061 buffer (@pxref{Case Changes}). You need a case table if you are using a | |
1062 language which has letters other than the standard @sc{ascii} letters. | |
1063 | |
1064 A case table is a list of this form: | |
1065 | |
1066 @example | |
1067 (@var{downcase} @var{upcase} @var{canonicalize} @var{equivalences}) | |
1068 @end example | |
1069 | |
1070 @noindent | |
1071 where each element is either @code{nil} or a string of length 256. The | |
1072 element @var{downcase} says how to map each character to its lower-case | |
1073 equivalent. The element @var{upcase} maps each character to its | |
1074 upper-case equivalent. If lower and upper case characters are in | |
1075 one-to-one correspondence, use @code{nil} for @var{upcase}; then XEmacs | |
1076 deduces the upcase table from @var{downcase}. | |
1077 | |
1078 For some languages, upper and lower case letters are not in one-to-one | |
1079 correspondence. There may be two different lower case letters with the | |
1080 same upper case equivalent. In these cases, you need to specify the | |
1081 maps for both directions. | |
1082 | |
1083 The element @var{canonicalize} maps each character to a canonical | |
1084 equivalent; any two characters that are related by case-conversion have | |
1085 the same canonical equivalent character. | |
1086 | |
1087 The element @var{equivalences} is a map that cyclicly permutes each | |
1088 equivalence class (of characters with the same canonical equivalent). | |
1089 (For ordinary @sc{ascii}, this would map @samp{a} into @samp{A} and | |
1090 @samp{A} into @samp{a}, and likewise for each set of equivalent | |
1091 characters.) | |
1092 | |
1093 When you construct a case table, you can provide @code{nil} for | |
1094 @var{canonicalize}; then Emacs fills in this string from @var{upcase} | |
1095 and @var{downcase}. You can also provide @code{nil} for | |
1096 @var{equivalences}; then Emacs fills in this string from | |
1097 @var{canonicalize}. In a case table that is actually in use, those | |
1098 components are non-@code{nil}. Do not try to specify @var{equivalences} | |
1099 without also specifying @var{canonicalize}. | |
1100 | |
1101 Each buffer has a case table. XEmacs also has a @dfn{standard case | |
1102 table} which is copied into each buffer when you create the buffer. | |
1103 Changing the standard case table doesn't affect any existing buffers. | |
1104 | |
1105 Here are the functions for working with case tables: | |
1106 | |
1107 @defun case-table-p object | |
1108 This predicate returns non-@code{nil} if @var{object} is a valid case | |
1109 table. | |
1110 @end defun | |
1111 | |
444 | 1112 @defun set-standard-case-table case-table |
1113 This function makes @var{case-table} the standard case table, so that it | |
1114 will apply to any buffers created subsequently. | |
428 | 1115 @end defun |
1116 | |
1117 @defun standard-case-table | |
1118 This returns the standard case table. | |
1119 @end defun | |
1120 | |
444 | 1121 @defun current-case-table &optional buffer |
1122 This function returns the case table of @var{buffer}, which defaults to | |
1123 the current buffer. | |
428 | 1124 @end defun |
1125 | |
444 | 1126 @defun set-case-table case-table |
1127 This sets the current buffer's case table to @var{case-table}. | |
428 | 1128 @end defun |
1129 | |
1130 The following three functions are convenient subroutines for packages | |
1131 that define non-@sc{ascii} character sets. They modify a string | |
1132 @var{downcase-table} provided as an argument; this should be a string to | |
1133 be used as the @var{downcase} part of a case table. They also modify | |
1134 the standard syntax table. @xref{Syntax Tables}. | |
1135 | |
1136 @defun set-case-syntax-pair uc lc downcase-table | |
1137 This function specifies a pair of corresponding letters, one upper case | |
1138 and one lower case. | |
1139 @end defun | |
1140 | |
1141 @defun set-case-syntax-delims l r downcase-table | |
1142 This function makes characters @var{l} and @var{r} a matching pair of | |
1143 case-invariant delimiters. | |
1144 @end defun | |
1145 | |
1146 @defun set-case-syntax char syntax downcase-table | |
1147 This function makes @var{char} case-invariant, with syntax | |
1148 @var{syntax}. | |
1149 @end defun | |
1150 | |
1151 @deffn Command describe-buffer-case-table | |
1152 This command displays a description of the contents of the current | |
1153 buffer's case table. | |
1154 @end deffn | |
1155 | |
1156 @cindex ISO Latin 1 | |
1157 @pindex iso-syntax | |
1158 You can load the library @file{iso-syntax} to set up the standard syntax | |
1159 table and define a case table for the 8-bit ISO Latin 1 character set. | |
1160 | |
5791
9fae6227ede5
Silence texinfo 5.2 warnings, primarily by adding next, prev, and up
Jerry James <james@xemacs.org>
parents:
5384
diff
changeset
|
1161 @node Char Tables, , Case Tables, Strings and Characters |
428 | 1162 @section The Char Table |
1163 | |
1164 A char table is a table that maps characters (or ranges of characters) | |
1165 to values. Char tables are specialized for characters, only allowing | |
1166 particular sorts of ranges to be assigned values. Although this | |
1167 loses in generality, it makes for extremely fast (constant-time) | |
1168 lookups, and thus is feasible for applications that do an extremely | |
1169 large number of lookups (e.g. scanning a buffer for a character in | |
1170 a particular syntax, where a lookup in the syntax table must occur | |
1171 once per character). | |
1172 | |
1173 Note that char tables as a primitive type, and all of the functions in | |
1174 this section, exist only in XEmacs 20. In XEmacs 19, char tables are | |
1175 generally implemented using a vector of 256 elements. | |
1176 | |
1177 When @sc{mule} support exists, the types of ranges that can be assigned | |
1178 values are | |
1179 | |
1180 @itemize @bullet | |
1181 @item | |
1182 all characters | |
1183 @item | |
1184 an entire charset | |
1185 @item | |
1186 a single row in a two-octet charset | |
1187 @item | |
1188 a single character | |
1189 @end itemize | |
1190 | |
1191 When @sc{mule} support is not present, the types of ranges that can be | |
1192 assigned values are | |
1193 | |
1194 @itemize @bullet | |
1195 @item | |
1196 all characters | |
1197 @item | |
1198 a single character | |
1199 @end itemize | |
1200 | |
1201 @defun char-table-p object | |
1202 This function returns non-@code{nil} if @var{object} is a char table. | |
1203 @end defun | |
1204 | |
1205 @menu | |
1206 * Char Table Types:: Char tables have different uses. | |
1207 * Working With Char Tables:: Creating and working with char tables. | |
1208 @end menu | |
1209 | |
5791
9fae6227ede5
Silence texinfo 5.2 warnings, primarily by adding next, prev, and up
Jerry James <james@xemacs.org>
parents:
5384
diff
changeset
|
1210 @node Char Table Types, Working With Char Tables, Char Tables, Char Tables |
428 | 1211 @subsection Char Table Types |
1212 | |
1213 Each char table type is used for a different purpose and allows different | |
1214 sorts of values. The different char table types are | |
1215 | |
1216 @table @code | |
1217 @item category | |
1218 Used for category tables, which specify the regexp categories | |
1219 that a character is in. The valid values are @code{nil} or a | |
1220 bit vector of 95 elements. Higher-level Lisp functions are | |
1221 provided for working with category tables. Currently categories | |
1222 and category tables only exist when @sc{mule} support is present. | |
1223 @item char | |
1224 A generalized char table, for mapping from one character to | |
1225 another. Used for case tables, syntax matching tables, | |
1226 @code{keyboard-translate-table}, etc. The valid values are characters. | |
1227 @item generic | |
1228 An even more generalized char table, for mapping from a | |
1229 character to anything. | |
1230 @item display | |
1231 Used for display tables, which specify how a particular character | |
1232 is to appear when displayed. #### Not yet implemented. | |
1233 @item syntax | |
1234 Used for syntax tables, which specify the syntax of a particular | |
1235 character. Higher-level Lisp functions are provided for | |
4885
6772ce4d982b
Fix hash tables, #'member*, #'assoc*, #'eql compiler macros if bignums
Aidan Kehoe <kehoea@parhasard.net>
parents:
4329
diff
changeset
|
1236 working with syntax tables. The valid values are fixnums. |
428 | 1237 @end table |
1238 | |
444 | 1239 @defun char-table-type char-table |
1240 This function returns the type of char table @var{char-table}. | |
428 | 1241 @end defun |
1242 | |
1243 @defun char-table-type-list | |
1244 This function returns a list of the recognized char table types. | |
1245 @end defun | |
1246 | |
1247 @defun valid-char-table-type-p type | |
1248 This function returns @code{t} if @var{type} if a recognized char table type. | |
1249 @end defun | |
1250 | |
5791
9fae6227ede5
Silence texinfo 5.2 warnings, primarily by adding next, prev, and up
Jerry James <james@xemacs.org>
parents:
5384
diff
changeset
|
1251 @node Working With Char Tables, , Char Table Types, Char Tables |
428 | 1252 @subsection Working With Char Tables |
1253 | |
1254 @defun make-char-table type | |
1255 This function makes a new, empty char table of type @var{type}. | |
1256 @var{type} should be a symbol, one of @code{char}, @code{category}, | |
1257 @code{display}, @code{generic}, or @code{syntax}. | |
1258 @end defun | |
1259 | |
444 | 1260 @defun put-char-table range value char-table |
1261 This function sets the value for chars in @var{range} to be @var{value} in | |
1262 @var{char-table}. | |
428 | 1263 |
1264 @var{range} specifies one or more characters to be affected and should be | |
1265 one of the following: | |
1266 | |
1267 @itemize @bullet | |
1268 @item | |
1269 @code{t} (all characters are affected) | |
1270 @item | |
1271 A charset (only allowed when @sc{mule} support is present) | |
1272 @item | |
1273 A vector of two elements: a two-octet charset and a row number | |
1274 (only allowed when @sc{mule} support is present) | |
1275 @item | |
1276 A single character | |
1277 @end itemize | |
1278 | |
444 | 1279 @var{value} must be a value appropriate for the type of @var{char-table}. |
428 | 1280 @end defun |
1281 | |
444 | 1282 @defun get-char-table character char-table |
1283 This function finds the value for @var{character} in @var{char-table}. | |
428 | 1284 @end defun |
1285 | |
444 | 1286 @defun get-range-char-table range char-table &optional multi |
1287 This function finds the value for a range in @var{char-table}. If there is | |
428 | 1288 more than one value, @var{multi} is returned (defaults to @code{nil}). |
1289 @end defun | |
1290 | |
444 | 1291 @defun reset-char-table char-table |
1292 This function resets @var{char-table} to its default state. | |
428 | 1293 @end defun |
1294 | |
444 | 1295 @defun map-char-table function char-table &optional range |
1296 This function maps @var{function} over entries in @var{char-table}, calling | |
428 | 1297 it with two args, each key and value in the table. |
1298 | |
1299 @var{range} specifies a subrange to map over and is in the same format | |
1300 as the @var{range} argument to @code{put-range-table}. If omitted or | |
1301 @code{t}, it defaults to the entire table. | |
1302 @end defun | |
1303 | |
1304 @defun valid-char-table-value-p value char-table-type | |
1305 This function returns non-@code{nil} if @var{value} is a valid value for | |
1306 @var{char-table-type}. | |
1307 @end defun | |
1308 | |
1309 @defun check-valid-char-table-value value char-table-type | |
1310 This function signals an error if @var{value} is not a valid value for | |
1311 @var{char-table-type}. | |
1312 @end defun |