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