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