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