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
|
|
55 #define REP_BYTES_BY_FIRST_BYTE(fb) 1
|
|
56 #define BYTE_ASCII_P(byte) 1
|
|
57 # define MAX_EMCHAR_LEN 1
|
|
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
|
|
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)
|
|
67
|
|
68 /* Does BYTE represent the first byte of a character? */
|
|
69
|
|
70 #define INTBYTE_FIRST_BYTE_P(byte) ((byte) < 0xA0)
|
|
71
|
|
72 /* Does BYTE represent the first byte of a multi-byte character? */
|
|
73
|
|
74 #define INTBYTE_LEADING_BYTE_P(byte) BYTE_C1_P (byte)
|
|
75
|
|
76 /* Table of number of bytes in the string representation of a character
|
|
77 indexed by the first byte of that representation.
|
|
78
|
|
79 This value can be derived in other ways -- e.g. something like
|
|
80 XCHARSET_REP_BYTES (CHARSET_BY_LEADING_BYTE (first_byte))
|
|
81 but it's faster this way. */
|
|
82 extern const Bytecount rep_bytes_by_first_byte[0xA0];
|
|
83
|
|
84 /* Number of bytes in the string representation of a character. */
|
788
|
85
|
|
86 #ifdef ERROR_CHECK_TYPECHECK
|
|
87
|
|
88 INLINE_HEADER int REP_BYTES_BY_FIRST_BYTE_1 (int fb, const char *file,
|
|
89 int line);
|
771
|
90 INLINE_HEADER int
|
788
|
91 REP_BYTES_BY_FIRST_BYTE_1 (int fb, const char *file, int line)
|
771
|
92 {
|
788
|
93 assert_at_line (fb < 0xA0, file, line);
|
771
|
94 return rep_bytes_by_first_byte[fb];
|
|
95 }
|
|
96
|
788
|
97 #define REP_BYTES_BY_FIRST_BYTE(fb) \
|
|
98 REP_BYTES_BY_FIRST_BYTE_1 (fb, __FILE__, __LINE__)
|
|
99
|
|
100 #else /* ERROR_CHECK_TYPECHECK */
|
|
101
|
|
102 #define REP_BYTES_BY_FIRST_BYTE(fb) (rep_bytes_by_first_byte[fb])
|
|
103
|
|
104 #endif /* ERROR_CHECK_TYPECHECK */
|
|
105
|
771
|
106 /* Is this character represented by more than one byte in a string? */
|
|
107
|
|
108 #define CHAR_MULTIBYTE_P(c) ((c) >= 0x80)
|
|
109
|
|
110 #define CHAR_ASCII_P(c) (!CHAR_MULTIBYTE_P (c))
|
|
111
|
|
112 #define MAX_EMCHAR_LEN 4
|
|
113
|
|
114 #endif /* MULE */
|
|
115
|
|
116 int dfc_coding_system_is_unicode (Lisp_Object coding_system);
|
|
117
|
|
118 DECLARE_INLINE_HEADER (
|
|
119 Bytecount dfc_external_data_len (const void *ptr, Lisp_Object codesys)
|
|
120 )
|
|
121 {
|
|
122 if (dfc_coding_system_is_unicode (codesys))
|
|
123 return sizeof (wchar_t) * wcslen ((wchar_t *) ptr);
|
|
124 else
|
|
125 return strlen ((char *) ptr);
|
|
126 }
|
|
127
|
|
128
|
|
129 /************************************************************************/
|
|
130 /* */
|
|
131 /* working with raw internal-format data */
|
|
132 /* */
|
|
133 /************************************************************************/
|
|
134
|
|
135 /* NOTE: In all the following macros, we follow these rules concerning
|
|
136 multiple evaluation of the arguments:
|
|
137
|
|
138 1) Anything that's an lvalue can be evaluated more than once.
|
|
139 2) Anything that's a Lisp Object can be evaluated more than once.
|
|
140 This should probably be changed, but this follows the way
|
|
141 that all the macros in lisp.h do things.
|
|
142 3) 'struct buffer *' arguments can be evaluated more than once.
|
|
143 4) Nothing else can be evaluated more than once. Use inline
|
|
144 functions, if necessary, to prevent multiple evaluation.
|
|
145 5) An exception to (4) is that there are some macros below that
|
|
146 may evaluate their arguments more than once. They are all
|
|
147 denoted with the word "unsafe" in their name and are generally
|
|
148 meant to be called only by other macros that have already
|
|
149 stored the calling values in temporary variables.
|
|
150
|
|
151
|
|
152 Use the following functions/macros on contiguous strings of data.
|
|
153 If the text you're operating on is known to come from a buffer, use
|
|
154 the buffer-level functions below -- they know about the gap and may
|
|
155 be more efficient.
|
|
156
|
|
157
|
|
158 ----------------------------------------------------------------------------
|
|
159 (A) For working with charptr's (pointers to internally-formatted text):
|
|
160 ----------------------------------------------------------------------------
|
|
161
|
|
162 VALID_CHARPTR_P (ptr):
|
|
163 Given a charptr, does it point to the beginning of a character?
|
|
164
|
|
165 ASSERT_VALID_CHARPTR (ptr):
|
|
166 If error-checking is enabled, assert that the given charptr
|
|
167 points to the beginning of a character. Otherwise, do nothing.
|
|
168
|
|
169 INC_CHARPTR (ptr):
|
|
170 Given a charptr (assumed to point at the beginning of a character),
|
|
171 modify that pointer so it points to the beginning of the next
|
|
172 character.
|
|
173
|
|
174 DEC_CHARPTR (ptr):
|
|
175 Given a charptr (assumed to point at the beginning of a
|
|
176 character or at the very end of the text), modify that pointer
|
|
177 so it points to the beginning of the previous character.
|
|
178
|
|
179 VALIDATE_CHARPTR_BACKWARD (ptr):
|
|
180 Make sure that PTR is pointing to the beginning of a character.
|
|
181 If not, back up until this is the case. Note that there are not
|
|
182 too many places where it is legitimate to do this sort of thing.
|
|
183 It's an error if you're passed an "invalid" char * pointer.
|
|
184 NOTE: PTR *must* be pointing to a valid part of the string (i.e.
|
|
185 not the very end, unless the string is zero-terminated or
|
|
186 something) in order for this function to not cause crashes.
|
|
187
|
|
188 VALIDATE_CHARPTR_FORWARD (ptr):
|
|
189 Make sure that PTR is pointing to the beginning of a character.
|
|
190 If not, move forward until this is the case. Note that there
|
|
191 are not too many places where it is legitimate to do this sort
|
|
192 of thing. It's an error if you're passed an "invalid" char *
|
|
193 pointer.
|
|
194
|
|
195 ---------------------------------------------------------------------
|
|
196 (B) For working with the length (in bytes and characters) of a
|
|
197 section of internally-formatted text:
|
|
198 ---------------------------------------------------------------------
|
|
199
|
|
200 bytecount_to_charcount (ptr, nbi):
|
|
201 Given a pointer to a text string and a length in bytes,
|
|
202 return the equivalent length in characters.
|
|
203
|
|
204 charcount_to_bytecount (ptr, nch):
|
|
205 Given a pointer to a text string and a length in characters,
|
|
206 return the equivalent length in bytes.
|
|
207
|
|
208 charptr_n_addr (ptr, n):
|
|
209 Return a pointer to the beginning of the character offset N
|
|
210 (in characters) from PTR.
|
|
211
|
|
212 -------------------------------------------------------------------------
|
|
213 (C) For retrieving or changing the character pointed to by a charptr:
|
|
214 -------------------------------------------------------------------------
|
|
215
|
|
216 charptr_emchar (ptr):
|
|
217 Retrieve the character pointed to by PTR as an Emchar.
|
|
218
|
|
219 charptr_emchar_n (ptr, n):
|
|
220 Retrieve the character at offset N (in characters) from PTR,
|
|
221 as an Emchar.
|
|
222
|
|
223 set_charptr_emchar (ptr, ch):
|
|
224 Store the character CH (an Emchar) as internally-formatted
|
|
225 text starting at PTR. Return the number of bytes stored.
|
|
226
|
|
227 charptr_copy_char (src, dst):
|
|
228 Retrieve the character pointed to by SRC and store it as
|
|
229 internally-formatted text in DST.
|
|
230
|
|
231 ----------------------------------
|
|
232 (D) For working with Emchars:
|
|
233 ----------------------------------
|
|
234
|
|
235 [Note that there are other functions/macros for working with Emchars
|
|
236 in charset.h, for retrieving the charset of an Emchar and such.]
|
|
237
|
|
238 valid_char_p (ch):
|
|
239 Return whether the given Emchar is valid.
|
|
240
|
|
241 CHARP (ch):
|
|
242 Return whether the given Lisp_Object is a character.
|
|
243
|
|
244 CHECK_CHAR_COERCE_INT (ch):
|
|
245 Signal an error if CH is not a valid character or integer Lisp_Object.
|
|
246 If CH is an integer Lisp_Object, convert it to a character Lisp_Object,
|
|
247 but merely by repackaging, without performing tests for char validity.
|
|
248
|
|
249 MAX_EMCHAR_LEN:
|
|
250 Maximum number of buffer bytes per Emacs character.
|
|
251 */
|
|
252
|
|
253 /* ---------------------------------------------------------------------- */
|
|
254 /* (A) For working with charptr's (pointers to internally-formatted text) */
|
|
255 /* ---------------------------------------------------------------------- */
|
|
256
|
|
257 #ifdef MULE
|
|
258 # define VALID_CHARPTR_P(ptr) INTBYTE_FIRST_BYTE_P (* (unsigned char *) ptr)
|
|
259 #else
|
|
260 # define VALID_CHARPTR_P(ptr) 1
|
|
261 #endif
|
|
262
|
|
263 #ifdef ERROR_CHECK_CHARBPOS
|
|
264 # define ASSERT_VALID_CHARPTR(ptr) assert (VALID_CHARPTR_P (ptr))
|
|
265 #else
|
|
266 # define ASSERT_VALID_CHARPTR(ptr)
|
|
267 #endif
|
|
268
|
|
269 /* Note that INC_CHARPTR() and DEC_CHARPTR() have to be written in
|
|
270 completely separate ways. INC_CHARPTR() cannot use the DEC_CHARPTR()
|
|
271 trick of looking for a valid first byte because it might run off
|
|
272 the end of the string. DEC_CHARPTR() can't use the INC_CHARPTR()
|
|
273 method because it doesn't have easy access to the first byte of
|
|
274 the character it's moving over. */
|
|
275
|
|
276 #define REAL_INC_CHARPTR(ptr) \
|
|
277 ((void) ((ptr) += REP_BYTES_BY_FIRST_BYTE (* (unsigned char *) (ptr))))
|
|
278
|
|
279 #define REAL_INC_CHARBYTEBPOS(ptr, pos) \
|
|
280 (pos += REP_BYTES_BY_FIRST_BYTE (* (unsigned char *) (ptr)))
|
|
281
|
|
282 #define REAL_DEC_CHARPTR(ptr) do { \
|
|
283 (ptr)--; \
|
|
284 } while (!VALID_CHARPTR_P (ptr))
|
|
285
|
|
286 #ifdef ERROR_CHECK_CHARBPOS
|
|
287 #define INC_CHARPTR(ptr) do { \
|
|
288 ASSERT_VALID_CHARPTR (ptr); \
|
|
289 REAL_INC_CHARPTR (ptr); \
|
|
290 } while (0)
|
|
291
|
|
292 #define INC_CHARBYTEBPOS(ptr, pos) do { \
|
|
293 ASSERT_VALID_CHARPTR (ptr); \
|
|
294 REAL_INC_CHARBYTEBPOS (ptr, pos); \
|
|
295 } while (0)
|
|
296
|
|
297 #define DEC_CHARPTR(ptr) do { \
|
|
298 const Intbyte *dc_ptr1 = (ptr); \
|
|
299 const Intbyte *dc_ptr2 = dc_ptr1; \
|
|
300 REAL_DEC_CHARPTR (dc_ptr2); \
|
|
301 assert (dc_ptr1 - dc_ptr2 == \
|
|
302 REP_BYTES_BY_FIRST_BYTE (*dc_ptr2)); \
|
|
303 (ptr) = (Intbyte *) dc_ptr2; \
|
|
304 } while (0)
|
|
305
|
|
306 #else /* ! ERROR_CHECK_CHARBPOS */
|
|
307 #define INC_CHARBYTEBPOS(ptr, pos) REAL_INC_CHARBYTEBPOS (ptr, pos)
|
|
308 #define INC_CHARPTR(ptr) REAL_INC_CHARPTR (ptr)
|
|
309 #define DEC_CHARPTR(ptr) REAL_DEC_CHARPTR (ptr)
|
|
310 #endif /* ! ERROR_CHECK_CHARBPOS */
|
|
311
|
|
312 #ifdef MULE
|
|
313
|
|
314 /* Note that this reads the byte at *PTR! */
|
|
315
|
|
316 #define VALIDATE_CHARPTR_BACKWARD(ptr) do { \
|
|
317 while (!VALID_CHARPTR_P (ptr)) ptr--; \
|
|
318 } while (0)
|
|
319
|
|
320 /* Given a Intbyte string at PTR of size N, possibly with a partial
|
|
321 character at the end, return the size of the longest substring of
|
|
322 complete characters. Does not assume that the byte at *(PTR + N) is
|
|
323 readable. */
|
|
324 DECLARE_INLINE_HEADER (
|
|
325 Bytecount
|
|
326 validate_intbyte_string_backward (Intbyte *ptr, Bytecount n)
|
|
327 )
|
|
328 {
|
|
329 Intbyte *ptr2;
|
|
330
|
|
331 if (n == 0)
|
|
332 return n;
|
|
333 ptr2 = ptr + n - 1;
|
|
334 VALIDATE_CHARPTR_BACKWARD (ptr2);
|
|
335 if (ptr2 + REP_BYTES_BY_FIRST_BYTE (*ptr2) != ptr + n)
|
|
336 return ptr2 - ptr;
|
|
337 return n;
|
|
338 }
|
|
339
|
|
340 /* This needs to be trickier than VALIDATE_CHARPTR_BACKWARD() to avoid the
|
|
341 possibility of running off the end of the string. */
|
|
342
|
|
343 #define VALIDATE_CHARPTR_FORWARD(ptr) do { \
|
|
344 Intbyte *vcf_ptr = (ptr); \
|
|
345 VALIDATE_CHARPTR_BACKWARD (vcf_ptr); \
|
|
346 if (vcf_ptr != (ptr)) \
|
|
347 { \
|
|
348 (ptr) = vcf_ptr; \
|
|
349 INC_CHARPTR (ptr); \
|
|
350 } \
|
|
351 } while (0)
|
|
352
|
|
353 #else /* not MULE */
|
|
354 #define VALIDATE_CHARPTR_BACKWARD(ptr)
|
|
355 #define VALIDATE_CHARPTR_FORWARD(ptr)
|
|
356 #define validate_intbyte_string_backward(ptr, n) (n)
|
|
357 #endif /* not MULE */
|
|
358
|
|
359 /* -------------------------------------------------------------- */
|
|
360 /* (B) For working with the length (in bytes and characters) of a */
|
|
361 /* section of internally-formatted text */
|
|
362 /* -------------------------------------------------------------- */
|
|
363
|
|
364 INLINE_HEADER const Intbyte *
|
|
365 charptr_n_addr (const Intbyte *ptr, Charcount offset);
|
|
366 INLINE_HEADER const Intbyte *
|
|
367 charptr_n_addr (const Intbyte *ptr, Charcount offset)
|
|
368 {
|
|
369 return ptr + charcount_to_bytecount (ptr, offset);
|
|
370 }
|
|
371
|
|
372 /* -------------------------------------------------------------------- */
|
|
373 /* (C) For retrieving or changing the character pointed to by a charptr */
|
|
374 /* -------------------------------------------------------------------- */
|
|
375
|
|
376 #define simple_charptr_emchar(ptr) ((Emchar) (ptr)[0])
|
|
377 #define simple_set_charptr_emchar(ptr, x) ((ptr)[0] = (Intbyte) (x), 1)
|
|
378 #define simple_charptr_copy_char(src, dst) ((dst)[0] = *(src), 1)
|
|
379
|
|
380 #ifdef MULE
|
|
381
|
|
382 Emchar non_ascii_charptr_emchar (const Intbyte *ptr);
|
|
383 Bytecount non_ascii_set_charptr_emchar (Intbyte *ptr, Emchar c);
|
|
384 Bytecount non_ascii_charptr_copy_char (const Intbyte *src, Intbyte *dst);
|
|
385
|
|
386 INLINE_HEADER Emchar charptr_emchar (const Intbyte *ptr);
|
|
387 INLINE_HEADER Emchar
|
|
388 charptr_emchar (const Intbyte *ptr)
|
|
389 {
|
|
390 return BYTE_ASCII_P (*ptr) ?
|
|
391 simple_charptr_emchar (ptr) :
|
|
392 non_ascii_charptr_emchar (ptr);
|
|
393 }
|
|
394
|
|
395 INLINE_HEADER Bytecount set_charptr_emchar (Intbyte *ptr, Emchar x);
|
|
396 INLINE_HEADER Bytecount
|
|
397 set_charptr_emchar (Intbyte *ptr, Emchar x)
|
|
398 {
|
|
399 return !CHAR_MULTIBYTE_P (x) ?
|
|
400 simple_set_charptr_emchar (ptr, x) :
|
|
401 non_ascii_set_charptr_emchar (ptr, x);
|
|
402 }
|
|
403
|
|
404 INLINE_HEADER Bytecount
|
|
405 charptr_copy_char (const Intbyte *src, Intbyte *dst);
|
|
406 INLINE_HEADER Bytecount
|
|
407 charptr_copy_char (const Intbyte *src, Intbyte *dst)
|
|
408 {
|
|
409 return BYTE_ASCII_P (*src) ?
|
|
410 simple_charptr_copy_char (src, dst) :
|
|
411 non_ascii_charptr_copy_char (src, dst);
|
|
412 }
|
|
413
|
|
414 #else /* not MULE */
|
|
415
|
|
416 # define charptr_emchar(ptr) simple_charptr_emchar (ptr)
|
|
417 # define set_charptr_emchar(ptr, x) simple_set_charptr_emchar (ptr, x)
|
|
418 # define charptr_copy_char(src, dst) simple_charptr_copy_char (src, dst)
|
|
419
|
|
420 #endif /* not MULE */
|
|
421
|
|
422 #define charptr_emchar_n(ptr, offset) \
|
|
423 charptr_emchar (charptr_n_addr (ptr, offset))
|
|
424
|
|
425
|
|
426 /* ---------------------------- */
|
|
427 /* (D) For working with Emchars */
|
|
428 /* ---------------------------- */
|
|
429
|
|
430 #ifdef MULE
|
|
431
|
|
432 int non_ascii_valid_char_p (Emchar ch);
|
|
433
|
|
434 INLINE_HEADER int valid_char_p (Emchar ch);
|
|
435 INLINE_HEADER int
|
|
436 valid_char_p (Emchar ch)
|
|
437 {
|
|
438 return (! (ch & ~0xFF)) || non_ascii_valid_char_p (ch);
|
|
439 }
|
|
440
|
|
441 #else /* not MULE */
|
|
442
|
|
443 #define valid_char_p(ch) (! (ch & ~0xFF))
|
|
444
|
|
445 #endif /* not MULE */
|
|
446
|
|
447 #define CHAR_INTP(x) (INTP (x) && valid_char_p (XINT (x)))
|
|
448
|
|
449 #define CHAR_OR_CHAR_INTP(x) (CHARP (x) || CHAR_INTP (x))
|
|
450
|
|
451 INLINE_HEADER Emchar XCHAR_OR_CHAR_INT (Lisp_Object obj);
|
|
452 INLINE_HEADER Emchar
|
|
453 XCHAR_OR_CHAR_INT (Lisp_Object obj)
|
|
454 {
|
|
455 return CHARP (obj) ? XCHAR (obj) : XINT (obj);
|
|
456 }
|
|
457
|
|
458 #define CHECK_CHAR_COERCE_INT(x) do { \
|
|
459 if (CHARP (x)) \
|
|
460 ; \
|
|
461 else if (CHAR_INTP (x)) \
|
|
462 x = make_char (XINT (x)); \
|
|
463 else \
|
|
464 x = wrong_type_argument (Qcharacterp, x); \
|
|
465 } while (0)
|
|
466
|
|
467
|
|
468
|
|
469 /************************************************************************/
|
|
470 /* */
|
|
471 /* working with Eistrings */
|
|
472 /* */
|
|
473 /************************************************************************/
|
|
474
|
|
475 /*
|
|
476 #### NOTE: This is a work in progress. Neither the API nor especially
|
|
477 the implementation is finished.
|
|
478
|
|
479 NOTE: An Eistring is a structure that makes it easy to work with
|
|
480 internally-formatted strings of data. It provides operations similar
|
|
481 in feel to the standard strcpy(), strcat(), strlen(), etc., but
|
|
482
|
|
483 (a) it is Mule-correct
|
|
484 (b) it does dynamic allocation so you never have to worry about size
|
|
485 restrictions (and all allocation is stack-local using alloca(), so
|
|
486 there is no need to explicitly clean up)
|
|
487 (c) it knows its own length, so it does not suffer from standard null
|
|
488 byte brain-damage
|
|
489 (d) it provides a much more powerful set of operations and knows about
|
|
490 all the standard places where string data might reside: Lisp_Objects,
|
|
491 other Eistrings, Intbyte * data with or without an explicit length,
|
|
492 ASCII strings, Emchars, etc.
|
|
493 (e) it provides easy operations to convert to/from externally-formatted
|
|
494 data, and is much easier to use than the standard TO_INTERNAL_FORMAT
|
|
495 and TO_EXTERNAL_FORMAT macros. (An Eistring can store both the internal
|
|
496 and external version of its data, but the external version is only
|
|
497 initialized or changed when you call eito_external().)
|
|
498
|
|
499 The idea is to make it as easy to write Mule-correct string
|
|
500 manipulation code as it is to write normal string manipulation
|
|
501 code. We also make the API sufficiently general that it can handle
|
|
502 multiple internal data formats (e.g. some fixed-width optimizing
|
|
503 formats and a default variable width format) and allows for *ANY*
|
|
504 data format we might choose in the future for the default format,
|
|
505 including UCS2. (In other words, we can't assume that the internal
|
|
506 format is ASCII-compatible and we can't assume it doesn't have
|
|
507 embedded null bytes. We do assume, however, that any chosen format
|
|
508 will have the concept of null-termination.) All of this is hidden
|
|
509 from the user.
|
|
510
|
|
511 #### It is really too bad that we don't have a real object-oriented
|
|
512 language, or at least a language with polymorphism!
|
|
513
|
|
514
|
|
515 **********************************************
|
|
516 * Declaration *
|
|
517 **********************************************
|
|
518
|
|
519 To declare an Eistring, either put one of the following in the local
|
|
520 variable section:
|
|
521
|
|
522 DECLARE_EISTRING (name);
|
|
523 Declare a new Eistring. This is a standard local variable declaration
|
|
524 and can go anywhere in the variable declaration section. NAME itself
|
|
525 is declared as an Eistring *, and its storage declared on the stack.
|
|
526
|
|
527 DECLARE_EISTRING_MALLOC (name);
|
|
528 Declare a new Eistring, which uses malloc()ed instead of alloca()ed
|
|
529 data. This is a standard local variable declaration and can go
|
|
530 anywhere in the variable declaration section. Once you initialize
|
|
531 the Eistring, you will have to free it using eifree() to avoid
|
|
532 memory leaks.
|
|
533
|
|
534 or use
|
|
535
|
|
536 Eistring name;
|
|
537 void eiinit (Eistring name);
|
|
538 void eiinit_malloc (Eistring name);
|
|
539 If you need to put an Eistring elsewhere than in a local variable
|
|
540 declaration (e.g. in a structure), declare it as shown and then
|
|
541 call one of the init macros.
|
|
542
|
|
543 Also note:
|
|
544
|
|
545 void eifree (Eistring ei);
|
|
546 If you declared an Eistring to use malloc() to hold its data,
|
|
547 or converted it to the heap using eito_malloc(), then this
|
|
548 releases any data in it and afterwards resets the Eistring
|
|
549 using eiinit_malloc(). Otherwise, it just resets the Eistring
|
|
550 using eiinit().
|
|
551
|
|
552
|
|
553 **********************************************
|
|
554 * Conventions *
|
|
555 **********************************************
|
|
556
|
|
557 - The names of the functions have been chosen, where possible, to
|
|
558 match the names of str*() functions in the standard C API.
|
|
559 -
|
|
560
|
|
561
|
|
562 **********************************************
|
|
563 * Initialization *
|
|
564 **********************************************
|
|
565
|
|
566 void eireset (Eistring *eistr);
|
|
567 Initialize the Eistring to the empty string.
|
|
568
|
|
569 void eicpy_* (Eistring *eistr, ...);
|
|
570 Initialize the Eistring from somewhere:
|
|
571
|
|
572 void eicpy_ei (Eistring *eistr, Eistring *eistr2);
|
|
573 ... from another Eistring.
|
|
574 void eicpy_lstr (Eistring *eistr, Lisp_Object lisp_string);
|
|
575 ... from a Lisp_Object string.
|
|
576 void eicpy_ch (Eistring *eistr, Emchar ch);
|
|
577 ... from an Emchar.
|
|
578
|
|
579 void eicpy_lstr_off (Eistring *eistr, Lisp_Object lisp_string,
|
|
580 Bytecount off, Charcount charoff,
|
|
581 Bytecount len, Charcount charlen);
|
|
582 ... from a section of a Lisp_Object string.
|
|
583 void eicpy_lbuf (Eistring *eistr, Lisp_Object lisp_buf,
|
|
584 Bytecount off, Charcount charoff,
|
|
585 Bytecount len, Charcount charlen);
|
|
586 ... from a section of a Lisp_Object buffer.
|
|
587 void eicpy_raw (Eistring *eistr, const Intbyte *data, Bytecount len);
|
|
588 ... from raw internal-format data in the default internal format.
|
|
589 void eicpy_rawz (Eistring *eistr, const Intbyte *data);
|
|
590 ... from raw internal-format data in the default internal format
|
|
591 that is "null-terminated" (the meaning of this depends on the nature
|
|
592 of the default internal format).
|
|
593 void eicpy_raw_fmt (Eistring *eistr, const Intbyte *data, Bytecount len,
|
|
594 Internal_Format intfmt);
|
|
595 ... from raw internal-format data in the specified format.
|
|
596 void eicpy_rawz_fmt (Eistring *eistr, const Intbyte *data,
|
|
597 Internal_Format intfmt);
|
|
598 ... from raw internal-format data in the specified format that is
|
|
599 "null-terminated" (the meaning of this depends on the nature of
|
|
600 the specific format).
|
|
601 void eicpy_c (Eistring *eistr, const Char_ASCII *c_string);
|
|
602 ... from an ASCII null-terminated string. Non-ASCII characters in
|
|
603 the string are *ILLEGAL* (read abort() with error-checking defined).
|
|
604 void eicpy_c_len (Eistring *eistr, const Char_ASCII *c_string, len);
|
|
605 ... from an ASCII string, with length specified. Non-ASCII characters
|
|
606 in the string are *ILLEGAL* (read abort() with error-checking defined).
|
|
607 void eicpy_ext (Eistring *eistr, const Extbyte *extdata,
|
|
608 Lisp_Object coding_system);
|
|
609 ... from external null-terminated data, with coding system specified.
|
|
610 void eicpy_ext_len (Eistring *eistr, const Extbyte *extdata,
|
|
611 Bytecount extlen, Lisp_Object coding_system);
|
|
612 ... from external data, with length and coding system specified.
|
|
613 void eicpy_lstream (Eistring *eistr, Lisp_Object lstream);
|
|
614 ... from an lstream; reads data till eof. Data must be in default
|
|
615 internal format; otherwise, interpose a decoding lstream.
|
|
616
|
|
617
|
|
618 **********************************************
|
|
619 * Getting the data out of the Eistring *
|
|
620 **********************************************
|
|
621
|
|
622 Intbyte *eidata (Eistring *eistr);
|
|
623 Return a pointer to the raw data in an Eistring. This is NOT
|
|
624 a copy.
|
|
625
|
|
626 Lisp_Object eimake_string (Eistring *eistr);
|
|
627 Make a Lisp string out of the Eistring.
|
|
628
|
|
629 Lisp_Object eimake_string_off (Eistring *eistr,
|
|
630 Bytecount off, Charcount charoff,
|
|
631 Bytecount len, Charcount charlen);
|
|
632 Make a Lisp string out of a section of the Eistring.
|
|
633
|
|
634 void eicpyout_alloca (Eistring *eistr, LVALUE: Intbyte *ptr_out,
|
|
635 LVALUE: Bytecount len_out);
|
|
636 Make an alloca() copy of the data in the Eistring, using the
|
|
637 default internal format. Due to the nature of alloca(), this
|
|
638 must be a macro, with all lvalues passed in as parameters.
|
|
639 A pointer to the alloca()ed data is stored in PTR_OUT, and
|
|
640 the length of the data (not including the terminating zero)
|
|
641 is stored in LEN_OUT.
|
|
642
|
|
643 void eicpyout_alloca_fmt (Eistring *eistr, LVALUE: Intbyte *ptr_out,
|
|
644 LVALUE: Bytecount len_out,
|
|
645 Internal_Format intfmt);
|
|
646 Like eicpyout_alloca(), but converts to the specified internal
|
|
647 format. (No formats other than FORMAT_DEFAULT are currently
|
|
648 implemented, and you get an assertion failure if you try.)
|
|
649
|
|
650 Intbyte *eicpyout_malloc (Eistring *eistr, Bytecount *intlen_out);
|
|
651 Make a malloc() copy of the data in the Eistring, using the
|
|
652 default internal format. This is a real function. No lvalues
|
|
653 passed in. Returns the new data, and stores the length (not
|
|
654 including the terminating zero) using INTLEN_OUT, unless it's
|
|
655 a NULL pointer.
|
|
656
|
|
657 Intbyte *eicpyout_malloc_fmt (Eistring *eistr, Internal_Format intfmt,
|
|
658 Bytecount *intlen_out);
|
|
659 Like eicpyout_malloc(), but converts to the specified internal
|
|
660 format. (No formats other than FORMAT_DEFAULT are currently
|
|
661 implemented, and you get an assertion failure if you try.)
|
|
662
|
|
663
|
|
664 **********************************************
|
|
665 * Moving to the heap *
|
|
666 **********************************************
|
|
667
|
|
668 void eito_malloc (Eistring *eistr);
|
|
669 Move this Eistring to the heap. Its data will be stored in a
|
|
670 malloc()ed block rather than the stack. Subsequent changes to
|
|
671 this Eistring will realloc() the block as necessary. Use this
|
|
672 when you want the Eistring to remain in scope past the end of
|
|
673 this function call. You will have to manually free the data
|
|
674 in the Eistring using eifree().
|
|
675
|
|
676 void eito_alloca (Eistring *eistr);
|
|
677 Move this Eistring back to the stack, if it was moved to the
|
|
678 heap with eito_malloc(). This will automatically free any
|
|
679 heap-allocated data.
|
|
680
|
|
681
|
|
682
|
|
683 **********************************************
|
|
684 * Retrieving the length *
|
|
685 **********************************************
|
|
686
|
|
687 Bytecount eilen (Eistring *eistr);
|
|
688 Return the length of the internal data, in bytes. See also
|
|
689 eiextlen(), below.
|
|
690 Charcount eicharlen (Eistring *eistr);
|
|
691 Return the length of the internal data, in characters.
|
|
692
|
|
693
|
|
694 **********************************************
|
|
695 * Working with positions *
|
|
696 **********************************************
|
|
697
|
|
698 Bytecount eicharpos_to_bytepos (Eistring *eistr, Charcount charpos);
|
|
699 Convert a char offset to a byte offset.
|
|
700 Charcount eibytepos_to_charpos (Eistring *eistr, Bytecount bytepos);
|
|
701 Convert a byte offset to a char offset.
|
|
702 Bytecount eiincpos (Eistring *eistr, Bytecount bytepos);
|
|
703 Increment the given position by one character.
|
|
704 Bytecount eiincpos_n (Eistring *eistr, Bytecount bytepos, Charcount n);
|
|
705 Increment the given position by N characters.
|
|
706 Bytecount eidecpos (Eistring *eistr, Bytecount bytepos);
|
|
707 Decrement the given position by one character.
|
|
708 Bytecount eidecpos_n (Eistring *eistr, Bytecount bytepos, Charcount n);
|
|
709 Deccrement the given position by N characters.
|
|
710
|
|
711
|
|
712 **********************************************
|
|
713 * Getting the character at a position *
|
|
714 **********************************************
|
|
715
|
|
716 Emchar eigetch (Eistring *eistr, Bytecount bytepos);
|
|
717 Return the character at a particular byte offset.
|
|
718 Emchar eigetch_char (Eistring *eistr, Charcount charpos);
|
|
719 Return the character at a particular character offset.
|
|
720
|
|
721
|
|
722 **********************************************
|
|
723 * Setting the character at a position *
|
|
724 **********************************************
|
|
725
|
|
726 Emchar eisetch (Eistring *eistr, Bytecount bytepos, Emchar chr);
|
|
727 Set the character at a particular byte offset.
|
|
728 Emchar eisetch_char (Eistring *eistr, Charcount charpos, Emchar chr);
|
|
729 Set the character at a particular character offset.
|
|
730
|
|
731
|
|
732 **********************************************
|
|
733 * Concatenation *
|
|
734 **********************************************
|
|
735
|
|
736 void eicat_* (Eistring *eistr, ...);
|
|
737 Concatenate onto the end of the Eistring, with data coming from the
|
|
738 same places as above:
|
|
739
|
|
740 void eicat_ei (Eistring *eistr, Eistring *eistr2);
|
|
741 ... from another Eistring.
|
|
742 void eicat_c (Eistring *eistr, Char_ASCII *c_string);
|
|
743 ... from an ASCII null-terminated string. Non-ASCII characters in
|
|
744 the string are *ILLEGAL* (read abort() with error-checking defined).
|
|
745 void eicat_raw (ei, const Intbyte *data, Bytecount len);
|
|
746 ... from raw internal-format data in the default internal format.
|
|
747 void eicat_rawz (ei, const Intbyte *data);
|
|
748 ... from raw internal-format data in the default internal format
|
|
749 that is "null-terminated" (the meaning of this depends on the nature
|
|
750 of the default internal format).
|
|
751 void eicat_lstr (ei, Lisp_Object lisp_string);
|
|
752 ... from a Lisp_Object string.
|
|
753 void eicat_ch (ei, Emchar ch);
|
|
754 ... from an Emchar.
|
|
755
|
|
756 (All except the first variety are convenience functions.
|
|
757 In the general case, create another Eistring from the source.)
|
|
758
|
|
759
|
|
760 **********************************************
|
|
761 * Replacement *
|
|
762 **********************************************
|
|
763
|
|
764 void eisub_* (Eistring *eistr, Bytecount off, Charcount charoff,
|
|
765 Bytecount len, Charcount charlen, ...);
|
|
766 Replace a section of the Eistring, specifically:
|
|
767
|
|
768 void eisub_ei (Eistring *eistr, Bytecount off, Charcount charoff,
|
|
769 Bytecount len, Charcount charlen, Eistring *eistr2);
|
|
770 ... with another Eistring.
|
|
771 void eisub_c (Eistring *eistr, Bytecount off, Charcount charoff,
|
|
772 Bytecount len, Charcount charlen, Char_ASCII *c_string);
|
|
773 ... with an ASCII null-terminated string. Non-ASCII characters in
|
|
774 the string are *ILLEGAL* (read abort() with error-checking defined).
|
|
775 void eisub_ch (Eistring *eistr, Bytecount off, Charcount charoff,
|
|
776 Bytecount len, Charcount charlen, Emchar ch);
|
|
777 ... with an Emchar.
|
|
778
|
|
779 void eidel (Eistring *eistr, Bytecount off, Charcount charoff,
|
|
780 Bytecount len, Charcount charlen);
|
|
781 Delete a section of the Eistring.
|
|
782
|
|
783
|
|
784 **********************************************
|
|
785 * Converting to an external format *
|
|
786 **********************************************
|
|
787
|
|
788 void eito_external (Eistring *eistr, Lisp_Object coding_system);
|
|
789 Convert the Eistring to an external format and store the result
|
|
790 in the string. NOTE: Further changes to the Eistring will *NOT*
|
|
791 change the external data stored in the string. You will have to
|
|
792 call eito_external() again in such a case if you want the external
|
|
793 data.
|
|
794
|
|
795 Extbyte *eiextdata (Eistring *eistr);
|
|
796 Return a pointer to the external data stored in the Eistring as
|
|
797 a result of a prior call to eito_external().
|
|
798
|
|
799 Bytecount eiextlen (Eistring *eistr);
|
|
800 Return the length in bytes of the external data stored in the
|
|
801 Eistring as a result of a prior call to eito_external().
|
|
802
|
|
803
|
|
804 **********************************************
|
|
805 * Searching in the Eistring for a character *
|
|
806 **********************************************
|
|
807
|
|
808 Bytecount eichr (Eistring *eistr, Emchar chr);
|
|
809 Charcount eichr_char (Eistring *eistr, Emchar chr);
|
|
810 Bytecount eichr_off (Eistring *eistr, Emchar chr, Bytecount off,
|
|
811 Charcount charoff);
|
|
812 Charcount eichr_off_char (Eistring *eistr, Emchar chr, Bytecount off,
|
|
813 Charcount charoff);
|
|
814 Bytecount eirchr (Eistring *eistr, Emchar chr);
|
|
815 Charcount eirchr_char (Eistring *eistr, Emchar chr);
|
|
816 Bytecount eirchr_off (Eistring *eistr, Emchar chr, Bytecount off,
|
|
817 Charcount charoff);
|
|
818 Charcount eirchr_off_char (Eistring *eistr, Emchar chr, Bytecount off,
|
|
819 Charcount charoff);
|
|
820
|
|
821
|
|
822 **********************************************
|
|
823 * Searching in the Eistring for a string *
|
|
824 **********************************************
|
|
825
|
|
826 Bytecount eistr_ei (Eistring *eistr, Eistring *eistr2);
|
|
827 Charcount eistr_ei_char (Eistring *eistr, Eistring *eistr2);
|
|
828 Bytecount eistr_ei_off (Eistring *eistr, Eistring *eistr2, Bytecount off,
|
|
829 Charcount charoff);
|
|
830 Charcount eistr_ei_off_char (Eistring *eistr, Eistring *eistr2,
|
|
831 Bytecount off, Charcount charoff);
|
|
832 Bytecount eirstr_ei (Eistring *eistr, Eistring *eistr2);
|
|
833 Charcount eirstr_ei_char (Eistring *eistr, Eistring *eistr2);
|
|
834 Bytecount eirstr_ei_off (Eistring *eistr, Eistring *eistr2, Bytecount off,
|
|
835 Charcount charoff);
|
|
836 Charcount eirstr_ei_off_char (Eistring *eistr, Eistring *eistr2,
|
|
837 Bytecount off, Charcount charoff);
|
|
838
|
|
839 Bytecount eistr_c (Eistring *eistr, Char_ASCII *c_string);
|
|
840 Charcount eistr_c_char (Eistring *eistr, Char_ASCII *c_string);
|
|
841 Bytecount eistr_c_off (Eistring *eistr, Char_ASCII *c_string, Bytecount off,
|
|
842 Charcount charoff);
|
|
843 Charcount eistr_c_off_char (Eistring *eistr, Char_ASCII *c_string,
|
|
844 Bytecount off, Charcount charoff);
|
|
845 Bytecount eirstr_c (Eistring *eistr, Char_ASCII *c_string);
|
|
846 Charcount eirstr_c_char (Eistring *eistr, Char_ASCII *c_string);
|
|
847 Bytecount eirstr_c_off (Eistring *eistr, Char_ASCII *c_string,
|
|
848 Bytecount off, Charcount charoff);
|
|
849 Charcount eirstr_c_off_char (Eistring *eistr, Char_ASCII *c_string,
|
|
850 Bytecount off, Charcount charoff);
|
|
851
|
|
852
|
|
853 **********************************************
|
|
854 * Comparison *
|
|
855 **********************************************
|
|
856
|
|
857 int eicmp_* (Eistring *eistr, ...);
|
|
858 int eicmp_off_* (Eistring *eistr, Bytecount off, Charcount charoff,
|
|
859 Bytecount len, Charcount charlen, ...);
|
|
860 int eicasecmp_* (Eistring *eistr, ...);
|
|
861 int eicasecmp_off_* (Eistring *eistr, Bytecount off, Charcount charoff,
|
|
862 Bytecount len, Charcount charlen, ...);
|
|
863 int eicasecmp_i18n_* (Eistring *eistr, ...);
|
|
864 int eicasecmp_i18n_off_* (Eistring *eistr, Bytecount off, Charcount charoff,
|
|
865 Bytecount len, Charcount charlen, ...);
|
|
866
|
|
867 Compare the Eistring with the other data. Return value same as
|
|
868 from strcmp. The `*' is either `ei' for another Eistring (in
|
|
869 which case `...' is an Eistring), or `c' for a pure-ASCII string
|
|
870 (in which case `...' is a pointer to that string). For anything
|
|
871 more complex, first create an Eistring out of the source.
|
|
872 Comparison is either simple (`eicmp_...'), ASCII case-folding
|
|
873 (`eicasecmp_...'), or multilingual case-folding
|
|
874 (`eicasecmp_i18n_...).
|
|
875
|
|
876
|
|
877 More specifically, the prototypes are:
|
|
878
|
|
879 int eicmp_ei (Eistring *eistr, Eistring *eistr2);
|
|
880 int eicmp_off_ei (Eistring *eistr, Bytecount off, Charcount charoff,
|
|
881 Bytecount len, Charcount charlen, Eistring *eistr2);
|
|
882 int eicasecmp_ei (Eistring *eistr, Eistring *eistr2);
|
|
883 int eicasecmp_off_ei (Eistring *eistr, Bytecount off, Charcount charoff,
|
|
884 Bytecount len, Charcount charlen, Eistring *eistr2);
|
|
885 int eicasecmp_i18n_ei (Eistring *eistr, Eistring *eistr2);
|
|
886 int eicasecmp_i18n_off_ei (Eistring *eistr, Bytecount off,
|
|
887 Charcount charoff, Bytecount len,
|
|
888 Charcount charlen, Eistring *eistr2);
|
|
889
|
|
890 int eicmp_c (Eistring *eistr, Char_ASCII *c_string);
|
|
891 int eicmp_off_c (Eistring *eistr, Bytecount off, Charcount charoff,
|
|
892 Bytecount len, Charcount charlen, Char_ASCII *c_string);
|
|
893 int eicasecmp_c (Eistring *eistr, Char_ASCII *c_string);
|
|
894 int eicasecmp_off_c (Eistring *eistr, Bytecount off, Charcount charoff,
|
|
895 Bytecount len, Charcount charlen,
|
|
896 Char_ASCII *c_string);
|
|
897 int eicasecmp_i18n_c (Eistring *eistr, Char_ASCII *c_string);
|
|
898 int eicasecmp_i18n_off_c (Eistring *eistr, Bytecount off, Charcount charoff,
|
|
899 Bytecount len, Charcount charlen,
|
|
900 Char_ASCII *c_string);
|
|
901
|
|
902
|
|
903 **********************************************
|
|
904 * Case-changing the Eistring *
|
|
905 **********************************************
|
|
906
|
|
907 void eilwr (Eistring *eistr);
|
|
908 Convert all characters in the Eistring to lowercase.
|
|
909 void eiupr (Eistring *eistr);
|
|
910 Convert all characters in the Eistring to uppercase.
|
|
911 */
|
|
912
|
|
913
|
|
914 /* Principles for writing Eistring functions:
|
|
915
|
|
916 (1) Unfortunately, we have to write most of the Eistring functions
|
|
917 as macros, because of the use of alloca(). The principle used
|
|
918 below to assure no conflict in local variables is to prefix all
|
|
919 local variables with "ei" plus a number, which should be unique
|
|
920 among macros. In practice, when finding a new number, find the
|
|
921 highest so far used, and add 1.
|
|
922
|
|
923 (2) We also suffix the Eistring fields with an _ to avoid problems
|
|
924 with macro parameters of the same name. (And as the standard
|
|
925 signal not to access these fields directly.)
|
|
926
|
|
927 (3) We maintain both the length in bytes and chars of the data in
|
|
928 the Eistring at all times, for convenient retrieval by outside
|
|
929 functions. That means when writing functions that manipulate
|
|
930 Eistrings, you too need to keep both lengths up to date for all
|
|
931 data that you work with.
|
|
932
|
|
933 (4) When writing a new type of operation (e.g. substitution), you
|
|
934 will often find yourself working with outside data, and thus
|
|
935 have a series of related API's, for different forms that the
|
|
936 outside data is in. Generally, you will want to choose a
|
|
937 subset of the forms supported by eicpy_*, which has to be
|
|
938 totally general because that's the fundamental way to get data
|
|
939 into an Eistring, and once the data is into the string, it
|
|
940 would be to create a whole series of Ei operations that work on
|
|
941 nothing but Eistrings. Although theoretically nice, in
|
|
942 practice it's a hassle, so we suggest that you provide
|
|
943 convenience functions. In particular, there are two paths you
|
|
944 can take. One is minimalist -- it only allows other Eistrings
|
|
945 and ASCII data, and Emchars if the particular operation makes
|
|
946 sense with a character. The other provides interfaces for the
|
|
947 most commonly-used forms -- Eistring, ASCII data, Lisp string,
|
|
948 raw internal-format string with length, raw internal-format
|
|
949 string without, and possibly Emchar. (In the function names,
|
|
950 these are designated `ei', `c', `lstr', `raw', `rawz', and
|
|
951 `ch', respectively.)
|
|
952
|
|
953 (5) When coding a new type of operation, such as was discussed in
|
|
954 previous section, the correct approach is to declare an worker
|
|
955 function that does the work of everything, and is called by the
|
|
956 other "container" macros that handle the different outside data
|
|
957 forms. The data coming into the worker function, which
|
|
958 typically ends in `_1', is in the form of three parameters:
|
|
959 DATA, LEN, CHARLEN. (See point [3] about having two lengths and
|
|
960 keeping them in sync.)
|
|
961
|
|
962 (6) Handling argument evaluation in macros: We take great care
|
|
963 never to evaluate any argument more than once in any macro,
|
|
964 except the initial Eistring parameter. This can and will be
|
|
965 evaluated multiple times, but it should pretty much always just
|
|
966 be a simple variable. This means, for example, that if an
|
|
967 Eistring is the second (not first) argument of a macro, it
|
|
968 doesn't fall under the "initial Eistring" exemption, so it
|
|
969 needs protection against multi-evaluation. (Take the address of
|
|
970 the Eistring structure, store in a temporary variable, and use
|
|
971 temporary variable for all access to the Eistring.
|
|
972 Essentially, we want it to appear as if these Eistring macros
|
|
973 are functions -- we would like to declare them as functions but
|
|
974 they use alloca(), so we can't (and we can't make them inline
|
|
975 functions either -- alloca() is explicitly disallowed in inline
|
|
976 functions.)
|
|
977
|
|
978 (7) Note that our rules regarding multiple evaluation are *more*
|
|
979 strict than the rules listed above under the heading "working
|
|
980 with raw internal-format data".
|
|
981 */
|
|
982
|
|
983
|
|
984 /* ----- Declaration ----- */
|
|
985
|
|
986 typedef struct
|
|
987 {
|
|
988 /* Data for the Eistring, stored in the default internal format.
|
|
989 Always includes terminating null. */
|
|
990 Intbyte *data_;
|
|
991 /* Total number of bytes allocated in DATA (including null). */
|
|
992 Bytecount max_size_allocated_;
|
|
993 Bytecount bytelen_;
|
|
994 Charcount charlen_;
|
|
995 int mallocp_;
|
|
996
|
|
997 Extbyte *extdata_;
|
|
998 Bytecount extlen_;
|
|
999 } Eistring;
|
|
1000
|
|
1001 typedef enum internal_format
|
|
1002 {
|
|
1003 FORMAT_DEFAULT,
|
|
1004 FORMAT_FIXED_8,
|
|
1005 FORMAT_FIXED_16,
|
|
1006 FORMAT_FIXED_32
|
|
1007 } Internal_Format;
|
|
1008
|
|
1009 extern Eistring the_eistring_zero_init, the_eistring_malloc_zero_init;
|
|
1010
|
|
1011 #define DECLARE_EISTRING(name) \
|
|
1012 Eistring __ ## name ## __storage__ = the_eistring_zero_init; \
|
|
1013 Eistring *name = & __ ## name ## __storage__
|
|
1014 #define DECLARE_EISTRING_MALLOC(name) \
|
|
1015 Eistring __ ## name ## __storage__ = the_eistring_malloc_zero_init; \
|
|
1016 Eistring *name = & __ ## name ## __storage__
|
|
1017
|
|
1018 #define eiinit(ei) \
|
|
1019 do { \
|
|
1020 (ei) = the_eistring_zero_init; \
|
|
1021 } while (0)
|
|
1022
|
|
1023 #define eiinit_malloc(ei) \
|
|
1024 do { \
|
|
1025 (ei) = the_eistring_malloc_zero_init; \
|
|
1026 } while (0)
|
|
1027
|
|
1028
|
|
1029 /* ----- Utility ----- */
|
|
1030
|
|
1031 /* Make sure both LEN and CHARLEN are specified, in case one is given
|
|
1032 as -1. PTR evaluated at most once, others multiply. */
|
|
1033 #define eifixup_bytechar(ptr, len, charlen) \
|
|
1034 do { \
|
|
1035 if ((len) == -1) \
|
|
1036 (len) = charcount_to_bytecount (ptr, charlen); \
|
|
1037 else if ((charlen) == -1) \
|
|
1038 (charlen) = bytecount_to_charcount (ptr, len); \
|
|
1039 } while (0)
|
|
1040
|
|
1041 /* Make sure LEN is specified, in case it's is given as -1. PTR
|
|
1042 evaluated at most once, others multiply. */
|
|
1043 #define eifixup_byte(ptr, len, charlen) \
|
|
1044 do { \
|
|
1045 if ((len) == -1) \
|
|
1046 (len) = charcount_to_bytecount (ptr, charlen); \
|
|
1047 } while (0)
|
|
1048
|
|
1049 /* Make sure CHARLEN is specified, in case it's is given as -1. PTR
|
|
1050 evaluated at most once, others multiply. */
|
|
1051 #define eifixup_char(ptr, len, charlen) \
|
|
1052 do { \
|
|
1053 if ((charlen) == -1) \
|
|
1054 (charlen) = bytecount_to_charcount (ptr, len); \
|
|
1055 } while (0)
|
|
1056
|
|
1057
|
|
1058
|
|
1059 /* Make sure we can hold NEWBYTELEN bytes (which is NEWCHARLEN chars)
|
|
1060 plus a zero terminator. Preserve existing data as much as possible,
|
|
1061 including existing zero terminator. Put a new zero terminator where it
|
|
1062 should go if NEWZ if non-zero. All args but EI are evalled only once. */
|
|
1063
|
|
1064 #define EI_ALLOC(ei, newbytelen, newcharlen, newz) \
|
|
1065 do { \
|
|
1066 int ei1oldeibytelen = (ei)->bytelen_; \
|
|
1067 \
|
|
1068 (ei)->charlen_ = (newcharlen); \
|
|
1069 (ei)->bytelen_ = (newbytelen); \
|
|
1070 \
|
|
1071 if (ei1oldeibytelen != (ei)->bytelen_) \
|
|
1072 { \
|
|
1073 int ei1newsize = (ei)->max_size_allocated_; \
|
|
1074 while (ei1newsize < (ei)->bytelen_ + 1) \
|
|
1075 { \
|
|
1076 ei1newsize = (int) (ei1newsize * 1.5); \
|
|
1077 if (ei1newsize < 32) \
|
|
1078 ei1newsize = 32; \
|
|
1079 } \
|
|
1080 if (ei1newsize != (ei)->max_size_allocated_) \
|
|
1081 { \
|
|
1082 if ((ei)->mallocp_) \
|
|
1083 /* xrealloc always preserves existing data as much as possible */ \
|
|
1084 (ei)->data_ = (Intbyte *) xrealloc ((ei)->data_, ei1newsize); \
|
|
1085 else \
|
|
1086 { \
|
|
1087 /* We don't have realloc, so alloca() more space and copy the \
|
|
1088 data into it. */ \
|
|
1089 Intbyte *ei1oldeidata = (ei)->data_; \
|
|
1090 (ei)->data_ = (Intbyte *) alloca (ei1newsize); \
|
|
1091 if (ei1oldeidata) \
|
|
1092 memcpy ((ei)->data_, ei1oldeidata, ei1oldeibytelen + 1); \
|
|
1093 } \
|
|
1094 (ei)->max_size_allocated_ = ei1newsize; \
|
|
1095 } \
|
|
1096 if (newz) \
|
|
1097 (ei)->data_[(ei)->bytelen_] = '\0'; \
|
|
1098 } \
|
|
1099 } while (0)
|
|
1100
|
|
1101 #define EI_ALLOC_AND_COPY(ei, data, bytelen, charlen) \
|
|
1102 do { \
|
|
1103 EI_ALLOC (ei, bytelen, charlen, 1); \
|
|
1104 memcpy ((ei)->data_, data, (ei)->bytelen_); \
|
|
1105 } while (0)
|
|
1106
|
|
1107 #ifdef ERROR_CHECK_CHARBPOS
|
|
1108 #define EI_ASSERT_ASCII(ptr, len) \
|
|
1109 do { \
|
|
1110 int ei5; \
|
|
1111 const Char_ASCII *ei5ptr = (ptr); \
|
|
1112 int ei5len = (len); \
|
|
1113 \
|
|
1114 for (ei5 = 0; ei5 < ei5len; ei5++) \
|
|
1115 assert (ei5ptr[ei5] >= 0x00 && ei5ptr[ei5] < 0x7F); \
|
|
1116 } while (0)
|
|
1117 #define EI_ASSERT_ASCIIZ(ptr) \
|
|
1118 do { \
|
|
1119 const Char_ASCII *ei5p1 = (ptr); \
|
|
1120 EI_ASSERT_ASCII (ei5p1, strlen (ei5p1)); \
|
|
1121 } while (0)
|
|
1122 #else
|
|
1123 #define EI_ASSERT_ASCII(ptr, len)
|
|
1124 #define EI_ASSERT_ASCIIZ(ptr)
|
|
1125 #endif
|
|
1126
|
|
1127
|
|
1128 /* ----- Initialization ----- */
|
|
1129
|
|
1130 #define eicpy_ei(ei, eicpy) \
|
|
1131 do { \
|
|
1132 const Eistring *ei2 = (eicpy); \
|
|
1133 EI_ALLOC_AND_COPY (ei, ei2->data_, ei2->bytelen_, ei2->charlen_); \
|
|
1134 } while (0)
|
|
1135
|
|
1136 #define eicpy_lstr(ei, lisp_string) \
|
|
1137 do { \
|
|
1138 Lisp_Object ei3 = (lisp_string); \
|
|
1139 EI_ALLOC_AND_COPY (ei, XSTRING_DATA (ei3), XSTRING_LENGTH (ei3), \
|
|
1140 XSTRING_CHAR_LENGTH (ei3)); \
|
|
1141 } while (0)
|
|
1142
|
|
1143 #define eicpy_lstr_off(ei, lisp_string, off, charoff, len, charlen) \
|
|
1144 do { \
|
|
1145 Lisp_Object ei23lstr = (lisp_string); \
|
|
1146 int ei23off = (off); \
|
|
1147 int ei23charoff = (charoff); \
|
|
1148 int ei23len = (len); \
|
|
1149 int ei23charlen = (charlen); \
|
|
1150 const Intbyte *ei23data = XSTRING_DATA (ei23lstr); \
|
|
1151 \
|
|
1152 int ei23oldbytelen = (ei)->bytelen_; \
|
|
1153 \
|
|
1154 eifixup_byte (ei23data, ei23off, ei23charoff); \
|
|
1155 eifixup_bytechar (ei23data + ei23off, ei23len, ei23charlen); \
|
|
1156 \
|
|
1157 EI_ALLOC_AND_COPY (ei, ei23data + ei23off, ei23len, ei23charlen); \
|
|
1158 } while (0)
|
|
1159
|
|
1160 #define eicpy_raw_fmt(ei, ptr, len, fmt) \
|
|
1161 do { \
|
|
1162 const Intbyte *ei12ptr = (ptr); \
|
|
1163 Internal_Format ei12fmt = (fmt); \
|
|
1164 int ei12len = (len); \
|
|
1165 assert (ei12fmt == FORMAT_DEFAULT); \
|
|
1166 EI_ALLOC_AND_COPY (ei, ei12ptr, ei12len, \
|
|
1167 bytecount_to_charcount (ei12ptr, ei12len)); \
|
|
1168 } while (0)
|
|
1169
|
|
1170 #define eicpy_raw(ei, ptr, len) eicpy_raw_fmt (ei, ptr, len, FORMAT_DEFAULT)
|
|
1171
|
|
1172 #define eicpy_rawz_fmt(ei, ptr, fmt) \
|
|
1173 do { \
|
|
1174 const Intbyte *ei12p1ptr = (ptr); \
|
|
1175 Internal_Format ei12p1fmt = (fmt); \
|
|
1176 assert (ei12p1fmt == FORMAT_DEFAULT); \
|
|
1177 eicpy_raw_fmt (ei, ei12p1ptr, qxestrlen (ei12p1ptr), fmt); \
|
|
1178 } while (0)
|
|
1179
|
|
1180 #define eicpy_rawz(ei, ptr) eicpy_rawz_fmt (ei, ptr, FORMAT_DEFAULT)
|
|
1181
|
|
1182 #define eicpy_ch(ei, ch) \
|
|
1183 do { \
|
|
1184 Intbyte ei12p2[MAX_EMCHAR_LEN]; \
|
|
1185 Bytecount ei12p2len = set_charptr_emchar (ei12p2, ch); \
|
|
1186 EI_ALLOC_AND_COPY (ei, ei12p2, ei12p2len, 1); \
|
|
1187 } while (0)
|
|
1188
|
|
1189 #define eicpy_c(ei, c_string) \
|
|
1190 do { \
|
|
1191 const Char_ASCII *ei4 = (c_string); \
|
|
1192 \
|
|
1193 EI_ASSERT_ASCIIZ (ei4); \
|
|
1194 eicpy_ext (ei, ei4, Qbinary); \
|
|
1195 } while (0)
|
|
1196
|
|
1197 #define eicpy_c_len(ei, c_string, c_len) \
|
|
1198 do { \
|
|
1199 const Char_ASCII *ei6 = (c_string); \
|
|
1200 int ei6len = (c_len); \
|
|
1201 \
|
|
1202 EI_ASSERT_ASCII (ei6, ei6len); \
|
|
1203 eicpy_ext_len (ei, ei6, ei6len, Qbinary); \
|
|
1204 } while (0)
|
|
1205
|
|
1206 #define eicpy_ext_len(ei, extdata, extlen, coding_system) \
|
|
1207 do { \
|
|
1208 const Extbyte *ei7 = (extdata); \
|
|
1209 int ei7len = (extlen); \
|
|
1210 \
|
|
1211 TO_INTERNAL_FORMAT (DATA, (ei7, ei7len), \
|
|
1212 ALLOCA, ((ei)->data_, (ei)->bytelen_), \
|
|
1213 coding_system); \
|
|
1214 (ei)->max_size_allocated_ = (ei)->bytelen_ + 1; \
|
|
1215 (ei)->charlen_ = bytecount_to_charcount ((ei)->data_, (ei)->bytelen_); \
|
|
1216 } while (0)
|
|
1217
|
|
1218 #define eicpy_ext(ei, extdata, coding_system) \
|
|
1219 do { \
|
|
1220 const Extbyte *ei8 = (extdata); \
|
|
1221 \
|
|
1222 eicpy_ext_len (ei, ei8, dfc_external_data_len (ei8, coding_system), \
|
|
1223 coding_system); \
|
|
1224 } while (0)
|
|
1225
|
|
1226 #define eicpy_lbuf(eistr, lisp_buf, off, charoff, len, charlen) \
|
|
1227 NOT YET IMPLEMENTED
|
|
1228
|
|
1229 #define eicpy_lstream(eistr, lstream) \
|
|
1230 NOT YET IMPLEMENTED
|
|
1231
|
|
1232 #define eireset(eistr) eicpy_rawz (eistr, (Intbyte *) "")
|
|
1233
|
|
1234 /* ----- Getting the data out of the Eistring ----- */
|
|
1235
|
|
1236 #define eidata(ei) ((ei)->data_)
|
|
1237
|
|
1238 #define eimake_string(ei) make_string (eidata (ei), eilen (ei))
|
|
1239
|
|
1240 #define eimake_string_off(eistr, off, charoff, len, charlen) \
|
|
1241 do { \
|
|
1242 Lisp_Object ei24lstr; \
|
|
1243 int ei24off = (off); \
|
|
1244 int ei24charoff = (charoff); \
|
|
1245 int ei24len = (len); \
|
|
1246 int ei24charlen = (charlen); \
|
|
1247 \
|
|
1248 eifixup_byte ((eistr)->data_, ei24off, ei24charoff); \
|
|
1249 eifixup_byte ((eistr)->data_ + ei24off, ei24len, ei24charlen); \
|
|
1250 \
|
|
1251 return make_string ((eistr)->data_ + ei24off, ei24len); \
|
|
1252 } while (0)
|
|
1253
|
|
1254 #define eicpyout_alloca(eistr, ptrout, lenout) \
|
|
1255 eicpyout_alloca_fmt (eistr, ptrout, lenout, FORMAT_DEFAULT)
|
|
1256 #define eicpyout_malloc(eistr, lenout) \
|
|
1257 eicpyout_malloc_fmt (eistr, lenout, FORMAT_DEFAULT)
|
|
1258 Intbyte *eicpyout_malloc_fmt (Eistring *eistr, Bytecount *len_out,
|
|
1259 Internal_Format fmt);
|
|
1260 #define eicpyout_alloca_fmt(eistr, ptrout, lenout, fmt) \
|
|
1261 do { \
|
|
1262 Internal_Format ei23fmt = (fmt); \
|
|
1263 Intbyte *ei23ptrout = &(ptrout); \
|
|
1264 Bytecount *ei23lenout = &(lenout); \
|
|
1265 \
|
|
1266 assert (ei23fmt == FORMAT_DEFAULT); \
|
|
1267 \
|
|
1268 *ei23lenout = (eistr)->bytelen_; \
|
|
1269 *ei23ptrout = alloca_array (Intbyte, (eistr)->bytelen_ + 1); \
|
|
1270 memcpy (*ei23ptrout, (eistr)->data_, (eistr)->bytelen_ + 1); \
|
|
1271 } while (0)
|
|
1272
|
|
1273
|
|
1274 /* ----- Moving to the heap ----- */
|
|
1275
|
|
1276 #define eifree(ei) \
|
|
1277 do { \
|
|
1278 if ((ei)->mallocp_) \
|
|
1279 { \
|
|
1280 if ((ei)->data_) \
|
|
1281 xfree ((ei)->data_); \
|
|
1282 if ((ei)->extdata_) \
|
|
1283 xfree ((ei)->extdata_); \
|
|
1284 eiinit_malloc (ei); \
|
|
1285 } \
|
|
1286 else \
|
|
1287 eiinit (ei); \
|
|
1288 } while (0)
|
|
1289
|
|
1290 int eifind_large_enough_buffer (int oldbufsize, int needed_size);
|
|
1291 void eito_malloc_1 (Eistring *ei);
|
|
1292
|
|
1293 #define eito_malloc(ei) eito_malloc_1 (ei)
|
|
1294
|
|
1295 #define eito_alloca(ei) \
|
|
1296 do { \
|
|
1297 if (!(ei)->mallocp_) \
|
|
1298 return; \
|
|
1299 (ei)->mallocp_ = 0; \
|
|
1300 if ((ei)->data_) \
|
|
1301 { \
|
|
1302 Intbyte *ei13newdata; \
|
|
1303 \
|
|
1304 (ei)->max_size_allocated_ = \
|
|
1305 eifind_large_enough_buffer (0, (ei)->bytelen_ + 1); \
|
|
1306 ei13newdata = (Intbyte *) alloca ((ei)->max_size_allocated_); \
|
|
1307 memcpy (ei13newdata, (ei)->data_, (ei)->bytelen_ + 1); \
|
|
1308 xfree ((ei)->data_); \
|
|
1309 (ei)->data_ = ei13newdata; \
|
|
1310 } \
|
|
1311 \
|
|
1312 if ((ei)->extdata_) \
|
|
1313 { \
|
|
1314 Extbyte *ei13newdata = (Extbyte *) alloca ((ei)->extlen_ + 2); \
|
|
1315 \
|
|
1316 memcpy (ei13newdata, (ei)->extdata_, (ei)->extlen_); \
|
|
1317 /* Double null-terminate in case of Unicode data */ \
|
|
1318 ei13newdata[(ei)->extlen_] = '\0'; \
|
|
1319 ei13newdata[(ei)->extlen_ + 1] = '\0'; \
|
|
1320 xfree ((ei)->extdata_); \
|
|
1321 (ei)->extdata_ = ei13newdata; \
|
|
1322 } \
|
|
1323 } while (0)
|
|
1324
|
|
1325
|
|
1326 /* ----- Retrieving the length ----- */
|
|
1327
|
|
1328 #define eilen(ei) ((ei)->bytelen_)
|
|
1329 #define eicharlen(ei) ((ei)->charlen_)
|
|
1330
|
|
1331
|
|
1332 /* ----- Working with positions ----- */
|
|
1333
|
|
1334 #define eicharpos_to_bytepos(ei, charpos) \
|
|
1335 charcount_to_bytecount ((ei)->data_, charpos)
|
|
1336 #define eibytepos_to_charpos(ei, bytepos) \
|
|
1337 bytecount_to_charcount ((ei)->data_, bytepos)
|
|
1338
|
|
1339 DECLARE_INLINE_HEADER (Bytecount eiincpos_1 (Eistring *eistr,
|
|
1340 Bytecount bytepos,
|
|
1341 Charcount n))
|
|
1342 {
|
|
1343 Intbyte *pos = eistr->data_ + bytepos;
|
|
1344 int i;
|
|
1345
|
|
1346 charbpos_checking_assert (bytepos >= 0 && bytepos <= eistr->bytelen_);
|
|
1347 charbpos_checking_assert (n >= 0 && n <= eistr->charlen_);
|
|
1348 /* We could check N more correctly now, but that would require a
|
|
1349 call to bytecount_to_charcount(), which would be needlessly
|
|
1350 expensive (it would convert O(N) algorithms into O(N^2) algorithms
|
|
1351 with ERROR_CHECK_CHARBPOS, which would be bad). If N is bad, we are
|
|
1352 guaranteed to catch it either inside INC_CHARPTR() or in the check
|
|
1353 below. */
|
|
1354 for (i = 0; i < n; i++)
|
|
1355 INC_CHARPTR (pos);
|
|
1356 charbpos_checking_assert (pos - eistr->data_ <= eistr->bytelen_);
|
|
1357 return pos - eistr->data_;
|
|
1358 }
|
|
1359
|
|
1360 #define eiincpos (ei, bytepos) eiincpos_1 (ei, bytepos, 1)
|
|
1361 #define eiincpos_n (ei, bytepos, n) eiincpos_1 (ei, bytepos, n)
|
|
1362
|
|
1363 DECLARE_INLINE_HEADER (Bytecount eidecpos_1 (Eistring *eistr,
|
|
1364 Bytecount bytepos,
|
|
1365 Charcount n))
|
|
1366 {
|
|
1367 Intbyte *pos = eistr->data_ + bytepos;
|
|
1368 int i;
|
|
1369
|
|
1370 charbpos_checking_assert (bytepos >= 0 && bytepos <= eistr->bytelen_);
|
|
1371 charbpos_checking_assert (n >= 0 && n <= eistr->charlen_);
|
|
1372 /* We could check N more correctly now, but ... see above. */
|
|
1373 for (i = 0; i < n; i++)
|
|
1374 DEC_CHARPTR (pos);
|
|
1375 charbpos_checking_assert (pos - eistr->data_ <= eistr->bytelen_);
|
|
1376 return pos - eistr->data_;
|
|
1377 }
|
|
1378
|
|
1379 #define eidecpos (ei, bytepos) eidecpos_1 (ei, bytepos, 1)
|
|
1380 #define eidecpos_n (ei, bytepos, n) eidecpos_1 (ei, bytepos, n)
|
|
1381
|
|
1382
|
|
1383 /* ----- Getting the character at a position ----- */
|
|
1384
|
|
1385 #define eigetch(ei, bytepos) \
|
|
1386 charptr_emchar ((ei)->data_ + (bytepos))
|
|
1387 #define eigetch_char(ei, charpos) charptr_emchar_n ((ei)->data_, charpos)
|
|
1388
|
|
1389
|
|
1390 /* ----- Setting the character at a position ----- */
|
|
1391
|
|
1392 #define eisetch(ei, bytepos, chr) \
|
|
1393 eisub_ch (ei, bytepos, -1, -1, 1, chr)
|
|
1394 #define eisetch_char(ei, charpos, chr) \
|
|
1395 eisub_ch (ei, -1, charpos, -1, 1, chr)
|
|
1396
|
|
1397
|
|
1398 /* ----- Concatenation ----- */
|
|
1399
|
|
1400 #define eicat_1(ei, data, bytelen, charlen) \
|
|
1401 do { \
|
|
1402 int ei14oldeibytelen = (ei)->bytelen_; \
|
|
1403 int ei14bytelen = (bytelen); \
|
|
1404 EI_ALLOC (ei, (ei)->bytelen_ + ei14bytelen, \
|
|
1405 (ei)->charlen_ + (charlen), 1); \
|
|
1406 memcpy ((ei)->data_ + ei14oldeibytelen, (data), \
|
|
1407 ei14bytelen); \
|
|
1408 } while (0)
|
|
1409
|
|
1410 #define eicat_ei(ei, ei2) \
|
|
1411 do { \
|
|
1412 const Eistring *ei9 = (ei2); \
|
|
1413 eicat_1 (ei, ei9->data_, ei9->bytelen_, ei9->charlen_); \
|
|
1414 } while (0)
|
|
1415
|
|
1416 #define eicat_c(ei, c_string) \
|
|
1417 do { \
|
|
1418 const Char_ASCII *ei15 = (c_string); \
|
|
1419 int ei15len = strlen (ei15); \
|
|
1420 \
|
|
1421 EI_ASSERT_ASCII (ei15, ei15len); \
|
|
1422 eicat_1 (ei, ei15, ei15len, \
|
|
1423 bytecount_to_charcount ((Intbyte *) ei15, ei15len)); \
|
|
1424 } while (0)
|
|
1425
|
|
1426 #define eicat_raw(ei, data, len) \
|
|
1427 do { \
|
|
1428 int ei16len = (len); \
|
|
1429 const Intbyte *ei16data = (data); \
|
|
1430 eicat_1 (ei, ei16data, ei16len, \
|
|
1431 bytecount_to_charcount (ei16data, ei16len)); \
|
|
1432 } while (0)
|
|
1433
|
|
1434 #define eicat_rawz(ei, ptr) \
|
|
1435 do { \
|
|
1436 const Intbyte *ei16p5ptr = (ptr); \
|
|
1437 eicat_raw (ei, ei16p5ptr, qxestrlen (ei16p5ptr)); \
|
|
1438 } while (0)
|
|
1439
|
|
1440 #define eicat_lstr(ei, lisp_string) \
|
|
1441 do { \
|
|
1442 Lisp_Object ei17 = (lisp_string); \
|
|
1443 eicat_1 (ei, XSTRING_DATA (ei17), XSTRING_LENGTH (ei17), \
|
|
1444 XSTRING_CHAR_LENGTH (ei17)); \
|
|
1445 } while (0)
|
|
1446
|
|
1447 #define eicat_ch(ei, ch) \
|
|
1448 do { \
|
|
1449 Intbyte ei22ch[MAX_EMCHAR_LEN]; \
|
|
1450 Bytecount ei22len = set_charptr_emchar (ei22ch, ch); \
|
|
1451 eicat_1 (ei, ei22ch, ei22len, 1); \
|
|
1452 } while (0)
|
|
1453
|
|
1454
|
|
1455 /* ----- Replacement ----- */
|
|
1456
|
|
1457 /* Replace the section of an Eistring at (OFF, LEN) with the data at
|
|
1458 SRC of length LEN. All positions have corresponding character values,
|
|
1459 and either can be -1 -- it will be computed from the other. */
|
|
1460
|
|
1461 #define eisub_1(ei, off, charoff, len, charlen, src, srclen, srccharlen) \
|
|
1462 do { \
|
|
1463 int ei18off = (off); \
|
|
1464 int ei18charoff = (charoff); \
|
|
1465 int ei18len = (len); \
|
|
1466 int ei18charlen = (charlen); \
|
|
1467 Intbyte *ei18src = (Intbyte *) (src); \
|
|
1468 int ei18srclen = (srclen); \
|
|
1469 int ei18srccharlen = (srccharlen); \
|
|
1470 \
|
|
1471 int ei18oldeibytelen = (ei)->bytelen_; \
|
|
1472 \
|
|
1473 eifixup_bytechar ((ei)->data_, ei18off, ei18charoff); \
|
|
1474 eifixup_bytechar ((ei)->data_ + ei18off, ei18len, ei18charlen); \
|
|
1475 eifixup_bytechar (ei18src, ei18srclen, ei18srccharlen); \
|
|
1476 \
|
|
1477 EI_ALLOC (ei, (ei)->bytelen_ + ei18srclen - ei18len, \
|
|
1478 (ei)->charlen_ + ei18srccharlen - ei18charlen, 0); \
|
|
1479 if (ei18len != ei18srclen) \
|
|
1480 memmove ((ei)->data_ + ei18off + ei18srclen, \
|
|
1481 (ei)->data_ + ei18off + ei18len, \
|
|
1482 /* include zero terminator. */ \
|
|
1483 ei18oldeibytelen - (ei18off + ei18len) + 1); \
|
|
1484 if (ei18srclen > 0) \
|
|
1485 memcpy ((ei)->data_ + ei18off, ei18src, ei18srclen); \
|
|
1486 } while (0)
|
|
1487
|
|
1488 #define eisub_ei(ei, off, charoff, len, charlen, ei2) \
|
|
1489 do { \
|
|
1490 const Eistring *ei19 = (ei2); \
|
|
1491 eisub_1 (ei, off, charoff, len, charlen, ei19->data_, ei19->bytelen_, \
|
|
1492 ei19->charlen_); \
|
|
1493 } while (0)
|
|
1494
|
|
1495 #define eisub_c(ei, off, charoff, len, charlen, c_string) \
|
|
1496 do { \
|
|
1497 const Char_ASCII *ei20 = (c_string); \
|
|
1498 int ei20len = strlen (ei20); \
|
|
1499 EI_ASSERT_ASCII (ei20, ei20len); \
|
|
1500 eisub_1 (ei, off, charoff, len, charlen, ei20, ei20len, -1); \
|
|
1501 } while (0)
|
|
1502
|
|
1503 #define eisub_ch(ei, off, charoff, len, charlen, ch) \
|
|
1504 do { \
|
|
1505 Intbyte ei21ch[MAX_EMCHAR_LEN]; \
|
|
1506 Bytecount ei21len = set_charptr_emchar (ei21ch, ch); \
|
|
1507 eisub_1 (ei, off, charoff, len, charlen, ei21ch, ei21len, 1); \
|
|
1508 } while (0)
|
|
1509
|
|
1510 #define eidel(ei, off, charoff, len, charlen) \
|
|
1511 eisub_1(ei, off, charoff, len, charlen, NULL, 0, 0)
|
|
1512
|
|
1513
|
|
1514 /* ----- Converting to an external format ----- */
|
|
1515
|
|
1516 #define eito_external(ei, coding_system) \
|
|
1517 do { \
|
|
1518 if ((ei)->mallocp_) \
|
|
1519 { \
|
|
1520 if ((ei)->extdata_) \
|
|
1521 { \
|
|
1522 xfree ((ei)->extdata_); \
|
|
1523 (ei)->extdata_ = 0; \
|
|
1524 } \
|
|
1525 TO_EXTERNAL_FORMAT (DATA, ((ei)->data_, (ei)->bytelen_), \
|
|
1526 MALLOC, ((ei)->extdata_, (ei)->extlen_), \
|
|
1527 coding_system); \
|
|
1528 } \
|
|
1529 else \
|
|
1530 TO_EXTERNAL_FORMAT (DATA, ((ei)->data_, (ei)->bytelen_), \
|
|
1531 ALLOCA, ((ei)->extdata_, (ei)->extlen_), \
|
|
1532 coding_system); \
|
|
1533 } while (0)
|
|
1534
|
|
1535 #define eiextdata(ei) ((ei)->extdata_)
|
|
1536 #define eiextlen(ei) ((ei)->extlen_)
|
|
1537
|
|
1538
|
|
1539 /* ----- Searching in the Eistring for a character ----- */
|
|
1540
|
|
1541 #define eichr(eistr, chr) \
|
|
1542 NOT YET IMPLEMENTED
|
|
1543 #define eichr_char(eistr, chr) \
|
|
1544 NOT YET IMPLEMENTED
|
|
1545 #define eichr_off(eistr, chr, off, charoff) \
|
|
1546 NOT YET IMPLEMENTED
|
|
1547 #define eichr_off_char(eistr, chr, off, charoff) \
|
|
1548 NOT YET IMPLEMENTED
|
|
1549 #define eirchr(eistr, chr) \
|
|
1550 NOT YET IMPLEMENTED
|
|
1551 #define eirchr_char(eistr, chr) \
|
|
1552 NOT YET IMPLEMENTED
|
|
1553 #define eirchr_off(eistr, chr, off, charoff) \
|
|
1554 NOT YET IMPLEMENTED
|
|
1555 #define eirchr_off_char(eistr, chr, off, charoff) \
|
|
1556 NOT YET IMPLEMENTED
|
|
1557
|
|
1558
|
|
1559 /* ----- Searching in the Eistring for a string ----- */
|
|
1560
|
|
1561 #define eistr_ei(eistr, eistr2) \
|
|
1562 NOT YET IMPLEMENTED
|
|
1563 #define eistr_ei_char(eistr, eistr2) \
|
|
1564 NOT YET IMPLEMENTED
|
|
1565 #define eistr_ei_off(eistr, eistr2, off, charoff) \
|
|
1566 NOT YET IMPLEMENTED
|
|
1567 #define eistr_ei_off_char(eistr, eistr2, off, charoff) \
|
|
1568 NOT YET IMPLEMENTED
|
|
1569 #define eirstr_ei(eistr, eistr2) \
|
|
1570 NOT YET IMPLEMENTED
|
|
1571 #define eirstr_ei_char(eistr, eistr2) \
|
|
1572 NOT YET IMPLEMENTED
|
|
1573 #define eirstr_ei_off(eistr, eistr2, off, charoff) \
|
|
1574 NOT YET IMPLEMENTED
|
|
1575 #define eirstr_ei_off_char(eistr, eistr2, off, charoff) \
|
|
1576 NOT YET IMPLEMENTED
|
|
1577
|
|
1578 #define eistr_c(eistr, c_string) \
|
|
1579 NOT YET IMPLEMENTED
|
|
1580 #define eistr_c_char(eistr, c_string) \
|
|
1581 NOT YET IMPLEMENTED
|
|
1582 #define eistr_c_off(eistr, c_string, off, charoff) \
|
|
1583 NOT YET IMPLEMENTED
|
|
1584 #define eistr_c_off_char(eistr, c_string, off, charoff) \
|
|
1585 NOT YET IMPLEMENTED
|
|
1586 #define eirstr_c(eistr, c_string) \
|
|
1587 NOT YET IMPLEMENTED
|
|
1588 #define eirstr_c_char(eistr, c_string) \
|
|
1589 NOT YET IMPLEMENTED
|
|
1590 #define eirstr_c_off(eistr, c_string, off, charoff) \
|
|
1591 NOT YET IMPLEMENTED
|
|
1592 #define eirstr_c_off_char(eistr, c_string, off, charoff) \
|
|
1593 NOT YET IMPLEMENTED
|
|
1594
|
|
1595
|
|
1596 /* ----- Comparison ----- */
|
|
1597
|
|
1598 int eicmp_1 (Eistring *ei, Bytecount off, Charcount charoff,
|
|
1599 Bytecount len, Charcount charlen, const Intbyte *data,
|
|
1600 const Eistring *ei2, int is_c, int fold_case);
|
|
1601
|
|
1602 #define eicmp_ei(eistr, eistr2) \
|
|
1603 eicmp_1 (eistr, 0, -1, -1, -1, 0, eistr2, 0, 0)
|
|
1604 #define eicmp_off_ei(eistr, off, charoff, len, charlen, eistr2) \
|
|
1605 eicmp_1 (eistr, off, charoff, len, charlen, 0, eistr2, 0, 0)
|
|
1606 #define eicasecmp_ei(eistr, eistr2) \
|
|
1607 eicmp_1 (eistr, 0, -1, -1, -1, 0, eistr2, 0, 1)
|
|
1608 #define eicasecmp_off_ei(eistr, off, charoff, len, charlen, eistr2) \
|
|
1609 eicmp_1 (eistr, off, charoff, len, charlen, 0, eistr2, 0, 1)
|
|
1610 #define eicasecmp_i18n_ei(eistr, eistr2) \
|
|
1611 eicmp_1 (eistr, 0, -1, -1, -1, 0, eistr2, 0, 2)
|
|
1612 #define eicasecmp_i18n_off_ei(eistr, off, charoff, len, charlen, eistr2) \
|
|
1613 eicmp_1 (eistr, off, charoff, len, charlen, 0, eistr2, 0, 2)
|
|
1614
|
|
1615 #define eicmp_c(eistr, c_string) \
|
|
1616 eicmp_1 (eistr, 0, -1, -1, -1, c_string, 0, 1, 0)
|
|
1617 #define eicmp_off_c(eistr, off, charoff, len, charlen, c_string) \
|
|
1618 eicmp_1 (eistr, off, charoff, len, charlen, c_string, 0, 1, 0)
|
|
1619 #define eicasecmp_c(eistr, c_string) \
|
|
1620 eicmp_1 (eistr, 0, -1, -1, -1, c_string, 0, 1, 1)
|
|
1621 #define eicasecmp_off_c(eistr, off, charoff, len, charlen, c_string) \
|
|
1622 eicmp_1 (eistr, off, charoff, len, charlen, c_string, 0, 1, 1)
|
|
1623 #define eicasecmp_i18n_c(eistr, c_string) \
|
|
1624 eicmp_1 (eistr, 0, -1, -1, -1, c_string, 0, 1, 2)
|
|
1625 #define eicasecmp_i18n_off_c(eistr, off, charoff, len, charlen, c_string) \
|
|
1626 eicmp_1 (eistr, off, charoff, len, charlen, c_string, 0, 1, 2)
|
|
1627
|
|
1628
|
|
1629 /* ----- Case-changing the Eistring ----- */
|
|
1630
|
|
1631 int eistr_casefiddle_1 (Intbyte *olddata, Bytecount len, Intbyte *newdata,
|
|
1632 int downp);
|
|
1633
|
|
1634 #define EI_CASECHANGE(ei, downp) \
|
|
1635 do { \
|
|
1636 int ei11new_allocmax = (ei)->charlen_ * MAX_EMCHAR_LEN + 1; \
|
|
1637 Intbyte *ei11storage = (Intbyte *) alloca_array (Intbyte, \
|
|
1638 ei11new_allocmax); \
|
|
1639 int ei11newlen = eistr_casefiddle_1 ((ei)->data_, (ei)->bytelen_, \
|
|
1640 ei11storage, downp); \
|
|
1641 \
|
|
1642 if (ei11newlen) \
|
|
1643 { \
|
|
1644 (ei)->max_size_allocated_ = ei11new_allocmax; \
|
|
1645 (ei)->data_ = ei11storage; \
|
|
1646 (ei)->bytelen_ = ei11newlen; \
|
|
1647 /* charlen is the same. */ \
|
|
1648 } \
|
|
1649 } while (0)
|
|
1650
|
|
1651 #define eilwr(ei) EI_CASECHANGE (ei, 1)
|
|
1652 #define eiupr(ei) EI_CASECHANGE (ei, 0)
|
|
1653
|
|
1654
|
|
1655 /************************************************************************/
|
|
1656 /* */
|
|
1657 /* Converting between internal and external format */
|
|
1658 /* */
|
|
1659 /************************************************************************/
|
|
1660 /*
|
|
1661 All client code should use only the two macros
|
|
1662
|
|
1663 TO_EXTERNAL_FORMAT (source_type, source, sink_type, sink, coding_system)
|
|
1664 TO_INTERNAL_FORMAT (source_type, source, sink_type, sink, coding_system)
|
|
1665
|
|
1666 Typical use is
|
|
1667
|
|
1668 TO_EXTERNAL_FORMAT (DATA, (ptr, len),
|
|
1669 LISP_BUFFER, buffer,
|
|
1670 Qfile_name);
|
|
1671
|
|
1672 NOTE: GC is inhibited during the entire operation of these macros. This
|
|
1673 is because frequently the data to be converted comes from strings but
|
|
1674 gets passed in as just DATA, and GC may move around the string data. If
|
|
1675 we didn't inhibit GC, there'd have to be a lot of messy recoding,
|
|
1676 alloca-copying of strings and other annoying stuff.
|
|
1677
|
|
1678 The source or sink can be specified in one of these ways:
|
|
1679
|
|
1680 DATA, (ptr, len), // input data is a fixed buffer of size len
|
|
1681 ALLOCA, (ptr, len), // output data is in a alloca()ed buffer of size len
|
|
1682 MALLOC, (ptr, len), // output data is in a malloc()ed buffer of size len
|
|
1683 C_STRING_ALLOCA, ptr, // equivalent to ALLOCA (ptr, len_ignored) on output
|
|
1684 C_STRING_MALLOC, ptr, // equivalent to MALLOC (ptr, len_ignored) on output
|
|
1685 C_STRING, ptr, // equivalent to DATA, (ptr, strlen/wcslen (ptr))
|
|
1686 // on input (the Unicode version is used when correct)
|
|
1687 LISP_STRING, string, // input or output is a Lisp_Object of type string
|
|
1688 LISP_BUFFER, buffer, // output is written to (point) in lisp buffer
|
|
1689 LISP_LSTREAM, lstream, // input or output is a Lisp_Object of type lstream
|
|
1690 LISP_OPAQUE, object, // input or output is a Lisp_Object of type opaque
|
|
1691
|
|
1692 When specifying the sink, use lvalues, since the macro will assign to them,
|
|
1693 except when the sink is an lstream or a lisp buffer.
|
|
1694
|
|
1695 The macros accept the kinds of sources and sinks appropriate for
|
|
1696 internal and external data representation. See the type_checking_assert
|
|
1697 macros below for the actual allowed types.
|
|
1698
|
|
1699 Since some sources and sinks use one argument (a Lisp_Object) to
|
|
1700 specify them, while others take a (pointer, length) pair, we use
|
|
1701 some C preprocessor trickery to allow pair arguments to be specified
|
|
1702 by parenthesizing them, as in the examples above.
|
|
1703
|
|
1704 Anything prefixed by dfc_ (`data format conversion') is private.
|
|
1705 They are only used to implement these macros.
|
|
1706
|
|
1707 [[Using C_STRING* is appropriate for using with external APIs that
|
|
1708 take null-terminated strings. For internal data, we should try to
|
|
1709 be '\0'-clean - i.e. allow arbitrary data to contain embedded '\0'.
|
|
1710
|
|
1711 Sometime in the future we might allow output to C_STRING_ALLOCA or
|
|
1712 C_STRING_MALLOC _only_ with TO_EXTERNAL_FORMAT(), not
|
|
1713 TO_INTERNAL_FORMAT().]]
|
|
1714
|
|
1715 The above comments are not true. Frequently (most of the time, in
|
|
1716 fact), external strings come as zero-terminated entities, where the
|
|
1717 zero-termination is the only way to find out the length. Even in
|
|
1718 cases where you can get the length, most of the time the system will
|
|
1719 still use the null to signal the end of the string, and there will
|
|
1720 still be no way to either send in or receive a string with embedded
|
|
1721 nulls. In such situations, it's pointless to track the length
|
|
1722 because null bytes can never be in the string. We have a lot of
|
|
1723 operations that make it easy to operate on zero-terminated strings,
|
|
1724 and forcing the user the deal with the length everywhere would only
|
|
1725 make the code uglier and more complicated, for no gain. --ben
|
|
1726
|
|
1727 There is no problem using the same lvalue for source and sink.
|
|
1728
|
|
1729 Also, when pointers are required, the code (currently at least) is
|
|
1730 lax and allows any pointer types, either in the source or the sink.
|
|
1731 This makes it possible, e.g., to deal with internal format data held
|
|
1732 in char *'s or external format data held in WCHAR * (i.e. Unicode).
|
|
1733
|
|
1734 Finally, whenever storage allocation is called for, extra space is
|
|
1735 allocated for a terminating zero, and such a zero is stored in the
|
|
1736 appropriate place, regardless of whether the source data was
|
|
1737 specified using a length or was specified as zero-terminated. This
|
|
1738 allows you to freely pass the resulting data, no matter how
|
|
1739 obtained, to a routine that expects zero termination (modulo, of
|
|
1740 course, that any embedded zeros in the resulting text will cause
|
|
1741 truncation). In fact, currently two embedded zeros are allocated
|
|
1742 and stored after the data result. This is to allow for the
|
|
1743 possibility of storing a Unicode value on output, which needs the
|
|
1744 two zeros. Currently, however, the two zeros are stored regardless
|
|
1745 of whether the conversion is internal or external and regardless of
|
|
1746 whether the external coding system is in fact Unicode. This
|
|
1747 behavior may change in the future, and you cannot rely on this --
|
|
1748 the most you can rely on is that sink data in Unicode format will
|
|
1749 have two terminating nulls, which combine to form one Unicode null
|
|
1750 character. */
|
|
1751
|
|
1752 #define TO_EXTERNAL_FORMAT(source_type, source, sink_type, sink, codesys) \
|
|
1753 do { \
|
|
1754 dfc_conversion_type dfc_simplified_source_type; \
|
|
1755 dfc_conversion_type dfc_simplified_sink_type; \
|
|
1756 dfc_conversion_data dfc_source; \
|
|
1757 dfc_conversion_data dfc_sink; \
|
|
1758 Lisp_Object dfc_codesys = (codesys); \
|
|
1759 \
|
|
1760 type_checking_assert \
|
|
1761 ((DFC_TYPE_##source_type == DFC_TYPE_DATA || \
|
|
1762 DFC_TYPE_##source_type == DFC_TYPE_C_STRING || \
|
|
1763 DFC_TYPE_##source_type == DFC_TYPE_LISP_STRING || \
|
|
1764 DFC_TYPE_##source_type == DFC_TYPE_LISP_OPAQUE || \
|
|
1765 DFC_TYPE_##source_type == DFC_TYPE_LISP_LSTREAM) \
|
|
1766 && \
|
|
1767 (DFC_TYPE_##sink_type == DFC_TYPE_ALLOCA || \
|
|
1768 DFC_TYPE_##sink_type == DFC_TYPE_MALLOC || \
|
|
1769 DFC_TYPE_##sink_type == DFC_TYPE_C_STRING_ALLOCA || \
|
|
1770 DFC_TYPE_##sink_type == DFC_TYPE_C_STRING_MALLOC || \
|
|
1771 DFC_TYPE_##sink_type == DFC_TYPE_LISP_LSTREAM || \
|
|
1772 DFC_TYPE_##sink_type == DFC_TYPE_LISP_OPAQUE)); \
|
|
1773 \
|
|
1774 DFC_EXT_SOURCE_##source_type##_TO_ARGS (source, dfc_codesys); \
|
|
1775 DFC_SINK_##sink_type##_TO_ARGS (sink); \
|
|
1776 \
|
|
1777 dfc_convert_to_external_format (dfc_simplified_source_type, &dfc_source, \
|
|
1778 dfc_codesys, \
|
|
1779 dfc_simplified_sink_type, &dfc_sink); \
|
|
1780 \
|
|
1781 DFC_##sink_type##_USE_CONVERTED_DATA (sink); \
|
|
1782 } while (0)
|
|
1783
|
|
1784 #define TO_INTERNAL_FORMAT(source_type, source, sink_type, sink, codesys) \
|
|
1785 do { \
|
|
1786 dfc_conversion_type dfc_simplified_source_type; \
|
|
1787 dfc_conversion_type dfc_simplified_sink_type; \
|
|
1788 dfc_conversion_data dfc_source; \
|
|
1789 dfc_conversion_data dfc_sink; \
|
|
1790 Lisp_Object dfc_codesys = (codesys); \
|
|
1791 \
|
|
1792 type_checking_assert \
|
|
1793 ((DFC_TYPE_##source_type == DFC_TYPE_DATA || \
|
|
1794 DFC_TYPE_##source_type == DFC_TYPE_C_STRING || \
|
|
1795 DFC_TYPE_##source_type == DFC_TYPE_LISP_OPAQUE || \
|
|
1796 DFC_TYPE_##source_type == DFC_TYPE_LISP_LSTREAM) \
|
|
1797 && \
|
|
1798 (DFC_TYPE_##sink_type == DFC_TYPE_ALLOCA || \
|
|
1799 DFC_TYPE_##sink_type == DFC_TYPE_MALLOC || \
|
|
1800 DFC_TYPE_##sink_type == DFC_TYPE_C_STRING_ALLOCA || \
|
|
1801 DFC_TYPE_##sink_type == DFC_TYPE_C_STRING_MALLOC || \
|
|
1802 DFC_TYPE_##sink_type == DFC_TYPE_LISP_STRING || \
|
|
1803 DFC_TYPE_##sink_type == DFC_TYPE_LISP_LSTREAM || \
|
|
1804 DFC_TYPE_##sink_type == DFC_TYPE_LISP_BUFFER)); \
|
|
1805 \
|
|
1806 DFC_INT_SOURCE_##source_type##_TO_ARGS (source, dfc_codesys); \
|
|
1807 DFC_SINK_##sink_type##_TO_ARGS (sink); \
|
|
1808 \
|
|
1809 dfc_convert_to_internal_format (dfc_simplified_source_type, &dfc_source, \
|
|
1810 dfc_codesys, \
|
|
1811 dfc_simplified_sink_type, &dfc_sink); \
|
|
1812 \
|
|
1813 DFC_##sink_type##_USE_CONVERTED_DATA (sink); \
|
|
1814 } while (0)
|
|
1815
|
|
1816
|
|
1817 typedef union
|
|
1818 {
|
|
1819 struct { const void *ptr; Bytecount len; } data;
|
|
1820 Lisp_Object lisp_object;
|
|
1821 } dfc_conversion_data;
|
|
1822
|
|
1823 enum dfc_conversion_type
|
|
1824 {
|
|
1825 DFC_TYPE_DATA,
|
|
1826 DFC_TYPE_ALLOCA,
|
|
1827 DFC_TYPE_MALLOC,
|
|
1828 DFC_TYPE_C_STRING,
|
|
1829 DFC_TYPE_C_STRING_ALLOCA,
|
|
1830 DFC_TYPE_C_STRING_MALLOC,
|
|
1831 DFC_TYPE_LISP_STRING,
|
|
1832 DFC_TYPE_LISP_LSTREAM,
|
|
1833 DFC_TYPE_LISP_OPAQUE,
|
|
1834 DFC_TYPE_LISP_BUFFER
|
|
1835 };
|
|
1836 typedef enum dfc_conversion_type dfc_conversion_type;
|
|
1837
|
|
1838 /* WARNING: These use a static buffer. This can lead to disaster if
|
|
1839 these functions are not used *very* carefully. Another reason to only use
|
|
1840 TO_EXTERNAL_FORMAT() and TO_INTERNAL_FORMAT(). */
|
|
1841 void
|
|
1842 dfc_convert_to_external_format (dfc_conversion_type source_type,
|
|
1843 dfc_conversion_data *source,
|
|
1844 Lisp_Object coding_system,
|
|
1845 dfc_conversion_type sink_type,
|
|
1846 dfc_conversion_data *sink);
|
|
1847 void
|
|
1848 dfc_convert_to_internal_format (dfc_conversion_type source_type,
|
|
1849 dfc_conversion_data *source,
|
|
1850 Lisp_Object coding_system,
|
|
1851 dfc_conversion_type sink_type,
|
|
1852 dfc_conversion_data *sink);
|
|
1853 /* CPP Trickery */
|
|
1854 #define DFC_CPP_CAR(x,y) (x)
|
|
1855 #define DFC_CPP_CDR(x,y) (y)
|
|
1856
|
|
1857 /* Convert `source' to args for dfc_convert_to_external_format() */
|
|
1858 #define DFC_EXT_SOURCE_DATA_TO_ARGS(val, codesys) do { \
|
|
1859 dfc_source.data.ptr = DFC_CPP_CAR val; \
|
|
1860 dfc_source.data.len = DFC_CPP_CDR val; \
|
|
1861 dfc_simplified_source_type = DFC_TYPE_DATA; \
|
|
1862 } while (0)
|
|
1863 #define DFC_EXT_SOURCE_C_STRING_TO_ARGS(val, codesys) do { \
|
|
1864 dfc_source.data.len = \
|
|
1865 strlen ((char *) (dfc_source.data.ptr = (val))); \
|
|
1866 dfc_simplified_source_type = DFC_TYPE_DATA; \
|
|
1867 } while (0)
|
|
1868 #define DFC_EXT_SOURCE_LISP_STRING_TO_ARGS(val, codesys) do { \
|
|
1869 Lisp_Object dfc_slsta = (val); \
|
|
1870 type_checking_assert (STRINGP (dfc_slsta)); \
|
|
1871 dfc_source.lisp_object = dfc_slsta; \
|
|
1872 dfc_simplified_source_type = DFC_TYPE_LISP_STRING; \
|
|
1873 } while (0)
|
|
1874 #define DFC_EXT_SOURCE_LISP_LSTREAM_TO_ARGS(val, codesys) do { \
|
|
1875 Lisp_Object dfc_sllta = (val); \
|
|
1876 type_checking_assert (LSTREAMP (dfc_sllta)); \
|
|
1877 dfc_source.lisp_object = dfc_sllta; \
|
|
1878 dfc_simplified_source_type = DFC_TYPE_LISP_LSTREAM; \
|
|
1879 } while (0)
|
|
1880 #define DFC_EXT_SOURCE_LISP_OPAQUE_TO_ARGS(val, codesys) do { \
|
|
1881 Lisp_Opaque *dfc_slota = XOPAQUE (val); \
|
|
1882 dfc_source.data.ptr = OPAQUE_DATA (dfc_slota); \
|
|
1883 dfc_source.data.len = OPAQUE_SIZE (dfc_slota); \
|
|
1884 dfc_simplified_source_type = DFC_TYPE_DATA; \
|
|
1885 } while (0)
|
|
1886
|
|
1887 /* Convert `source' to args for dfc_convert_to_internal_format() */
|
|
1888 #define DFC_INT_SOURCE_DATA_TO_ARGS(val, codesys) \
|
|
1889 DFC_EXT_SOURCE_DATA_TO_ARGS (val, codesys)
|
|
1890 #define DFC_INT_SOURCE_C_STRING_TO_ARGS(val, codesys) do { \
|
|
1891 dfc_source.data.len = dfc_external_data_len (dfc_source.data.ptr = (val), \
|
|
1892 codesys); \
|
|
1893 dfc_simplified_source_type = DFC_TYPE_DATA; \
|
|
1894 } while (0)
|
|
1895 #define DFC_INT_SOURCE_LISP_STRING_TO_ARGS(val, codesys) \
|
|
1896 DFC_EXT_SOURCE_LISP_STRING_TO_ARGS (val, codesys)
|
|
1897 #define DFC_INT_SOURCE_LISP_LSTREAM_TO_ARGS(val, codesys) \
|
|
1898 DFC_EXT_SOURCE_LISP_LSTREAM_TO_ARGS (val, codesys)
|
|
1899 #define DFC_INT_SOURCE_LISP_OPAQUE_TO_ARGS(val, codesys) \
|
|
1900 DFC_EXT_SOURCE_LISP_OPAQUE_TO_ARGS (val, codesys)
|
|
1901
|
|
1902 /* Convert `sink' to args for dfc_convert_to_*_format() */
|
|
1903 #define DFC_SINK_ALLOCA_TO_ARGS(val) \
|
|
1904 dfc_simplified_sink_type = DFC_TYPE_DATA
|
|
1905 #define DFC_SINK_C_STRING_ALLOCA_TO_ARGS(val) \
|
|
1906 dfc_simplified_sink_type = DFC_TYPE_DATA
|
|
1907 #define DFC_SINK_MALLOC_TO_ARGS(val) \
|
|
1908 dfc_simplified_sink_type = DFC_TYPE_DATA
|
|
1909 #define DFC_SINK_C_STRING_MALLOC_TO_ARGS(val) \
|
|
1910 dfc_simplified_sink_type = DFC_TYPE_DATA
|
|
1911 #define DFC_SINK_LISP_STRING_TO_ARGS(val) \
|
|
1912 dfc_simplified_sink_type = DFC_TYPE_DATA
|
|
1913 #define DFC_SINK_LISP_OPAQUE_TO_ARGS(val) \
|
|
1914 dfc_simplified_sink_type = DFC_TYPE_DATA
|
|
1915 #define DFC_SINK_LISP_LSTREAM_TO_ARGS(val) do { \
|
|
1916 Lisp_Object dfc_sllta = (val); \
|
|
1917 type_checking_assert (LSTREAMP (dfc_sllta)); \
|
|
1918 dfc_sink.lisp_object = dfc_sllta; \
|
|
1919 dfc_simplified_sink_type = DFC_TYPE_LISP_LSTREAM; \
|
|
1920 } while (0)
|
|
1921 #define DFC_SINK_LISP_BUFFER_TO_ARGS(val) do { \
|
|
1922 struct buffer *dfc_slbta = XBUFFER (val); \
|
|
1923 dfc_sink.lisp_object = \
|
|
1924 make_lisp_buffer_output_stream \
|
|
1925 (dfc_slbta, BUF_PT (dfc_slbta), 0); \
|
|
1926 dfc_simplified_sink_type = DFC_TYPE_LISP_LSTREAM; \
|
|
1927 } while (0)
|
|
1928
|
|
1929 /* Assign to the `sink' lvalue(s) using the converted data. */
|
|
1930 /* + 2 because we double zero-extended to account for Unicode conversion */
|
|
1931 typedef union { char c; void *p; } *dfc_aliasing_voidpp;
|
|
1932 #define DFC_ALLOCA_USE_CONVERTED_DATA(sink) do { \
|
|
1933 void * dfc_sink_ret = alloca (dfc_sink.data.len + 2); \
|
|
1934 memcpy (dfc_sink_ret, dfc_sink.data.ptr, dfc_sink.data.len + 2); \
|
|
1935 ((dfc_aliasing_voidpp) &(DFC_CPP_CAR sink))->p = dfc_sink_ret; \
|
|
1936 (DFC_CPP_CDR sink) = dfc_sink.data.len; \
|
|
1937 } while (0)
|
|
1938 #define DFC_MALLOC_USE_CONVERTED_DATA(sink) do { \
|
|
1939 void * dfc_sink_ret = xmalloc (dfc_sink.data.len + 2); \
|
|
1940 memcpy (dfc_sink_ret, dfc_sink.data.ptr, dfc_sink.data.len + 2); \
|
|
1941 ((dfc_aliasing_voidpp) &(DFC_CPP_CAR sink))->p = dfc_sink_ret; \
|
|
1942 (DFC_CPP_CDR sink) = dfc_sink.data.len; \
|
|
1943 } while (0)
|
|
1944 #define DFC_C_STRING_ALLOCA_USE_CONVERTED_DATA(sink) do { \
|
|
1945 void * dfc_sink_ret = alloca (dfc_sink.data.len + 2); \
|
|
1946 memcpy (dfc_sink_ret, dfc_sink.data.ptr, dfc_sink.data.len + 2); \
|
|
1947 ((dfc_aliasing_voidpp) &(sink))->p = dfc_sink_ret; \
|
|
1948 } while (0)
|
|
1949 #define DFC_C_STRING_MALLOC_USE_CONVERTED_DATA(sink) do { \
|
|
1950 void * dfc_sink_ret = xmalloc (dfc_sink.data.len + 2); \
|
|
1951 memcpy (dfc_sink_ret, dfc_sink.data.ptr, dfc_sink.data.len + 2); \
|
|
1952 ((dfc_aliasing_voidpp) &(sink))->p = dfc_sink_ret; \
|
|
1953 } while (0)
|
|
1954 #define DFC_LISP_STRING_USE_CONVERTED_DATA(sink) \
|
|
1955 sink = make_string ((Intbyte *) dfc_sink.data.ptr, dfc_sink.data.len)
|
|
1956 #define DFC_LISP_OPAQUE_USE_CONVERTED_DATA(sink) \
|
|
1957 sink = make_opaque (dfc_sink.data.ptr, dfc_sink.data.len)
|
|
1958 #define DFC_LISP_LSTREAM_USE_CONVERTED_DATA(sink) /* data already used */
|
|
1959 #define DFC_LISP_BUFFER_USE_CONVERTED_DATA(sink) \
|
|
1960 Lstream_delete (XLSTREAM (dfc_sink.lisp_object))
|
|
1961
|
|
1962 /* Convenience macros for extremely common invocations */
|
|
1963 #define C_STRING_TO_EXTERNAL(in, out, coding_system) \
|
|
1964 TO_EXTERNAL_FORMAT (C_STRING, in, C_STRING_ALLOCA, out, coding_system)
|
|
1965 #define C_STRING_TO_EXTERNAL_MALLOC(in, out, coding_system) \
|
|
1966 TO_EXTERNAL_FORMAT (C_STRING, in, C_STRING_MALLOC, out, coding_system)
|
|
1967 #define EXTERNAL_TO_C_STRING(in, out, coding_system) \
|
|
1968 TO_INTERNAL_FORMAT (C_STRING, in, C_STRING_ALLOCA, out, coding_system)
|
|
1969 #define EXTERNAL_TO_C_STRING_MALLOC(in, out, coding_system) \
|
|
1970 TO_INTERNAL_FORMAT (C_STRING, in, C_STRING_MALLOC, out, coding_system)
|
|
1971 #define LISP_STRING_TO_EXTERNAL(in, out, coding_system) \
|
|
1972 TO_EXTERNAL_FORMAT (LISP_STRING, in, C_STRING_ALLOCA, out, coding_system)
|
|
1973 #define LISP_STRING_TO_EXTERNAL_MALLOC(in, out, coding_system) \
|
|
1974 TO_EXTERNAL_FORMAT (LISP_STRING, in, C_STRING_MALLOC, out, coding_system)
|
|
1975
|
|
1976 /* Standins for various encodings, until we know them better */
|
|
1977 #define Qcommand_argument_encoding Qnative
|
|
1978 #define Qenvironment_variable_encoding Qnative
|
|
1979 #define Qunix_host_name_encoding Qnative
|
|
1980 #define Qunix_service_name_encoding Qnative
|
|
1981 #define Qmswindows_host_name_encoding Qmswindows_multibyte
|
|
1982 #define Qmswindows_service_name_encoding Qmswindows_multibyte
|
|
1983
|
|
1984 /* Standins for various X encodings, until we know them better */
|
|
1985
|
|
1986 /* !!#### Need to verify the encoding used in lwlib -- Qnative or Qctext?
|
|
1987 Almost certainly the former. Use a standin for now. */
|
|
1988 #define Qlwlib_encoding Qnative
|
|
1989
|
|
1990 #define Qx_atom_name_encoding Qctext
|
|
1991 /* font names are often stored in atoms, so it gets sticky if we set this
|
|
1992 to something different from atom-name encoding */
|
|
1993 #define Qx_font_name_encoding Qctext
|
|
1994
|
|
1995 #define Qx_color_name_encoding Qctext
|
|
1996
|
|
1997 /* the following probably must agree with Qcommand_argument_encoding and
|
|
1998 Qenvironment_variable_encoding */
|
|
1999 #define Qx_display_name_encoding Qnative
|
|
2000
|
|
2001 #define Qstrerror_encoding Qnative
|
|
2002
|
|
2003 #define GET_STRERROR(var, num) \
|
|
2004 do { \
|
|
2005 int __gsnum__ = (num); \
|
|
2006 Extbyte * __gserr__ = strerror (__gsnum__); \
|
|
2007 \
|
|
2008 if (!__gserr__) \
|
|
2009 { \
|
|
2010 var = alloca_intbytes (99); \
|
|
2011 qxesprintf (var, "Unknown error %d", __gsnum__); \
|
|
2012 } \
|
|
2013 else \
|
|
2014 EXTERNAL_TO_C_STRING (__gserr__, var, Qstrerror_encoding); \
|
|
2015 } while (0)
|
|
2016
|
|
2017 /************************************************************************/
|
|
2018 /* Lisp string representation convenience functions */
|
|
2019 /************************************************************************/
|
|
2020
|
|
2021 /* Because the representation of internally formatted data is subject
|
|
2022 to change, it's bad style to do something like
|
|
2023
|
|
2024 strcmp (XSTRING_DATA (s), "foo")
|
|
2025
|
|
2026 Instead, use the portable:
|
|
2027
|
|
2028 intbyte_strcmp (XSTRING_DATA (s), "foo") or
|
|
2029 intbyte_memcmp (XSTRING_DATA (s), "foo", 3)
|
|
2030
|
|
2031 */
|
|
2032
|
|
2033 /* Like strcmp, except first arg points at internally formatted data,
|
|
2034 while the second points at a string of only ASCII chars. */
|
|
2035 DECLARE_INLINE_HEADER (
|
|
2036 int
|
|
2037 intbyte_strcmp (const Intbyte *bp, const char *ascii_string)
|
|
2038 )
|
|
2039 {
|
|
2040 #ifdef MULE
|
|
2041 while (1)
|
|
2042 {
|
|
2043 int diff;
|
|
2044 type_checking_assert (BYTE_ASCII_P (*ascii_string));
|
|
2045 if ((diff = charptr_emchar (bp) - *(Intbyte *) ascii_string) != 0)
|
|
2046 return diff;
|
|
2047 if (*ascii_string == '\0')
|
|
2048 return 0;
|
|
2049 ascii_string++;
|
|
2050 INC_CHARPTR (bp);
|
|
2051 }
|
|
2052 #else
|
|
2053 return strcmp ((char *)bp, ascii_string);
|
|
2054 #endif
|
|
2055 }
|
|
2056
|
|
2057 /* Like memcmp, except first arg points at internally formatted data,
|
|
2058 while the second points at a string of only ASCII chars. */
|
|
2059
|
|
2060 DECLARE_INLINE_HEADER (
|
|
2061 int
|
|
2062 intbyte_memcmp (const Intbyte *bp, const char *ascii_string, Bytecount len)
|
|
2063 )
|
|
2064 {
|
|
2065 #ifdef MULE
|
|
2066 while (len--)
|
|
2067 {
|
|
2068 int diff = charptr_emchar (bp) - *(Intbyte *) ascii_string;
|
|
2069 type_checking_assert (BYTE_ASCII_P (*ascii_string));
|
|
2070 if (diff != 0)
|
|
2071 return diff;
|
|
2072 ascii_string++;
|
|
2073 INC_CHARPTR (bp);
|
|
2074 }
|
|
2075 return 0;
|
|
2076 #else
|
|
2077 return memcmp (bp, ascii_string, len);
|
|
2078 #endif
|
|
2079 }
|
|
2080
|
|
2081 #endif /* INCLUDED_text_h_ */
|