771
|
1 /* Header file for text manipulation primitives and macros.
|
|
2 Copyright (C) 1985-1995 Free Software Foundation, Inc.
|
|
3 Copyright (C) 1995 Sun Microsystems, Inc.
|
788
|
4 Copyright (C) 2000, 2001, 2002 Ben Wing.
|
771
|
5
|
|
6 This file is part of XEmacs.
|
|
7
|
|
8 XEmacs is free software; you can redistribute it and/or modify it
|
|
9 under the terms of the GNU General Public License as published by the
|
|
10 Free Software Foundation; either version 2, or (at your option) any
|
|
11 later version.
|
|
12
|
|
13 XEmacs is distributed in the hope that it will be useful, but WITHOUT
|
|
14 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
|
15 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
|
|
16 for more details.
|
|
17
|
|
18 You should have received a copy of the GNU General Public License
|
|
19 along with XEmacs; see the file COPYING. If not, write to
|
|
20 the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
|
|
21 Boston, MA 02111-1307, USA. */
|
|
22
|
|
23 /* Synched up with: FSF 19.30. */
|
|
24
|
|
25 /* Authorship:
|
|
26
|
|
27 Mostly written by Ben Wing, starting around 1995.
|
|
28 Current TO_IN/EXTERNAL_FORMAT macros written by Martin Buchholz,
|
|
29 designed by Ben Wing based on earlier macros by Ben Wing.
|
|
30 Separated out June 18, 2000 from buffer.h into text.h.
|
|
31 */
|
|
32
|
|
33 #ifndef INCLUDED_text_h_
|
|
34 #define INCLUDED_text_h_
|
|
35
|
|
36 #include <wchar.h>
|
|
37
|
|
38 /* ---------------------------------------------------------------------- */
|
|
39 /* Super-basic character properties */
|
|
40 /* ---------------------------------------------------------------------- */
|
|
41
|
|
42 /* These properties define the specifics of how our current encoding fits
|
|
43 in the basic model used for the encoding. Because this model is the same
|
|
44 as is used for UTF-8, all these properties could be defined for it, too.
|
|
45 This would instantly make the rest of this file work with UTF-8 (with
|
|
46 the exception of a few called functions that would need to be redefined).
|
|
47
|
|
48 (UTF-2000 implementers, take note!)
|
|
49 */
|
|
50
|
|
51 /* If you want more than this, you need to include charset.h */
|
|
52
|
|
53 #ifndef MULE
|
|
54
|
826
|
55 #define rep_bytes_by_first_byte(fb) 1
|
|
56 #define byte_ascii_p(byte) 1
|
|
57 #define MAX_EMCHAR_LEN 1
|
771
|
58
|
|
59 #else /* MULE */
|
|
60
|
|
61 /* These are carefully designed to work if BYTE is signed or unsigned. */
|
|
62 /* Note that SPC and DEL are considered ASCII, not control. */
|
|
63
|
826
|
64 #define byte_ascii_p(byte) (((byte) & ~0x7f) == 0)
|
|
65 #define byte_c0_p(byte) (((byte) & ~0x1f) == 0)
|
|
66 #define byte_c1_p(byte) (((byte) & ~0x1f) == 0x80)
|
771
|
67
|
|
68 /* Does BYTE represent the first byte of a character? */
|
|
69
|
826
|
70 #ifdef ERROR_CHECK_TEXT
|
|
71
|
|
72 DECLARE_INLINE_HEADER (
|
|
73 int
|
|
74 intbyte_first_byte_p_1 (int byte, const char *file, int line)
|
|
75 )
|
|
76 {
|
|
77 assert_at_line (byte >= 0 && byte < 256, file, line);
|
|
78 return byte < 0xA0;
|
|
79 }
|
|
80
|
|
81 #define intbyte_first_byte_p(byte) \
|
|
82 intbyte_first_byte_p_1 (byte, __FILE__, __LINE__)
|
|
83
|
|
84 #else
|
|
85
|
|
86 #define intbyte_first_byte_p(byte) ((byte) < 0xA0)
|
|
87
|
|
88 #endif
|
|
89
|
|
90 #ifdef ERROR_CHECK_TEXT
|
771
|
91
|
|
92 /* Does BYTE represent the first byte of a multi-byte character? */
|
|
93
|
826
|
94 DECLARE_INLINE_HEADER (
|
|
95 int
|
|
96 intbyte_leading_byte_p_1 (int byte, const char *file, int line)
|
|
97 )
|
|
98 {
|
|
99 assert_at_line (byte >= 0 && byte < 256, file, line);
|
|
100 return byte_c1_p (byte);
|
|
101 }
|
|
102
|
|
103 #define intbyte_leading_byte_p(byte) \
|
|
104 intbyte_leading_byte_p_1 (byte, __FILE__, __LINE__)
|
|
105
|
|
106 #else
|
|
107
|
|
108 #define intbyte_leading_byte_p(byte) byte_c1_p (byte)
|
|
109
|
|
110 #endif
|
771
|
111
|
|
112 /* Table of number of bytes in the string representation of a character
|
|
113 indexed by the first byte of that representation.
|
|
114
|
|
115 This value can be derived in other ways -- e.g. something like
|
826
|
116 XCHARSET_REP_BYTES (charset_by_leading_byte (first_byte))
|
771
|
117 but it's faster this way. */
|
|
118 extern const Bytecount rep_bytes_by_first_byte[0xA0];
|
|
119
|
|
120 /* Number of bytes in the string representation of a character. */
|
788
|
121
|
800
|
122 #ifdef ERROR_CHECK_TEXT
|
788
|
123
|
826
|
124 DECLARE_INLINE_HEADER (
|
|
125 Bytecount
|
|
126 rep_bytes_by_first_byte_1 (int fb, const char *file, int line)
|
|
127 )
|
771
|
128 {
|
826
|
129 assert_at_line (fb >= 0 && fb < 0xA0, file, line);
|
771
|
130 return rep_bytes_by_first_byte[fb];
|
|
131 }
|
|
132
|
826
|
133 #define rep_bytes_by_first_byte(fb) \
|
|
134 rep_bytes_by_first_byte_1 (fb, __FILE__, __LINE__)
|
788
|
135
|
800
|
136 #else /* ERROR_CHECK_TEXT */
|
788
|
137
|
826
|
138 #define rep_bytes_by_first_byte(fb) (rep_bytes_by_first_byte[fb])
|
788
|
139
|
800
|
140 #endif /* ERROR_CHECK_TEXT */
|
788
|
141
|
826
|
142 /* Is this character represented by more than one byte in a string in the
|
|
143 default format? */
|
|
144
|
|
145 #define emchar_multibyte_p(c) ((c) >= 0x80)
|
|
146
|
|
147 #define emchar_ascii_p(c) (!emchar_multibyte_p (c))
|
|
148
|
|
149 /* Maximum number of bytes per Emacs character when represented as text, in
|
|
150 any format.
|
|
151 */
|
771
|
152
|
|
153 #define MAX_EMCHAR_LEN 4
|
|
154
|
826
|
155 #endif /* not MULE */
|
|
156
|
|
157 /* ---------------- Handling non-default formats ----------------- */
|
|
158
|
|
159 /* We support, at least to some extent, formats other than the default
|
|
160 variable-width format, for speed; all of these alternative formats are
|
|
161 fixed-width. Currently we only handle these non-default formats in
|
|
162 buffers, because access to their text is strictly controlled and thus
|
|
163 the details of the format mostly compartmentalized. The only really
|
|
164 tricky part is the search code -- the regex, Boyer-Moore, and
|
|
165 simple-search algorithms in search.c and regex.c. All other code that
|
|
166 knows directly about the buffer representation is the basic code to
|
|
167 modify or retrieve the buffer text.
|
|
168
|
|
169 Supporting fixed-width formats in Lisp strings is harder, but possible
|
|
170 -- FSF currently does this, for example. In this case, however,
|
|
171 probably only 8-bit-fixed is reasonable for Lisp strings -- getting
|
|
172 non-ASCII-compatible fixed-width formats to work is much, much harder
|
|
173 because a lot of code assumes that strings are ASCII-compatible
|
|
174 (i.e. ASCII + other characters represented exclusively using high-bit
|
|
175 bytes) and a lot of code mixes Lisp strings and non-Lisp strings freely.
|
|
176
|
|
177 The different possible fixed-width formats are 8-bit fixed, 16-bit
|
|
178 fixed, and 32-bit fixed. The latter can represent all possible
|
|
179 characters, but at a substantial memory penalty. The other two can
|
|
180 represent only a subset of the possible characters. How these subsets
|
|
181 are defined can be simple or very tricky.
|
|
182
|
|
183 Currently we support only the default format and the 8-bit fixed format,
|
|
184 and in the latter, we only allow these to be the first 256 characters in
|
|
185 an Emchar (ASCII and Latin 1).
|
|
186
|
|
187 One reasonable approach for 8-bit fixed is to allow the upper half to
|
|
188 represent any 1-byte charset, which is specified on a per-buffer basis.
|
|
189 This should work fairly well in practice since most documents are in
|
|
190 only one foreign language (possibly with some English mixed in). I
|
|
191 think FSF does something like this; or at least, they have something
|
|
192 called nonascii-translation-table and use it when converting from
|
|
193 8-bit-fixed text ("unibyte text") to default text ("multibyte text").
|
|
194 With 16-bit fixed, you could do something like assign chunks of the 64K
|
|
195 worth of characters to charsets as they're encountered in documents.
|
|
196 This should work well with most Asian documents.
|
|
197
|
|
198 If/when we switch to using Unicode internally, we might have formats more
|
|
199 like this:
|
|
200
|
|
201 -- UTF-8 or some extension as the default format. Perl uses an
|
|
202 extension that handles 64-bit chars and requires as much as 13 bytes per
|
|
203 char, vs. the standard of 31-bit chars and 6 bytes max. UTF-8 has the
|
|
204 same basic properties as our own variable-width format (see text.c,
|
|
205 Internal String Encoding) and so most code would not need to be changed.
|
|
206
|
|
207 -- UTF-16 as a "pseudo-fixed" format (i.e. 16-bit fixed plus surrogates
|
|
208 for representing characters not in the BMP, aka >= 65536). The vast
|
|
209 majority of documents will have no surrogates in them so byte/char
|
|
210 conversion will be very fast.
|
|
211
|
|
212 -- an 8-bit fixed format, like currently.
|
|
213
|
|
214 -- possibly, UCS-4 as a 32-bit fixed format.
|
|
215
|
|
216 The fixed-width formats essentially treat the buffer as an array of
|
|
217 8-bit, 16-bit or 32-bit integers. This means that how they are stored
|
|
218 in memory (in particular, big-endian or little-endian) depends on the
|
|
219 native format of the machine's processor. It also means we have to
|
|
220 worry a bit about alignment (basically, we just need to keep the gap an
|
|
221 integral size of the character size, and get things aligned properly
|
|
222 when converting the buffer between formats).
|
|
223 */
|
|
224 typedef enum internal_format
|
|
225 {
|
|
226 FORMAT_DEFAULT,
|
|
227 FORMAT_8_BIT_FIXED,
|
|
228 FORMAT_16_BIT_FIXED, /* not implemented */
|
|
229 FORMAT_32_BIT_FIXED /* not implemented */
|
|
230 } Internal_Format;
|
|
231
|
|
232 #ifdef MULE
|
|
233 /* "OBJECT" below will usually be a buffer, string, or nil. This needs to
|
|
234 be passed in because the interpretation of 8-bit-fixed and 16-bit-fixed
|
|
235 values may depend on the buffer, e.g. depending on what language the
|
|
236 text in the buffer is in. */
|
|
237
|
|
238 /* True if Emchar CH can be represented in 8-bit-fixed format. */
|
|
239 #define emchar_8_bit_fixed_p(ch, object) (((ch) & ~0xff) == 0)
|
|
240 /* Convert Emchar CH to an 8-bit int, as will be stored in the buffer. */
|
|
241 #define emchar_to_raw_8_bit_fixed(ch, object) ((Intbyte) (ch))
|
|
242 /* Convert the other way. */
|
|
243 #define raw_8_bit_fixed_to_emchar(ch, object) ((Emchar) (ch))
|
|
244
|
|
245 #define emchar_16_bit_fixed_p(ch, object) (((ch) & ~0xffff) == 0)
|
|
246 /* Convert Emchar CH to a 16-bit int, as will be stored in the buffer. */
|
|
247 #define emchar_to_raw_16_bit_fixed(ch, object) ((UINT_16_BIT) (ch))
|
|
248 /* Convert the other way. */
|
|
249 #define raw_16_bit_fixed_to_emchar(ch, object) ((Emchar) (ch))
|
|
250
|
|
251 /* Convert Emchar CH to a 32-bit int, as will be stored in the buffer. */
|
|
252 #define emchar_to_raw_32_bit_fixed(ch, object) ((UINT_32_BIT) (ch))
|
|
253 /* Convert the other way. */
|
|
254 #define raw_32_bit_fixed_to_emchar(ch, object) ((Emchar) (ch))
|
|
255
|
|
256 /* Return the "raw value" of a character as stored in the buffer. In the
|
|
257 default format, this is just the same as the character. In fixed-width
|
|
258 formats, this is the actual value in the buffer, which will be limited
|
|
259 to the range as established by the format. This is used when searching
|
|
260 for a character in a buffer -- it's faster to convert the character to
|
|
261 the raw value and look for that, than repeatedly convert each raw value
|
|
262 in the buffer into a character. */
|
|
263
|
|
264 DECLARE_INLINE_HEADER (
|
|
265 Raw_Emchar
|
|
266 emchar_to_raw (Emchar ch, Internal_Format fmt, Lisp_Object object)
|
|
267 )
|
|
268 {
|
|
269 switch (fmt)
|
|
270 {
|
|
271 case FORMAT_DEFAULT:
|
|
272 return (Raw_Emchar) ch;
|
|
273 case FORMAT_16_BIT_FIXED:
|
|
274 text_checking_assert (emchar_16_bit_fixed_p (ch, object));
|
|
275 return (Raw_Emchar) emchar_to_raw_16_bit_fixed (ch, object);
|
|
276 case FORMAT_32_BIT_FIXED:
|
|
277 return (Raw_Emchar) emchar_to_raw_32_bit_fixed (ch, object);
|
|
278 default:
|
|
279 text_checking_assert (fmt == FORMAT_8_BIT_FIXED);
|
|
280 text_checking_assert (emchar_8_bit_fixed_p (ch, object));
|
|
281 return (Raw_Emchar) emchar_to_raw_8_bit_fixed (ch, object);
|
|
282 }
|
|
283 }
|
|
284
|
|
285 /* Return whether CH is representable in the given format in the given
|
|
286 object. */
|
|
287
|
|
288 DECLARE_INLINE_HEADER (
|
|
289 int
|
|
290 emchar_fits_in_format (Emchar ch, Internal_Format fmt, Lisp_Object object)
|
|
291 )
|
|
292 {
|
|
293 switch (fmt)
|
|
294 {
|
|
295 case FORMAT_DEFAULT:
|
|
296 return 1;
|
|
297 case FORMAT_16_BIT_FIXED:
|
|
298 return emchar_16_bit_fixed_p (ch, object);
|
|
299 case FORMAT_32_BIT_FIXED:
|
|
300 return 1;
|
|
301 default:
|
|
302 text_checking_assert (fmt == FORMAT_8_BIT_FIXED);
|
|
303 return emchar_8_bit_fixed_p (ch, object);
|
|
304 }
|
|
305 }
|
|
306
|
|
307 /* Assuming the formats are the same, return whether the two objects
|
|
308 represent text in exactly the same way. */
|
|
309
|
|
310 DECLARE_INLINE_HEADER (
|
|
311 int
|
|
312 objects_have_same_internal_representation (Lisp_Object srcobj,
|
|
313 Lisp_Object dstobj)
|
|
314 )
|
|
315 {
|
|
316 /* &&#### implement this properly when we allow per-object format
|
|
317 differences */
|
|
318 return 1;
|
|
319 }
|
|
320
|
|
321 #else
|
|
322
|
|
323 #define emchar_to_raw(ch, fmt, object) ((Raw_Emchar) (ch))
|
|
324 #define emchar_fits_in_format(ch, fmt, object) 1
|
|
325 #define objects_have_same_internal_representation(srcobj, dstobj) 1
|
|
326
|
771
|
327 #endif /* MULE */
|
|
328
|
|
329 int dfc_coding_system_is_unicode (Lisp_Object coding_system);
|
|
330
|
|
331 DECLARE_INLINE_HEADER (
|
|
332 Bytecount dfc_external_data_len (const void *ptr, Lisp_Object codesys)
|
|
333 )
|
|
334 {
|
|
335 if (dfc_coding_system_is_unicode (codesys))
|
|
336 return sizeof (wchar_t) * wcslen ((wchar_t *) ptr);
|
|
337 else
|
|
338 return strlen ((char *) ptr);
|
|
339 }
|
|
340
|
|
341
|
|
342 /************************************************************************/
|
|
343 /* */
|
|
344 /* working with raw internal-format data */
|
|
345 /* */
|
|
346 /************************************************************************/
|
|
347
|
826
|
348 /*
|
|
349 Use the following functions/macros on contiguous text in any of the
|
|
350 internal formats. Those that take a format arg work on all internal
|
|
351 formats; the others work only on the default (variable-width under Mule)
|
|
352 format. If the text you're operating on is known to come from a buffer,
|
|
353 use the buffer-level functions in buffer.h, which automatically know the
|
|
354 correct format and handle the gap.
|
|
355
|
|
356 Some terminology:
|
|
357
|
|
358 "charptr" appearing in the macros means "internal-format text" -- type
|
|
359 `Intbyte *'. Operations on such pointers themselves, rather than on the
|
|
360 text being pointed to, have "charptr" instead of "charptr" in the macro
|
|
361 name. "emchar" in the macro names means an Emchar -- the representation
|
|
362 of a character as a single integer rather than a series of bytes, as part
|
|
363 of "charptr". Many of the macros below are for converting between the
|
|
364 two representations of characters.
|
|
365
|
|
366 Note also that we try to consistently distinguish between an "Emchar" and
|
|
367 a Lisp character. Stuff working with Lisp characters often just says
|
|
368 "char", so we consistently use "Emchar" when that's what we're working
|
|
369 with. */
|
|
370
|
|
371 /* The three golden rules of macros:
|
771
|
372
|
|
373 1) Anything that's an lvalue can be evaluated more than once.
|
826
|
374
|
|
375 2) Macros where anything else can be evaluated more than once should
|
|
376 have the word "unsafe" in their name (exceptions may be made for
|
|
377 large sets of macros that evaluate arguments of certain types more
|
|
378 than once, e.g. struct buffer * arguments, when clearly indicated in
|
|
379 the macro documentation). These macros are generally meant to be
|
|
380 called only by other macros that have already stored the calling
|
|
381 values in temporary variables.
|
|
382
|
|
383 3) Nothing else can be evaluated more than once. Use inline
|
771
|
384 functions, if necessary, to prevent multiple evaluation.
|
826
|
385
|
|
386 NOTE: The functions and macros below are given full prototypes in their
|
|
387 docs, even when the implementation is a macro. In such cases, passing
|
|
388 an argument of a type other than expected will produce undefined
|
|
389 results. Also, given that macros can do things functions can't (in
|
|
390 particular, directly modify arguments as if they were passed by
|
|
391 reference), the declaration syntax has been extended to include the
|
|
392 call-by-reference syntax from C++, where an & after a type indicates
|
|
393 that the argument is an lvalue and is passed by reference, i.e. the
|
|
394 function can modify its value. (This is equivalent in C to passing a
|
|
395 pointer to the argument, but without the need to explicitly worry about
|
|
396 pointers.)
|
|
397
|
|
398 When to capitalize macros:
|
|
399
|
|
400 -- Capitalize macros doing stuff obviously impossible with (C)
|
|
401 functions, e.g. directly modifying arguments as if they were passed by
|
|
402 reference.
|
|
403
|
|
404 -- Capitalize macros that evaluate *any* argument more than once regardless
|
|
405 of whether that's "allowed" (e.g. buffer arguments).
|
|
406
|
|
407 -- Capitalize macros that directly access a field in a Lisp_Object or
|
|
408 its equivalent underlying structure. In such cases, access through the
|
|
409 Lisp_Object precedes the macro with an X, and access through the underlying
|
|
410 structure doesn't.
|
|
411
|
|
412 -- Capitalize certain other basic macros relating to Lisp_Objects; e.g.
|
|
413 FRAMEP, CHECK_FRAME, etc.
|
|
414
|
|
415 -- Try to avoid capitalizing any other macros.
|
771
|
416 */
|
|
417
|
|
418 /* ---------------------------------------------------------------------- */
|
826
|
419 /* Working with charptr's (pointers to internally-formatted text) */
|
771
|
420 /* ---------------------------------------------------------------------- */
|
|
421
|
826
|
422 /* Given an charptr, does it point to the beginning of a character?
|
|
423 */
|
|
424
|
771
|
425 #ifdef MULE
|
826
|
426 # define valid_charptr_p(ptr) intbyte_first_byte_p (* (ptr))
|
771
|
427 #else
|
826
|
428 # define valid_charptr_p(ptr) 1
|
771
|
429 #endif
|
|
430
|
826
|
431 /* If error-checking is enabled, assert that the given charptr points to
|
|
432 the beginning of a character. Otherwise, do nothing.
|
|
433 */
|
|
434
|
|
435 #define assert_valid_charptr(ptr) text_checking_assert (valid_charptr_p (ptr))
|
|
436
|
|
437 /* Given a charptr (assumed to point at the beginning of a character),
|
|
438 modify that pointer so it points to the beginning of the next character.
|
|
439
|
|
440 Note that INC_CHARPTR() and DEC_CHARPTR() have to be written in
|
771
|
441 completely separate ways. INC_CHARPTR() cannot use the DEC_CHARPTR()
|
|
442 trick of looking for a valid first byte because it might run off
|
|
443 the end of the string. DEC_CHARPTR() can't use the INC_CHARPTR()
|
|
444 method because it doesn't have easy access to the first byte of
|
|
445 the character it's moving over. */
|
|
446
|
826
|
447 #define INC_CHARPTR(ptr) do { \
|
|
448 assert_valid_charptr (ptr); \
|
|
449 (ptr) += rep_bytes_by_first_byte (* (ptr)); \
|
|
450 } while (0)
|
|
451
|
|
452 #define INC_CHARPTR_FMT(ptr, fmt) \
|
|
453 do { \
|
|
454 Internal_Format __icf_fmt = (fmt); \
|
|
455 switch (__icf_fmt) \
|
|
456 { \
|
|
457 case FORMAT_DEFAULT: \
|
|
458 INC_CHARPTR (ptr); \
|
|
459 break; \
|
|
460 case FORMAT_16_BIT_FIXED: \
|
|
461 text_checking_assert (ptr == ALIGN_PTR (ptr, UINT_16_BIT)); \
|
|
462 (ptr) += 2; \
|
|
463 break; \
|
|
464 case FORMAT_32_BIT_FIXED: \
|
|
465 text_checking_assert (ptr == ALIGN_PTR (ptr, UINT_32_BIT)); \
|
|
466 (ptr) += 4; \
|
|
467 break; \
|
|
468 default: \
|
|
469 text_checking_assert (fmt == FORMAT_8_BIT_FIXED); \
|
|
470 (ptr)++; \
|
|
471 break; \
|
|
472 } \
|
|
473 } while (0)
|
|
474
|
|
475 /* Given a charptr (assumed to point at the beginning of a character or at
|
|
476 the very end of the text), modify that pointer so it points to the
|
|
477 beginning of the previous character.
|
|
478 */
|
771
|
479
|
800
|
480 #ifdef ERROR_CHECK_TEXT
|
826
|
481 /* We use a separate definition to avoid warnings about unused dc_ptr1 */
|
|
482 #define DEC_CHARPTR(ptr) do { \
|
|
483 const Intbyte *dc_ptr1 = (ptr); \
|
|
484 do { \
|
|
485 (ptr)--; \
|
|
486 } while (!valid_charptr_p (ptr)); \
|
|
487 text_checking_assert (dc_ptr1 - (ptr) == rep_bytes_by_first_byte (*(ptr))); \
|
771
|
488 } while (0)
|
826
|
489 #else
|
|
490 #define DEC_CHARPTR(ptr) do { \
|
|
491 do { \
|
|
492 (ptr)--; \
|
|
493 } while (!valid_charptr_p (ptr)); \
|
771
|
494 } while (0)
|
826
|
495 #endif /* ERROR_CHECK_TEXT */
|
|
496
|
|
497 #define DEC_CHARPTR_FMT(ptr, fmt) \
|
|
498 do { \
|
|
499 Internal_Format __icf_fmt = (fmt); \
|
|
500 switch (__icf_fmt) \
|
|
501 { \
|
|
502 case FORMAT_DEFAULT: \
|
|
503 DEC_CHARPTR (ptr); \
|
|
504 break; \
|
|
505 case FORMAT_16_BIT_FIXED: \
|
|
506 text_checking_assert (ptr == ALIGN_PTR (ptr, UINT_16_BIT)); \
|
|
507 (ptr) -= 2; \
|
|
508 break; \
|
|
509 case FORMAT_32_BIT_FIXED: \
|
|
510 text_checking_assert (ptr == ALIGN_PTR (ptr, UINT_32_BIT)); \
|
|
511 (ptr) -= 4; \
|
|
512 break; \
|
|
513 default: \
|
|
514 text_checking_assert (fmt == FORMAT_8_BIT_FIXED); \
|
|
515 (ptr)--; \
|
|
516 break; \
|
|
517 } \
|
771
|
518 } while (0)
|
|
519
|
|
520 #ifdef MULE
|
|
521
|
826
|
522 /* Make sure that PTR is pointing to the beginning of a character. If not,
|
|
523 back up until this is the case. Note that there are not too many places
|
|
524 where it is legitimate to do this sort of thing. It's an error if
|
|
525 you're passed an "invalid" char * pointer. NOTE: PTR *must* be pointing
|
|
526 to a valid part of the string (i.e. not the very end, unless the string
|
|
527 is zero-terminated or something) in order for this function to not cause
|
|
528 crashes.
|
|
529 */
|
|
530
|
771
|
531 /* Note that this reads the byte at *PTR! */
|
|
532
|
|
533 #define VALIDATE_CHARPTR_BACKWARD(ptr) do { \
|
826
|
534 while (!valid_charptr_p (ptr)) ptr--; \
|
771
|
535 } while (0)
|
|
536
|
826
|
537 /* Make sure that PTR is pointing to the beginning of a character. If not,
|
|
538 move forward until this is the case. Note that there are not too many
|
|
539 places where it is legitimate to do this sort of thing. It's an error
|
|
540 if you're passed an "invalid" char * pointer.
|
|
541 */
|
771
|
542
|
|
543 /* This needs to be trickier than VALIDATE_CHARPTR_BACKWARD() to avoid the
|
|
544 possibility of running off the end of the string. */
|
|
545
|
|
546 #define VALIDATE_CHARPTR_FORWARD(ptr) do { \
|
|
547 Intbyte *vcf_ptr = (ptr); \
|
|
548 VALIDATE_CHARPTR_BACKWARD (vcf_ptr); \
|
|
549 if (vcf_ptr != (ptr)) \
|
|
550 { \
|
|
551 (ptr) = vcf_ptr; \
|
|
552 INC_CHARPTR (ptr); \
|
|
553 } \
|
|
554 } while (0)
|
|
555
|
|
556 #else /* not MULE */
|
|
557 #define VALIDATE_CHARPTR_BACKWARD(ptr)
|
|
558 #define VALIDATE_CHARPTR_FORWARD(ptr)
|
826
|
559 #endif /* not MULE */
|
|
560
|
|
561 #ifdef MULE
|
|
562
|
|
563 /* Given a Intbyte string at PTR of size N, possibly with a partial
|
|
564 character at the end, return the size of the longest substring of
|
|
565 complete characters. Does not assume that the byte at *(PTR + N) is
|
|
566 readable. Note that there are not too many places where it is
|
|
567 legitimate to do this sort of thing. It's an error if you're passed an
|
|
568 "invalid" offset. */
|
|
569
|
|
570 DECLARE_INLINE_HEADER (
|
|
571 Bytecount
|
|
572 validate_intbyte_string_backward (const Intbyte *ptr, Bytecount n)
|
|
573 )
|
|
574 {
|
|
575 const Intbyte *ptr2;
|
|
576
|
|
577 if (n == 0)
|
|
578 return n;
|
|
579 ptr2 = ptr + n - 1;
|
|
580 VALIDATE_CHARPTR_BACKWARD (ptr2);
|
|
581 if (ptr2 + rep_bytes_by_first_byte (*ptr2) != ptr + n)
|
|
582 return ptr2 - ptr;
|
|
583 return n;
|
|
584 }
|
|
585
|
|
586 #else
|
|
587
|
771
|
588 #define validate_intbyte_string_backward(ptr, n) (n)
|
826
|
589
|
|
590 #endif /* MULE */
|
771
|
591
|
|
592 /* -------------------------------------------------------------- */
|
826
|
593 /* Working with the length (in bytes and characters) of a */
|
|
594 /* section of internally-formatted text */
|
771
|
595 /* -------------------------------------------------------------- */
|
|
596
|
826
|
597 #ifdef MULE
|
|
598
|
|
599 Charcount bytecount_to_charcount_fun (const Intbyte *ptr, Bytecount len);
|
|
600 Bytecount charcount_to_bytecount_fun (const Intbyte *ptr, Charcount len);
|
|
601
|
|
602 /* Given a pointer to a text string and a length in bytes, return
|
|
603 the equivalent length in characters. */
|
|
604
|
|
605 DECLARE_INLINE_HEADER (
|
|
606 Charcount
|
|
607 bytecount_to_charcount (const Intbyte *ptr, Bytecount len)
|
|
608 )
|
|
609 {
|
|
610 if (len < 20) /* Just a random guess, but it should be more or less correct.
|
|
611 If number of bytes is small, just do a simple loop,
|
|
612 which should be more efficient. */
|
|
613 {
|
|
614 Charcount count = 0;
|
|
615 const Intbyte *end = ptr + len;
|
|
616 while (ptr < end)
|
|
617 {
|
|
618 INC_CHARPTR (ptr);
|
|
619 count++;
|
|
620 }
|
|
621 /* Bomb out if the specified substring ends in the middle
|
|
622 of a character. Note that we might have already gotten
|
|
623 a core dump above from an invalid reference, but at least
|
|
624 we will get no farther than here.
|
|
625
|
|
626 This also catches len < 0. */
|
|
627 text_checking_assert (ptr == end);
|
|
628
|
|
629 return count;
|
|
630 }
|
|
631 else
|
|
632 return bytecount_to_charcount_fun (ptr, len);
|
|
633 }
|
|
634
|
|
635 /* Given a pointer to a text string and a length in characters, return the
|
|
636 equivalent length in bytes.
|
|
637 */
|
|
638
|
|
639 DECLARE_INLINE_HEADER (
|
|
640 Bytecount
|
|
641 charcount_to_bytecount (const Intbyte *ptr, Charcount len)
|
|
642 )
|
|
643 {
|
|
644 text_checking_assert (len >= 0);
|
|
645 if (len < 20) /* See above */
|
|
646 {
|
|
647 const Intbyte *newptr = ptr;
|
|
648 while (len > 0)
|
|
649 {
|
|
650 INC_CHARPTR (newptr);
|
|
651 len--;
|
|
652 }
|
|
653 return newptr - ptr;
|
|
654 }
|
|
655 else
|
|
656 return charcount_to_bytecount_fun (ptr, len);
|
|
657 }
|
|
658
|
|
659 /* Given a pointer to a text string in the specified format and a length in
|
|
660 bytes, return the equivalent length in characters.
|
|
661 */
|
|
662
|
|
663 DECLARE_INLINE_HEADER (
|
|
664 Charcount
|
|
665 bytecount_to_charcount_fmt (const Intbyte *ptr, Bytecount len,
|
|
666 Internal_Format fmt)
|
|
667 )
|
|
668 {
|
|
669 switch (fmt)
|
|
670 {
|
|
671 case FORMAT_DEFAULT:
|
|
672 return bytecount_to_charcount (ptr, len);
|
|
673 case FORMAT_16_BIT_FIXED:
|
|
674 text_checking_assert (ptr == ALIGN_PTR (ptr, UINT_16_BIT));
|
|
675 return (Charcount) (len << 1);
|
|
676 case FORMAT_32_BIT_FIXED:
|
|
677 text_checking_assert (ptr == ALIGN_PTR (ptr, UINT_32_BIT));
|
|
678 return (Charcount) (len << 2);
|
|
679 default:
|
|
680 text_checking_assert (fmt == FORMAT_8_BIT_FIXED);
|
|
681 return (Charcount) len;
|
|
682 }
|
|
683 }
|
|
684
|
|
685 /* Given a pointer to a text string in the specified format and a length in
|
|
686 characters, return the equivalent length in bytes.
|
|
687 */
|
|
688
|
|
689 DECLARE_INLINE_HEADER (
|
|
690 Bytecount
|
|
691 charcount_to_bytecount_fmt (const Intbyte *ptr, Charcount len,
|
|
692 Internal_Format fmt)
|
|
693 )
|
|
694 {
|
|
695 switch (fmt)
|
|
696 {
|
|
697 case FORMAT_DEFAULT:
|
|
698 return charcount_to_bytecount (ptr, len);
|
|
699 case FORMAT_16_BIT_FIXED:
|
|
700 text_checking_assert (ptr == ALIGN_PTR (ptr, UINT_16_BIT));
|
|
701 text_checking_assert (!(len & 1));
|
|
702 return (Bytecount) (len >> 1);
|
|
703 case FORMAT_32_BIT_FIXED:
|
|
704 text_checking_assert (!(len & 3));
|
|
705 text_checking_assert (ptr == ALIGN_PTR (ptr, UINT_32_BIT));
|
|
706 return (Bytecount) (len >> 2);
|
|
707 default:
|
|
708 text_checking_assert (fmt == FORMAT_8_BIT_FIXED);
|
|
709 return (Bytecount) len;
|
|
710 }
|
|
711 }
|
|
712
|
|
713 #else
|
|
714
|
|
715 #define bytecount_to_charcount(ptr, len) ((Charcount) (len))
|
|
716 #define bytecount_to_charcount_fmt(ptr, len, fmt) ((Charcount) (len))
|
|
717 #define charcount_to_bytecount(ptr, len) ((Bytecount) (len))
|
|
718 #define charcount_to_bytecount_fmt(ptr, len, fmt) ((Bytecount) (len))
|
|
719
|
|
720 #endif /* MULE */
|
|
721
|
|
722 /* Return the length of the first character at PTR. Equivalent to
|
|
723 charcount_to_bytecount (ptr, 1).
|
|
724
|
|
725 [Since charcount_to_bytecount() is Written as inline, a smart compiler
|
|
726 should really optimize charcount_to_bytecount (ptr, 1) to the same as
|
|
727 the following, with no error checking. But since this idiom occurs so
|
|
728 often, we'll be helpful and define a special macro for it.]
|
|
729 */
|
|
730
|
|
731 #define charptr_emchar_len(ptr) rep_bytes_by_first_byte (*(ptr))
|
|
732
|
|
733 /* Return the length of the first character at PTR, which is in the
|
|
734 specified internal format. Equivalent to charcount_to_bytecount_fmt
|
|
735 (ptr, 1, fmt).
|
|
736 */
|
|
737
|
|
738 DECLARE_INLINE_HEADER (
|
|
739 Bytecount
|
|
740 charptr_emchar_len_fmt (const Intbyte *ptr, Internal_Format fmt)
|
|
741 )
|
|
742 {
|
|
743 switch (fmt)
|
|
744 {
|
|
745 case FORMAT_DEFAULT:
|
|
746 return charptr_emchar_len (ptr);
|
|
747 case FORMAT_16_BIT_FIXED:
|
|
748 text_checking_assert (ptr == ALIGN_PTR (ptr, UINT_16_BIT));
|
|
749 return 2;
|
|
750 case FORMAT_32_BIT_FIXED:
|
|
751 text_checking_assert (ptr == ALIGN_PTR (ptr, UINT_32_BIT));
|
|
752 return 4;
|
|
753 default:
|
|
754 text_checking_assert (fmt == FORMAT_8_BIT_FIXED);
|
|
755 return 1;
|
|
756 }
|
|
757 }
|
|
758
|
|
759 /* Return a pointer to the beginning of the character offset N (in
|
|
760 characters) from PTR.
|
|
761 */
|
|
762
|
|
763 DECLARE_INLINE_HEADER (
|
|
764 const Intbyte *
|
771
|
765 charptr_n_addr (const Intbyte *ptr, Charcount offset)
|
826
|
766 )
|
771
|
767 {
|
|
768 return ptr + charcount_to_bytecount (ptr, offset);
|
|
769 }
|
|
770
|
826
|
771 /* Given a charptr and an offset into the text pointed to by the charptr,
|
|
772 modify the offset so it points to the beginning of the next character.
|
|
773 */
|
|
774
|
|
775 #define INC_BYTECOUNT(ptr, pos) do { \
|
|
776 assert_valid_charptr (ptr); \
|
|
777 (pos += rep_bytes_by_first_byte (* ((ptr) + (pos)))); \
|
|
778 } while (0)
|
|
779
|
771
|
780 /* -------------------------------------------------------------------- */
|
826
|
781 /* Retrieving or changing the character pointed to by a charptr */
|
771
|
782 /* -------------------------------------------------------------------- */
|
|
783
|
|
784 #define simple_charptr_emchar(ptr) ((Emchar) (ptr)[0])
|
814
|
785 #define simple_set_charptr_emchar(ptr, x) \
|
|
786 ((ptr)[0] = (Intbyte) (x), (Bytecount) 1)
|
826
|
787 #define simple_charptr_copy_emchar(src, dst) \
|
814
|
788 ((dst)[0] = *(src), (Bytecount) 1)
|
771
|
789
|
|
790 #ifdef MULE
|
|
791
|
|
792 Emchar non_ascii_charptr_emchar (const Intbyte *ptr);
|
|
793 Bytecount non_ascii_set_charptr_emchar (Intbyte *ptr, Emchar c);
|
826
|
794 Bytecount non_ascii_charptr_copy_emchar (const Intbyte *src, Intbyte *dst);
|
|
795
|
|
796 /* Retrieve the character pointed to by PTR as an Emchar. */
|
|
797
|
|
798 DECLARE_INLINE_HEADER (
|
|
799 Emchar
|
771
|
800 charptr_emchar (const Intbyte *ptr)
|
826
|
801 )
|
771
|
802 {
|
826
|
803 return byte_ascii_p (*ptr) ?
|
771
|
804 simple_charptr_emchar (ptr) :
|
|
805 non_ascii_charptr_emchar (ptr);
|
|
806 }
|
|
807
|
826
|
808 /* Retrieve the character pointed to by PTR (a pointer to text in the
|
|
809 format FMT, coming from OBJECT [a buffer, string?, or nil]) as an
|
|
810 Emchar.
|
|
811
|
|
812 Note: For these and other *_fmt() functions, if you pass in a constant
|
|
813 FMT, the switch will be optimized out of existence. Therefore, there is
|
|
814 no need to create separate versions for the various formats for
|
|
815 "efficiency reasons". In fact, we don't really need charptr_emchar()
|
|
816 and such written separately, but they are used often so it's simpler
|
|
817 that way. */
|
|
818
|
|
819 DECLARE_INLINE_HEADER (
|
|
820 Emchar
|
|
821 charptr_emchar_fmt (const Intbyte *ptr, Internal_Format fmt,
|
|
822 Lisp_Object object)
|
|
823 )
|
|
824 {
|
|
825 switch (fmt)
|
|
826 {
|
|
827 case FORMAT_DEFAULT:
|
|
828 return charptr_emchar (ptr);
|
|
829 case FORMAT_16_BIT_FIXED:
|
|
830 text_checking_assert (ptr == ALIGN_PTR (ptr, UINT_16_BIT));
|
|
831 return raw_16_bit_fixed_to_emchar (* (UINT_16_BIT *) ptr, object);
|
|
832 case FORMAT_32_BIT_FIXED:
|
|
833 text_checking_assert (ptr == ALIGN_PTR (ptr, UINT_32_BIT));
|
|
834 return raw_32_bit_fixed_to_emchar (* (UINT_32_BIT *) ptr, object);
|
|
835 default:
|
|
836 text_checking_assert (fmt == FORMAT_8_BIT_FIXED);
|
|
837 return raw_8_bit_fixed_to_emchar (*ptr, object);
|
|
838 }
|
|
839 }
|
|
840
|
|
841 /* Return the character at PTR (which is in format FMT), suitable for
|
|
842 comparison with an ASCII character. This guarantees that if the
|
|
843 character at PTR is ASCII (range 0 - 127), that character will be
|
|
844 returned; otherwise, some character outside of the ASCII range will be
|
|
845 returned, but not necessarily the character actually at PTR. This will
|
|
846 be faster than charptr_emchar_fmt() for some formats -- in particular,
|
|
847 FORMAT_DEFAULT. */
|
|
848
|
|
849 DECLARE_INLINE_HEADER (
|
|
850 Emchar
|
|
851 charptr_emchar_ascii_fmt (const Intbyte *ptr, Internal_Format fmt,
|
|
852 Lisp_Object object)
|
|
853 )
|
|
854 {
|
|
855 switch (fmt)
|
|
856 {
|
|
857 case FORMAT_DEFAULT:
|
|
858 return (Emchar) *ptr;
|
|
859 case FORMAT_16_BIT_FIXED:
|
|
860 text_checking_assert (ptr == ALIGN_PTR (ptr, UINT_16_BIT));
|
|
861 return raw_16_bit_fixed_to_emchar (* (UINT_16_BIT *) ptr, object);
|
|
862 case FORMAT_32_BIT_FIXED:
|
|
863 text_checking_assert (ptr == ALIGN_PTR (ptr, UINT_32_BIT));
|
|
864 return raw_32_bit_fixed_to_emchar (* (UINT_32_BIT *) ptr, object);
|
|
865 default:
|
|
866 text_checking_assert (fmt == FORMAT_8_BIT_FIXED);
|
|
867 return raw_8_bit_fixed_to_emchar (*ptr, object);
|
|
868 }
|
|
869 }
|
|
870
|
|
871 /* Return the "raw value" of the character at PTR, in format FMT. This is
|
|
872 useful when searching for a character; convert the character using
|
|
873 emchar_to_raw(). */
|
|
874
|
|
875 DECLARE_INLINE_HEADER (
|
|
876 Raw_Emchar
|
|
877 charptr_emchar_raw_fmt (const Intbyte *ptr, Internal_Format fmt)
|
|
878 )
|
|
879 {
|
|
880 switch (fmt)
|
|
881 {
|
|
882 case FORMAT_DEFAULT:
|
|
883 return (Raw_Emchar) charptr_emchar (ptr);
|
|
884 case FORMAT_16_BIT_FIXED:
|
|
885 text_checking_assert (ptr == ALIGN_PTR (ptr, UINT_16_BIT));
|
|
886 return (Raw_Emchar) (* (UINT_16_BIT *) ptr);
|
|
887 case FORMAT_32_BIT_FIXED:
|
|
888 text_checking_assert (ptr == ALIGN_PTR (ptr, UINT_32_BIT));
|
|
889 return (Raw_Emchar) (* (UINT_32_BIT *) ptr);
|
|
890 default:
|
|
891 text_checking_assert (fmt == FORMAT_8_BIT_FIXED);
|
|
892 return (Raw_Emchar) (*ptr);
|
|
893 }
|
|
894 }
|
|
895
|
|
896 /* Store the character CH (an Emchar) as internally-formatted text starting
|
|
897 at PTR. Return the number of bytes stored.
|
|
898 */
|
|
899
|
|
900 DECLARE_INLINE_HEADER (
|
|
901 Bytecount
|
771
|
902 set_charptr_emchar (Intbyte *ptr, Emchar x)
|
826
|
903 )
|
771
|
904 {
|
826
|
905 return !emchar_multibyte_p (x) ?
|
771
|
906 simple_set_charptr_emchar (ptr, x) :
|
|
907 non_ascii_set_charptr_emchar (ptr, x);
|
|
908 }
|
|
909
|
826
|
910 /* Store the character CH (an Emchar) as internally-formatted text of
|
|
911 format FMT starting at PTR, which comes from OBJECT. Return the number
|
|
912 of bytes stored.
|
|
913 */
|
|
914
|
|
915 DECLARE_INLINE_HEADER (
|
|
916 Bytecount
|
|
917 set_charptr_emchar_fmt (Intbyte *ptr, Emchar x, Internal_Format fmt,
|
|
918 Lisp_Object object)
|
|
919 )
|
771
|
920 {
|
826
|
921 switch (fmt)
|
|
922 {
|
|
923 case FORMAT_DEFAULT:
|
|
924 return set_charptr_emchar (ptr, x);
|
|
925 case FORMAT_16_BIT_FIXED:
|
|
926 text_checking_assert (emchar_16_bit_fixed_p (x, object));
|
|
927 text_checking_assert (ptr == ALIGN_PTR (ptr, UINT_16_BIT));
|
|
928 * (UINT_16_BIT *) ptr = emchar_to_raw_16_bit_fixed (x, object);
|
|
929 return 2;
|
|
930 case FORMAT_32_BIT_FIXED:
|
|
931 text_checking_assert (ptr == ALIGN_PTR (ptr, UINT_32_BIT));
|
|
932 * (UINT_32_BIT *) ptr = emchar_to_raw_32_bit_fixed (x, object);
|
|
933 return 4;
|
|
934 default:
|
|
935 text_checking_assert (fmt == FORMAT_8_BIT_FIXED);
|
|
936 text_checking_assert (emchar_8_bit_fixed_p (x, object));
|
|
937 *ptr = emchar_to_raw_8_bit_fixed (x, object);
|
|
938 return 1;
|
|
939 }
|
|
940 }
|
|
941
|
|
942 /* Retrieve the character pointed to by SRC and store it as
|
|
943 internally-formatted text in DST.
|
|
944 */
|
|
945
|
|
946 DECLARE_INLINE_HEADER (
|
|
947 Bytecount
|
|
948 charptr_copy_emchar (const Intbyte *src, Intbyte *dst)
|
|
949 )
|
|
950 {
|
|
951 return byte_ascii_p (*src) ?
|
|
952 simple_charptr_copy_emchar (src, dst) :
|
|
953 non_ascii_charptr_copy_emchar (src, dst);
|
771
|
954 }
|
|
955
|
|
956 #else /* not MULE */
|
|
957
|
826
|
958 # define charptr_emchar(ptr) simple_charptr_emchar (ptr)
|
|
959 # define charptr_emchar_fmt(ptr, fmt, object) charptr_emchar (ptr)
|
|
960 # define charptr_emchar_ascii_fmt(ptr, fmt, object) charptr_emchar (ptr)
|
|
961 # define charptr_emchar_raw_fmt(ptr, fmt) charptr_emchar (ptr)
|
|
962 # define set_charptr_emchar(ptr, x) simple_set_charptr_emchar (ptr, x)
|
|
963 # define set_charptr_emchar_fmt(ptr, x, fmt, obj) set_charptr_emchar (ptr, x)
|
|
964 # define charptr_copy_emchar(src, dst) simple_charptr_copy_emchar (src, dst)
|
771
|
965
|
|
966 #endif /* not MULE */
|
|
967
|
826
|
968 /* Retrieve the character at offset N (in characters) from PTR, as an
|
|
969 Emchar.
|
|
970 */
|
|
971
|
771
|
972 #define charptr_emchar_n(ptr, offset) \
|
|
973 charptr_emchar (charptr_n_addr (ptr, offset))
|
|
974
|
|
975
|
|
976 /* ---------------------------- */
|
826
|
977 /* Working with Emchars */
|
771
|
978 /* ---------------------------- */
|
|
979
|
826
|
980 /* NOTE: There are other functions/macros for working with Emchars in
|
|
981 charset.h, for retrieving the charset of an Emchar, the length of an
|
|
982 Emchar when converted to text, etc.
|
|
983 */
|
|
984
|
771
|
985 #ifdef MULE
|
|
986
|
826
|
987 int non_ascii_valid_emchar_p (Emchar ch);
|
|
988
|
|
989 /* Return whether the given Emchar is valid.
|
|
990 */
|
|
991
|
|
992 DECLARE_INLINE_HEADER (
|
|
993 int
|
|
994 valid_emchar_p (Emchar ch)
|
|
995 )
|
771
|
996 {
|
826
|
997 return (! (ch & ~0xFF)) || non_ascii_valid_emchar_p (ch);
|
771
|
998 }
|
|
999
|
|
1000 #else /* not MULE */
|
|
1001
|
826
|
1002 #define valid_emchar_p(ch) (! (ch & ~0xFF))
|
771
|
1003
|
|
1004 #endif /* not MULE */
|
|
1005
|
831
|
1006 DECLARE_INLINE_HEADER (
|
|
1007 Lisp_Object
|
|
1008 make_char (Emchar val)
|
|
1009 )
|
|
1010 {
|
|
1011 type_checking_assert (valid_emchar_p (val));
|
|
1012 return make_char_1 (val);
|
|
1013 }
|
|
1014
|
826
|
1015 #define CHAR_INTP(x) (INTP (x) && valid_emchar_p (XINT (x)))
|
771
|
1016
|
|
1017 #define CHAR_OR_CHAR_INTP(x) (CHARP (x) || CHAR_INTP (x))
|
|
1018
|
826
|
1019 DECLARE_INLINE_HEADER (
|
|
1020 Emchar
|
771
|
1021 XCHAR_OR_CHAR_INT (Lisp_Object obj)
|
826
|
1022 )
|
771
|
1023 {
|
|
1024 return CHARP (obj) ? XCHAR (obj) : XINT (obj);
|
|
1025 }
|
|
1026
|
826
|
1027 /* Signal an error if CH is not a valid character or integer Lisp_Object.
|
|
1028 If CH is an integer Lisp_Object, convert it to a character Lisp_Object,
|
|
1029 but merely by repackaging, without performing tests for char validity.
|
|
1030 */
|
|
1031
|
771
|
1032 #define CHECK_CHAR_COERCE_INT(x) do { \
|
|
1033 if (CHARP (x)) \
|
|
1034 ; \
|
|
1035 else if (CHAR_INTP (x)) \
|
|
1036 x = make_char (XINT (x)); \
|
|
1037 else \
|
|
1038 x = wrong_type_argument (Qcharacterp, x); \
|
|
1039 } while (0)
|
|
1040
|
|
1041
|
|
1042
|
|
1043 /************************************************************************/
|
|
1044 /* */
|
826
|
1045 /* working with Lisp strings */
|
|
1046 /* */
|
|
1047 /************************************************************************/
|
|
1048
|
|
1049 #define string_char_length(s) \
|
|
1050 string_index_byte_to_char (s, XSTRING_LENGTH (s))
|
|
1051 #define string_byte(s, i) (XSTRING_DATA (s)[i] + 0)
|
|
1052 /* In case we ever allow strings to be in a different format ... */
|
|
1053 #define set_string_byte(s, i, c) (XSTRING_DATA (s)[i] = (c))
|
|
1054
|
|
1055 #define ASSERT_VALID_CHAR_STRING_INDEX_UNSAFE(s, x) do { \
|
|
1056 text_checking_assert ((x) >= 0 && x <= string_char_length (s)); \
|
|
1057 } while (0)
|
|
1058
|
|
1059 #define ASSERT_VALID_BYTE_STRING_INDEX_UNSAFE(s, x) do { \
|
|
1060 text_checking_assert ((x) >= 0 && x <= XSTRING_LENGTH (s)); \
|
|
1061 text_checking_assert (valid_charptr_p (string_byte_addr (s, x))); \
|
|
1062 } while (0)
|
|
1063
|
|
1064 /* Convert offset I in string S to a pointer to text there. */
|
|
1065 #define string_byte_addr(s, i) (&(XSTRING_DATA (s)[i]))
|
|
1066 /* Convert pointer to text in string S into the byte offset to that text. */
|
|
1067 #define string_addr_to_byte(s, ptr) ((Bytecount) ((ptr) - XSTRING_DATA (s)))
|
|
1068 /* Return the Emchar at *CHARACTER* offset I. */
|
|
1069 #define string_emchar(s, i) charptr_emchar (string_char_addr (s, i))
|
|
1070
|
|
1071 #ifdef ERROR_CHECK_TEXT
|
|
1072 #define SLEDGEHAMMER_CHECK_ASCII_BEGIN
|
|
1073 #endif
|
|
1074
|
|
1075 #ifdef SLEDGEHAMMER_CHECK_ASCII_BEGIN
|
|
1076 void sledgehammer_check_ascii_begin (Lisp_Object str);
|
|
1077 #else
|
|
1078 #define sledgehammer_check_ascii_begin(str)
|
|
1079 #endif
|
|
1080
|
|
1081 /* Make an alloca'd copy of a Lisp string */
|
|
1082 #define LISP_STRING_TO_ALLOCA(s, lval) \
|
|
1083 do { \
|
|
1084 Intbyte **_lta_ = (Intbyte **) &(lval); \
|
|
1085 Lisp_Object _lta_2 = (s); \
|
|
1086 *_lta_ = alloca_array (Intbyte, 1 + XSTRING_LENGTH (_lta_2)); \
|
|
1087 memcpy (*_lta_, XSTRING_DATA (_lta_2), 1 + XSTRING_LENGTH (_lta_2)); \
|
|
1088 } while (0)
|
|
1089
|
|
1090 /* Make an alloca'd copy of a Intbyte * */
|
|
1091 #define INTBYTE_STRING_TO_ALLOCA(p, lval) \
|
|
1092 do { \
|
|
1093 Intbyte **_bsta_ = (Intbyte **) &(lval); \
|
|
1094 const Intbyte *_bsta_2 = (p); \
|
|
1095 Bytecount _bsta_3 = qxestrlen (_bsta_2); \
|
|
1096 *_bsta_ = alloca_array (Intbyte, 1 + _bsta_3); \
|
|
1097 memcpy (*_bsta_, _bsta_2, 1 + _bsta_3); \
|
|
1098 } while (0)
|
|
1099
|
|
1100 #define alloca_intbytes(num) alloca_array (Intbyte, num)
|
|
1101 #define alloca_extbytes(num) alloca_array (Extbyte, num)
|
|
1102
|
|
1103 void resize_string (Lisp_Object s, Bytecount pos, Bytecount delta);
|
|
1104
|
|
1105 /* Convert a byte index into a string into a char index. */
|
|
1106 DECLARE_INLINE_HEADER (
|
|
1107 Charcount
|
|
1108 string_index_byte_to_char (Lisp_Object s, Bytecount idx)
|
|
1109 )
|
|
1110 {
|
|
1111 Charcount retval;
|
|
1112 ASSERT_VALID_BYTE_STRING_INDEX_UNSAFE (s, idx);
|
|
1113 #ifdef MULE
|
|
1114 if (idx <= (Bytecount) XSTRING_ASCII_BEGIN (s))
|
|
1115 retval = (Charcount) idx;
|
|
1116 else
|
|
1117 retval = (XSTRING_ASCII_BEGIN (s) +
|
|
1118 bytecount_to_charcount (XSTRING_DATA (s) +
|
|
1119 XSTRING_ASCII_BEGIN (s),
|
|
1120 idx - XSTRING_ASCII_BEGIN (s)));
|
|
1121 # ifdef SLEDGEHAMMER_CHECK_ASCII_BEGIN
|
|
1122 assert (retval == bytecount_to_charcount (XSTRING_DATA (s), idx));
|
|
1123 # endif
|
|
1124 #else
|
|
1125 retval = (Charcount) idx;
|
|
1126 #endif
|
|
1127 /* Don't call ASSERT_VALID_CHAR_STRING_INDEX_UNSAFE() here because it will
|
|
1128 call string_index_byte_to_char(). */
|
|
1129 return retval;
|
|
1130 }
|
|
1131
|
|
1132 /* Convert a char index into a string into a byte index. */
|
|
1133 DECLARE_INLINE_HEADER (
|
|
1134 Bytecount
|
|
1135 string_index_char_to_byte (Lisp_Object s, Charcount idx)
|
|
1136 )
|
|
1137 {
|
|
1138 Bytecount retval;
|
|
1139 ASSERT_VALID_CHAR_STRING_INDEX_UNSAFE (s, idx);
|
|
1140 #ifdef MULE
|
|
1141 if (idx <= (Charcount) XSTRING_ASCII_BEGIN (s))
|
|
1142 retval = (Bytecount) idx;
|
|
1143 else
|
|
1144 retval = (XSTRING_ASCII_BEGIN (s) +
|
|
1145 charcount_to_bytecount (XSTRING_DATA (s) +
|
|
1146 XSTRING_ASCII_BEGIN (s),
|
|
1147 idx - XSTRING_ASCII_BEGIN (s)));
|
|
1148 # ifdef SLEDGEHAMMER_CHECK_ASCII_BEGIN
|
|
1149 assert (retval == charcount_to_bytecount (XSTRING_DATA (s), idx));
|
|
1150 # endif
|
|
1151 #else
|
|
1152 retval = (Bytecount) idx;
|
|
1153 #endif
|
|
1154 ASSERT_VALID_BYTE_STRING_INDEX_UNSAFE (s, retval);
|
|
1155 return retval;
|
|
1156 }
|
|
1157
|
|
1158 /* Convert a substring length (starting at byte offset OFF) from bytes to
|
|
1159 chars. */
|
|
1160 DECLARE_INLINE_HEADER (
|
|
1161 Charcount
|
|
1162 string_offset_byte_to_char_len (Lisp_Object s, Bytecount off, Bytecount len)
|
|
1163 )
|
|
1164 {
|
|
1165 Charcount retval;
|
|
1166 ASSERT_VALID_BYTE_STRING_INDEX_UNSAFE (s, off);
|
|
1167 ASSERT_VALID_BYTE_STRING_INDEX_UNSAFE (s, off + len);
|
|
1168 #ifdef MULE
|
|
1169 if (off + len <= (Bytecount) XSTRING_ASCII_BEGIN (s))
|
|
1170 retval = (Charcount) len;
|
|
1171 else if (off < (Bytecount) XSTRING_ASCII_BEGIN (s))
|
|
1172 retval =
|
|
1173 XSTRING_ASCII_BEGIN (s) - (Charcount) off +
|
|
1174 bytecount_to_charcount (XSTRING_DATA (s) + XSTRING_ASCII_BEGIN (s),
|
|
1175 len - (XSTRING_ASCII_BEGIN (s) - off));
|
|
1176 else
|
|
1177 retval = bytecount_to_charcount (XSTRING_DATA (s) + off, len);
|
|
1178 # ifdef SLEDGEHAMMER_CHECK_ASCII_BEGIN
|
|
1179 assert (retval == bytecount_to_charcount (XSTRING_DATA (s) + off, len));
|
|
1180 # endif
|
|
1181 #else
|
|
1182 retval = (Charcount) len;
|
|
1183 #endif
|
|
1184 return retval;
|
|
1185 }
|
|
1186
|
|
1187 /* Convert a substring length (starting at byte offset OFF) from chars to
|
|
1188 bytes. */
|
|
1189 DECLARE_INLINE_HEADER (
|
|
1190 Bytecount
|
|
1191 string_offset_char_to_byte_len (Lisp_Object s, Bytecount off, Charcount len)
|
|
1192 )
|
|
1193 {
|
|
1194 Bytecount retval;
|
|
1195 ASSERT_VALID_BYTE_STRING_INDEX_UNSAFE (s, off);
|
|
1196 #ifdef MULE
|
|
1197 /* casts to avoid errors from combining Bytecount/Charcount and warnings
|
|
1198 from signed/unsigned comparisons */
|
|
1199 if (off + (Bytecount) len <= (Bytecount) XSTRING_ASCII_BEGIN (s))
|
|
1200 retval = (Bytecount) len;
|
|
1201 else if (off < (Bytecount) XSTRING_ASCII_BEGIN (s))
|
|
1202 retval =
|
|
1203 XSTRING_ASCII_BEGIN (s) - off +
|
|
1204 charcount_to_bytecount (XSTRING_DATA (s) + XSTRING_ASCII_BEGIN (s),
|
|
1205 len - (XSTRING_ASCII_BEGIN (s) -
|
|
1206 (Charcount) off));
|
|
1207 else
|
|
1208 retval = charcount_to_bytecount (XSTRING_DATA (s) + off, len);
|
|
1209 # ifdef SLEDGEHAMMER_CHECK_ASCII_BEGIN
|
|
1210 assert (retval == charcount_to_bytecount (XSTRING_DATA (s) + off, len));
|
|
1211 # endif
|
|
1212 #else
|
|
1213 retval = (Bytecount) len;
|
|
1214 #endif
|
|
1215 ASSERT_VALID_BYTE_STRING_INDEX_UNSAFE (s, off + retval);
|
|
1216 return retval;
|
|
1217 }
|
|
1218
|
|
1219 DECLARE_INLINE_HEADER (
|
|
1220 const Intbyte *
|
|
1221 string_char_addr (Lisp_Object s, Charcount idx)
|
|
1222 )
|
|
1223 {
|
|
1224 return XSTRING_DATA (s) + string_index_char_to_byte (s, idx);
|
|
1225 }
|
|
1226
|
|
1227 /* WARNING: If you modify an existing string, you must call
|
|
1228 bump_string_modiff() afterwards. */
|
|
1229 #ifdef MULE
|
|
1230 void set_string_char (Lisp_Object s, Charcount i, Emchar c);
|
|
1231 #else
|
|
1232 #define set_string_char(s, i, c) set_string_byte (s, i, c)
|
|
1233 #endif /* not MULE */
|
|
1234
|
|
1235 /* Return index to character before the one at IDX. */
|
|
1236 DECLARE_INLINE_HEADER (
|
|
1237 Bytecount
|
|
1238 prev_string_index (Lisp_Object s, Bytecount idx)
|
|
1239 )
|
|
1240 {
|
|
1241 const Intbyte *ptr = string_byte_addr (s, idx);
|
|
1242 DEC_CHARPTR (ptr);
|
|
1243 return string_addr_to_byte (s, ptr);
|
|
1244 }
|
|
1245
|
|
1246 /* Return index to character after the one at IDX. */
|
|
1247 DECLARE_INLINE_HEADER (
|
|
1248 Bytecount
|
|
1249 next_string_index (Lisp_Object s, Bytecount idx)
|
|
1250 )
|
|
1251 {
|
|
1252 const Intbyte *ptr = string_byte_addr (s, idx);
|
|
1253 INC_CHARPTR (ptr);
|
|
1254 return string_addr_to_byte (s, ptr);
|
|
1255 }
|
|
1256
|
|
1257
|
|
1258 /************************************************************************/
|
|
1259 /* */
|
771
|
1260 /* working with Eistrings */
|
|
1261 /* */
|
|
1262 /************************************************************************/
|
|
1263
|
|
1264 /*
|
|
1265 #### NOTE: This is a work in progress. Neither the API nor especially
|
|
1266 the implementation is finished.
|
|
1267
|
|
1268 NOTE: An Eistring is a structure that makes it easy to work with
|
|
1269 internally-formatted strings of data. It provides operations similar
|
|
1270 in feel to the standard strcpy(), strcat(), strlen(), etc., but
|
|
1271
|
|
1272 (a) it is Mule-correct
|
|
1273 (b) it does dynamic allocation so you never have to worry about size
|
793
|
1274 restrictions
|
|
1275 (c) it comes in an alloca() variety (all allocation is stack-local,
|
|
1276 so there is no need to explicitly clean up) as well as a malloc()
|
|
1277 variety
|
|
1278 (d) it knows its own length, so it does not suffer from standard null
|
|
1279 byte brain-damage -- but it null-terminates the data anyway, so
|
|
1280 it can be passed to standard routines
|
|
1281 (e) it provides a much more powerful set of operations and knows about
|
771
|
1282 all the standard places where string data might reside: Lisp_Objects,
|
|
1283 other Eistrings, Intbyte * data with or without an explicit length,
|
|
1284 ASCII strings, Emchars, etc.
|
793
|
1285 (f) it provides easy operations to convert to/from externally-formatted
|
|
1286 data, and is easier to use than the standard TO_INTERNAL_FORMAT
|
771
|
1287 and TO_EXTERNAL_FORMAT macros. (An Eistring can store both the internal
|
|
1288 and external version of its data, but the external version is only
|
|
1289 initialized or changed when you call eito_external().)
|
|
1290
|
793
|
1291 The idea is to make it as easy to write Mule-correct string manipulation
|
|
1292 code as it is to write normal string manipulation code. We also make
|
|
1293 the API sufficiently general that it can handle multiple internal data
|
|
1294 formats (e.g. some fixed-width optimizing formats and a default variable
|
|
1295 width format) and allows for *ANY* data format we might choose in the
|
|
1296 future for the default format, including UCS2. (In other words, we can't
|
|
1297 assume that the internal format is ASCII-compatible and we can't assume
|
|
1298 it doesn't have embedded null bytes. We do assume, however, that any
|
|
1299 chosen format will have the concept of null-termination.) All of this is
|
|
1300 hidden from the user.
|
771
|
1301
|
|
1302 #### It is really too bad that we don't have a real object-oriented
|
|
1303 language, or at least a language with polymorphism!
|
|
1304
|
|
1305
|
|
1306 **********************************************
|
|
1307 * Declaration *
|
|
1308 **********************************************
|
|
1309
|
|
1310 To declare an Eistring, either put one of the following in the local
|
|
1311 variable section:
|
|
1312
|
|
1313 DECLARE_EISTRING (name);
|
|
1314 Declare a new Eistring. This is a standard local variable declaration
|
|
1315 and can go anywhere in the variable declaration section. NAME itself
|
|
1316 is declared as an Eistring *, and its storage declared on the stack.
|
|
1317
|
|
1318 DECLARE_EISTRING_MALLOC (name);
|
|
1319 Declare a new Eistring, which uses malloc()ed instead of alloca()ed
|
|
1320 data. This is a standard local variable declaration and can go
|
|
1321 anywhere in the variable declaration section. Once you initialize
|
|
1322 the Eistring, you will have to free it using eifree() to avoid
|
793
|
1323 memory leaks. You will need to use this form if you are passing
|
|
1324 an Eistring to any function that modifies it (otherwise, the
|
|
1325 modified data may be in stack space and get overwritten when the
|
|
1326 function returns).
|
771
|
1327
|
|
1328 or use
|
|
1329
|
793
|
1330 Eistring ei;
|
|
1331 void eiinit (Eistring *ei);
|
|
1332 void eiinit_malloc (Eistring *einame);
|
771
|
1333 If you need to put an Eistring elsewhere than in a local variable
|
|
1334 declaration (e.g. in a structure), declare it as shown and then
|
|
1335 call one of the init macros.
|
|
1336
|
|
1337 Also note:
|
|
1338
|
793
|
1339 void eifree (Eistring *ei);
|
771
|
1340 If you declared an Eistring to use malloc() to hold its data,
|
|
1341 or converted it to the heap using eito_malloc(), then this
|
|
1342 releases any data in it and afterwards resets the Eistring
|
|
1343 using eiinit_malloc(). Otherwise, it just resets the Eistring
|
|
1344 using eiinit().
|
|
1345
|
|
1346
|
|
1347 **********************************************
|
|
1348 * Conventions *
|
|
1349 **********************************************
|
|
1350
|
|
1351 - The names of the functions have been chosen, where possible, to
|
|
1352 match the names of str*() functions in the standard C API.
|
|
1353 -
|
|
1354
|
|
1355
|
|
1356 **********************************************
|
|
1357 * Initialization *
|
|
1358 **********************************************
|
|
1359
|
|
1360 void eireset (Eistring *eistr);
|
|
1361 Initialize the Eistring to the empty string.
|
|
1362
|
|
1363 void eicpy_* (Eistring *eistr, ...);
|
|
1364 Initialize the Eistring from somewhere:
|
|
1365
|
|
1366 void eicpy_ei (Eistring *eistr, Eistring *eistr2);
|
|
1367 ... from another Eistring.
|
|
1368 void eicpy_lstr (Eistring *eistr, Lisp_Object lisp_string);
|
|
1369 ... from a Lisp_Object string.
|
|
1370 void eicpy_ch (Eistring *eistr, Emchar ch);
|
793
|
1371 ... from an Emchar (this can be a conventional C character).
|
771
|
1372
|
|
1373 void eicpy_lstr_off (Eistring *eistr, Lisp_Object lisp_string,
|
|
1374 Bytecount off, Charcount charoff,
|
|
1375 Bytecount len, Charcount charlen);
|
|
1376 ... from a section of a Lisp_Object string.
|
|
1377 void eicpy_lbuf (Eistring *eistr, Lisp_Object lisp_buf,
|
|
1378 Bytecount off, Charcount charoff,
|
|
1379 Bytecount len, Charcount charlen);
|
|
1380 ... from a section of a Lisp_Object buffer.
|
|
1381 void eicpy_raw (Eistring *eistr, const Intbyte *data, Bytecount len);
|
|
1382 ... from raw internal-format data in the default internal format.
|
|
1383 void eicpy_rawz (Eistring *eistr, const Intbyte *data);
|
|
1384 ... from raw internal-format data in the default internal format
|
|
1385 that is "null-terminated" (the meaning of this depends on the nature
|
|
1386 of the default internal format).
|
|
1387 void eicpy_raw_fmt (Eistring *eistr, const Intbyte *data, Bytecount len,
|
826
|
1388 Internal_Format intfmt, Lisp_Object object);
|
771
|
1389 ... from raw internal-format data in the specified format.
|
|
1390 void eicpy_rawz_fmt (Eistring *eistr, const Intbyte *data,
|
826
|
1391 Internal_Format intfmt, Lisp_Object object);
|
771
|
1392 ... from raw internal-format data in the specified format that is
|
|
1393 "null-terminated" (the meaning of this depends on the nature of
|
|
1394 the specific format).
|
|
1395 void eicpy_c (Eistring *eistr, const Char_ASCII *c_string);
|
|
1396 ... from an ASCII null-terminated string. Non-ASCII characters in
|
|
1397 the string are *ILLEGAL* (read abort() with error-checking defined).
|
|
1398 void eicpy_c_len (Eistring *eistr, const Char_ASCII *c_string, len);
|
|
1399 ... from an ASCII string, with length specified. Non-ASCII characters
|
|
1400 in the string are *ILLEGAL* (read abort() with error-checking defined).
|
|
1401 void eicpy_ext (Eistring *eistr, const Extbyte *extdata,
|
|
1402 Lisp_Object coding_system);
|
|
1403 ... from external null-terminated data, with coding system specified.
|
|
1404 void eicpy_ext_len (Eistring *eistr, const Extbyte *extdata,
|
|
1405 Bytecount extlen, Lisp_Object coding_system);
|
|
1406 ... from external data, with length and coding system specified.
|
|
1407 void eicpy_lstream (Eistring *eistr, Lisp_Object lstream);
|
|
1408 ... from an lstream; reads data till eof. Data must be in default
|
|
1409 internal format; otherwise, interpose a decoding lstream.
|
|
1410
|
|
1411
|
|
1412 **********************************************
|
|
1413 * Getting the data out of the Eistring *
|
|
1414 **********************************************
|
|
1415
|
|
1416 Intbyte *eidata (Eistring *eistr);
|
|
1417 Return a pointer to the raw data in an Eistring. This is NOT
|
|
1418 a copy.
|
|
1419
|
|
1420 Lisp_Object eimake_string (Eistring *eistr);
|
|
1421 Make a Lisp string out of the Eistring.
|
|
1422
|
|
1423 Lisp_Object eimake_string_off (Eistring *eistr,
|
|
1424 Bytecount off, Charcount charoff,
|
|
1425 Bytecount len, Charcount charlen);
|
|
1426 Make a Lisp string out of a section of the Eistring.
|
|
1427
|
|
1428 void eicpyout_alloca (Eistring *eistr, LVALUE: Intbyte *ptr_out,
|
|
1429 LVALUE: Bytecount len_out);
|
|
1430 Make an alloca() copy of the data in the Eistring, using the
|
|
1431 default internal format. Due to the nature of alloca(), this
|
|
1432 must be a macro, with all lvalues passed in as parameters.
|
793
|
1433 (More specifically, not all compilers correctly handle using
|
|
1434 alloca() as the argument to a function call -- GCC on x86
|
|
1435 didn't used to, for example.) A pointer to the alloca()ed data
|
|
1436 is stored in PTR_OUT, and the length of the data (not including
|
|
1437 the terminating zero) is stored in LEN_OUT.
|
771
|
1438
|
|
1439 void eicpyout_alloca_fmt (Eistring *eistr, LVALUE: Intbyte *ptr_out,
|
|
1440 LVALUE: Bytecount len_out,
|
826
|
1441 Internal_Format intfmt, Lisp_Object object);
|
771
|
1442 Like eicpyout_alloca(), but converts to the specified internal
|
|
1443 format. (No formats other than FORMAT_DEFAULT are currently
|
|
1444 implemented, and you get an assertion failure if you try.)
|
|
1445
|
|
1446 Intbyte *eicpyout_malloc (Eistring *eistr, Bytecount *intlen_out);
|
|
1447 Make a malloc() copy of the data in the Eistring, using the
|
|
1448 default internal format. This is a real function. No lvalues
|
|
1449 passed in. Returns the new data, and stores the length (not
|
|
1450 including the terminating zero) using INTLEN_OUT, unless it's
|
|
1451 a NULL pointer.
|
|
1452
|
|
1453 Intbyte *eicpyout_malloc_fmt (Eistring *eistr, Internal_Format intfmt,
|
826
|
1454 Bytecount *intlen_out, Lisp_Object object);
|
771
|
1455 Like eicpyout_malloc(), but converts to the specified internal
|
|
1456 format. (No formats other than FORMAT_DEFAULT are currently
|
|
1457 implemented, and you get an assertion failure if you try.)
|
|
1458
|
|
1459
|
|
1460 **********************************************
|
|
1461 * Moving to the heap *
|
|
1462 **********************************************
|
|
1463
|
|
1464 void eito_malloc (Eistring *eistr);
|
|
1465 Move this Eistring to the heap. Its data will be stored in a
|
|
1466 malloc()ed block rather than the stack. Subsequent changes to
|
|
1467 this Eistring will realloc() the block as necessary. Use this
|
|
1468 when you want the Eistring to remain in scope past the end of
|
|
1469 this function call. You will have to manually free the data
|
|
1470 in the Eistring using eifree().
|
|
1471
|
|
1472 void eito_alloca (Eistring *eistr);
|
|
1473 Move this Eistring back to the stack, if it was moved to the
|
|
1474 heap with eito_malloc(). This will automatically free any
|
|
1475 heap-allocated data.
|
|
1476
|
|
1477
|
|
1478
|
|
1479 **********************************************
|
|
1480 * Retrieving the length *
|
|
1481 **********************************************
|
|
1482
|
|
1483 Bytecount eilen (Eistring *eistr);
|
|
1484 Return the length of the internal data, in bytes. See also
|
|
1485 eiextlen(), below.
|
|
1486 Charcount eicharlen (Eistring *eistr);
|
|
1487 Return the length of the internal data, in characters.
|
|
1488
|
|
1489
|
|
1490 **********************************************
|
|
1491 * Working with positions *
|
|
1492 **********************************************
|
|
1493
|
|
1494 Bytecount eicharpos_to_bytepos (Eistring *eistr, Charcount charpos);
|
|
1495 Convert a char offset to a byte offset.
|
|
1496 Charcount eibytepos_to_charpos (Eistring *eistr, Bytecount bytepos);
|
|
1497 Convert a byte offset to a char offset.
|
|
1498 Bytecount eiincpos (Eistring *eistr, Bytecount bytepos);
|
|
1499 Increment the given position by one character.
|
|
1500 Bytecount eiincpos_n (Eistring *eistr, Bytecount bytepos, Charcount n);
|
|
1501 Increment the given position by N characters.
|
|
1502 Bytecount eidecpos (Eistring *eistr, Bytecount bytepos);
|
|
1503 Decrement the given position by one character.
|
|
1504 Bytecount eidecpos_n (Eistring *eistr, Bytecount bytepos, Charcount n);
|
|
1505 Deccrement the given position by N characters.
|
|
1506
|
|
1507
|
|
1508 **********************************************
|
|
1509 * Getting the character at a position *
|
|
1510 **********************************************
|
|
1511
|
|
1512 Emchar eigetch (Eistring *eistr, Bytecount bytepos);
|
|
1513 Return the character at a particular byte offset.
|
|
1514 Emchar eigetch_char (Eistring *eistr, Charcount charpos);
|
|
1515 Return the character at a particular character offset.
|
|
1516
|
|
1517
|
|
1518 **********************************************
|
|
1519 * Setting the character at a position *
|
|
1520 **********************************************
|
|
1521
|
|
1522 Emchar eisetch (Eistring *eistr, Bytecount bytepos, Emchar chr);
|
|
1523 Set the character at a particular byte offset.
|
|
1524 Emchar eisetch_char (Eistring *eistr, Charcount charpos, Emchar chr);
|
|
1525 Set the character at a particular character offset.
|
|
1526
|
|
1527
|
|
1528 **********************************************
|
|
1529 * Concatenation *
|
|
1530 **********************************************
|
|
1531
|
|
1532 void eicat_* (Eistring *eistr, ...);
|
|
1533 Concatenate onto the end of the Eistring, with data coming from the
|
|
1534 same places as above:
|
|
1535
|
|
1536 void eicat_ei (Eistring *eistr, Eistring *eistr2);
|
|
1537 ... from another Eistring.
|
|
1538 void eicat_c (Eistring *eistr, Char_ASCII *c_string);
|
|
1539 ... from an ASCII null-terminated string. Non-ASCII characters in
|
|
1540 the string are *ILLEGAL* (read abort() with error-checking defined).
|
|
1541 void eicat_raw (ei, const Intbyte *data, Bytecount len);
|
|
1542 ... from raw internal-format data in the default internal format.
|
|
1543 void eicat_rawz (ei, const Intbyte *data);
|
|
1544 ... from raw internal-format data in the default internal format
|
|
1545 that is "null-terminated" (the meaning of this depends on the nature
|
|
1546 of the default internal format).
|
|
1547 void eicat_lstr (ei, Lisp_Object lisp_string);
|
|
1548 ... from a Lisp_Object string.
|
|
1549 void eicat_ch (ei, Emchar ch);
|
|
1550 ... from an Emchar.
|
|
1551
|
|
1552 (All except the first variety are convenience functions.
|
|
1553 In the general case, create another Eistring from the source.)
|
|
1554
|
|
1555
|
|
1556 **********************************************
|
|
1557 * Replacement *
|
|
1558 **********************************************
|
|
1559
|
|
1560 void eisub_* (Eistring *eistr, Bytecount off, Charcount charoff,
|
|
1561 Bytecount len, Charcount charlen, ...);
|
|
1562 Replace a section of the Eistring, specifically:
|
|
1563
|
|
1564 void eisub_ei (Eistring *eistr, Bytecount off, Charcount charoff,
|
|
1565 Bytecount len, Charcount charlen, Eistring *eistr2);
|
|
1566 ... with another Eistring.
|
|
1567 void eisub_c (Eistring *eistr, Bytecount off, Charcount charoff,
|
|
1568 Bytecount len, Charcount charlen, Char_ASCII *c_string);
|
|
1569 ... with an ASCII null-terminated string. Non-ASCII characters in
|
|
1570 the string are *ILLEGAL* (read abort() with error-checking defined).
|
|
1571 void eisub_ch (Eistring *eistr, Bytecount off, Charcount charoff,
|
|
1572 Bytecount len, Charcount charlen, Emchar ch);
|
|
1573 ... with an Emchar.
|
|
1574
|
|
1575 void eidel (Eistring *eistr, Bytecount off, Charcount charoff,
|
|
1576 Bytecount len, Charcount charlen);
|
|
1577 Delete a section of the Eistring.
|
|
1578
|
|
1579
|
|
1580 **********************************************
|
|
1581 * Converting to an external format *
|
|
1582 **********************************************
|
|
1583
|
|
1584 void eito_external (Eistring *eistr, Lisp_Object coding_system);
|
|
1585 Convert the Eistring to an external format and store the result
|
|
1586 in the string. NOTE: Further changes to the Eistring will *NOT*
|
|
1587 change the external data stored in the string. You will have to
|
|
1588 call eito_external() again in such a case if you want the external
|
|
1589 data.
|
|
1590
|
|
1591 Extbyte *eiextdata (Eistring *eistr);
|
|
1592 Return a pointer to the external data stored in the Eistring as
|
|
1593 a result of a prior call to eito_external().
|
|
1594
|
|
1595 Bytecount eiextlen (Eistring *eistr);
|
|
1596 Return the length in bytes of the external data stored in the
|
|
1597 Eistring as a result of a prior call to eito_external().
|
|
1598
|
|
1599
|
|
1600 **********************************************
|
|
1601 * Searching in the Eistring for a character *
|
|
1602 **********************************************
|
|
1603
|
|
1604 Bytecount eichr (Eistring *eistr, Emchar chr);
|
|
1605 Charcount eichr_char (Eistring *eistr, Emchar chr);
|
|
1606 Bytecount eichr_off (Eistring *eistr, Emchar chr, Bytecount off,
|
|
1607 Charcount charoff);
|
|
1608 Charcount eichr_off_char (Eistring *eistr, Emchar chr, Bytecount off,
|
|
1609 Charcount charoff);
|
|
1610 Bytecount eirchr (Eistring *eistr, Emchar chr);
|
|
1611 Charcount eirchr_char (Eistring *eistr, Emchar chr);
|
|
1612 Bytecount eirchr_off (Eistring *eistr, Emchar chr, Bytecount off,
|
|
1613 Charcount charoff);
|
|
1614 Charcount eirchr_off_char (Eistring *eistr, Emchar chr, Bytecount off,
|
|
1615 Charcount charoff);
|
|
1616
|
|
1617
|
|
1618 **********************************************
|
|
1619 * Searching in the Eistring for a string *
|
|
1620 **********************************************
|
|
1621
|
|
1622 Bytecount eistr_ei (Eistring *eistr, Eistring *eistr2);
|
|
1623 Charcount eistr_ei_char (Eistring *eistr, Eistring *eistr2);
|
|
1624 Bytecount eistr_ei_off (Eistring *eistr, Eistring *eistr2, Bytecount off,
|
|
1625 Charcount charoff);
|
|
1626 Charcount eistr_ei_off_char (Eistring *eistr, Eistring *eistr2,
|
|
1627 Bytecount off, Charcount charoff);
|
|
1628 Bytecount eirstr_ei (Eistring *eistr, Eistring *eistr2);
|
|
1629 Charcount eirstr_ei_char (Eistring *eistr, Eistring *eistr2);
|
|
1630 Bytecount eirstr_ei_off (Eistring *eistr, Eistring *eistr2, Bytecount off,
|
|
1631 Charcount charoff);
|
|
1632 Charcount eirstr_ei_off_char (Eistring *eistr, Eistring *eistr2,
|
|
1633 Bytecount off, Charcount charoff);
|
|
1634
|
|
1635 Bytecount eistr_c (Eistring *eistr, Char_ASCII *c_string);
|
|
1636 Charcount eistr_c_char (Eistring *eistr, Char_ASCII *c_string);
|
|
1637 Bytecount eistr_c_off (Eistring *eistr, Char_ASCII *c_string, Bytecount off,
|
|
1638 Charcount charoff);
|
|
1639 Charcount eistr_c_off_char (Eistring *eistr, Char_ASCII *c_string,
|
|
1640 Bytecount off, Charcount charoff);
|
|
1641 Bytecount eirstr_c (Eistring *eistr, Char_ASCII *c_string);
|
|
1642 Charcount eirstr_c_char (Eistring *eistr, Char_ASCII *c_string);
|
|
1643 Bytecount eirstr_c_off (Eistring *eistr, Char_ASCII *c_string,
|
|
1644 Bytecount off, Charcount charoff);
|
|
1645 Charcount eirstr_c_off_char (Eistring *eistr, Char_ASCII *c_string,
|
|
1646 Bytecount off, Charcount charoff);
|
|
1647
|
|
1648
|
|
1649 **********************************************
|
|
1650 * Comparison *
|
|
1651 **********************************************
|
|
1652
|
|
1653 int eicmp_* (Eistring *eistr, ...);
|
|
1654 int eicmp_off_* (Eistring *eistr, Bytecount off, Charcount charoff,
|
|
1655 Bytecount len, Charcount charlen, ...);
|
|
1656 int eicasecmp_* (Eistring *eistr, ...);
|
|
1657 int eicasecmp_off_* (Eistring *eistr, Bytecount off, Charcount charoff,
|
|
1658 Bytecount len, Charcount charlen, ...);
|
|
1659 int eicasecmp_i18n_* (Eistring *eistr, ...);
|
|
1660 int eicasecmp_i18n_off_* (Eistring *eistr, Bytecount off, Charcount charoff,
|
|
1661 Bytecount len, Charcount charlen, ...);
|
|
1662
|
|
1663 Compare the Eistring with the other data. Return value same as
|
|
1664 from strcmp. The `*' is either `ei' for another Eistring (in
|
|
1665 which case `...' is an Eistring), or `c' for a pure-ASCII string
|
|
1666 (in which case `...' is a pointer to that string). For anything
|
|
1667 more complex, first create an Eistring out of the source.
|
|
1668 Comparison is either simple (`eicmp_...'), ASCII case-folding
|
|
1669 (`eicasecmp_...'), or multilingual case-folding
|
|
1670 (`eicasecmp_i18n_...).
|
|
1671
|
|
1672
|
|
1673 More specifically, the prototypes are:
|
|
1674
|
|
1675 int eicmp_ei (Eistring *eistr, Eistring *eistr2);
|
|
1676 int eicmp_off_ei (Eistring *eistr, Bytecount off, Charcount charoff,
|
|
1677 Bytecount len, Charcount charlen, Eistring *eistr2);
|
|
1678 int eicasecmp_ei (Eistring *eistr, Eistring *eistr2);
|
|
1679 int eicasecmp_off_ei (Eistring *eistr, Bytecount off, Charcount charoff,
|
|
1680 Bytecount len, Charcount charlen, Eistring *eistr2);
|
|
1681 int eicasecmp_i18n_ei (Eistring *eistr, Eistring *eistr2);
|
|
1682 int eicasecmp_i18n_off_ei (Eistring *eistr, Bytecount off,
|
|
1683 Charcount charoff, Bytecount len,
|
|
1684 Charcount charlen, Eistring *eistr2);
|
|
1685
|
|
1686 int eicmp_c (Eistring *eistr, Char_ASCII *c_string);
|
|
1687 int eicmp_off_c (Eistring *eistr, Bytecount off, Charcount charoff,
|
|
1688 Bytecount len, Charcount charlen, Char_ASCII *c_string);
|
|
1689 int eicasecmp_c (Eistring *eistr, Char_ASCII *c_string);
|
|
1690 int eicasecmp_off_c (Eistring *eistr, Bytecount off, Charcount charoff,
|
|
1691 Bytecount len, Charcount charlen,
|
|
1692 Char_ASCII *c_string);
|
|
1693 int eicasecmp_i18n_c (Eistring *eistr, Char_ASCII *c_string);
|
|
1694 int eicasecmp_i18n_off_c (Eistring *eistr, Bytecount off, Charcount charoff,
|
|
1695 Bytecount len, Charcount charlen,
|
|
1696 Char_ASCII *c_string);
|
|
1697
|
|
1698
|
|
1699 **********************************************
|
|
1700 * Case-changing the Eistring *
|
|
1701 **********************************************
|
|
1702
|
|
1703 void eilwr (Eistring *eistr);
|
|
1704 Convert all characters in the Eistring to lowercase.
|
|
1705 void eiupr (Eistring *eistr);
|
|
1706 Convert all characters in the Eistring to uppercase.
|
|
1707 */
|
|
1708
|
|
1709
|
|
1710 /* Principles for writing Eistring functions:
|
|
1711
|
|
1712 (1) Unfortunately, we have to write most of the Eistring functions
|
|
1713 as macros, because of the use of alloca(). The principle used
|
|
1714 below to assure no conflict in local variables is to prefix all
|
|
1715 local variables with "ei" plus a number, which should be unique
|
|
1716 among macros. In practice, when finding a new number, find the
|
|
1717 highest so far used, and add 1.
|
|
1718
|
|
1719 (2) We also suffix the Eistring fields with an _ to avoid problems
|
|
1720 with macro parameters of the same name. (And as the standard
|
|
1721 signal not to access these fields directly.)
|
|
1722
|
|
1723 (3) We maintain both the length in bytes and chars of the data in
|
|
1724 the Eistring at all times, for convenient retrieval by outside
|
|
1725 functions. That means when writing functions that manipulate
|
|
1726 Eistrings, you too need to keep both lengths up to date for all
|
|
1727 data that you work with.
|
|
1728
|
|
1729 (4) When writing a new type of operation (e.g. substitution), you
|
|
1730 will often find yourself working with outside data, and thus
|
|
1731 have a series of related API's, for different forms that the
|
|
1732 outside data is in. Generally, you will want to choose a
|
|
1733 subset of the forms supported by eicpy_*, which has to be
|
|
1734 totally general because that's the fundamental way to get data
|
|
1735 into an Eistring, and once the data is into the string, it
|
|
1736 would be to create a whole series of Ei operations that work on
|
|
1737 nothing but Eistrings. Although theoretically nice, in
|
|
1738 practice it's a hassle, so we suggest that you provide
|
|
1739 convenience functions. In particular, there are two paths you
|
|
1740 can take. One is minimalist -- it only allows other Eistrings
|
|
1741 and ASCII data, and Emchars if the particular operation makes
|
|
1742 sense with a character. The other provides interfaces for the
|
|
1743 most commonly-used forms -- Eistring, ASCII data, Lisp string,
|
|
1744 raw internal-format string with length, raw internal-format
|
|
1745 string without, and possibly Emchar. (In the function names,
|
|
1746 these are designated `ei', `c', `lstr', `raw', `rawz', and
|
|
1747 `ch', respectively.)
|
|
1748
|
|
1749 (5) When coding a new type of operation, such as was discussed in
|
|
1750 previous section, the correct approach is to declare an worker
|
|
1751 function that does the work of everything, and is called by the
|
|
1752 other "container" macros that handle the different outside data
|
|
1753 forms. The data coming into the worker function, which
|
|
1754 typically ends in `_1', is in the form of three parameters:
|
|
1755 DATA, LEN, CHARLEN. (See point [3] about having two lengths and
|
|
1756 keeping them in sync.)
|
|
1757
|
|
1758 (6) Handling argument evaluation in macros: We take great care
|
|
1759 never to evaluate any argument more than once in any macro,
|
|
1760 except the initial Eistring parameter. This can and will be
|
|
1761 evaluated multiple times, but it should pretty much always just
|
|
1762 be a simple variable. This means, for example, that if an
|
|
1763 Eistring is the second (not first) argument of a macro, it
|
|
1764 doesn't fall under the "initial Eistring" exemption, so it
|
|
1765 needs protection against multi-evaluation. (Take the address of
|
|
1766 the Eistring structure, store in a temporary variable, and use
|
|
1767 temporary variable for all access to the Eistring.
|
|
1768 Essentially, we want it to appear as if these Eistring macros
|
|
1769 are functions -- we would like to declare them as functions but
|
|
1770 they use alloca(), so we can't (and we can't make them inline
|
|
1771 functions either -- alloca() is explicitly disallowed in inline
|
|
1772 functions.)
|
|
1773
|
|
1774 (7) Note that our rules regarding multiple evaluation are *more*
|
|
1775 strict than the rules listed above under the heading "working
|
|
1776 with raw internal-format data".
|
|
1777 */
|
|
1778
|
|
1779
|
|
1780 /* ----- Declaration ----- */
|
|
1781
|
|
1782 typedef struct
|
|
1783 {
|
|
1784 /* Data for the Eistring, stored in the default internal format.
|
|
1785 Always includes terminating null. */
|
|
1786 Intbyte *data_;
|
|
1787 /* Total number of bytes allocated in DATA (including null). */
|
|
1788 Bytecount max_size_allocated_;
|
|
1789 Bytecount bytelen_;
|
|
1790 Charcount charlen_;
|
|
1791 int mallocp_;
|
|
1792
|
|
1793 Extbyte *extdata_;
|
|
1794 Bytecount extlen_;
|
|
1795 } Eistring;
|
|
1796
|
|
1797 extern Eistring the_eistring_zero_init, the_eistring_malloc_zero_init;
|
|
1798
|
|
1799 #define DECLARE_EISTRING(name) \
|
|
1800 Eistring __ ## name ## __storage__ = the_eistring_zero_init; \
|
|
1801 Eistring *name = & __ ## name ## __storage__
|
|
1802 #define DECLARE_EISTRING_MALLOC(name) \
|
|
1803 Eistring __ ## name ## __storage__ = the_eistring_malloc_zero_init; \
|
|
1804 Eistring *name = & __ ## name ## __storage__
|
|
1805
|
|
1806 #define eiinit(ei) \
|
|
1807 do { \
|
793
|
1808 *(ei) = the_eistring_zero_init; \
|
771
|
1809 } while (0)
|
|
1810
|
|
1811 #define eiinit_malloc(ei) \
|
|
1812 do { \
|
793
|
1813 *(ei) = the_eistring_malloc_zero_init; \
|
771
|
1814 } while (0)
|
|
1815
|
|
1816
|
|
1817 /* ----- Utility ----- */
|
|
1818
|
|
1819 /* Make sure both LEN and CHARLEN are specified, in case one is given
|
|
1820 as -1. PTR evaluated at most once, others multiply. */
|
|
1821 #define eifixup_bytechar(ptr, len, charlen) \
|
|
1822 do { \
|
|
1823 if ((len) == -1) \
|
|
1824 (len) = charcount_to_bytecount (ptr, charlen); \
|
|
1825 else if ((charlen) == -1) \
|
|
1826 (charlen) = bytecount_to_charcount (ptr, len); \
|
|
1827 } while (0)
|
|
1828
|
|
1829 /* Make sure LEN is specified, in case it's is given as -1. PTR
|
|
1830 evaluated at most once, others multiply. */
|
|
1831 #define eifixup_byte(ptr, len, charlen) \
|
|
1832 do { \
|
|
1833 if ((len) == -1) \
|
|
1834 (len) = charcount_to_bytecount (ptr, charlen); \
|
|
1835 } while (0)
|
|
1836
|
|
1837 /* Make sure CHARLEN is specified, in case it's is given as -1. PTR
|
|
1838 evaluated at most once, others multiply. */
|
|
1839 #define eifixup_char(ptr, len, charlen) \
|
|
1840 do { \
|
|
1841 if ((charlen) == -1) \
|
|
1842 (charlen) = bytecount_to_charcount (ptr, len); \
|
|
1843 } while (0)
|
|
1844
|
|
1845
|
|
1846
|
|
1847 /* Make sure we can hold NEWBYTELEN bytes (which is NEWCHARLEN chars)
|
|
1848 plus a zero terminator. Preserve existing data as much as possible,
|
|
1849 including existing zero terminator. Put a new zero terminator where it
|
|
1850 should go if NEWZ if non-zero. All args but EI are evalled only once. */
|
|
1851
|
|
1852 #define EI_ALLOC(ei, newbytelen, newcharlen, newz) \
|
|
1853 do { \
|
|
1854 int ei1oldeibytelen = (ei)->bytelen_; \
|
|
1855 \
|
|
1856 (ei)->charlen_ = (newcharlen); \
|
|
1857 (ei)->bytelen_ = (newbytelen); \
|
|
1858 \
|
|
1859 if (ei1oldeibytelen != (ei)->bytelen_) \
|
|
1860 { \
|
|
1861 int ei1newsize = (ei)->max_size_allocated_; \
|
|
1862 while (ei1newsize < (ei)->bytelen_ + 1) \
|
|
1863 { \
|
|
1864 ei1newsize = (int) (ei1newsize * 1.5); \
|
|
1865 if (ei1newsize < 32) \
|
|
1866 ei1newsize = 32; \
|
|
1867 } \
|
|
1868 if (ei1newsize != (ei)->max_size_allocated_) \
|
|
1869 { \
|
|
1870 if ((ei)->mallocp_) \
|
|
1871 /* xrealloc always preserves existing data as much as possible */ \
|
|
1872 (ei)->data_ = (Intbyte *) xrealloc ((ei)->data_, ei1newsize); \
|
|
1873 else \
|
|
1874 { \
|
|
1875 /* We don't have realloc, so alloca() more space and copy the \
|
|
1876 data into it. */ \
|
|
1877 Intbyte *ei1oldeidata = (ei)->data_; \
|
|
1878 (ei)->data_ = (Intbyte *) alloca (ei1newsize); \
|
|
1879 if (ei1oldeidata) \
|
|
1880 memcpy ((ei)->data_, ei1oldeidata, ei1oldeibytelen + 1); \
|
|
1881 } \
|
|
1882 (ei)->max_size_allocated_ = ei1newsize; \
|
|
1883 } \
|
|
1884 if (newz) \
|
|
1885 (ei)->data_[(ei)->bytelen_] = '\0'; \
|
|
1886 } \
|
|
1887 } while (0)
|
|
1888
|
|
1889 #define EI_ALLOC_AND_COPY(ei, data, bytelen, charlen) \
|
|
1890 do { \
|
|
1891 EI_ALLOC (ei, bytelen, charlen, 1); \
|
|
1892 memcpy ((ei)->data_, data, (ei)->bytelen_); \
|
|
1893 } while (0)
|
|
1894
|
800
|
1895 #ifdef ERROR_CHECK_TEXT
|
771
|
1896 #define EI_ASSERT_ASCII(ptr, len) \
|
|
1897 do { \
|
|
1898 int ei5; \
|
|
1899 const Char_ASCII *ei5ptr = (ptr); \
|
|
1900 int ei5len = (len); \
|
|
1901 \
|
|
1902 for (ei5 = 0; ei5 < ei5len; ei5++) \
|
|
1903 assert (ei5ptr[ei5] >= 0x00 && ei5ptr[ei5] < 0x7F); \
|
|
1904 } while (0)
|
|
1905 #define EI_ASSERT_ASCIIZ(ptr) \
|
|
1906 do { \
|
|
1907 const Char_ASCII *ei5p1 = (ptr); \
|
|
1908 EI_ASSERT_ASCII (ei5p1, strlen (ei5p1)); \
|
|
1909 } while (0)
|
|
1910 #else
|
|
1911 #define EI_ASSERT_ASCII(ptr, len)
|
|
1912 #define EI_ASSERT_ASCIIZ(ptr)
|
|
1913 #endif
|
|
1914
|
|
1915
|
|
1916 /* ----- Initialization ----- */
|
|
1917
|
|
1918 #define eicpy_ei(ei, eicpy) \
|
|
1919 do { \
|
|
1920 const Eistring *ei2 = (eicpy); \
|
|
1921 EI_ALLOC_AND_COPY (ei, ei2->data_, ei2->bytelen_, ei2->charlen_); \
|
|
1922 } while (0)
|
|
1923
|
|
1924 #define eicpy_lstr(ei, lisp_string) \
|
|
1925 do { \
|
|
1926 Lisp_Object ei3 = (lisp_string); \
|
|
1927 EI_ALLOC_AND_COPY (ei, XSTRING_DATA (ei3), XSTRING_LENGTH (ei3), \
|
826
|
1928 string_char_length (ei3)); \
|
771
|
1929 } while (0)
|
|
1930
|
|
1931 #define eicpy_lstr_off(ei, lisp_string, off, charoff, len, charlen) \
|
|
1932 do { \
|
|
1933 Lisp_Object ei23lstr = (lisp_string); \
|
|
1934 int ei23off = (off); \
|
|
1935 int ei23charoff = (charoff); \
|
|
1936 int ei23len = (len); \
|
|
1937 int ei23charlen = (charlen); \
|
|
1938 const Intbyte *ei23data = XSTRING_DATA (ei23lstr); \
|
|
1939 \
|
|
1940 int ei23oldbytelen = (ei)->bytelen_; \
|
|
1941 \
|
|
1942 eifixup_byte (ei23data, ei23off, ei23charoff); \
|
|
1943 eifixup_bytechar (ei23data + ei23off, ei23len, ei23charlen); \
|
|
1944 \
|
|
1945 EI_ALLOC_AND_COPY (ei, ei23data + ei23off, ei23len, ei23charlen); \
|
|
1946 } while (0)
|
|
1947
|
826
|
1948 #define eicpy_raw_fmt(ei, ptr, len, fmt, object) \
|
771
|
1949 do { \
|
|
1950 const Intbyte *ei12ptr = (ptr); \
|
|
1951 Internal_Format ei12fmt = (fmt); \
|
|
1952 int ei12len = (len); \
|
|
1953 assert (ei12fmt == FORMAT_DEFAULT); \
|
|
1954 EI_ALLOC_AND_COPY (ei, ei12ptr, ei12len, \
|
|
1955 bytecount_to_charcount (ei12ptr, ei12len)); \
|
|
1956 } while (0)
|
|
1957
|
826
|
1958 #define eicpy_raw(ei, ptr, len) \
|
|
1959 eicpy_raw_fmt (ei, ptr, len, FORMAT_DEFAULT, Qnil)
|
|
1960
|
|
1961 #define eicpy_rawz_fmt(ei, ptr, fmt, object) \
|
|
1962 do { \
|
|
1963 const Intbyte *ei12p1ptr = (ptr); \
|
|
1964 Internal_Format ei12p1fmt = (fmt); \
|
|
1965 assert (ei12p1fmt == FORMAT_DEFAULT); \
|
|
1966 eicpy_raw_fmt (ei, ei12p1ptr, qxestrlen (ei12p1ptr), fmt, object); \
|
771
|
1967 } while (0)
|
|
1968
|
826
|
1969 #define eicpy_rawz(ei, ptr) eicpy_rawz_fmt (ei, ptr, FORMAT_DEFAULT, Qnil)
|
771
|
1970
|
|
1971 #define eicpy_ch(ei, ch) \
|
|
1972 do { \
|
|
1973 Intbyte ei12p2[MAX_EMCHAR_LEN]; \
|
|
1974 Bytecount ei12p2len = set_charptr_emchar (ei12p2, ch); \
|
|
1975 EI_ALLOC_AND_COPY (ei, ei12p2, ei12p2len, 1); \
|
|
1976 } while (0)
|
|
1977
|
|
1978 #define eicpy_c(ei, c_string) \
|
|
1979 do { \
|
|
1980 const Char_ASCII *ei4 = (c_string); \
|
|
1981 \
|
|
1982 EI_ASSERT_ASCIIZ (ei4); \
|
|
1983 eicpy_ext (ei, ei4, Qbinary); \
|
|
1984 } while (0)
|
|
1985
|
|
1986 #define eicpy_c_len(ei, c_string, c_len) \
|
|
1987 do { \
|
|
1988 const Char_ASCII *ei6 = (c_string); \
|
|
1989 int ei6len = (c_len); \
|
|
1990 \
|
|
1991 EI_ASSERT_ASCII (ei6, ei6len); \
|
|
1992 eicpy_ext_len (ei, ei6, ei6len, Qbinary); \
|
|
1993 } while (0)
|
|
1994
|
|
1995 #define eicpy_ext_len(ei, extdata, extlen, coding_system) \
|
|
1996 do { \
|
|
1997 const Extbyte *ei7 = (extdata); \
|
|
1998 int ei7len = (extlen); \
|
|
1999 \
|
|
2000 TO_INTERNAL_FORMAT (DATA, (ei7, ei7len), \
|
|
2001 ALLOCA, ((ei)->data_, (ei)->bytelen_), \
|
|
2002 coding_system); \
|
|
2003 (ei)->max_size_allocated_ = (ei)->bytelen_ + 1; \
|
|
2004 (ei)->charlen_ = bytecount_to_charcount ((ei)->data_, (ei)->bytelen_); \
|
|
2005 } while (0)
|
|
2006
|
|
2007 #define eicpy_ext(ei, extdata, coding_system) \
|
|
2008 do { \
|
|
2009 const Extbyte *ei8 = (extdata); \
|
|
2010 \
|
|
2011 eicpy_ext_len (ei, ei8, dfc_external_data_len (ei8, coding_system), \
|
|
2012 coding_system); \
|
|
2013 } while (0)
|
|
2014
|
|
2015 #define eicpy_lbuf(eistr, lisp_buf, off, charoff, len, charlen) \
|
|
2016 NOT YET IMPLEMENTED
|
|
2017
|
|
2018 #define eicpy_lstream(eistr, lstream) \
|
|
2019 NOT YET IMPLEMENTED
|
|
2020
|
|
2021 #define eireset(eistr) eicpy_rawz (eistr, (Intbyte *) "")
|
|
2022
|
|
2023 /* ----- Getting the data out of the Eistring ----- */
|
|
2024
|
|
2025 #define eidata(ei) ((ei)->data_)
|
|
2026
|
|
2027 #define eimake_string(ei) make_string (eidata (ei), eilen (ei))
|
|
2028
|
|
2029 #define eimake_string_off(eistr, off, charoff, len, charlen) \
|
|
2030 do { \
|
|
2031 Lisp_Object ei24lstr; \
|
|
2032 int ei24off = (off); \
|
|
2033 int ei24charoff = (charoff); \
|
|
2034 int ei24len = (len); \
|
|
2035 int ei24charlen = (charlen); \
|
|
2036 \
|
|
2037 eifixup_byte ((eistr)->data_, ei24off, ei24charoff); \
|
|
2038 eifixup_byte ((eistr)->data_ + ei24off, ei24len, ei24charlen); \
|
|
2039 \
|
|
2040 return make_string ((eistr)->data_ + ei24off, ei24len); \
|
|
2041 } while (0)
|
|
2042
|
|
2043 #define eicpyout_alloca(eistr, ptrout, lenout) \
|
826
|
2044 eicpyout_alloca_fmt (eistr, ptrout, lenout, FORMAT_DEFAULT, Qnil)
|
771
|
2045 #define eicpyout_malloc(eistr, lenout) \
|
826
|
2046 eicpyout_malloc_fmt (eistr, lenout, FORMAT_DEFAULT, Qnil)
|
771
|
2047 Intbyte *eicpyout_malloc_fmt (Eistring *eistr, Bytecount *len_out,
|
826
|
2048 Internal_Format fmt, Lisp_Object object);
|
|
2049 #define eicpyout_alloca_fmt(eistr, ptrout, lenout, fmt, object) \
|
771
|
2050 do { \
|
|
2051 Internal_Format ei23fmt = (fmt); \
|
|
2052 Intbyte *ei23ptrout = &(ptrout); \
|
|
2053 Bytecount *ei23lenout = &(lenout); \
|
|
2054 \
|
|
2055 assert (ei23fmt == FORMAT_DEFAULT); \
|
|
2056 \
|
|
2057 *ei23lenout = (eistr)->bytelen_; \
|
|
2058 *ei23ptrout = alloca_array (Intbyte, (eistr)->bytelen_ + 1); \
|
|
2059 memcpy (*ei23ptrout, (eistr)->data_, (eistr)->bytelen_ + 1); \
|
|
2060 } while (0)
|
|
2061
|
|
2062 /* ----- Moving to the heap ----- */
|
|
2063
|
|
2064 #define eifree(ei) \
|
|
2065 do { \
|
|
2066 if ((ei)->mallocp_) \
|
|
2067 { \
|
|
2068 if ((ei)->data_) \
|
|
2069 xfree ((ei)->data_); \
|
|
2070 if ((ei)->extdata_) \
|
|
2071 xfree ((ei)->extdata_); \
|
|
2072 eiinit_malloc (ei); \
|
|
2073 } \
|
|
2074 else \
|
|
2075 eiinit (ei); \
|
|
2076 } while (0)
|
|
2077
|
|
2078 int eifind_large_enough_buffer (int oldbufsize, int needed_size);
|
|
2079 void eito_malloc_1 (Eistring *ei);
|
|
2080
|
|
2081 #define eito_malloc(ei) eito_malloc_1 (ei)
|
|
2082
|
|
2083 #define eito_alloca(ei) \
|
|
2084 do { \
|
|
2085 if (!(ei)->mallocp_) \
|
|
2086 return; \
|
|
2087 (ei)->mallocp_ = 0; \
|
|
2088 if ((ei)->data_) \
|
|
2089 { \
|
|
2090 Intbyte *ei13newdata; \
|
|
2091 \
|
|
2092 (ei)->max_size_allocated_ = \
|
|
2093 eifind_large_enough_buffer (0, (ei)->bytelen_ + 1); \
|
|
2094 ei13newdata = (Intbyte *) alloca ((ei)->max_size_allocated_); \
|
|
2095 memcpy (ei13newdata, (ei)->data_, (ei)->bytelen_ + 1); \
|
|
2096 xfree ((ei)->data_); \
|
|
2097 (ei)->data_ = ei13newdata; \
|
|
2098 } \
|
|
2099 \
|
|
2100 if ((ei)->extdata_) \
|
|
2101 { \
|
|
2102 Extbyte *ei13newdata = (Extbyte *) alloca ((ei)->extlen_ + 2); \
|
|
2103 \
|
|
2104 memcpy (ei13newdata, (ei)->extdata_, (ei)->extlen_); \
|
|
2105 /* Double null-terminate in case of Unicode data */ \
|
|
2106 ei13newdata[(ei)->extlen_] = '\0'; \
|
|
2107 ei13newdata[(ei)->extlen_ + 1] = '\0'; \
|
|
2108 xfree ((ei)->extdata_); \
|
|
2109 (ei)->extdata_ = ei13newdata; \
|
|
2110 } \
|
|
2111 } while (0)
|
|
2112
|
|
2113
|
|
2114 /* ----- Retrieving the length ----- */
|
|
2115
|
|
2116 #define eilen(ei) ((ei)->bytelen_)
|
|
2117 #define eicharlen(ei) ((ei)->charlen_)
|
|
2118
|
|
2119
|
|
2120 /* ----- Working with positions ----- */
|
|
2121
|
|
2122 #define eicharpos_to_bytepos(ei, charpos) \
|
|
2123 charcount_to_bytecount ((ei)->data_, charpos)
|
|
2124 #define eibytepos_to_charpos(ei, bytepos) \
|
|
2125 bytecount_to_charcount ((ei)->data_, bytepos)
|
|
2126
|
|
2127 DECLARE_INLINE_HEADER (Bytecount eiincpos_1 (Eistring *eistr,
|
|
2128 Bytecount bytepos,
|
|
2129 Charcount n))
|
|
2130 {
|
|
2131 Intbyte *pos = eistr->data_ + bytepos;
|
814
|
2132 Charcount i;
|
771
|
2133
|
800
|
2134 text_checking_assert (bytepos >= 0 && bytepos <= eistr->bytelen_);
|
|
2135 text_checking_assert (n >= 0 && n <= eistr->charlen_);
|
771
|
2136 /* We could check N more correctly now, but that would require a
|
|
2137 call to bytecount_to_charcount(), which would be needlessly
|
|
2138 expensive (it would convert O(N) algorithms into O(N^2) algorithms
|
800
|
2139 with ERROR_CHECK_TEXT, which would be bad). If N is bad, we are
|
771
|
2140 guaranteed to catch it either inside INC_CHARPTR() or in the check
|
|
2141 below. */
|
|
2142 for (i = 0; i < n; i++)
|
|
2143 INC_CHARPTR (pos);
|
800
|
2144 text_checking_assert (pos - eistr->data_ <= eistr->bytelen_);
|
771
|
2145 return pos - eistr->data_;
|
|
2146 }
|
|
2147
|
|
2148 #define eiincpos (ei, bytepos) eiincpos_1 (ei, bytepos, 1)
|
|
2149 #define eiincpos_n (ei, bytepos, n) eiincpos_1 (ei, bytepos, n)
|
|
2150
|
|
2151 DECLARE_INLINE_HEADER (Bytecount eidecpos_1 (Eistring *eistr,
|
|
2152 Bytecount bytepos,
|
|
2153 Charcount n))
|
|
2154 {
|
|
2155 Intbyte *pos = eistr->data_ + bytepos;
|
|
2156 int i;
|
|
2157
|
800
|
2158 text_checking_assert (bytepos >= 0 && bytepos <= eistr->bytelen_);
|
|
2159 text_checking_assert (n >= 0 && n <= eistr->charlen_);
|
771
|
2160 /* We could check N more correctly now, but ... see above. */
|
|
2161 for (i = 0; i < n; i++)
|
|
2162 DEC_CHARPTR (pos);
|
800
|
2163 text_checking_assert (pos - eistr->data_ <= eistr->bytelen_);
|
771
|
2164 return pos - eistr->data_;
|
|
2165 }
|
|
2166
|
|
2167 #define eidecpos (ei, bytepos) eidecpos_1 (ei, bytepos, 1)
|
|
2168 #define eidecpos_n (ei, bytepos, n) eidecpos_1 (ei, bytepos, n)
|
|
2169
|
|
2170
|
|
2171 /* ----- Getting the character at a position ----- */
|
|
2172
|
|
2173 #define eigetch(ei, bytepos) \
|
|
2174 charptr_emchar ((ei)->data_ + (bytepos))
|
|
2175 #define eigetch_char(ei, charpos) charptr_emchar_n ((ei)->data_, charpos)
|
|
2176
|
|
2177
|
|
2178 /* ----- Setting the character at a position ----- */
|
|
2179
|
|
2180 #define eisetch(ei, bytepos, chr) \
|
|
2181 eisub_ch (ei, bytepos, -1, -1, 1, chr)
|
|
2182 #define eisetch_char(ei, charpos, chr) \
|
|
2183 eisub_ch (ei, -1, charpos, -1, 1, chr)
|
|
2184
|
|
2185
|
|
2186 /* ----- Concatenation ----- */
|
|
2187
|
|
2188 #define eicat_1(ei, data, bytelen, charlen) \
|
|
2189 do { \
|
|
2190 int ei14oldeibytelen = (ei)->bytelen_; \
|
|
2191 int ei14bytelen = (bytelen); \
|
|
2192 EI_ALLOC (ei, (ei)->bytelen_ + ei14bytelen, \
|
|
2193 (ei)->charlen_ + (charlen), 1); \
|
|
2194 memcpy ((ei)->data_ + ei14oldeibytelen, (data), \
|
|
2195 ei14bytelen); \
|
|
2196 } while (0)
|
|
2197
|
|
2198 #define eicat_ei(ei, ei2) \
|
|
2199 do { \
|
|
2200 const Eistring *ei9 = (ei2); \
|
|
2201 eicat_1 (ei, ei9->data_, ei9->bytelen_, ei9->charlen_); \
|
|
2202 } while (0)
|
|
2203
|
|
2204 #define eicat_c(ei, c_string) \
|
|
2205 do { \
|
|
2206 const Char_ASCII *ei15 = (c_string); \
|
|
2207 int ei15len = strlen (ei15); \
|
|
2208 \
|
|
2209 EI_ASSERT_ASCII (ei15, ei15len); \
|
|
2210 eicat_1 (ei, ei15, ei15len, \
|
|
2211 bytecount_to_charcount ((Intbyte *) ei15, ei15len)); \
|
|
2212 } while (0)
|
|
2213
|
|
2214 #define eicat_raw(ei, data, len) \
|
|
2215 do { \
|
|
2216 int ei16len = (len); \
|
|
2217 const Intbyte *ei16data = (data); \
|
|
2218 eicat_1 (ei, ei16data, ei16len, \
|
|
2219 bytecount_to_charcount (ei16data, ei16len)); \
|
|
2220 } while (0)
|
|
2221
|
|
2222 #define eicat_rawz(ei, ptr) \
|
|
2223 do { \
|
|
2224 const Intbyte *ei16p5ptr = (ptr); \
|
|
2225 eicat_raw (ei, ei16p5ptr, qxestrlen (ei16p5ptr)); \
|
|
2226 } while (0)
|
|
2227
|
|
2228 #define eicat_lstr(ei, lisp_string) \
|
|
2229 do { \
|
|
2230 Lisp_Object ei17 = (lisp_string); \
|
|
2231 eicat_1 (ei, XSTRING_DATA (ei17), XSTRING_LENGTH (ei17), \
|
826
|
2232 string_char_length (ei17)); \
|
771
|
2233 } while (0)
|
|
2234
|
|
2235 #define eicat_ch(ei, ch) \
|
|
2236 do { \
|
|
2237 Intbyte ei22ch[MAX_EMCHAR_LEN]; \
|
|
2238 Bytecount ei22len = set_charptr_emchar (ei22ch, ch); \
|
|
2239 eicat_1 (ei, ei22ch, ei22len, 1); \
|
|
2240 } while (0)
|
|
2241
|
|
2242
|
|
2243 /* ----- Replacement ----- */
|
|
2244
|
|
2245 /* Replace the section of an Eistring at (OFF, LEN) with the data at
|
|
2246 SRC of length LEN. All positions have corresponding character values,
|
|
2247 and either can be -1 -- it will be computed from the other. */
|
|
2248
|
|
2249 #define eisub_1(ei, off, charoff, len, charlen, src, srclen, srccharlen) \
|
|
2250 do { \
|
|
2251 int ei18off = (off); \
|
|
2252 int ei18charoff = (charoff); \
|
|
2253 int ei18len = (len); \
|
|
2254 int ei18charlen = (charlen); \
|
|
2255 Intbyte *ei18src = (Intbyte *) (src); \
|
|
2256 int ei18srclen = (srclen); \
|
|
2257 int ei18srccharlen = (srccharlen); \
|
|
2258 \
|
|
2259 int ei18oldeibytelen = (ei)->bytelen_; \
|
|
2260 \
|
|
2261 eifixup_bytechar ((ei)->data_, ei18off, ei18charoff); \
|
|
2262 eifixup_bytechar ((ei)->data_ + ei18off, ei18len, ei18charlen); \
|
|
2263 eifixup_bytechar (ei18src, ei18srclen, ei18srccharlen); \
|
|
2264 \
|
|
2265 EI_ALLOC (ei, (ei)->bytelen_ + ei18srclen - ei18len, \
|
|
2266 (ei)->charlen_ + ei18srccharlen - ei18charlen, 0); \
|
|
2267 if (ei18len != ei18srclen) \
|
|
2268 memmove ((ei)->data_ + ei18off + ei18srclen, \
|
|
2269 (ei)->data_ + ei18off + ei18len, \
|
|
2270 /* include zero terminator. */ \
|
|
2271 ei18oldeibytelen - (ei18off + ei18len) + 1); \
|
|
2272 if (ei18srclen > 0) \
|
|
2273 memcpy ((ei)->data_ + ei18off, ei18src, ei18srclen); \
|
|
2274 } while (0)
|
|
2275
|
|
2276 #define eisub_ei(ei, off, charoff, len, charlen, ei2) \
|
|
2277 do { \
|
|
2278 const Eistring *ei19 = (ei2); \
|
|
2279 eisub_1 (ei, off, charoff, len, charlen, ei19->data_, ei19->bytelen_, \
|
|
2280 ei19->charlen_); \
|
|
2281 } while (0)
|
|
2282
|
|
2283 #define eisub_c(ei, off, charoff, len, charlen, c_string) \
|
|
2284 do { \
|
|
2285 const Char_ASCII *ei20 = (c_string); \
|
|
2286 int ei20len = strlen (ei20); \
|
|
2287 EI_ASSERT_ASCII (ei20, ei20len); \
|
|
2288 eisub_1 (ei, off, charoff, len, charlen, ei20, ei20len, -1); \
|
|
2289 } while (0)
|
|
2290
|
|
2291 #define eisub_ch(ei, off, charoff, len, charlen, ch) \
|
|
2292 do { \
|
|
2293 Intbyte ei21ch[MAX_EMCHAR_LEN]; \
|
|
2294 Bytecount ei21len = set_charptr_emchar (ei21ch, ch); \
|
|
2295 eisub_1 (ei, off, charoff, len, charlen, ei21ch, ei21len, 1); \
|
|
2296 } while (0)
|
|
2297
|
|
2298 #define eidel(ei, off, charoff, len, charlen) \
|
|
2299 eisub_1(ei, off, charoff, len, charlen, NULL, 0, 0)
|
|
2300
|
|
2301
|
|
2302 /* ----- Converting to an external format ----- */
|
|
2303
|
|
2304 #define eito_external(ei, coding_system) \
|
|
2305 do { \
|
|
2306 if ((ei)->mallocp_) \
|
|
2307 { \
|
|
2308 if ((ei)->extdata_) \
|
|
2309 { \
|
|
2310 xfree ((ei)->extdata_); \
|
|
2311 (ei)->extdata_ = 0; \
|
|
2312 } \
|
|
2313 TO_EXTERNAL_FORMAT (DATA, ((ei)->data_, (ei)->bytelen_), \
|
|
2314 MALLOC, ((ei)->extdata_, (ei)->extlen_), \
|
|
2315 coding_system); \
|
|
2316 } \
|
|
2317 else \
|
|
2318 TO_EXTERNAL_FORMAT (DATA, ((ei)->data_, (ei)->bytelen_), \
|
|
2319 ALLOCA, ((ei)->extdata_, (ei)->extlen_), \
|
|
2320 coding_system); \
|
|
2321 } while (0)
|
|
2322
|
|
2323 #define eiextdata(ei) ((ei)->extdata_)
|
|
2324 #define eiextlen(ei) ((ei)->extlen_)
|
|
2325
|
|
2326
|
|
2327 /* ----- Searching in the Eistring for a character ----- */
|
|
2328
|
|
2329 #define eichr(eistr, chr) \
|
|
2330 NOT YET IMPLEMENTED
|
|
2331 #define eichr_char(eistr, chr) \
|
|
2332 NOT YET IMPLEMENTED
|
|
2333 #define eichr_off(eistr, chr, off, charoff) \
|
|
2334 NOT YET IMPLEMENTED
|
|
2335 #define eichr_off_char(eistr, chr, off, charoff) \
|
|
2336 NOT YET IMPLEMENTED
|
|
2337 #define eirchr(eistr, chr) \
|
|
2338 NOT YET IMPLEMENTED
|
|
2339 #define eirchr_char(eistr, chr) \
|
|
2340 NOT YET IMPLEMENTED
|
|
2341 #define eirchr_off(eistr, chr, off, charoff) \
|
|
2342 NOT YET IMPLEMENTED
|
|
2343 #define eirchr_off_char(eistr, chr, off, charoff) \
|
|
2344 NOT YET IMPLEMENTED
|
|
2345
|
|
2346
|
|
2347 /* ----- Searching in the Eistring for a string ----- */
|
|
2348
|
|
2349 #define eistr_ei(eistr, eistr2) \
|
|
2350 NOT YET IMPLEMENTED
|
|
2351 #define eistr_ei_char(eistr, eistr2) \
|
|
2352 NOT YET IMPLEMENTED
|
|
2353 #define eistr_ei_off(eistr, eistr2, off, charoff) \
|
|
2354 NOT YET IMPLEMENTED
|
|
2355 #define eistr_ei_off_char(eistr, eistr2, off, charoff) \
|
|
2356 NOT YET IMPLEMENTED
|
|
2357 #define eirstr_ei(eistr, eistr2) \
|
|
2358 NOT YET IMPLEMENTED
|
|
2359 #define eirstr_ei_char(eistr, eistr2) \
|
|
2360 NOT YET IMPLEMENTED
|
|
2361 #define eirstr_ei_off(eistr, eistr2, off, charoff) \
|
|
2362 NOT YET IMPLEMENTED
|
|
2363 #define eirstr_ei_off_char(eistr, eistr2, off, charoff) \
|
|
2364 NOT YET IMPLEMENTED
|
|
2365
|
|
2366 #define eistr_c(eistr, c_string) \
|
|
2367 NOT YET IMPLEMENTED
|
|
2368 #define eistr_c_char(eistr, c_string) \
|
|
2369 NOT YET IMPLEMENTED
|
|
2370 #define eistr_c_off(eistr, c_string, off, charoff) \
|
|
2371 NOT YET IMPLEMENTED
|
|
2372 #define eistr_c_off_char(eistr, c_string, off, charoff) \
|
|
2373 NOT YET IMPLEMENTED
|
|
2374 #define eirstr_c(eistr, c_string) \
|
|
2375 NOT YET IMPLEMENTED
|
|
2376 #define eirstr_c_char(eistr, c_string) \
|
|
2377 NOT YET IMPLEMENTED
|
|
2378 #define eirstr_c_off(eistr, c_string, off, charoff) \
|
|
2379 NOT YET IMPLEMENTED
|
|
2380 #define eirstr_c_off_char(eistr, c_string, off, charoff) \
|
|
2381 NOT YET IMPLEMENTED
|
|
2382
|
|
2383
|
|
2384 /* ----- Comparison ----- */
|
|
2385
|
|
2386 int eicmp_1 (Eistring *ei, Bytecount off, Charcount charoff,
|
|
2387 Bytecount len, Charcount charlen, const Intbyte *data,
|
|
2388 const Eistring *ei2, int is_c, int fold_case);
|
|
2389
|
|
2390 #define eicmp_ei(eistr, eistr2) \
|
|
2391 eicmp_1 (eistr, 0, -1, -1, -1, 0, eistr2, 0, 0)
|
|
2392 #define eicmp_off_ei(eistr, off, charoff, len, charlen, eistr2) \
|
|
2393 eicmp_1 (eistr, off, charoff, len, charlen, 0, eistr2, 0, 0)
|
|
2394 #define eicasecmp_ei(eistr, eistr2) \
|
|
2395 eicmp_1 (eistr, 0, -1, -1, -1, 0, eistr2, 0, 1)
|
|
2396 #define eicasecmp_off_ei(eistr, off, charoff, len, charlen, eistr2) \
|
|
2397 eicmp_1 (eistr, off, charoff, len, charlen, 0, eistr2, 0, 1)
|
|
2398 #define eicasecmp_i18n_ei(eistr, eistr2) \
|
|
2399 eicmp_1 (eistr, 0, -1, -1, -1, 0, eistr2, 0, 2)
|
|
2400 #define eicasecmp_i18n_off_ei(eistr, off, charoff, len, charlen, eistr2) \
|
|
2401 eicmp_1 (eistr, off, charoff, len, charlen, 0, eistr2, 0, 2)
|
|
2402
|
|
2403 #define eicmp_c(eistr, c_string) \
|
|
2404 eicmp_1 (eistr, 0, -1, -1, -1, c_string, 0, 1, 0)
|
|
2405 #define eicmp_off_c(eistr, off, charoff, len, charlen, c_string) \
|
|
2406 eicmp_1 (eistr, off, charoff, len, charlen, c_string, 0, 1, 0)
|
|
2407 #define eicasecmp_c(eistr, c_string) \
|
|
2408 eicmp_1 (eistr, 0, -1, -1, -1, c_string, 0, 1, 1)
|
|
2409 #define eicasecmp_off_c(eistr, off, charoff, len, charlen, c_string) \
|
|
2410 eicmp_1 (eistr, off, charoff, len, charlen, c_string, 0, 1, 1)
|
|
2411 #define eicasecmp_i18n_c(eistr, c_string) \
|
|
2412 eicmp_1 (eistr, 0, -1, -1, -1, c_string, 0, 1, 2)
|
|
2413 #define eicasecmp_i18n_off_c(eistr, off, charoff, len, charlen, c_string) \
|
|
2414 eicmp_1 (eistr, off, charoff, len, charlen, c_string, 0, 1, 2)
|
|
2415
|
|
2416
|
|
2417 /* ----- Case-changing the Eistring ----- */
|
|
2418
|
|
2419 int eistr_casefiddle_1 (Intbyte *olddata, Bytecount len, Intbyte *newdata,
|
|
2420 int downp);
|
|
2421
|
|
2422 #define EI_CASECHANGE(ei, downp) \
|
|
2423 do { \
|
|
2424 int ei11new_allocmax = (ei)->charlen_ * MAX_EMCHAR_LEN + 1; \
|
|
2425 Intbyte *ei11storage = (Intbyte *) alloca_array (Intbyte, \
|
|
2426 ei11new_allocmax); \
|
|
2427 int ei11newlen = eistr_casefiddle_1 ((ei)->data_, (ei)->bytelen_, \
|
|
2428 ei11storage, downp); \
|
|
2429 \
|
|
2430 if (ei11newlen) \
|
|
2431 { \
|
|
2432 (ei)->max_size_allocated_ = ei11new_allocmax; \
|
|
2433 (ei)->data_ = ei11storage; \
|
|
2434 (ei)->bytelen_ = ei11newlen; \
|
|
2435 /* charlen is the same. */ \
|
|
2436 } \
|
|
2437 } while (0)
|
|
2438
|
|
2439 #define eilwr(ei) EI_CASECHANGE (ei, 1)
|
|
2440 #define eiupr(ei) EI_CASECHANGE (ei, 0)
|
|
2441
|
|
2442
|
|
2443 /************************************************************************/
|
|
2444 /* */
|
|
2445 /* Converting between internal and external format */
|
|
2446 /* */
|
|
2447 /************************************************************************/
|
|
2448 /*
|
|
2449 All client code should use only the two macros
|
|
2450
|
|
2451 TO_EXTERNAL_FORMAT (source_type, source, sink_type, sink, coding_system)
|
|
2452 TO_INTERNAL_FORMAT (source_type, source, sink_type, sink, coding_system)
|
|
2453
|
|
2454 Typical use is
|
|
2455
|
|
2456 TO_EXTERNAL_FORMAT (DATA, (ptr, len),
|
|
2457 LISP_BUFFER, buffer,
|
|
2458 Qfile_name);
|
|
2459
|
|
2460 NOTE: GC is inhibited during the entire operation of these macros. This
|
|
2461 is because frequently the data to be converted comes from strings but
|
|
2462 gets passed in as just DATA, and GC may move around the string data. If
|
|
2463 we didn't inhibit GC, there'd have to be a lot of messy recoding,
|
|
2464 alloca-copying of strings and other annoying stuff.
|
|
2465
|
|
2466 The source or sink can be specified in one of these ways:
|
|
2467
|
|
2468 DATA, (ptr, len), // input data is a fixed buffer of size len
|
|
2469 ALLOCA, (ptr, len), // output data is in a alloca()ed buffer of size len
|
|
2470 MALLOC, (ptr, len), // output data is in a malloc()ed buffer of size len
|
|
2471 C_STRING_ALLOCA, ptr, // equivalent to ALLOCA (ptr, len_ignored) on output
|
|
2472 C_STRING_MALLOC, ptr, // equivalent to MALLOC (ptr, len_ignored) on output
|
|
2473 C_STRING, ptr, // equivalent to DATA, (ptr, strlen/wcslen (ptr))
|
|
2474 // on input (the Unicode version is used when correct)
|
|
2475 LISP_STRING, string, // input or output is a Lisp_Object of type string
|
|
2476 LISP_BUFFER, buffer, // output is written to (point) in lisp buffer
|
|
2477 LISP_LSTREAM, lstream, // input or output is a Lisp_Object of type lstream
|
|
2478 LISP_OPAQUE, object, // input or output is a Lisp_Object of type opaque
|
|
2479
|
|
2480 When specifying the sink, use lvalues, since the macro will assign to them,
|
|
2481 except when the sink is an lstream or a lisp buffer.
|
|
2482
|
|
2483 The macros accept the kinds of sources and sinks appropriate for
|
|
2484 internal and external data representation. See the type_checking_assert
|
|
2485 macros below for the actual allowed types.
|
|
2486
|
|
2487 Since some sources and sinks use one argument (a Lisp_Object) to
|
|
2488 specify them, while others take a (pointer, length) pair, we use
|
|
2489 some C preprocessor trickery to allow pair arguments to be specified
|
|
2490 by parenthesizing them, as in the examples above.
|
|
2491
|
|
2492 Anything prefixed by dfc_ (`data format conversion') is private.
|
|
2493 They are only used to implement these macros.
|
|
2494
|
|
2495 [[Using C_STRING* is appropriate for using with external APIs that
|
|
2496 take null-terminated strings. For internal data, we should try to
|
|
2497 be '\0'-clean - i.e. allow arbitrary data to contain embedded '\0'.
|
|
2498
|
|
2499 Sometime in the future we might allow output to C_STRING_ALLOCA or
|
|
2500 C_STRING_MALLOC _only_ with TO_EXTERNAL_FORMAT(), not
|
|
2501 TO_INTERNAL_FORMAT().]]
|
|
2502
|
|
2503 The above comments are not true. Frequently (most of the time, in
|
|
2504 fact), external strings come as zero-terminated entities, where the
|
|
2505 zero-termination is the only way to find out the length. Even in
|
|
2506 cases where you can get the length, most of the time the system will
|
|
2507 still use the null to signal the end of the string, and there will
|
|
2508 still be no way to either send in or receive a string with embedded
|
|
2509 nulls. In such situations, it's pointless to track the length
|
|
2510 because null bytes can never be in the string. We have a lot of
|
|
2511 operations that make it easy to operate on zero-terminated strings,
|
|
2512 and forcing the user the deal with the length everywhere would only
|
|
2513 make the code uglier and more complicated, for no gain. --ben
|
|
2514
|
|
2515 There is no problem using the same lvalue for source and sink.
|
|
2516
|
|
2517 Also, when pointers are required, the code (currently at least) is
|
|
2518 lax and allows any pointer types, either in the source or the sink.
|
|
2519 This makes it possible, e.g., to deal with internal format data held
|
|
2520 in char *'s or external format data held in WCHAR * (i.e. Unicode).
|
|
2521
|
|
2522 Finally, whenever storage allocation is called for, extra space is
|
|
2523 allocated for a terminating zero, and such a zero is stored in the
|
|
2524 appropriate place, regardless of whether the source data was
|
|
2525 specified using a length or was specified as zero-terminated. This
|
|
2526 allows you to freely pass the resulting data, no matter how
|
|
2527 obtained, to a routine that expects zero termination (modulo, of
|
|
2528 course, that any embedded zeros in the resulting text will cause
|
|
2529 truncation). In fact, currently two embedded zeros are allocated
|
|
2530 and stored after the data result. This is to allow for the
|
|
2531 possibility of storing a Unicode value on output, which needs the
|
|
2532 two zeros. Currently, however, the two zeros are stored regardless
|
|
2533 of whether the conversion is internal or external and regardless of
|
|
2534 whether the external coding system is in fact Unicode. This
|
|
2535 behavior may change in the future, and you cannot rely on this --
|
|
2536 the most you can rely on is that sink data in Unicode format will
|
|
2537 have two terminating nulls, which combine to form one Unicode null
|
|
2538 character. */
|
|
2539
|
|
2540 #define TO_EXTERNAL_FORMAT(source_type, source, sink_type, sink, codesys) \
|
|
2541 do { \
|
|
2542 dfc_conversion_type dfc_simplified_source_type; \
|
|
2543 dfc_conversion_type dfc_simplified_sink_type; \
|
|
2544 dfc_conversion_data dfc_source; \
|
|
2545 dfc_conversion_data dfc_sink; \
|
|
2546 Lisp_Object dfc_codesys = (codesys); \
|
|
2547 \
|
|
2548 type_checking_assert \
|
|
2549 ((DFC_TYPE_##source_type == DFC_TYPE_DATA || \
|
|
2550 DFC_TYPE_##source_type == DFC_TYPE_C_STRING || \
|
|
2551 DFC_TYPE_##source_type == DFC_TYPE_LISP_STRING || \
|
|
2552 DFC_TYPE_##source_type == DFC_TYPE_LISP_OPAQUE || \
|
|
2553 DFC_TYPE_##source_type == DFC_TYPE_LISP_LSTREAM) \
|
|
2554 && \
|
|
2555 (DFC_TYPE_##sink_type == DFC_TYPE_ALLOCA || \
|
|
2556 DFC_TYPE_##sink_type == DFC_TYPE_MALLOC || \
|
|
2557 DFC_TYPE_##sink_type == DFC_TYPE_C_STRING_ALLOCA || \
|
|
2558 DFC_TYPE_##sink_type == DFC_TYPE_C_STRING_MALLOC || \
|
|
2559 DFC_TYPE_##sink_type == DFC_TYPE_LISP_LSTREAM || \
|
|
2560 DFC_TYPE_##sink_type == DFC_TYPE_LISP_OPAQUE)); \
|
|
2561 \
|
|
2562 DFC_EXT_SOURCE_##source_type##_TO_ARGS (source, dfc_codesys); \
|
|
2563 DFC_SINK_##sink_type##_TO_ARGS (sink); \
|
|
2564 \
|
|
2565 dfc_convert_to_external_format (dfc_simplified_source_type, &dfc_source, \
|
|
2566 dfc_codesys, \
|
|
2567 dfc_simplified_sink_type, &dfc_sink); \
|
|
2568 \
|
|
2569 DFC_##sink_type##_USE_CONVERTED_DATA (sink); \
|
|
2570 } while (0)
|
|
2571
|
|
2572 #define TO_INTERNAL_FORMAT(source_type, source, sink_type, sink, codesys) \
|
|
2573 do { \
|
|
2574 dfc_conversion_type dfc_simplified_source_type; \
|
|
2575 dfc_conversion_type dfc_simplified_sink_type; \
|
|
2576 dfc_conversion_data dfc_source; \
|
|
2577 dfc_conversion_data dfc_sink; \
|
|
2578 Lisp_Object dfc_codesys = (codesys); \
|
|
2579 \
|
|
2580 type_checking_assert \
|
|
2581 ((DFC_TYPE_##source_type == DFC_TYPE_DATA || \
|
|
2582 DFC_TYPE_##source_type == DFC_TYPE_C_STRING || \
|
|
2583 DFC_TYPE_##source_type == DFC_TYPE_LISP_OPAQUE || \
|
|
2584 DFC_TYPE_##source_type == DFC_TYPE_LISP_LSTREAM) \
|
|
2585 && \
|
|
2586 (DFC_TYPE_##sink_type == DFC_TYPE_ALLOCA || \
|
|
2587 DFC_TYPE_##sink_type == DFC_TYPE_MALLOC || \
|
|
2588 DFC_TYPE_##sink_type == DFC_TYPE_C_STRING_ALLOCA || \
|
|
2589 DFC_TYPE_##sink_type == DFC_TYPE_C_STRING_MALLOC || \
|
|
2590 DFC_TYPE_##sink_type == DFC_TYPE_LISP_STRING || \
|
|
2591 DFC_TYPE_##sink_type == DFC_TYPE_LISP_LSTREAM || \
|
|
2592 DFC_TYPE_##sink_type == DFC_TYPE_LISP_BUFFER)); \
|
|
2593 \
|
|
2594 DFC_INT_SOURCE_##source_type##_TO_ARGS (source, dfc_codesys); \
|
|
2595 DFC_SINK_##sink_type##_TO_ARGS (sink); \
|
|
2596 \
|
|
2597 dfc_convert_to_internal_format (dfc_simplified_source_type, &dfc_source, \
|
|
2598 dfc_codesys, \
|
|
2599 dfc_simplified_sink_type, &dfc_sink); \
|
|
2600 \
|
|
2601 DFC_##sink_type##_USE_CONVERTED_DATA (sink); \
|
|
2602 } while (0)
|
|
2603
|
814
|
2604 #ifdef __cplusplus
|
771
|
2605
|
814
|
2606 /* Error if you try to use a union here: "member `struct {anonymous
|
|
2607 union}::{anonymous} {anonymous union}::data' with constructor not allowed
|
|
2608 in union" (Bytecount is a class) */
|
|
2609
|
|
2610 typedef struct
|
|
2611 #else
|
771
|
2612 typedef union
|
814
|
2613 #endif
|
771
|
2614 {
|
|
2615 struct { const void *ptr; Bytecount len; } data;
|
|
2616 Lisp_Object lisp_object;
|
|
2617 } dfc_conversion_data;
|
|
2618
|
|
2619 enum dfc_conversion_type
|
|
2620 {
|
|
2621 DFC_TYPE_DATA,
|
|
2622 DFC_TYPE_ALLOCA,
|
|
2623 DFC_TYPE_MALLOC,
|
|
2624 DFC_TYPE_C_STRING,
|
|
2625 DFC_TYPE_C_STRING_ALLOCA,
|
|
2626 DFC_TYPE_C_STRING_MALLOC,
|
|
2627 DFC_TYPE_LISP_STRING,
|
|
2628 DFC_TYPE_LISP_LSTREAM,
|
|
2629 DFC_TYPE_LISP_OPAQUE,
|
|
2630 DFC_TYPE_LISP_BUFFER
|
|
2631 };
|
|
2632 typedef enum dfc_conversion_type dfc_conversion_type;
|
|
2633
|
|
2634 /* WARNING: These use a static buffer. This can lead to disaster if
|
|
2635 these functions are not used *very* carefully. Another reason to only use
|
|
2636 TO_EXTERNAL_FORMAT() and TO_INTERNAL_FORMAT(). */
|
|
2637 void
|
|
2638 dfc_convert_to_external_format (dfc_conversion_type source_type,
|
|
2639 dfc_conversion_data *source,
|
|
2640 Lisp_Object coding_system,
|
|
2641 dfc_conversion_type sink_type,
|
|
2642 dfc_conversion_data *sink);
|
|
2643 void
|
|
2644 dfc_convert_to_internal_format (dfc_conversion_type source_type,
|
|
2645 dfc_conversion_data *source,
|
|
2646 Lisp_Object coding_system,
|
|
2647 dfc_conversion_type sink_type,
|
|
2648 dfc_conversion_data *sink);
|
|
2649 /* CPP Trickery */
|
|
2650 #define DFC_CPP_CAR(x,y) (x)
|
|
2651 #define DFC_CPP_CDR(x,y) (y)
|
|
2652
|
|
2653 /* Convert `source' to args for dfc_convert_to_external_format() */
|
|
2654 #define DFC_EXT_SOURCE_DATA_TO_ARGS(val, codesys) do { \
|
|
2655 dfc_source.data.ptr = DFC_CPP_CAR val; \
|
|
2656 dfc_source.data.len = DFC_CPP_CDR val; \
|
|
2657 dfc_simplified_source_type = DFC_TYPE_DATA; \
|
|
2658 } while (0)
|
|
2659 #define DFC_EXT_SOURCE_C_STRING_TO_ARGS(val, codesys) do { \
|
|
2660 dfc_source.data.len = \
|
|
2661 strlen ((char *) (dfc_source.data.ptr = (val))); \
|
|
2662 dfc_simplified_source_type = DFC_TYPE_DATA; \
|
|
2663 } while (0)
|
|
2664 #define DFC_EXT_SOURCE_LISP_STRING_TO_ARGS(val, codesys) do { \
|
|
2665 Lisp_Object dfc_slsta = (val); \
|
|
2666 type_checking_assert (STRINGP (dfc_slsta)); \
|
|
2667 dfc_source.lisp_object = dfc_slsta; \
|
|
2668 dfc_simplified_source_type = DFC_TYPE_LISP_STRING; \
|
|
2669 } while (0)
|
|
2670 #define DFC_EXT_SOURCE_LISP_LSTREAM_TO_ARGS(val, codesys) do { \
|
|
2671 Lisp_Object dfc_sllta = (val); \
|
|
2672 type_checking_assert (LSTREAMP (dfc_sllta)); \
|
|
2673 dfc_source.lisp_object = dfc_sllta; \
|
|
2674 dfc_simplified_source_type = DFC_TYPE_LISP_LSTREAM; \
|
|
2675 } while (0)
|
|
2676 #define DFC_EXT_SOURCE_LISP_OPAQUE_TO_ARGS(val, codesys) do { \
|
|
2677 Lisp_Opaque *dfc_slota = XOPAQUE (val); \
|
|
2678 dfc_source.data.ptr = OPAQUE_DATA (dfc_slota); \
|
|
2679 dfc_source.data.len = OPAQUE_SIZE (dfc_slota); \
|
|
2680 dfc_simplified_source_type = DFC_TYPE_DATA; \
|
|
2681 } while (0)
|
|
2682
|
|
2683 /* Convert `source' to args for dfc_convert_to_internal_format() */
|
|
2684 #define DFC_INT_SOURCE_DATA_TO_ARGS(val, codesys) \
|
|
2685 DFC_EXT_SOURCE_DATA_TO_ARGS (val, codesys)
|
|
2686 #define DFC_INT_SOURCE_C_STRING_TO_ARGS(val, codesys) do { \
|
|
2687 dfc_source.data.len = dfc_external_data_len (dfc_source.data.ptr = (val), \
|
|
2688 codesys); \
|
|
2689 dfc_simplified_source_type = DFC_TYPE_DATA; \
|
|
2690 } while (0)
|
|
2691 #define DFC_INT_SOURCE_LISP_STRING_TO_ARGS(val, codesys) \
|
|
2692 DFC_EXT_SOURCE_LISP_STRING_TO_ARGS (val, codesys)
|
|
2693 #define DFC_INT_SOURCE_LISP_LSTREAM_TO_ARGS(val, codesys) \
|
|
2694 DFC_EXT_SOURCE_LISP_LSTREAM_TO_ARGS (val, codesys)
|
|
2695 #define DFC_INT_SOURCE_LISP_OPAQUE_TO_ARGS(val, codesys) \
|
|
2696 DFC_EXT_SOURCE_LISP_OPAQUE_TO_ARGS (val, codesys)
|
|
2697
|
|
2698 /* Convert `sink' to args for dfc_convert_to_*_format() */
|
|
2699 #define DFC_SINK_ALLOCA_TO_ARGS(val) \
|
|
2700 dfc_simplified_sink_type = DFC_TYPE_DATA
|
|
2701 #define DFC_SINK_C_STRING_ALLOCA_TO_ARGS(val) \
|
|
2702 dfc_simplified_sink_type = DFC_TYPE_DATA
|
|
2703 #define DFC_SINK_MALLOC_TO_ARGS(val) \
|
|
2704 dfc_simplified_sink_type = DFC_TYPE_DATA
|
|
2705 #define DFC_SINK_C_STRING_MALLOC_TO_ARGS(val) \
|
|
2706 dfc_simplified_sink_type = DFC_TYPE_DATA
|
|
2707 #define DFC_SINK_LISP_STRING_TO_ARGS(val) \
|
|
2708 dfc_simplified_sink_type = DFC_TYPE_DATA
|
|
2709 #define DFC_SINK_LISP_OPAQUE_TO_ARGS(val) \
|
|
2710 dfc_simplified_sink_type = DFC_TYPE_DATA
|
|
2711 #define DFC_SINK_LISP_LSTREAM_TO_ARGS(val) do { \
|
|
2712 Lisp_Object dfc_sllta = (val); \
|
|
2713 type_checking_assert (LSTREAMP (dfc_sllta)); \
|
|
2714 dfc_sink.lisp_object = dfc_sllta; \
|
|
2715 dfc_simplified_sink_type = DFC_TYPE_LISP_LSTREAM; \
|
|
2716 } while (0)
|
|
2717 #define DFC_SINK_LISP_BUFFER_TO_ARGS(val) do { \
|
|
2718 struct buffer *dfc_slbta = XBUFFER (val); \
|
|
2719 dfc_sink.lisp_object = \
|
|
2720 make_lisp_buffer_output_stream \
|
|
2721 (dfc_slbta, BUF_PT (dfc_slbta), 0); \
|
|
2722 dfc_simplified_sink_type = DFC_TYPE_LISP_LSTREAM; \
|
|
2723 } while (0)
|
|
2724
|
|
2725 /* Assign to the `sink' lvalue(s) using the converted data. */
|
|
2726 /* + 2 because we double zero-extended to account for Unicode conversion */
|
|
2727 typedef union { char c; void *p; } *dfc_aliasing_voidpp;
|
|
2728 #define DFC_ALLOCA_USE_CONVERTED_DATA(sink) do { \
|
|
2729 void * dfc_sink_ret = alloca (dfc_sink.data.len + 2); \
|
|
2730 memcpy (dfc_sink_ret, dfc_sink.data.ptr, dfc_sink.data.len + 2); \
|
|
2731 ((dfc_aliasing_voidpp) &(DFC_CPP_CAR sink))->p = dfc_sink_ret; \
|
|
2732 (DFC_CPP_CDR sink) = dfc_sink.data.len; \
|
|
2733 } while (0)
|
|
2734 #define DFC_MALLOC_USE_CONVERTED_DATA(sink) do { \
|
|
2735 void * dfc_sink_ret = xmalloc (dfc_sink.data.len + 2); \
|
|
2736 memcpy (dfc_sink_ret, dfc_sink.data.ptr, dfc_sink.data.len + 2); \
|
|
2737 ((dfc_aliasing_voidpp) &(DFC_CPP_CAR sink))->p = dfc_sink_ret; \
|
|
2738 (DFC_CPP_CDR sink) = dfc_sink.data.len; \
|
|
2739 } while (0)
|
|
2740 #define DFC_C_STRING_ALLOCA_USE_CONVERTED_DATA(sink) do { \
|
|
2741 void * dfc_sink_ret = alloca (dfc_sink.data.len + 2); \
|
|
2742 memcpy (dfc_sink_ret, dfc_sink.data.ptr, dfc_sink.data.len + 2); \
|
|
2743 ((dfc_aliasing_voidpp) &(sink))->p = dfc_sink_ret; \
|
|
2744 } while (0)
|
|
2745 #define DFC_C_STRING_MALLOC_USE_CONVERTED_DATA(sink) do { \
|
|
2746 void * dfc_sink_ret = xmalloc (dfc_sink.data.len + 2); \
|
|
2747 memcpy (dfc_sink_ret, dfc_sink.data.ptr, dfc_sink.data.len + 2); \
|
|
2748 ((dfc_aliasing_voidpp) &(sink))->p = dfc_sink_ret; \
|
|
2749 } while (0)
|
|
2750 #define DFC_LISP_STRING_USE_CONVERTED_DATA(sink) \
|
|
2751 sink = make_string ((Intbyte *) dfc_sink.data.ptr, dfc_sink.data.len)
|
|
2752 #define DFC_LISP_OPAQUE_USE_CONVERTED_DATA(sink) \
|
|
2753 sink = make_opaque (dfc_sink.data.ptr, dfc_sink.data.len)
|
|
2754 #define DFC_LISP_LSTREAM_USE_CONVERTED_DATA(sink) /* data already used */
|
|
2755 #define DFC_LISP_BUFFER_USE_CONVERTED_DATA(sink) \
|
|
2756 Lstream_delete (XLSTREAM (dfc_sink.lisp_object))
|
|
2757
|
|
2758 /* Convenience macros for extremely common invocations */
|
|
2759 #define C_STRING_TO_EXTERNAL(in, out, coding_system) \
|
|
2760 TO_EXTERNAL_FORMAT (C_STRING, in, C_STRING_ALLOCA, out, coding_system)
|
|
2761 #define C_STRING_TO_EXTERNAL_MALLOC(in, out, coding_system) \
|
|
2762 TO_EXTERNAL_FORMAT (C_STRING, in, C_STRING_MALLOC, out, coding_system)
|
|
2763 #define EXTERNAL_TO_C_STRING(in, out, coding_system) \
|
|
2764 TO_INTERNAL_FORMAT (C_STRING, in, C_STRING_ALLOCA, out, coding_system)
|
|
2765 #define EXTERNAL_TO_C_STRING_MALLOC(in, out, coding_system) \
|
|
2766 TO_INTERNAL_FORMAT (C_STRING, in, C_STRING_MALLOC, out, coding_system)
|
|
2767 #define LISP_STRING_TO_EXTERNAL(in, out, coding_system) \
|
|
2768 TO_EXTERNAL_FORMAT (LISP_STRING, in, C_STRING_ALLOCA, out, coding_system)
|
|
2769 #define LISP_STRING_TO_EXTERNAL_MALLOC(in, out, coding_system) \
|
|
2770 TO_EXTERNAL_FORMAT (LISP_STRING, in, C_STRING_MALLOC, out, coding_system)
|
|
2771
|
|
2772 /* Standins for various encodings, until we know them better */
|
|
2773 #define Qcommand_argument_encoding Qnative
|
|
2774 #define Qenvironment_variable_encoding Qnative
|
|
2775 #define Qunix_host_name_encoding Qnative
|
|
2776 #define Qunix_service_name_encoding Qnative
|
|
2777 #define Qmswindows_host_name_encoding Qmswindows_multibyte
|
|
2778 #define Qmswindows_service_name_encoding Qmswindows_multibyte
|
|
2779
|
|
2780 /* Standins for various X encodings, until we know them better */
|
|
2781
|
|
2782 /* !!#### Need to verify the encoding used in lwlib -- Qnative or Qctext?
|
|
2783 Almost certainly the former. Use a standin for now. */
|
|
2784 #define Qlwlib_encoding Qnative
|
|
2785
|
|
2786 #define Qx_atom_name_encoding Qctext
|
|
2787 /* font names are often stored in atoms, so it gets sticky if we set this
|
|
2788 to something different from atom-name encoding */
|
|
2789 #define Qx_font_name_encoding Qctext
|
|
2790
|
|
2791 #define Qx_color_name_encoding Qctext
|
|
2792
|
|
2793 /* the following probably must agree with Qcommand_argument_encoding and
|
|
2794 Qenvironment_variable_encoding */
|
|
2795 #define Qx_display_name_encoding Qnative
|
|
2796
|
|
2797 #define Qstrerror_encoding Qnative
|
|
2798
|
|
2799 #define GET_STRERROR(var, num) \
|
|
2800 do { \
|
|
2801 int __gsnum__ = (num); \
|
|
2802 Extbyte * __gserr__ = strerror (__gsnum__); \
|
|
2803 \
|
|
2804 if (!__gserr__) \
|
|
2805 { \
|
826
|
2806 var = alloca_intbytes (99); \
|
771
|
2807 qxesprintf (var, "Unknown error %d", __gsnum__); \
|
|
2808 } \
|
|
2809 else \
|
|
2810 EXTERNAL_TO_C_STRING (__gserr__, var, Qstrerror_encoding); \
|
|
2811 } while (0)
|
|
2812
|
|
2813 #endif /* INCLUDED_text_h_ */
|