771
|
1 /* Buffer manipulation primitives for XEmacs.
|
|
2 Copyright (C) 1995 Sun Microsystems, Inc.
|
|
3 Copyright (C) 1995, 1996, 2000, 2001, 2002 Ben Wing.
|
|
4 Copyright (C) 1999 Martin Buchholz.
|
|
5
|
|
6 This file is part of XEmacs.
|
|
7
|
|
8 XEmacs is free software; you can redistribute it and/or modify it
|
|
9 under the terms of the GNU General Public License as published by the
|
|
10 Free Software Foundation; either version 2, or (at your option) any
|
|
11 later version.
|
|
12
|
|
13 XEmacs is distributed in the hope that it will be useful, but WITHOUT
|
|
14 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
|
15 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
|
|
16 for more details.
|
|
17
|
|
18 You should have received a copy of the GNU General Public License
|
|
19 along with XEmacs; see the file COPYING. If not, write to
|
|
20 the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
|
|
21 Boston, MA 02111-1307, USA. */
|
|
22
|
|
23 /* Synched up with: Not in FSF. */
|
|
24
|
|
25 /* Authorship:
|
|
26 */
|
|
27
|
|
28 #include <config.h>
|
|
29 #include "lisp.h"
|
|
30
|
|
31 #include "buffer.h"
|
|
32 #include "charset.h"
|
|
33 #include "file-coding.h"
|
|
34 #include "lstream.h"
|
|
35
|
|
36
|
|
37 /************************************************************************/
|
|
38 /* long comments */
|
|
39 /************************************************************************/
|
|
40
|
|
41 /*
|
|
42 There are three possible ways to specify positions in a buffer. All
|
|
43 of these are one-based: the beginning of the buffer is position or
|
|
44 index 1, and 0 is not a valid position.
|
|
45
|
|
46 As a "buffer position" (typedef Charbpos):
|
|
47
|
|
48 This is an index specifying an offset in characters from the
|
|
49 beginning of the buffer. Note that buffer positions are
|
|
50 logically *between* characters, not on a character. The
|
|
51 difference between two buffer positions specifies the number of
|
|
52 characters between those positions. Buffer positions are the
|
|
53 only kind of position externally visible to the user.
|
|
54
|
|
55 As a "byte index" (typedef Bytebpos):
|
|
56
|
|
57 This is an index over the bytes used to represent the characters
|
|
58 in the buffer. If there is no Mule support, this is identical
|
|
59 to a buffer position, because each character is represented
|
|
60 using one byte. However, with Mule support, many characters
|
|
61 require two or more bytes for their representation, and so a
|
|
62 byte index may be greater than the corresponding buffer
|
|
63 position.
|
|
64
|
|
65 As a "memory index" (typedef Membpos):
|
|
66
|
|
67 This is the byte index adjusted for the gap. For positions
|
|
68 before the gap, this is identical to the byte index. For
|
|
69 positions after the gap, this is the byte index plus the gap
|
|
70 size. There are two possible memory indices for the gap
|
|
71 position; the memory index at the beginning of the gap should
|
|
72 always be used, except in code that deals with manipulating the
|
|
73 gap, where both indices may be seen. The address of the
|
|
74 character "at" (i.e. following) a particular position can be
|
|
75 obtained from the formula
|
|
76
|
|
77 buffer_start_address + memory_index(position) - 1
|
|
78
|
|
79 except in the case of characters at the gap position.
|
|
80
|
|
81 Other typedefs:
|
|
82 ===============
|
|
83
|
|
84 Emchar:
|
|
85 -------
|
|
86 This typedef represents a single Emacs character, which can be
|
|
87 ASCII, ISO-8859, or some extended character, as would typically
|
|
88 be used for Kanji. Note that the representation of a character
|
|
89 as an Emchar is *not* the same as the representation of that
|
|
90 same character in a string; thus, you cannot do the standard
|
|
91 C trick of passing a pointer to a character to a function that
|
|
92 expects a string.
|
|
93
|
|
94 An Emchar takes up 19 bits of representation and (for code
|
|
95 compatibility and such) is compatible with an int. This
|
|
96 representation is visible on the Lisp level. The important
|
|
97 characteristics of the Emchar representation are
|
|
98
|
|
99 -- values 0x00 - 0x7f represent ASCII.
|
|
100 -- values 0x80 - 0xff represent the right half of ISO-8859-1.
|
|
101 -- values 0x100 and up represent all other characters.
|
|
102
|
|
103 This means that Emchar values are upwardly compatible with
|
|
104 the standard 8-bit representation of ASCII/ISO-8859-1.
|
|
105
|
|
106 Intbyte:
|
|
107 --------
|
|
108 The data in a buffer or string is logically made up of Intbyte
|
|
109 objects, where a Intbyte takes up the same amount of space as a
|
|
110 char. (It is declared differently, though, to catch invalid
|
|
111 usages.) Strings stored using Intbytes are said to be in
|
|
112 "internal format". The important characteristics of internal
|
|
113 format are
|
|
114
|
|
115 -- ASCII characters are represented as a single Intbyte,
|
|
116 in the range 0 - 0x7f.
|
|
117 -- All other characters are represented as a Intbyte in
|
|
118 the range 0x80 - 0x9f followed by one or more Intbytes
|
|
119 in the range 0xa0 to 0xff.
|
|
120
|
|
121 This leads to a number of desirable properties:
|
|
122
|
|
123 -- Given the position of the beginning of a character,
|
|
124 you can find the beginning of the next or previous
|
|
125 character in constant time.
|
|
126 -- When searching for a substring or an ASCII character
|
|
127 within the string, you need merely use standard
|
|
128 searching routines.
|
|
129
|
|
130 array of char:
|
|
131 --------------
|
|
132 Strings that go in or out of Emacs are in "external format",
|
|
133 typedef'ed as an array of char or a char *. There is more
|
|
134 than one external format (JIS, EUC, etc.) but they all
|
|
135 have similar properties. They are modal encodings,
|
|
136 which is to say that the meaning of particular bytes is
|
|
137 not fixed but depends on what "mode" the string is currently
|
|
138 in (e.g. bytes in the range 0 - 0x7f might be
|
|
139 interpreted as ASCII, or as Hiragana, or as 2-byte Kanji,
|
|
140 depending on the current mode). The mode starts out in
|
|
141 ASCII/ISO-8859-1 and is switched using escape sequences --
|
|
142 for example, in the JIS encoding, 'ESC $ B' switches to a
|
|
143 mode where pairs of bytes in the range 0 - 0x7f
|
|
144 are interpreted as Kanji characters.
|
|
145
|
|
146 External-formatted data is generally desirable for passing
|
|
147 data between programs because it is upwardly compatible
|
|
148 with standard ASCII/ISO-8859-1 strings and may require
|
|
149 less space than internal encodings such as the one
|
|
150 described above. In addition, some encodings (e.g. JIS)
|
|
151 keep all characters (except the ESC used to switch modes)
|
|
152 in the printing ASCII range 0x20 - 0x7e, which results in
|
|
153 a much higher probability that the data will avoid being
|
|
154 garbled in transmission. Externally-formatted data is
|
|
155 generally not very convenient to work with, however, and
|
|
156 for this reason is usually converted to internal format
|
|
157 before any work is done on the string.
|
|
158
|
|
159 NOTE: filenames need to be in external format so that
|
|
160 ISO-8859-1 characters come out correctly.
|
|
161
|
|
162 Charcount:
|
|
163 ----------
|
|
164 This typedef represents a count of characters, such as
|
|
165 a character offset into a string or the number of
|
|
166 characters between two positions in a buffer. The
|
|
167 difference between two Charbpos's is a Charcount, and
|
|
168 character positions in a string are represented using
|
|
169 a Charcount.
|
|
170
|
|
171 Bytecount:
|
|
172 ----------
|
|
173 Similar to a Charcount but represents a count of bytes.
|
|
174 The difference between two Bytebpos's is a Bytecount.
|
|
175
|
|
176
|
|
177 Usage of the various representations:
|
|
178 =====================================
|
|
179
|
|
180 Memory indices are used in low-level functions in insdel.c and for
|
|
181 extent endpoints and marker positions. The reason for this is that
|
|
182 this way, the extents and markers don't need to be updated for most
|
|
183 insertions, which merely shrink the gap and don't move any
|
|
184 characters around in memory.
|
|
185
|
|
186 (The beginning-of-gap memory index simplifies insertions w.r.t.
|
|
187 markers, because text usually gets inserted after markers. For
|
|
188 extents, it is merely for consistency, because text can get
|
|
189 inserted either before or after an extent's endpoint depending on
|
|
190 the open/closedness of the endpoint.)
|
|
191
|
|
192 Byte indices are used in other code that needs to be fast,
|
|
193 such as the searching, redisplay, and extent-manipulation code.
|
|
194
|
|
195 Buffer positions are used in all other code. This is because this
|
|
196 representation is easiest to work with (especially since Lisp
|
|
197 code always uses buffer positions), necessitates the fewest
|
|
198 changes to existing code, and is the safest (e.g. if the text gets
|
|
199 shifted underneath a buffer position, it will still point to a
|
|
200 character; if text is shifted under a byte index, it might point
|
|
201 to the middle of a character, which would be bad).
|
|
202
|
|
203 Similarly, Charcounts are used in all code that deals with strings
|
|
204 except for code that needs to be fast, which used Bytecounts.
|
|
205
|
|
206 Strings are always passed around internally using internal format.
|
|
207 Conversions between external format are performed at the time
|
|
208 that the data goes in or out of Emacs.
|
|
209
|
|
210 Working with the various representations:
|
|
211 ========================================= */
|
|
212
|
|
213 /* We write things this way because it's very important the
|
|
214 MAX_BYTEBPOS_GAP_SIZE_3 is a multiple of 3. (As it happens,
|
|
215 65535 is a multiple of 3, but this may not always be the
|
|
216 case.) */
|
|
217
|
|
218
|
|
219 /*
|
|
220 1. Character Sets
|
|
221 =================
|
|
222
|
|
223 A character set (or "charset") is an ordered set of characters.
|
|
224 A particular character in a charset is indexed using one or
|
|
225 more "position codes", which are non-negative integers.
|
|
226 The number of position codes needed to identify a particular
|
|
227 character in a charset is called the "dimension" of the
|
|
228 charset. In XEmacs/Mule, all charsets have 1 or 2 dimensions,
|
|
229 and the size of all charsets (except for a few special cases)
|
|
230 is either 94, 96, 94 by 94, or 96 by 96. The range of
|
|
231 position codes used to index characters from any of these
|
|
232 types of character sets is as follows:
|
|
233
|
|
234 Charset type Position code 1 Position code 2
|
|
235 ------------------------------------------------------------
|
|
236 94 33 - 126 N/A
|
|
237 96 32 - 127 N/A
|
|
238 94x94 33 - 126 33 - 126
|
|
239 96x96 32 - 127 32 - 127
|
|
240
|
|
241 Note that in the above cases position codes do not start at
|
|
242 an expected value such as 0 or 1. The reason for this will
|
|
243 become clear later.
|
|
244
|
|
245 For example, Latin-1 is a 96-character charset, and JISX0208
|
|
246 (the Japanese national character set) is a 94x94-character
|
|
247 charset.
|
|
248
|
|
249 [Note that, although the ranges above define the *valid*
|
|
250 position codes for a charset, some of the slots in a particular
|
|
251 charset may in fact be empty. This is the case for JISX0208,
|
|
252 for example, where (e.g.) all the slots whose first
|
|
253 position code is in the range 118 - 127 are empty.]
|
|
254
|
|
255 There are three charsets that do not follow the above rules.
|
|
256 All of them have one dimension, and have ranges of position
|
|
257 codes as follows:
|
|
258
|
|
259 Charset name Position code 1
|
|
260 ------------------------------------
|
|
261 ASCII 0 - 127
|
|
262 Control-1 0 - 31
|
|
263 Composite 0 - some large number
|
|
264
|
|
265 (The upper bound of the position code for composite characters
|
|
266 has not yet been determined, but it will probably be at
|
|
267 least 16,383).
|
|
268
|
|
269 ASCII is the union of two subsidiary character sets:
|
|
270 Printing-ASCII (the printing ASCII character set,
|
|
271 consisting of position codes 33 - 126, like for a standard
|
|
272 94-character charset) and Control-ASCII (the non-printing
|
|
273 characters that would appear in a binary file with codes 0
|
|
274 - 32 and 127).
|
|
275
|
|
276 Control-1 contains the non-printing characters that would
|
|
277 appear in a binary file with codes 128 - 159.
|
|
278
|
|
279 Composite contains characters that are generated by
|
|
280 overstriking one or more characters from other charsets.
|
|
281
|
|
282 Note that some characters in ASCII, and all characters
|
|
283 in Control-1, are "control" (non-printing) characters.
|
|
284 These have no printed representation but instead control
|
|
285 some other function of the printing (e.g. TAB or 8 moves
|
|
286 the current character position to the next tab stop).
|
|
287 All other characters in all charsets are "graphic"
|
|
288 (printing) characters.
|
|
289
|
|
290 When a binary file is read in, the bytes in the file are
|
|
291 assigned to character sets as follows:
|
|
292
|
|
293 Bytes Character set Range
|
|
294 --------------------------------------------------
|
|
295 0 - 127 ASCII 0 - 127
|
|
296 128 - 159 Control-1 0 - 31
|
|
297 160 - 255 Latin-1 32 - 127
|
|
298
|
|
299 This is a bit ad-hoc but gets the job done.
|
|
300
|
|
301 2. Encodings
|
|
302 ============
|
|
303
|
|
304 An "encoding" is a way of numerically representing
|
|
305 characters from one or more character sets. If an encoding
|
|
306 only encompasses one character set, then the position codes
|
|
307 for the characters in that character set could be used
|
|
308 directly. This is not possible, however, if more than one
|
|
309 character set is to be used in the encoding.
|
|
310
|
|
311 For example, the conversion detailed above between bytes in
|
|
312 a binary file and characters is effectively an encoding
|
|
313 that encompasses the three character sets ASCII, Control-1,
|
|
314 and Latin-1 in a stream of 8-bit bytes.
|
|
315
|
|
316 Thus, an encoding can be viewed as a way of encoding
|
|
317 characters from a specified group of character sets using a
|
|
318 stream of bytes, each of which contains a fixed number of
|
|
319 bits (but not necessarily 8, as in the common usage of
|
|
320 "byte").
|
|
321
|
|
322 Here are descriptions of a couple of common
|
|
323 encodings:
|
|
324
|
|
325
|
|
326 A. Japanese EUC (Extended Unix Code)
|
|
327
|
|
328 This encompasses the character sets:
|
|
329 - Printing-ASCII,
|
|
330 - Katakana-JISX0201 (half-width katakana, the right half of JISX0201).
|
|
331 - Japanese-JISX0208
|
|
332 - Japanese-JISX0212
|
|
333 It uses 8-bit bytes.
|
|
334
|
|
335 Note that Printing-ASCII and Katakana-JISX0201 are 94-character
|
|
336 charsets, while Japanese-JISX0208 is a 94x94-character charset.
|
|
337
|
|
338 The encoding is as follows:
|
|
339
|
|
340 Character set Representation (PC == position-code)
|
|
341 ------------- --------------
|
|
342 Printing-ASCII PC1
|
|
343 Japanese-JISX0208 PC1 + 0x80 | PC2 + 0x80
|
|
344 Katakana-JISX0201 0x8E | PC1 + 0x80
|
|
345
|
|
346
|
|
347 B. JIS7
|
|
348
|
|
349 This encompasses the character sets:
|
|
350 - Printing-ASCII
|
|
351 - Latin-JISX0201 (the left half of JISX0201; this character set is
|
|
352 very similar to Printing-ASCII and is a 94-character charset)
|
|
353 - Japanese-JISX0208
|
|
354 - Katakana-JISX0201
|
|
355 It uses 7-bit bytes.
|
|
356
|
|
357 Unlike Japanese EUC, this is a "modal" encoding, which
|
|
358 means that there are multiple states that the encoding can
|
|
359 be in, which affect how the bytes are to be interpreted.
|
|
360 Special sequences of bytes (called "escape sequences")
|
|
361 are used to change states.
|
|
362
|
|
363 The encoding is as follows:
|
|
364
|
|
365 Character set Representation
|
|
366 ------------- --------------
|
|
367 Printing-ASCII PC1
|
|
368 Latin-JISX0201 PC1
|
|
369 Katakana-JISX0201 PC1
|
|
370 Japanese-JISX0208 PC1 | PC2
|
|
371
|
|
372 Escape sequence ASCII equivalent Meaning
|
|
373 --------------- ---------------- -------
|
|
374 0x1B 0x28 0x42 ESC ( B invoke Printing-ASCII
|
|
375 0x1B 0x28 0x4A ESC ( J invoke Latin-JISX0201
|
|
376 0x1B 0x28 0x49 ESC ( I invoke Katakana-JISX0201
|
|
377 0x1B 0x24 0x42 ESC $ B invoke Japanese-JISX0208
|
|
378
|
|
379 Initially, Printing-ASCII is invoked.
|
|
380
|
|
381 3. Internal Mule Encodings
|
|
382 ==========================
|
|
383
|
|
384 In XEmacs/Mule, each character set is assigned a unique number,
|
|
385 called a "leading byte". This is used in the encodings of a
|
|
386 character. Leading bytes are in the range 0x80 - 0xFF
|
|
387 (except for ASCII, which has a leading byte of 0), although
|
|
388 some leading bytes are reserved.
|
|
389
|
|
390 Charsets whose leading byte is in the range 0x80 - 0x9F are
|
|
391 called "official" and are used for built-in charsets.
|
|
392 Other charsets are called "private" and have leading bytes
|
|
393 in the range 0xA0 - 0xFF; these are user-defined charsets.
|
|
394
|
|
395 More specifically:
|
|
396
|
|
397 Character set Leading byte
|
|
398 ------------- ------------
|
|
399 ASCII 0 (0x7F in arrays indexed by leading byte)
|
|
400 Composite 0x8D
|
|
401 Dimension-1 Official 0x80 - 0x8C/0x8D
|
|
402 (0x8E is free)
|
|
403 Control 0x8F
|
|
404 Dimension-2 Official 0x90 - 0x99
|
|
405 (0x9A - 0x9D are free)
|
|
406 Dimension-1 Private Marker 0x9E
|
|
407 Dimension-2 Private Marker 0x9F
|
|
408 Dimension-1 Private 0xA0 - 0xEF
|
|
409 Dimension-2 Private 0xF0 - 0xFF
|
|
410
|
|
411 There are two internal encodings for characters in XEmacs/Mule.
|
|
412 One is called "string encoding" and is an 8-bit encoding that
|
|
413 is used for representing characters in a buffer or string.
|
|
414 It uses 1 to 4 bytes per character. The other is called
|
|
415 "character encoding" and is a 19-bit encoding that is used
|
|
416 for representing characters individually in a variable.
|
|
417
|
|
418 (In the following descriptions, we'll ignore composite
|
|
419 characters for the moment. We also give a general (structural)
|
|
420 overview first, followed later by the exact details.)
|
|
421
|
|
422 A. Internal String Encoding
|
|
423
|
|
424 ASCII characters are encoded using their position code directly.
|
|
425 Other characters are encoded using their leading byte followed
|
|
426 by their position code(s) with the high bit set. Characters
|
|
427 in private character sets have their leading byte prefixed with
|
|
428 a "leading byte prefix", which is either 0x9E or 0x9F. (No
|
|
429 character sets are ever assigned these leading bytes.) Specifically:
|
|
430
|
|
431 Character set Encoding (PC == position-code)
|
|
432 ------------- -------- (LB == leading-byte)
|
|
433 ASCII PC1 |
|
|
434 Control-1 LB | PC1 + 0xA0
|
|
435 Dimension-1 official LB | PC1 + 0x80
|
|
436 Dimension-1 private 0x9E | LB | PC1 + 0x80
|
|
437 Dimension-2 official LB | PC1 | PC2 + 0x80
|
|
438 Dimension-2 private 0x9F | LB | PC1 + 0x80 | PC2 + 0x80
|
|
439
|
|
440 The basic characteristic of this encoding is that the first byte
|
|
441 of all characters is in the range 0x00 - 0x9F, and the second and
|
|
442 following bytes of all characters is in the range 0xA0 - 0xFF.
|
|
443 This means that it is impossible to get out of sync, or more
|
|
444 specifically:
|
|
445
|
|
446 1. Given any byte position, the beginning of the character it is
|
|
447 within can be determined in constant time.
|
|
448 2. Given any byte position at the beginning of a character, the
|
|
449 beginning of the next character can be determined in constant
|
|
450 time.
|
|
451 3. Given any byte position at the beginning of a character, the
|
|
452 beginning of the previous character can be determined in constant
|
|
453 time.
|
|
454 4. Textual searches can simply treat encoded strings as if they
|
|
455 were encoded in a one-byte-per-character fashion rather than
|
|
456 the actual multi-byte encoding.
|
|
457
|
|
458 None of the standard non-modal encodings meet all of these
|
|
459 conditions. For example, EUC satisfies only (2) and (3), while
|
|
460 Shift-JIS and Big5 (not yet described) satisfy only (2). (All
|
|
461 non-modal encodings must satisfy (2), in order to be unambiguous.)
|
|
462
|
|
463 B. Internal Character Encoding
|
|
464
|
|
465 One 19-bit word represents a single character. The word is
|
|
466 separated into three fields:
|
|
467
|
|
468 Bit number: 18 17 16 15 14 13 12 11 10 09 08 07 06 05 04 03 02 01 00
|
|
469 <------------> <------------------> <------------------>
|
|
470 Field: 1 2 3
|
|
471
|
|
472 Note that fields 2 and 3 hold 7 bits each, while field 1 holds 5 bits.
|
|
473
|
|
474 Character set Field 1 Field 2 Field 3
|
|
475 ------------- ------- ------- -------
|
|
476 ASCII 0 0 PC1
|
|
477 range: (00 - 7F)
|
|
478 Control-1 0 1 PC1
|
|
479 range: (00 - 1F)
|
|
480 Dimension-1 official 0 LB - 0x7F PC1
|
|
481 range: (01 - 0D) (20 - 7F)
|
|
482 Dimension-1 private 0 LB - 0x80 PC1
|
|
483 range: (20 - 6F) (20 - 7F)
|
|
484 Dimension-2 official LB - 0x8F PC1 PC2
|
|
485 range: (01 - 0A) (20 - 7F) (20 - 7F)
|
|
486 Dimension-2 private LB - 0xE1 PC1 PC2
|
|
487 range: (0F - 1E) (20 - 7F) (20 - 7F)
|
|
488 Composite 0x1F ? ?
|
|
489
|
|
490 Note that character codes 0 - 255 are the same as the "binary encoding"
|
|
491 described above.
|
|
492 */
|
|
493
|
|
494 /*
|
|
495 About Unicode support:
|
|
496
|
|
497 Adding Unicode support is very desirable. Unicode will likely be a
|
|
498 very common representation in the future, and thus we should
|
|
499 represent Unicode characters using three bytes instead of four.
|
|
500 This means we need to find leading bytes for Unicode. Given that
|
|
501 there are 65,536 characters in Unicode and we can attach 96x96 =
|
|
502 9,216 characters per leading byte, we need eight leading bytes for
|
|
503 Unicode. We currently have four free (0x9A - 0x9D), and with a
|
|
504 little bit of rearranging we can get five: ASCII doesn't really
|
|
505 need to take up a leading byte. (We could just as well use 0x7F,
|
|
506 with a little change to the functions that assume that 0x80 is the
|
|
507 lowest leading byte.) This means we still need to dump three
|
|
508 leading bytes and move them into private space. The CNS charsets
|
|
509 are good candidates since they are rarely used, and
|
|
510 JAPANESE_JISX0208_1978 is becoming less and less used and could
|
|
511 also be dumped. */
|
|
512
|
|
513
|
|
514 /* Composite characters are characters constructed by overstriking two
|
|
515 or more regular characters.
|
|
516
|
|
517 1) The old Mule implementation involves storing composite characters
|
|
518 in a buffer as a tag followed by all of the actual characters
|
|
519 used to make up the composite character. I think this is a bad
|
|
520 idea; it greatly complicates code that wants to handle strings
|
|
521 one character at a time because it has to deal with the possibility
|
|
522 of great big ungainly characters. It's much more reasonable to
|
|
523 simply store an index into a table of composite characters.
|
|
524
|
|
525 2) The current implementation only allows for 16,384 separate
|
|
526 composite characters over the lifetime of the XEmacs process.
|
|
527 This could become a potential problem if the user
|
|
528 edited lots of different files that use composite characters.
|
|
529 Due to FSF bogosity, increasing the number of allowable
|
|
530 composite characters under Mule would decrease the number
|
|
531 of possible faces that can exist. Mule already has shrunk
|
|
532 this to 2048, and further shrinkage would become uncomfortable.
|
|
533 No such problems exist in XEmacs.
|
|
534
|
|
535 Composite characters could be represented as 0x8D C1 C2 C3,
|
|
536 where each C[1-3] is in the range 0xA0 - 0xFF. This allows
|
|
537 for slightly under 2^20 (one million) composite characters
|
|
538 over the XEmacs process lifetime, and you only need to
|
|
539 increase the size of a Mule character from 19 to 21 bits.
|
|
540 Or you could use 0x8D C1 C2 C3 C4, allowing for about
|
|
541 85 million (slightly over 2^26) composite characters. */
|
|
542
|
|
543
|
|
544 /************************************************************************/
|
|
545 /* declarations */
|
|
546 /************************************************************************/
|
|
547
|
|
548 Eistring the_eistring_zero_init, the_eistring_malloc_zero_init;
|
|
549
|
|
550 #define MAX_CHARBPOS_GAP_SIZE_3 (65535/3)
|
|
551 #define MAX_BYTEBPOS_GAP_SIZE_3 (3 * MAX_CHARBPOS_GAP_SIZE_3)
|
|
552
|
|
553 short three_to_one_table[1 + MAX_BYTEBPOS_GAP_SIZE_3];
|
|
554
|
|
555 #ifdef MULE
|
|
556
|
|
557 /* Table of number of bytes in the string representation of a character
|
|
558 indexed by the first byte of that representation.
|
|
559
|
|
560 rep_bytes_by_first_byte(c) is more efficient than the equivalent
|
|
561 canonical computation:
|
|
562
|
|
563 XCHARSET_REP_BYTES (CHARSET_BY_LEADING_BYTE (c)) */
|
|
564
|
|
565 const Bytecount rep_bytes_by_first_byte[0xA0] =
|
|
566 { /* 0x00 - 0x7f are for straight ASCII */
|
|
567 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
|
|
568 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
|
|
569 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
|
|
570 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
|
|
571 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
|
|
572 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
|
|
573 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
|
|
574 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
|
|
575 /* 0x80 - 0x8f are for Dimension-1 official charsets */
|
|
576 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
|
|
577 /* 0x90 - 0x9d are for Dimension-2 official charsets */
|
|
578 /* 0x9e is for Dimension-1 private charsets */
|
|
579 /* 0x9f is for Dimension-2 private charsets */
|
|
580 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 4
|
|
581 };
|
|
582
|
|
583 #ifdef ENABLE_COMPOSITE_CHARS
|
|
584
|
|
585 /* Hash tables for composite chars. One maps string representing
|
|
586 composed chars to their equivalent chars; one goes the
|
|
587 other way. */
|
|
588 Lisp_Object Vcomposite_char_char2string_hash_table;
|
|
589 Lisp_Object Vcomposite_char_string2char_hash_table;
|
|
590
|
|
591 static int composite_char_row_next;
|
|
592 static int composite_char_col_next;
|
|
593
|
|
594 #endif /* ENABLE_COMPOSITE_CHARS */
|
|
595
|
|
596 #endif /* MULE */
|
|
597
|
|
598
|
|
599 /************************************************************************/
|
|
600 /* qxestr***() functions */
|
|
601 /************************************************************************/
|
|
602
|
|
603 /* Most are inline functions in lisp.h */
|
|
604
|
|
605 int
|
|
606 qxesprintf (Intbyte *buffer, const CIntbyte *format, ...)
|
|
607 {
|
|
608 va_list args;
|
|
609 int retval;
|
|
610
|
|
611 va_start (args, format);
|
|
612 retval = vsprintf ((char *) buffer, format, args);
|
|
613 va_end (args);
|
|
614
|
|
615 return retval;
|
|
616 }
|
|
617
|
|
618 /* strcasecmp() implementation from BSD */
|
|
619 static Intbyte strcasecmp_charmap[] = {
|
|
620 '\000', '\001', '\002', '\003', '\004', '\005', '\006', '\007',
|
|
621 '\010', '\011', '\012', '\013', '\014', '\015', '\016', '\017',
|
|
622 '\020', '\021', '\022', '\023', '\024', '\025', '\026', '\027',
|
|
623 '\030', '\031', '\032', '\033', '\034', '\035', '\036', '\037',
|
|
624 '\040', '\041', '\042', '\043', '\044', '\045', '\046', '\047',
|
|
625 '\050', '\051', '\052', '\053', '\054', '\055', '\056', '\057',
|
|
626 '\060', '\061', '\062', '\063', '\064', '\065', '\066', '\067',
|
|
627 '\070', '\071', '\072', '\073', '\074', '\075', '\076', '\077',
|
|
628 '\100', '\141', '\142', '\143', '\144', '\145', '\146', '\147',
|
|
629 '\150', '\151', '\152', '\153', '\154', '\155', '\156', '\157',
|
|
630 '\160', '\161', '\162', '\163', '\164', '\165', '\166', '\167',
|
|
631 '\170', '\171', '\172', '\133', '\134', '\135', '\136', '\137',
|
|
632 '\140', '\141', '\142', '\143', '\144', '\145', '\146', '\147',
|
|
633 '\150', '\151', '\152', '\153', '\154', '\155', '\156', '\157',
|
|
634 '\160', '\161', '\162', '\163', '\164', '\165', '\166', '\167',
|
|
635 '\170', '\171', '\172', '\173', '\174', '\175', '\176', '\177',
|
|
636 '\200', '\201', '\202', '\203', '\204', '\205', '\206', '\207',
|
|
637 '\210', '\211', '\212', '\213', '\214', '\215', '\216', '\217',
|
|
638 '\220', '\221', '\222', '\223', '\224', '\225', '\226', '\227',
|
|
639 '\230', '\231', '\232', '\233', '\234', '\235', '\236', '\237',
|
|
640 '\240', '\241', '\242', '\243', '\244', '\245', '\246', '\247',
|
|
641 '\250', '\251', '\252', '\253', '\254', '\255', '\256', '\257',
|
|
642 '\260', '\261', '\262', '\263', '\264', '\265', '\266', '\267',
|
|
643 '\270', '\271', '\272', '\273', '\274', '\275', '\276', '\277',
|
|
644 '\300', '\301', '\302', '\303', '\304', '\305', '\306', '\307',
|
|
645 '\310', '\311', '\312', '\313', '\314', '\315', '\316', '\317',
|
|
646 '\320', '\321', '\322', '\323', '\324', '\325', '\326', '\327',
|
|
647 '\330', '\331', '\332', '\333', '\334', '\335', '\336', '\337',
|
|
648 '\340', '\341', '\342', '\343', '\344', '\345', '\346', '\347',
|
|
649 '\350', '\351', '\352', '\353', '\354', '\355', '\356', '\357',
|
|
650 '\360', '\361', '\362', '\363', '\364', '\365', '\366', '\367',
|
|
651 '\370', '\371', '\372', '\373', '\374', '\375', '\376', '\377',
|
|
652 };
|
|
653
|
|
654 /* A version that works like generic strcasecmp() -- only collapsing
|
|
655 case in ASCII A-Z/a-z. This is safe on Mule strings due to the
|
|
656 current representation.
|
|
657
|
|
658 This version was written by some Berkeley coder, favoring
|
|
659 nanosecond improvements over clarity. In all other versions below,
|
|
660 we use symmetrical algorithms that may sacrifice a few machine
|
|
661 cycles but are MUCH MUCH clearer, which counts a lot more.
|
|
662 */
|
|
663
|
|
664 int
|
|
665 qxestrcasecmp (const Intbyte *s1, const Intbyte *s2)
|
|
666 {
|
|
667 Intbyte *cm = strcasecmp_charmap;
|
|
668
|
|
669 while (cm[*s1] == cm[*s2++])
|
|
670 if (*s1++ == '\0')
|
|
671 return (0);
|
|
672
|
|
673 return (cm[*s1] - cm[*--s2]);
|
|
674 }
|
|
675
|
|
676 int
|
|
677 ascii_strcasecmp (const Char_ASCII *s1, const Char_ASCII *s2)
|
|
678 {
|
|
679 return qxestrcasecmp ((const Intbyte *) s1, (const Intbyte *) s2);
|
|
680 }
|
|
681
|
|
682 int
|
|
683 qxestrcasecmp_c (const Intbyte *s1, const Char_ASCII *s2)
|
|
684 {
|
|
685 return qxestrcasecmp (s1, (const Intbyte *) s2);
|
|
686 }
|
|
687
|
|
688 /* An internationalized version that collapses case in a general fashion.
|
|
689 */
|
|
690
|
|
691 int
|
|
692 qxestrcasecmp_i18n (const Intbyte *s1, const Intbyte *s2)
|
|
693 {
|
|
694 while (*s1 && *s2)
|
|
695 {
|
|
696 if (DOWNCASE (0, charptr_emchar (s1)) !=
|
|
697 DOWNCASE (0, charptr_emchar (s2)))
|
|
698 break;
|
|
699 INC_CHARPTR (s1);
|
|
700 INC_CHARPTR (s2);
|
|
701 }
|
|
702
|
|
703 return (DOWNCASE (0, charptr_emchar (s1)) -
|
|
704 DOWNCASE (0, charptr_emchar (s2)));
|
|
705 }
|
|
706
|
|
707 /* The only difference between these next two and
|
|
708 qxememcasecmp()/qxememcasecmp_i18n() is that these two will stop if
|
|
709 both strings are equal and less than LEN in length, while
|
|
710 the mem...() versions would would run off the end. */
|
|
711
|
|
712 int
|
|
713 qxestrncasecmp (const Intbyte *s1, const Intbyte *s2, Bytecount len)
|
|
714 {
|
|
715 Intbyte *cm = strcasecmp_charmap;
|
|
716
|
|
717 while (len--)
|
|
718 {
|
|
719 int diff = cm[*s1] - cm[*s2];
|
|
720 if (diff != 0)
|
|
721 return diff;
|
|
722 if (!*s1)
|
|
723 return 0;
|
|
724 s1++, s2++;
|
|
725 }
|
|
726
|
|
727 return 0;
|
|
728 }
|
|
729
|
|
730 int
|
|
731 ascii_strncasecmp (const Char_ASCII *s1, const Char_ASCII *s2, Bytecount len)
|
|
732 {
|
|
733 return qxestrncasecmp ((const Intbyte *) s1, (const Intbyte *) s2, len);
|
|
734 }
|
|
735
|
|
736 int
|
|
737 qxestrncasecmp_c (const Intbyte *s1, const Char_ASCII *s2, Bytecount len)
|
|
738 {
|
|
739 return qxestrncasecmp (s1, (const Intbyte *) s2, len);
|
|
740 }
|
|
741
|
801
|
742 /* Compare LEN_FROM_S1 worth of characters from S1 with the same number of
|
|
743 characters from S2, case insensitive. NOTE: Downcasing can convert
|
|
744 characters from one length in bytes to another, so reversing S1 and S2
|
|
745 is *NOT* a symmetric operations! You must choose a length that agrees
|
|
746 with S1. */
|
|
747
|
771
|
748 int
|
801
|
749 qxestrncasecmp_i18n (const Intbyte *s1, const Intbyte *s2,
|
|
750 Bytecount len_from_s1)
|
771
|
751 {
|
801
|
752 while (len_from_s1 > 0)
|
771
|
753 {
|
|
754 const Intbyte *old_s1 = s1;
|
|
755 int diff = (DOWNCASE (0, charptr_emchar (s1)) -
|
|
756 DOWNCASE (0, charptr_emchar (s2)));
|
|
757 if (diff != 0)
|
|
758 return diff;
|
|
759 if (!*s1)
|
|
760 return 0;
|
|
761 INC_CHARPTR (s1);
|
|
762 INC_CHARPTR (s2);
|
801
|
763 len_from_s1 -= s1 - old_s1;
|
771
|
764 }
|
|
765
|
|
766 return 0;
|
|
767 }
|
|
768
|
|
769 int
|
|
770 qxememcmp (const Intbyte *s1, const Intbyte *s2, Bytecount len)
|
|
771 {
|
|
772 return memcmp (s1, s2, len);
|
|
773 }
|
|
774
|
|
775 int
|
801
|
776 qxememcmp4 (const Intbyte *s1, Bytecount len1,
|
|
777 const Intbyte *s2, Bytecount len2)
|
|
778 {
|
|
779 int retval = qxememcmp (s1, s2, min (len1, len2));
|
|
780 if (retval)
|
|
781 return retval;
|
|
782 return len1 - len2;
|
|
783 }
|
|
784
|
|
785 int
|
771
|
786 qxememcasecmp (const Intbyte *s1, const Intbyte *s2, Bytecount len)
|
|
787 {
|
|
788 Intbyte *cm = strcasecmp_charmap;
|
|
789
|
|
790 while (len--)
|
|
791 {
|
|
792 int diff = cm[*s1] - cm[*s2];
|
|
793 if (diff != 0)
|
|
794 return diff;
|
|
795 s1++, s2++;
|
|
796 }
|
|
797
|
|
798 return 0;
|
|
799 }
|
|
800
|
|
801 int
|
801
|
802 qxememcasecmp4 (const Intbyte *s1, Bytecount len1,
|
|
803 const Intbyte *s2, Bytecount len2)
|
771
|
804 {
|
801
|
805 int retval = qxememcasecmp (s1, s2, min (len1, len2));
|
|
806 if (retval)
|
|
807 return retval;
|
|
808 return len1 - len2;
|
|
809 }
|
|
810
|
|
811 /* Do a character-by-character comparison, returning "which is greater" by
|
|
812 comparing the Emchar values. (#### Should have option to compare Unicode
|
|
813 points) */
|
|
814
|
|
815 int
|
|
816 qxetextcmp (const Intbyte *s1, Bytecount len1,
|
|
817 const Intbyte *s2, Bytecount len2)
|
|
818 {
|
|
819 while (len1 > 0 && len2 > 0)
|
771
|
820 {
|
|
821 const Intbyte *old_s1 = s1;
|
801
|
822 const Intbyte *old_s2 = s2;
|
|
823 int diff = charptr_emchar (s1) - charptr_emchar (s2);
|
|
824 if (diff != 0)
|
|
825 return diff;
|
|
826 INC_CHARPTR (s1);
|
|
827 INC_CHARPTR (s2);
|
|
828 len1 -= s1 - old_s1;
|
|
829 len2 -= s2 - old_s2;
|
|
830 }
|
|
831
|
|
832 assert (len1 >= 0 && len2 >= 0);
|
|
833 return len1 - len2;
|
|
834 }
|
|
835
|
|
836 int
|
|
837 qxetextcmp_matching (const Intbyte *s1, Bytecount len1,
|
|
838 const Intbyte *s2, Bytecount len2,
|
|
839 Charcount *matching)
|
|
840 {
|
|
841 *matching = 0;
|
|
842 while (len1 > 0 && len2 > 0)
|
|
843 {
|
|
844 const Intbyte *old_s1 = s1;
|
|
845 const Intbyte *old_s2 = s2;
|
|
846 int diff = charptr_emchar (s1) - charptr_emchar (s2);
|
|
847 if (diff != 0)
|
|
848 return diff;
|
|
849 INC_CHARPTR (s1);
|
|
850 INC_CHARPTR (s2);
|
|
851 len1 -= s1 - old_s1;
|
|
852 len2 -= s2 - old_s2;
|
|
853 (*matching)++;
|
|
854 }
|
|
855
|
|
856 assert (len1 >= 0 && len2 >= 0);
|
|
857 return len1 - len2;
|
|
858 }
|
|
859
|
|
860 /* Do a character-by-character comparison, returning "which is greater" by
|
|
861 comparing the Emchar values, case insensitively (by downcasing both
|
|
862 first). (#### Should have option to compare Unicode points)
|
|
863
|
|
864 In this case, both lengths must be specified becaused downcasing can
|
|
865 convert characters from one length in bytes to another; therefore, two
|
|
866 blocks of text of different length might be equal. If both compare
|
|
867 equal up to the limit in length of one but not the other, the longer one
|
|
868 is "greater". */
|
|
869
|
|
870 int
|
|
871 qxetextcasecmp (const Intbyte *s1, Bytecount len1,
|
|
872 const Intbyte *s2, Bytecount len2)
|
|
873 {
|
|
874 while (len1 > 0 && len2 > 0)
|
|
875 {
|
|
876 const Intbyte *old_s1 = s1;
|
|
877 const Intbyte *old_s2 = s2;
|
771
|
878 int diff = (DOWNCASE (0, charptr_emchar (s1)) -
|
|
879 DOWNCASE (0, charptr_emchar (s2)));
|
|
880 if (diff != 0)
|
|
881 return diff;
|
|
882 INC_CHARPTR (s1);
|
|
883 INC_CHARPTR (s2);
|
801
|
884 len1 -= s1 - old_s1;
|
|
885 len2 -= s2 - old_s2;
|
771
|
886 }
|
|
887
|
801
|
888 assert (len1 >= 0 && len2 >= 0);
|
|
889 return len1 - len2;
|
|
890 }
|
|
891
|
|
892 /* Like qxetextcasecmp() but also return number of characters at
|
|
893 beginning that match. */
|
|
894
|
|
895 int
|
|
896 qxetextcasecmp_matching (const Intbyte *s1, Bytecount len1,
|
|
897 const Intbyte *s2, Bytecount len2,
|
|
898 Charcount *matching)
|
|
899 {
|
|
900 *matching = 0;
|
|
901 while (len1 > 0 && len2 > 0)
|
|
902 {
|
|
903 const Intbyte *old_s1 = s1;
|
|
904 const Intbyte *old_s2 = s2;
|
|
905 int diff = (DOWNCASE (0, charptr_emchar (s1)) -
|
|
906 DOWNCASE (0, charptr_emchar (s2)));
|
|
907 if (diff != 0)
|
|
908 return diff;
|
|
909 INC_CHARPTR (s1);
|
|
910 INC_CHARPTR (s2);
|
|
911 len1 -= s1 - old_s1;
|
|
912 len2 -= s2 - old_s2;
|
|
913 (*matching)++;
|
|
914 }
|
|
915
|
|
916 assert (len1 >= 0 && len2 >= 0);
|
|
917 return len1 - len2;
|
771
|
918 }
|
|
919
|
|
920 int
|
|
921 lisp_strcasecmp (Lisp_Object s1, Lisp_Object s2)
|
|
922 {
|
|
923 Intbyte *cm = strcasecmp_charmap;
|
|
924 Intbyte *p1 = XSTRING_DATA (s1);
|
|
925 Intbyte *p2 = XSTRING_DATA (s2);
|
|
926 Intbyte *e1 = p1 + XSTRING_LENGTH (s1);
|
|
927 Intbyte *e2 = p2 + XSTRING_LENGTH (s2);
|
|
928
|
|
929 /* again, we use a symmetric algorithm and favor clarity over
|
|
930 nanosecond improvements. */
|
|
931 while (1)
|
|
932 {
|
|
933 /* if we reached the end of either string, compare lengths.
|
|
934 do NOT compare the final null byte against anything, in case
|
|
935 the other string also has a null byte at that position. */
|
|
936 if (p1 == e1 || p2 == e2)
|
|
937 return e1 - e2;
|
|
938 if (cm[*p1] != cm[*p2])
|
|
939 return cm[*p1] - cm[*p2];
|
|
940 p1++, p2++;
|
|
941 }
|
|
942 }
|
|
943
|
|
944 int
|
|
945 lisp_strcasecmp_i18n (Lisp_Object s1, Lisp_Object s2)
|
|
946 {
|
801
|
947 return qxetextcasecmp (XSTRING_DATA (s1), XSTRING_LENGTH (s1),
|
|
948 XSTRING_DATA (s2), XSTRING_LENGTH (s2));
|
771
|
949 }
|
|
950
|
|
951
|
|
952 /************************************************************************/
|
|
953 /* conversion between textual representations */
|
|
954 /************************************************************************/
|
|
955
|
|
956 /* NOTE: Does not reset the Dynarr. */
|
|
957
|
|
958 void
|
|
959 convert_intbyte_string_into_emchar_dynarr (const Intbyte *str, Bytecount len,
|
|
960 Emchar_dynarr *dyn)
|
|
961 {
|
|
962 const Intbyte *strend = str + len;
|
|
963
|
|
964 while (str < strend)
|
|
965 {
|
|
966 Emchar ch = charptr_emchar (str);
|
|
967 Dynarr_add (dyn, ch);
|
|
968 INC_CHARPTR (str);
|
|
969 }
|
|
970 }
|
|
971
|
|
972 Charcount
|
|
973 convert_intbyte_string_into_emchar_string (const Intbyte *str, Bytecount len,
|
|
974 Emchar *arr)
|
|
975 {
|
|
976 const Intbyte *strend = str + len;
|
|
977 Charcount newlen = 0;
|
|
978 while (str < strend)
|
|
979 {
|
|
980 Emchar ch = charptr_emchar (str);
|
|
981 arr[newlen++] = ch;
|
|
982 INC_CHARPTR (str);
|
|
983 }
|
|
984 return newlen;
|
|
985 }
|
|
986
|
|
987 /* Convert an array of Emchars into the equivalent string representation.
|
|
988 Store into the given Intbyte dynarr. Does not reset the dynarr.
|
|
989 Does not add a terminating zero. */
|
|
990
|
|
991 void
|
|
992 convert_emchar_string_into_intbyte_dynarr (Emchar *arr, int nels,
|
|
993 Intbyte_dynarr *dyn)
|
|
994 {
|
|
995 Intbyte str[MAX_EMCHAR_LEN];
|
|
996 int i;
|
|
997
|
|
998 for (i = 0; i < nels; i++)
|
|
999 {
|
|
1000 Bytecount len = set_charptr_emchar (str, arr[i]);
|
|
1001 Dynarr_add_many (dyn, str, len);
|
|
1002 }
|
|
1003 }
|
|
1004
|
|
1005 /* Convert an array of Emchars into the equivalent string representation.
|
|
1006 Malloc the space needed for this and return it. If LEN_OUT is not a
|
|
1007 NULL pointer, store into LEN_OUT the number of Intbytes in the
|
|
1008 malloc()ed string. Note that the actual number of Intbytes allocated
|
|
1009 is one more than this: the returned string is zero-terminated. */
|
|
1010
|
|
1011 Intbyte *
|
|
1012 convert_emchar_string_into_malloced_string (Emchar *arr, int nels,
|
|
1013 Bytecount *len_out)
|
|
1014 {
|
|
1015 /* Damn zero-termination. */
|
|
1016 Intbyte *str = (Intbyte *) alloca (nels * MAX_EMCHAR_LEN + 1);
|
|
1017 Intbyte *strorig = str;
|
|
1018 Bytecount len;
|
|
1019
|
|
1020 int i;
|
|
1021
|
|
1022 for (i = 0; i < nels; i++)
|
|
1023 str += set_charptr_emchar (str, arr[i]);
|
|
1024 *str = '\0';
|
|
1025 len = str - strorig;
|
|
1026 str = (Intbyte *) xmalloc (1 + len);
|
|
1027 memcpy (str, strorig, 1 + len);
|
|
1028 if (len_out)
|
|
1029 *len_out = len;
|
|
1030 return str;
|
|
1031 }
|
|
1032
|
|
1033
|
|
1034 /************************************************************************/
|
|
1035 /* charset properties of strings */
|
|
1036 /************************************************************************/
|
|
1037
|
|
1038 void
|
|
1039 find_charsets_in_intbyte_string (unsigned char *charsets, const Intbyte *str,
|
|
1040 Bytecount len)
|
|
1041 {
|
|
1042 #ifndef MULE
|
|
1043 /* Telescope this. */
|
|
1044 charsets[0] = 1;
|
|
1045 #else
|
|
1046 const Intbyte *strend = str + len;
|
|
1047 memset (charsets, 0, NUM_LEADING_BYTES);
|
|
1048
|
|
1049 /* #### SJT doesn't like this. */
|
|
1050 if (len == 0)
|
|
1051 {
|
|
1052 charsets[XCHARSET_LEADING_BYTE (Vcharset_ascii) - MIN_LEADING_BYTE] = 1;
|
|
1053 return;
|
|
1054 }
|
|
1055
|
|
1056 while (str < strend)
|
|
1057 {
|
|
1058 charsets[CHAR_LEADING_BYTE (charptr_emchar (str)) - MIN_LEADING_BYTE] =
|
|
1059 1;
|
|
1060 INC_CHARPTR (str);
|
|
1061 }
|
|
1062 #endif
|
|
1063 }
|
|
1064
|
|
1065 void
|
|
1066 find_charsets_in_emchar_string (unsigned char *charsets, const Emchar *str,
|
|
1067 Charcount len)
|
|
1068 {
|
|
1069 #ifndef MULE
|
|
1070 /* Telescope this. */
|
|
1071 charsets[0] = 1;
|
|
1072 #else
|
|
1073 int i;
|
|
1074
|
|
1075 memset (charsets, 0, NUM_LEADING_BYTES);
|
|
1076
|
|
1077 /* #### SJT doesn't like this. */
|
|
1078 if (len == 0)
|
|
1079 {
|
|
1080 charsets[XCHARSET_LEADING_BYTE (Vcharset_ascii) - MIN_LEADING_BYTE] = 1;
|
|
1081 return;
|
|
1082 }
|
|
1083
|
|
1084 for (i = 0; i < len; i++)
|
|
1085 {
|
|
1086 charsets[CHAR_LEADING_BYTE (str[i]) - MIN_LEADING_BYTE] = 1;
|
|
1087 }
|
|
1088 #endif
|
|
1089 }
|
|
1090
|
|
1091 int
|
|
1092 intbyte_string_displayed_columns (const Intbyte *str, Bytecount len)
|
|
1093 {
|
|
1094 int cols = 0;
|
|
1095 const Intbyte *end = str + len;
|
|
1096
|
|
1097 while (str < end)
|
|
1098 {
|
|
1099 #ifdef MULE
|
|
1100 Emchar ch = charptr_emchar (str);
|
|
1101 cols += XCHARSET_COLUMNS (CHAR_CHARSET (ch));
|
|
1102 #else
|
|
1103 cols++;
|
|
1104 #endif
|
|
1105 INC_CHARPTR (str);
|
|
1106 }
|
|
1107
|
|
1108 return cols;
|
|
1109 }
|
|
1110
|
|
1111 int
|
|
1112 emchar_string_displayed_columns (const Emchar *str, Charcount len)
|
|
1113 {
|
|
1114 #ifdef MULE
|
|
1115 int cols = 0;
|
|
1116 int i;
|
|
1117
|
|
1118 for (i = 0; i < len; i++)
|
|
1119 cols += XCHARSET_COLUMNS (CHAR_CHARSET (str[i]));
|
|
1120
|
|
1121 return cols;
|
|
1122 #else /* not MULE */
|
|
1123 return len;
|
|
1124 #endif
|
|
1125 }
|
|
1126
|
|
1127 Charcount
|
|
1128 intbyte_string_nonascii_chars (const Intbyte *str, Bytecount len)
|
|
1129 {
|
|
1130 #ifdef MULE
|
|
1131 const Intbyte *end = str + len;
|
|
1132 Charcount retval = 0;
|
|
1133
|
|
1134 while (str < end)
|
|
1135 {
|
|
1136 if (!BYTE_ASCII_P (*str))
|
|
1137 retval++;
|
|
1138 INC_CHARPTR (str);
|
|
1139 }
|
|
1140
|
|
1141 return retval;
|
|
1142 #else
|
|
1143 return 0;
|
|
1144 #endif
|
|
1145 }
|
|
1146
|
|
1147
|
|
1148 /***************************************************************************/
|
|
1149 /* Eistring helper functions */
|
|
1150 /***************************************************************************/
|
|
1151
|
|
1152 int
|
|
1153 eistr_casefiddle_1 (Intbyte *olddata, Bytecount len, Intbyte *newdata,
|
|
1154 int downp)
|
|
1155 {
|
|
1156 Intbyte *endp = olddata + len;
|
|
1157 Intbyte *newp = newdata;
|
|
1158 int changedp = 0;
|
|
1159
|
|
1160 while (olddata < endp)
|
|
1161 {
|
|
1162 Emchar c = charptr_emchar (olddata);
|
|
1163 Emchar newc;
|
|
1164
|
|
1165 if (downp)
|
|
1166 newc = DOWNCASE (0, c);
|
|
1167 else
|
|
1168 newc = UPCASE (0, c);
|
|
1169
|
|
1170 if (c != newc)
|
|
1171 changedp = 1;
|
|
1172
|
|
1173 newp += set_charptr_emchar (newp, newc);
|
|
1174 INC_CHARPTR (olddata);
|
|
1175 }
|
|
1176
|
|
1177 *newp = '\0';
|
|
1178
|
|
1179 return changedp ? newp - newdata : 0;
|
|
1180 }
|
|
1181
|
|
1182 int
|
|
1183 eifind_large_enough_buffer (int oldbufsize, int needed_size)
|
|
1184 {
|
|
1185 while (oldbufsize < needed_size)
|
|
1186 {
|
|
1187 oldbufsize = oldbufsize * 3 / 2;
|
|
1188 oldbufsize = max (oldbufsize, 32);
|
|
1189 }
|
|
1190
|
|
1191 return oldbufsize;
|
|
1192 }
|
|
1193
|
|
1194 void
|
|
1195 eito_malloc_1 (Eistring *ei)
|
|
1196 {
|
|
1197 if (ei->mallocp_)
|
|
1198 return;
|
|
1199 ei->mallocp_ = 1;
|
|
1200 if (ei->data_)
|
|
1201 {
|
|
1202 Intbyte *newdata;
|
|
1203
|
|
1204 ei->max_size_allocated_ =
|
|
1205 eifind_large_enough_buffer (0, ei->bytelen_ + 1);
|
|
1206 newdata = (Intbyte *) xmalloc (ei->max_size_allocated_);
|
|
1207 memcpy (newdata, ei->data_, ei->bytelen_ + 1);
|
|
1208 ei->data_ = newdata;
|
|
1209 }
|
|
1210
|
|
1211 if (ei->extdata_)
|
|
1212 {
|
|
1213 Extbyte *newdata = (Extbyte *) xmalloc (ei->extlen_ + 2);
|
|
1214
|
|
1215 memcpy (newdata, ei->extdata_, ei->extlen_);
|
|
1216 /* Double null-terminate in case of Unicode data */
|
|
1217 newdata[ei->extlen_] = '\0';
|
|
1218 newdata[ei->extlen_ + 1] = '\0';
|
|
1219 ei->extdata_ = newdata;
|
|
1220 }
|
|
1221 }
|
|
1222
|
|
1223 int
|
|
1224 eicmp_1 (Eistring *ei, Bytecount off, Charcount charoff,
|
|
1225 Bytecount len, Charcount charlen, const Intbyte *data,
|
|
1226 const Eistring *ei2, int is_c, int fold_case)
|
|
1227 {
|
|
1228 assert ((off < 0) != (charoff < 0));
|
|
1229 if (off < 0)
|
|
1230 {
|
|
1231 off = charcount_to_bytecount (ei->data_, charoff);
|
|
1232 if (charlen < 0)
|
|
1233 len = -1;
|
|
1234 else
|
|
1235 len = charcount_to_bytecount (ei->data_ + off, charlen);
|
|
1236 }
|
|
1237 if (len < 0)
|
|
1238 len = ei->bytelen_ - off;
|
|
1239
|
|
1240 assert (off >= 0 && off <= ei->bytelen_);
|
|
1241 assert (len >= 0 && off + len <= ei->bytelen_);
|
|
1242 assert ((data == 0) != (ei == 0));
|
|
1243 assert ((is_c != 0) == (data != 0));
|
|
1244 assert (fold_case >= 0 && fold_case <= 2);
|
|
1245
|
|
1246 {
|
|
1247 Bytecount dstlen;
|
|
1248 const Intbyte *src = ei->data_, *dst;
|
|
1249
|
|
1250 if (data)
|
|
1251 {
|
|
1252 dst = data;
|
|
1253 dstlen = qxestrlen (data);
|
|
1254 }
|
|
1255 else
|
|
1256 {
|
|
1257 dst = ei2->data_;
|
|
1258 dstlen = ei2->bytelen_;
|
|
1259 }
|
|
1260
|
|
1261 if (is_c)
|
|
1262 EI_ASSERT_ASCII ((Char_ASCII *) dst, dstlen);
|
|
1263
|
801
|
1264 return (fold_case == 0 ? qxememcmp4 (src, len, dst, dstlen) :
|
|
1265 fold_case == 1 ? qxememcasecmp4 (src, len, dst, dstlen) :
|
|
1266 qxetextcasecmp (src, len, dst, dstlen));
|
771
|
1267 }
|
|
1268 }
|
|
1269
|
|
1270 Intbyte *
|
|
1271 eicpyout_malloc_fmt (Eistring *eistr, Bytecount *len_out, Internal_Format fmt)
|
|
1272 {
|
|
1273 Intbyte *ptr;
|
|
1274
|
|
1275 assert (fmt == FORMAT_DEFAULT);
|
|
1276 ptr = xnew_array (Intbyte, eistr->bytelen_ + 1);
|
|
1277 if (len_out)
|
|
1278 *len_out = eistr->bytelen_;
|
|
1279 memcpy (ptr, eistr->data_, eistr->bytelen_ + 1);
|
|
1280 return ptr;
|
|
1281 }
|
|
1282
|
|
1283
|
|
1284 /************************************************************************/
|
|
1285 /* Charcount/Bytecount conversion */
|
|
1286 /************************************************************************/
|
|
1287
|
|
1288 /* Optimization. Do it. Live it. Love it. */
|
|
1289
|
|
1290 #ifdef MULE
|
|
1291
|
|
1292 /* We include the basic functions here that require no specific
|
|
1293 knowledge of how data is Mule-encoded into a buffer other
|
|
1294 than the basic (00 - 7F), (80 - 9F), (A0 - FF) scheme.
|
|
1295 Anything that requires more specific knowledge goes into
|
|
1296 mule-charset.c. */
|
|
1297
|
|
1298 /* Given a pointer to a text string and a length in bytes, return
|
|
1299 the equivalent length in characters. */
|
|
1300
|
|
1301 Charcount
|
|
1302 bytecount_to_charcount (const Intbyte *ptr, Bytecount len)
|
|
1303 {
|
|
1304 Charcount count = 0;
|
|
1305 const Intbyte *end = ptr + len;
|
|
1306
|
|
1307 #if SIZEOF_LONG == 8
|
|
1308 # define STRIDE_TYPE long
|
|
1309 # define HIGH_BIT_MASK 0x8080808080808080UL
|
|
1310 #elif SIZEOF_LONG_LONG == 8 && !(defined (i386) || defined (__i386__))
|
|
1311 # define STRIDE_TYPE long long
|
|
1312 # define HIGH_BIT_MASK 0x8080808080808080ULL
|
|
1313 #elif SIZEOF_LONG == 4
|
|
1314 # define STRIDE_TYPE long
|
|
1315 # define HIGH_BIT_MASK 0x80808080UL
|
|
1316 #else
|
|
1317 # error Add support for 128-bit systems here
|
|
1318 #endif
|
|
1319
|
|
1320 #define ALIGN_BITS ((EMACS_UINT) (ALIGNOF (STRIDE_TYPE) - 1))
|
|
1321 #define ALIGN_MASK (~ ALIGN_BITS)
|
|
1322 #define ALIGNED(ptr) ((((EMACS_UINT) ptr) & ALIGN_BITS) == 0)
|
|
1323 #define STRIDE sizeof (STRIDE_TYPE)
|
|
1324
|
|
1325 while (ptr < end)
|
|
1326 {
|
|
1327 if (BYTE_ASCII_P (*ptr))
|
|
1328 {
|
|
1329 /* optimize for long stretches of ASCII */
|
|
1330 if (! ALIGNED (ptr))
|
|
1331 ptr++, count++;
|
|
1332 else
|
|
1333 {
|
|
1334 const unsigned STRIDE_TYPE *ascii_end =
|
|
1335 (const unsigned STRIDE_TYPE *) ptr;
|
|
1336 /* This loop screams, because we can detect ASCII
|
|
1337 characters 4 or 8 at a time. */
|
|
1338 while ((const Intbyte *) ascii_end + STRIDE <= end
|
|
1339 && !(*ascii_end & HIGH_BIT_MASK))
|
|
1340 ascii_end++;
|
|
1341 if ((Intbyte *) ascii_end == ptr)
|
|
1342 ptr++, count++;
|
|
1343 else
|
|
1344 {
|
|
1345 count += (Intbyte *) ascii_end - ptr;
|
|
1346 ptr = (Intbyte *) ascii_end;
|
|
1347 }
|
|
1348 }
|
|
1349 }
|
|
1350 else
|
|
1351 {
|
|
1352 /* optimize for successive characters from the same charset */
|
|
1353 Intbyte leading_byte = *ptr;
|
|
1354 int bytes = REP_BYTES_BY_FIRST_BYTE (leading_byte);
|
|
1355 while ((ptr < end) && (*ptr == leading_byte))
|
|
1356 ptr += bytes, count++;
|
|
1357 }
|
|
1358 }
|
|
1359
|
|
1360 /* Bomb out if the specified substring ends in the middle
|
|
1361 of a character. Note that we might have already gotten
|
|
1362 a core dump above from an invalid reference, but at least
|
|
1363 we will get no farther than here.
|
|
1364
|
|
1365 This also catches len < 0. */
|
800
|
1366 text_checking_assert (ptr == end);
|
771
|
1367
|
|
1368 return count;
|
|
1369 }
|
|
1370
|
|
1371 /* Given a pointer to a text string and a length in characters, return
|
|
1372 the equivalent length in bytes. */
|
|
1373
|
|
1374 Bytecount
|
|
1375 charcount_to_bytecount (const Intbyte *ptr, Charcount len)
|
|
1376 {
|
|
1377 const Intbyte *newptr = ptr;
|
|
1378
|
800
|
1379 text_checking_assert (len >= 0);
|
771
|
1380 while (len > 0)
|
|
1381 {
|
|
1382 INC_CHARPTR (newptr);
|
|
1383 len--;
|
|
1384 }
|
|
1385 return newptr - ptr;
|
|
1386 }
|
|
1387
|
|
1388 inline static void
|
|
1389 update_entirely_ascii_p_flag (struct buffer *buf)
|
|
1390 {
|
814
|
1391 buf->text->entirely_ascii_p = buf->text->z == buf->text->bufz;
|
771
|
1392 }
|
|
1393
|
|
1394 /* The next two functions are the actual meat behind the
|
|
1395 charbpos-to-bytebpos and bytebpos-to-charbpos conversions. Currently
|
|
1396 the method they use is fairly unsophisticated; see buffer.h.
|
|
1397
|
|
1398 Note that charbpos_to_bytebpos_func() is probably the most-called
|
|
1399 function in all of XEmacs. Therefore, it must be FAST FAST FAST.
|
|
1400 This is the reason why so much of the code is duplicated.
|
|
1401
|
|
1402 Similar considerations apply to bytebpos_to_charbpos_func(), although
|
|
1403 less so because the function is not called so often.
|
|
1404
|
|
1405 #### At some point this should use a more sophisticated method;
|
|
1406 see buffer.h. */
|
|
1407
|
|
1408 static int not_very_random_number;
|
|
1409
|
|
1410 Bytebpos
|
|
1411 charbpos_to_bytebpos_func (struct buffer *buf, Charbpos x)
|
|
1412 {
|
|
1413 Charbpos bufmin;
|
|
1414 Charbpos bufmax;
|
|
1415 Bytebpos bytmin;
|
|
1416 Bytebpos bytmax;
|
|
1417 int size;
|
|
1418 int forward_p;
|
|
1419 Bytebpos retval;
|
|
1420 int diff_so_far;
|
|
1421 int add_to_cache = 0;
|
|
1422
|
|
1423 /* Check for some cached positions, for speed. */
|
|
1424 if (x == BUF_PT (buf))
|
|
1425 return BI_BUF_PT (buf);
|
|
1426 if (x == BUF_ZV (buf))
|
|
1427 return BI_BUF_ZV (buf);
|
|
1428 if (x == BUF_BEGV (buf))
|
|
1429 return BI_BUF_BEGV (buf);
|
|
1430
|
|
1431 bufmin = buf->text->mule_bufmin;
|
|
1432 bufmax = buf->text->mule_bufmax;
|
|
1433 bytmin = buf->text->mule_bytmin;
|
|
1434 bytmax = buf->text->mule_bytmax;
|
|
1435 size = (1 << buf->text->mule_shifter) + !!buf->text->mule_three_p;
|
|
1436
|
|
1437 /* The basic idea here is that we shift the "known region" up or down
|
|
1438 until it overlaps the specified position. We do this by moving
|
|
1439 the upper bound of the known region up one character at a time,
|
|
1440 and moving the lower bound of the known region up as necessary
|
|
1441 when the size of the character just seen changes.
|
|
1442
|
|
1443 We optimize this, however, by first shifting the known region to
|
|
1444 one of the cached points if it's close by. (We don't check BEG or
|
|
1445 Z, even though they're cached; most of the time these will be the
|
|
1446 same as BEGV and ZV, and when they're not, they're not likely
|
|
1447 to be used.) */
|
|
1448
|
|
1449 if (x > bufmax)
|
|
1450 {
|
|
1451 Charbpos diffmax = x - bufmax;
|
|
1452 Charbpos diffpt = x - BUF_PT (buf);
|
|
1453 Charbpos diffzv = BUF_ZV (buf) - x;
|
|
1454 /* #### This value could stand some more exploration. */
|
|
1455 Charcount heuristic_hack = (bufmax - bufmin) >> 2;
|
|
1456
|
|
1457 /* Check if the position is closer to PT or ZV than to the
|
|
1458 end of the known region. */
|
|
1459
|
|
1460 if (diffpt < 0)
|
|
1461 diffpt = -diffpt;
|
|
1462 if (diffzv < 0)
|
|
1463 diffzv = -diffzv;
|
|
1464
|
|
1465 /* But also implement a heuristic that favors the known region
|
|
1466 over PT or ZV. The reason for this is that switching to
|
|
1467 PT or ZV will wipe out the knowledge in the known region,
|
|
1468 which might be annoying if the known region is large and
|
|
1469 PT or ZV is not that much closer than the end of the known
|
|
1470 region. */
|
|
1471
|
|
1472 diffzv += heuristic_hack;
|
|
1473 diffpt += heuristic_hack;
|
|
1474 if (diffpt < diffmax && diffpt <= diffzv)
|
|
1475 {
|
|
1476 bufmax = bufmin = BUF_PT (buf);
|
|
1477 bytmax = bytmin = BI_BUF_PT (buf);
|
|
1478 /* We set the size to 1 even though it doesn't really
|
|
1479 matter because the new known region contains no
|
|
1480 characters. We do this because this is the most
|
|
1481 likely size of the characters around the new known
|
|
1482 region, and we avoid potential yuckiness that is
|
|
1483 done when size == 3. */
|
|
1484 size = 1;
|
|
1485 }
|
|
1486 if (diffzv < diffmax)
|
|
1487 {
|
|
1488 bufmax = bufmin = BUF_ZV (buf);
|
|
1489 bytmax = bytmin = BI_BUF_ZV (buf);
|
|
1490 size = 1;
|
|
1491 }
|
|
1492 }
|
800
|
1493 #ifdef ERROR_CHECK_TEXT
|
771
|
1494 else if (x >= bufmin)
|
|
1495 abort ();
|
|
1496 #endif
|
|
1497 else
|
|
1498 {
|
|
1499 Charbpos diffmin = bufmin - x;
|
|
1500 Charbpos diffpt = BUF_PT (buf) - x;
|
|
1501 Charbpos diffbegv = x - BUF_BEGV (buf);
|
|
1502 /* #### This value could stand some more exploration. */
|
|
1503 Charcount heuristic_hack = (bufmax - bufmin) >> 2;
|
|
1504
|
|
1505 if (diffpt < 0)
|
|
1506 diffpt = -diffpt;
|
|
1507 if (diffbegv < 0)
|
|
1508 diffbegv = -diffbegv;
|
|
1509
|
|
1510 /* But also implement a heuristic that favors the known region --
|
|
1511 see above. */
|
|
1512
|
|
1513 diffbegv += heuristic_hack;
|
|
1514 diffpt += heuristic_hack;
|
|
1515
|
|
1516 if (diffpt < diffmin && diffpt <= diffbegv)
|
|
1517 {
|
|
1518 bufmax = bufmin = BUF_PT (buf);
|
|
1519 bytmax = bytmin = BI_BUF_PT (buf);
|
|
1520 /* We set the size to 1 even though it doesn't really
|
|
1521 matter because the new known region contains no
|
|
1522 characters. We do this because this is the most
|
|
1523 likely size of the characters around the new known
|
|
1524 region, and we avoid potential yuckiness that is
|
|
1525 done when size == 3. */
|
|
1526 size = 1;
|
|
1527 }
|
|
1528 if (diffbegv < diffmin)
|
|
1529 {
|
|
1530 bufmax = bufmin = BUF_BEGV (buf);
|
|
1531 bytmax = bytmin = BI_BUF_BEGV (buf);
|
|
1532 size = 1;
|
|
1533 }
|
|
1534 }
|
|
1535
|
|
1536 diff_so_far = x > bufmax ? x - bufmax : bufmin - x;
|
|
1537 if (diff_so_far > 50)
|
|
1538 {
|
|
1539 /* If we have to move more than a certain amount, then look
|
|
1540 into our cache. */
|
|
1541 int minval = INT_MAX;
|
|
1542 int found = 0;
|
|
1543 int i;
|
|
1544
|
|
1545 add_to_cache = 1;
|
|
1546 /* I considered keeping the positions ordered. This would speed
|
|
1547 up this loop, but updating the cache would take longer, so
|
|
1548 it doesn't seem like it would really matter. */
|
|
1549 for (i = 0; i < 16; i++)
|
|
1550 {
|
|
1551 int diff = buf->text->mule_charbpos_cache[i] - x;
|
|
1552
|
|
1553 if (diff < 0)
|
|
1554 diff = -diff;
|
|
1555 if (diff < minval)
|
|
1556 {
|
|
1557 minval = diff;
|
|
1558 found = i;
|
|
1559 }
|
|
1560 }
|
|
1561
|
|
1562 if (minval < diff_so_far)
|
|
1563 {
|
|
1564 bufmax = bufmin = buf->text->mule_charbpos_cache[found];
|
|
1565 bytmax = bytmin = buf->text->mule_bytebpos_cache[found];
|
|
1566 size = 1;
|
|
1567 }
|
|
1568 }
|
|
1569
|
|
1570 /* It's conceivable that the caching above could lead to X being
|
|
1571 the same as one of the range edges. */
|
|
1572 if (x >= bufmax)
|
|
1573 {
|
|
1574 Bytebpos newmax;
|
|
1575 Bytecount newsize;
|
|
1576
|
|
1577 forward_p = 1;
|
|
1578 while (x > bufmax)
|
|
1579 {
|
|
1580 newmax = bytmax;
|
|
1581
|
|
1582 INC_BYTEBPOS (buf, newmax);
|
|
1583 newsize = newmax - bytmax;
|
|
1584 if (newsize != size)
|
|
1585 {
|
|
1586 bufmin = bufmax;
|
|
1587 bytmin = bytmax;
|
|
1588 size = newsize;
|
|
1589 }
|
|
1590 bytmax = newmax;
|
|
1591 bufmax++;
|
|
1592 }
|
|
1593 retval = bytmax;
|
|
1594
|
|
1595 /* #### Should go past the found location to reduce the number
|
|
1596 of times that this function is called */
|
|
1597 }
|
|
1598 else /* x < bufmin */
|
|
1599 {
|
|
1600 Bytebpos newmin;
|
|
1601 Bytecount newsize;
|
|
1602
|
|
1603 forward_p = 0;
|
|
1604 while (x < bufmin)
|
|
1605 {
|
|
1606 newmin = bytmin;
|
|
1607
|
|
1608 DEC_BYTEBPOS (buf, newmin);
|
|
1609 newsize = bytmin - newmin;
|
|
1610 if (newsize != size)
|
|
1611 {
|
|
1612 bufmax = bufmin;
|
|
1613 bytmax = bytmin;
|
|
1614 size = newsize;
|
|
1615 }
|
|
1616 bytmin = newmin;
|
|
1617 bufmin--;
|
|
1618 }
|
|
1619 retval = bytmin;
|
|
1620
|
|
1621 /* #### Should go past the found location to reduce the number
|
|
1622 of times that this function is called
|
|
1623 */
|
|
1624 }
|
|
1625
|
|
1626 /* If size is three, than we have to max sure that the range we
|
|
1627 discovered isn't too large, because we use a fixed-length
|
|
1628 table to divide by 3. */
|
|
1629
|
|
1630 if (size == 3)
|
|
1631 {
|
|
1632 int gap = bytmax - bytmin;
|
|
1633 buf->text->mule_three_p = 1;
|
|
1634 buf->text->mule_shifter = 1;
|
|
1635
|
|
1636 if (gap > MAX_BYTEBPOS_GAP_SIZE_3)
|
|
1637 {
|
|
1638 if (forward_p)
|
|
1639 {
|
|
1640 bytmin = bytmax - MAX_BYTEBPOS_GAP_SIZE_3;
|
|
1641 bufmin = bufmax - MAX_CHARBPOS_GAP_SIZE_3;
|
|
1642 }
|
|
1643 else
|
|
1644 {
|
|
1645 bytmax = bytmin + MAX_BYTEBPOS_GAP_SIZE_3;
|
|
1646 bufmax = bufmin + MAX_CHARBPOS_GAP_SIZE_3;
|
|
1647 }
|
|
1648 }
|
|
1649 }
|
|
1650 else
|
|
1651 {
|
|
1652 buf->text->mule_three_p = 0;
|
|
1653 if (size == 4)
|
|
1654 buf->text->mule_shifter = 2;
|
|
1655 else
|
|
1656 buf->text->mule_shifter = size - 1;
|
|
1657 }
|
|
1658
|
|
1659 buf->text->mule_bufmin = bufmin;
|
|
1660 buf->text->mule_bufmax = bufmax;
|
|
1661 buf->text->mule_bytmin = bytmin;
|
|
1662 buf->text->mule_bytmax = bytmax;
|
|
1663
|
|
1664 if (add_to_cache)
|
|
1665 {
|
|
1666 int replace_loc;
|
|
1667
|
|
1668 /* We throw away a "random" cached value and replace it with
|
|
1669 the new value. It doesn't actually have to be very random
|
|
1670 at all, just evenly distributed.
|
|
1671
|
|
1672 #### It would be better to use a least-recently-used algorithm
|
|
1673 or something that tries to space things out, but I'm not sure
|
|
1674 it's worth it to go to the trouble of maintaining that. */
|
|
1675 not_very_random_number += 621;
|
|
1676 replace_loc = not_very_random_number & 15;
|
|
1677 buf->text->mule_charbpos_cache[replace_loc] = x;
|
|
1678 buf->text->mule_bytebpos_cache[replace_loc] = retval;
|
|
1679 }
|
|
1680
|
|
1681 return retval;
|
|
1682 }
|
|
1683
|
|
1684 /* The logic in this function is almost identical to the logic in
|
|
1685 the previous function. */
|
|
1686
|
|
1687 Charbpos
|
|
1688 bytebpos_to_charbpos_func (struct buffer *buf, Bytebpos x)
|
|
1689 {
|
|
1690 Charbpos bufmin;
|
|
1691 Charbpos bufmax;
|
|
1692 Bytebpos bytmin;
|
|
1693 Bytebpos bytmax;
|
|
1694 int size;
|
|
1695 int forward_p;
|
|
1696 Charbpos retval;
|
|
1697 int diff_so_far;
|
|
1698 int add_to_cache = 0;
|
|
1699
|
|
1700 /* Check for some cached positions, for speed. */
|
|
1701 if (x == BI_BUF_PT (buf))
|
|
1702 return BUF_PT (buf);
|
|
1703 if (x == BI_BUF_ZV (buf))
|
|
1704 return BUF_ZV (buf);
|
|
1705 if (x == BI_BUF_BEGV (buf))
|
|
1706 return BUF_BEGV (buf);
|
|
1707
|
|
1708 bufmin = buf->text->mule_bufmin;
|
|
1709 bufmax = buf->text->mule_bufmax;
|
|
1710 bytmin = buf->text->mule_bytmin;
|
|
1711 bytmax = buf->text->mule_bytmax;
|
|
1712 size = (1 << buf->text->mule_shifter) + !!buf->text->mule_three_p;
|
|
1713
|
|
1714 /* The basic idea here is that we shift the "known region" up or down
|
|
1715 until it overlaps the specified position. We do this by moving
|
|
1716 the upper bound of the known region up one character at a time,
|
|
1717 and moving the lower bound of the known region up as necessary
|
|
1718 when the size of the character just seen changes.
|
|
1719
|
|
1720 We optimize this, however, by first shifting the known region to
|
|
1721 one of the cached points if it's close by. (We don't check BI_BEG or
|
|
1722 BI_Z, even though they're cached; most of the time these will be the
|
|
1723 same as BI_BEGV and BI_ZV, and when they're not, they're not likely
|
|
1724 to be used.) */
|
|
1725
|
|
1726 if (x > bytmax)
|
|
1727 {
|
|
1728 Bytebpos diffmax = x - bytmax;
|
|
1729 Bytebpos diffpt = x - BI_BUF_PT (buf);
|
|
1730 Bytebpos diffzv = BI_BUF_ZV (buf) - x;
|
|
1731 /* #### This value could stand some more exploration. */
|
|
1732 Bytecount heuristic_hack = (bytmax - bytmin) >> 2;
|
|
1733
|
|
1734 /* Check if the position is closer to PT or ZV than to the
|
|
1735 end of the known region. */
|
|
1736
|
|
1737 if (diffpt < 0)
|
|
1738 diffpt = -diffpt;
|
|
1739 if (diffzv < 0)
|
|
1740 diffzv = -diffzv;
|
|
1741
|
|
1742 /* But also implement a heuristic that favors the known region
|
|
1743 over BI_PT or BI_ZV. The reason for this is that switching to
|
|
1744 BI_PT or BI_ZV will wipe out the knowledge in the known region,
|
|
1745 which might be annoying if the known region is large and
|
|
1746 BI_PT or BI_ZV is not that much closer than the end of the known
|
|
1747 region. */
|
|
1748
|
|
1749 diffzv += heuristic_hack;
|
|
1750 diffpt += heuristic_hack;
|
|
1751 if (diffpt < diffmax && diffpt <= diffzv)
|
|
1752 {
|
|
1753 bufmax = bufmin = BUF_PT (buf);
|
|
1754 bytmax = bytmin = BI_BUF_PT (buf);
|
|
1755 /* We set the size to 1 even though it doesn't really
|
|
1756 matter because the new known region contains no
|
|
1757 characters. We do this because this is the most
|
|
1758 likely size of the characters around the new known
|
|
1759 region, and we avoid potential yuckiness that is
|
|
1760 done when size == 3. */
|
|
1761 size = 1;
|
|
1762 }
|
|
1763 if (diffzv < diffmax)
|
|
1764 {
|
|
1765 bufmax = bufmin = BUF_ZV (buf);
|
|
1766 bytmax = bytmin = BI_BUF_ZV (buf);
|
|
1767 size = 1;
|
|
1768 }
|
|
1769 }
|
800
|
1770 #ifdef ERROR_CHECK_TEXT
|
771
|
1771 else if (x >= bytmin)
|
|
1772 abort ();
|
|
1773 #endif
|
|
1774 else
|
|
1775 {
|
|
1776 Bytebpos diffmin = bytmin - x;
|
|
1777 Bytebpos diffpt = BI_BUF_PT (buf) - x;
|
|
1778 Bytebpos diffbegv = x - BI_BUF_BEGV (buf);
|
|
1779 /* #### This value could stand some more exploration. */
|
|
1780 Bytecount heuristic_hack = (bytmax - bytmin) >> 2;
|
|
1781
|
|
1782 if (diffpt < 0)
|
|
1783 diffpt = -diffpt;
|
|
1784 if (diffbegv < 0)
|
|
1785 diffbegv = -diffbegv;
|
|
1786
|
|
1787 /* But also implement a heuristic that favors the known region --
|
|
1788 see above. */
|
|
1789
|
|
1790 diffbegv += heuristic_hack;
|
|
1791 diffpt += heuristic_hack;
|
|
1792
|
|
1793 if (diffpt < diffmin && diffpt <= diffbegv)
|
|
1794 {
|
|
1795 bufmax = bufmin = BUF_PT (buf);
|
|
1796 bytmax = bytmin = BI_BUF_PT (buf);
|
|
1797 /* We set the size to 1 even though it doesn't really
|
|
1798 matter because the new known region contains no
|
|
1799 characters. We do this because this is the most
|
|
1800 likely size of the characters around the new known
|
|
1801 region, and we avoid potential yuckiness that is
|
|
1802 done when size == 3. */
|
|
1803 size = 1;
|
|
1804 }
|
|
1805 if (diffbegv < diffmin)
|
|
1806 {
|
|
1807 bufmax = bufmin = BUF_BEGV (buf);
|
|
1808 bytmax = bytmin = BI_BUF_BEGV (buf);
|
|
1809 size = 1;
|
|
1810 }
|
|
1811 }
|
|
1812
|
|
1813 diff_so_far = x > bytmax ? x - bytmax : bytmin - x;
|
|
1814 if (diff_so_far > 50)
|
|
1815 {
|
|
1816 /* If we have to move more than a certain amount, then look
|
|
1817 into our cache. */
|
|
1818 int minval = INT_MAX;
|
|
1819 int found = 0;
|
|
1820 int i;
|
|
1821
|
|
1822 add_to_cache = 1;
|
|
1823 /* I considered keeping the positions ordered. This would speed
|
|
1824 up this loop, but updating the cache would take longer, so
|
|
1825 it doesn't seem like it would really matter. */
|
|
1826 for (i = 0; i < 16; i++)
|
|
1827 {
|
|
1828 int diff = buf->text->mule_bytebpos_cache[i] - x;
|
|
1829
|
|
1830 if (diff < 0)
|
|
1831 diff = -diff;
|
|
1832 if (diff < minval)
|
|
1833 {
|
|
1834 minval = diff;
|
|
1835 found = i;
|
|
1836 }
|
|
1837 }
|
|
1838
|
|
1839 if (minval < diff_so_far)
|
|
1840 {
|
|
1841 bufmax = bufmin = buf->text->mule_charbpos_cache[found];
|
|
1842 bytmax = bytmin = buf->text->mule_bytebpos_cache[found];
|
|
1843 size = 1;
|
|
1844 }
|
|
1845 }
|
|
1846
|
|
1847 /* It's conceivable that the caching above could lead to X being
|
|
1848 the same as one of the range edges. */
|
|
1849 if (x >= bytmax)
|
|
1850 {
|
|
1851 Bytebpos newmax;
|
|
1852 Bytecount newsize;
|
|
1853
|
|
1854 forward_p = 1;
|
|
1855 while (x > bytmax)
|
|
1856 {
|
|
1857 newmax = bytmax;
|
|
1858
|
|
1859 INC_BYTEBPOS (buf, newmax);
|
|
1860 newsize = newmax - bytmax;
|
|
1861 if (newsize != size)
|
|
1862 {
|
|
1863 bufmin = bufmax;
|
|
1864 bytmin = bytmax;
|
|
1865 size = newsize;
|
|
1866 }
|
|
1867 bytmax = newmax;
|
|
1868 bufmax++;
|
|
1869 }
|
|
1870 retval = bufmax;
|
|
1871
|
|
1872 /* #### Should go past the found location to reduce the number
|
|
1873 of times that this function is called */
|
|
1874 }
|
|
1875 else /* x <= bytmin */
|
|
1876 {
|
|
1877 Bytebpos newmin;
|
|
1878 Bytecount newsize;
|
|
1879
|
|
1880 forward_p = 0;
|
|
1881 while (x < bytmin)
|
|
1882 {
|
|
1883 newmin = bytmin;
|
|
1884
|
|
1885 DEC_BYTEBPOS (buf, newmin);
|
|
1886 newsize = bytmin - newmin;
|
|
1887 if (newsize != size)
|
|
1888 {
|
|
1889 bufmax = bufmin;
|
|
1890 bytmax = bytmin;
|
|
1891 size = newsize;
|
|
1892 }
|
|
1893 bytmin = newmin;
|
|
1894 bufmin--;
|
|
1895 }
|
|
1896 retval = bufmin;
|
|
1897
|
|
1898 /* #### Should go past the found location to reduce the number
|
|
1899 of times that this function is called
|
|
1900 */
|
|
1901 }
|
|
1902
|
|
1903 /* If size is three, than we have to max sure that the range we
|
|
1904 discovered isn't too large, because we use a fixed-length
|
|
1905 table to divide by 3. */
|
|
1906
|
|
1907 if (size == 3)
|
|
1908 {
|
|
1909 int gap = bytmax - bytmin;
|
|
1910 buf->text->mule_three_p = 1;
|
|
1911 buf->text->mule_shifter = 1;
|
|
1912
|
|
1913 if (gap > MAX_BYTEBPOS_GAP_SIZE_3)
|
|
1914 {
|
|
1915 if (forward_p)
|
|
1916 {
|
|
1917 bytmin = bytmax - MAX_BYTEBPOS_GAP_SIZE_3;
|
|
1918 bufmin = bufmax - MAX_CHARBPOS_GAP_SIZE_3;
|
|
1919 }
|
|
1920 else
|
|
1921 {
|
|
1922 bytmax = bytmin + MAX_BYTEBPOS_GAP_SIZE_3;
|
|
1923 bufmax = bufmin + MAX_CHARBPOS_GAP_SIZE_3;
|
|
1924 }
|
|
1925 }
|
|
1926 }
|
|
1927 else
|
|
1928 {
|
|
1929 buf->text->mule_three_p = 0;
|
|
1930 if (size == 4)
|
|
1931 buf->text->mule_shifter = 2;
|
|
1932 else
|
|
1933 buf->text->mule_shifter = size - 1;
|
|
1934 }
|
|
1935
|
|
1936 buf->text->mule_bufmin = bufmin;
|
|
1937 buf->text->mule_bufmax = bufmax;
|
|
1938 buf->text->mule_bytmin = bytmin;
|
|
1939 buf->text->mule_bytmax = bytmax;
|
|
1940
|
|
1941 if (add_to_cache)
|
|
1942 {
|
|
1943 int replace_loc;
|
|
1944
|
|
1945 /* We throw away a "random" cached value and replace it with
|
|
1946 the new value. It doesn't actually have to be very random
|
|
1947 at all, just evenly distributed.
|
|
1948
|
|
1949 #### It would be better to use a least-recently-used algorithm
|
|
1950 or something that tries to space things out, but I'm not sure
|
|
1951 it's worth it to go to the trouble of maintaining that. */
|
|
1952 not_very_random_number += 621;
|
|
1953 replace_loc = not_very_random_number & 15;
|
|
1954 buf->text->mule_charbpos_cache[replace_loc] = retval;
|
|
1955 buf->text->mule_bytebpos_cache[replace_loc] = x;
|
|
1956 }
|
|
1957
|
|
1958 return retval;
|
|
1959 }
|
|
1960
|
|
1961 /* Text of length BYTELENGTH and CHARLENGTH (in different units)
|
|
1962 was inserted at charbpos START. */
|
|
1963
|
|
1964 void
|
|
1965 buffer_mule_signal_inserted_region (struct buffer *buf, Charbpos start,
|
|
1966 Bytecount bytelength,
|
|
1967 Charcount charlength)
|
|
1968 {
|
|
1969 int size = (1 << buf->text->mule_shifter) + !!buf->text->mule_three_p;
|
|
1970 int i;
|
|
1971
|
|
1972 /* Adjust the cache of known positions. */
|
|
1973 for (i = 0; i < 16; i++)
|
|
1974 {
|
|
1975
|
|
1976 if (buf->text->mule_charbpos_cache[i] > start)
|
|
1977 {
|
|
1978 buf->text->mule_charbpos_cache[i] += charlength;
|
|
1979 buf->text->mule_bytebpos_cache[i] += bytelength;
|
|
1980 }
|
|
1981 }
|
|
1982
|
|
1983 if (start >= buf->text->mule_bufmax)
|
|
1984 goto done;
|
|
1985
|
|
1986 /* The insertion is either before the known region, in which case
|
|
1987 it shoves it forward; or within the known region, in which case
|
|
1988 it shoves the end forward. (But it may make the known region
|
|
1989 inconsistent, so we may have to shorten it.) */
|
|
1990
|
|
1991 if (start <= buf->text->mule_bufmin)
|
|
1992 {
|
|
1993 buf->text->mule_bufmin += charlength;
|
|
1994 buf->text->mule_bufmax += charlength;
|
|
1995 buf->text->mule_bytmin += bytelength;
|
|
1996 buf->text->mule_bytmax += bytelength;
|
|
1997 }
|
|
1998 else
|
|
1999 {
|
|
2000 Charbpos end = start + charlength;
|
|
2001 /* the insertion point divides the known region in two.
|
|
2002 Keep the longer half, at least, and expand into the
|
|
2003 inserted chunk as much as possible. */
|
|
2004
|
|
2005 if (start - buf->text->mule_bufmin > buf->text->mule_bufmax - start)
|
|
2006 {
|
|
2007 Bytebpos bytestart = (buf->text->mule_bytmin
|
|
2008 + size * (start - buf->text->mule_bufmin));
|
|
2009 Bytebpos bytenew;
|
|
2010
|
|
2011 while (start < end)
|
|
2012 {
|
|
2013 bytenew = bytestart;
|
|
2014 INC_BYTEBPOS (buf, bytenew);
|
|
2015 if (bytenew - bytestart != size)
|
|
2016 break;
|
|
2017 start++;
|
|
2018 bytestart = bytenew;
|
|
2019 }
|
|
2020 if (start != end)
|
|
2021 {
|
|
2022 buf->text->mule_bufmax = start;
|
|
2023 buf->text->mule_bytmax = bytestart;
|
|
2024 }
|
|
2025 else
|
|
2026 {
|
|
2027 buf->text->mule_bufmax += charlength;
|
|
2028 buf->text->mule_bytmax += bytelength;
|
|
2029 }
|
|
2030 }
|
|
2031 else
|
|
2032 {
|
|
2033 Bytebpos byteend = (buf->text->mule_bytmin
|
|
2034 + size * (start - buf->text->mule_bufmin)
|
|
2035 + bytelength);
|
|
2036 Bytebpos bytenew;
|
|
2037
|
|
2038 buf->text->mule_bufmax += charlength;
|
|
2039 buf->text->mule_bytmax += bytelength;
|
|
2040
|
|
2041 while (end > start)
|
|
2042 {
|
|
2043 bytenew = byteend;
|
|
2044 DEC_BYTEBPOS (buf, bytenew);
|
|
2045 if (byteend - bytenew != size)
|
|
2046 break;
|
|
2047 end--;
|
|
2048 byteend = bytenew;
|
|
2049 }
|
|
2050 if (start != end)
|
|
2051 {
|
|
2052 buf->text->mule_bufmin = end;
|
|
2053 buf->text->mule_bytmin = byteend;
|
|
2054 }
|
|
2055 }
|
|
2056 }
|
|
2057 done:
|
|
2058 update_entirely_ascii_p_flag (buf);
|
|
2059 }
|
|
2060
|
|
2061 /* Text from START to END (equivalent in Bytebposs: from BI_START to
|
|
2062 BI_END) was deleted. */
|
|
2063
|
|
2064 void
|
|
2065 buffer_mule_signal_deleted_region (struct buffer *buf, Charbpos start,
|
|
2066 Charbpos end, Bytebpos bi_start,
|
|
2067 Bytebpos bi_end)
|
|
2068 {
|
|
2069 int i;
|
|
2070
|
|
2071 /* Adjust the cache of known positions. */
|
|
2072 for (i = 0; i < 16; i++)
|
|
2073 {
|
|
2074 /* After the end; gets shoved backward */
|
|
2075 if (buf->text->mule_charbpos_cache[i] > end)
|
|
2076 {
|
|
2077 buf->text->mule_charbpos_cache[i] -= end - start;
|
|
2078 buf->text->mule_bytebpos_cache[i] -= bi_end - bi_start;
|
|
2079 }
|
|
2080 /* In the range; moves to start of range */
|
|
2081 else if (buf->text->mule_charbpos_cache[i] > start)
|
|
2082 {
|
|
2083 buf->text->mule_charbpos_cache[i] = start;
|
|
2084 buf->text->mule_bytebpos_cache[i] = bi_start;
|
|
2085 }
|
|
2086 }
|
|
2087
|
|
2088 /* We don't care about any text after the end of the known region. */
|
|
2089
|
|
2090 end = min (end, buf->text->mule_bufmax);
|
|
2091 bi_end = min (bi_end, buf->text->mule_bytmax);
|
|
2092 if (start >= end)
|
|
2093 goto done;
|
|
2094
|
|
2095 /* The end of the known region offsets by the total amount of deletion,
|
|
2096 since it's all before it. */
|
|
2097
|
|
2098 buf->text->mule_bufmax -= end - start;
|
|
2099 buf->text->mule_bytmax -= bi_end - bi_start;
|
|
2100
|
|
2101 /* Now we don't care about any text after the start of the known region. */
|
|
2102
|
|
2103 end = min (end, buf->text->mule_bufmin);
|
|
2104 bi_end = min (bi_end, buf->text->mule_bytmin);
|
|
2105 if (start < end)
|
|
2106 {
|
|
2107 buf->text->mule_bufmin -= end - start;
|
|
2108 buf->text->mule_bytmin -= bi_end - bi_start;
|
|
2109 }
|
|
2110
|
|
2111 done:
|
|
2112 update_entirely_ascii_p_flag (buf);
|
|
2113 }
|
|
2114
|
|
2115 #endif /* MULE */
|
|
2116
|
800
|
2117 #ifdef ERROR_CHECK_TEXT
|
771
|
2118
|
|
2119 Bytebpos
|
|
2120 charbpos_to_bytebpos (struct buffer *buf, Charbpos x)
|
|
2121 {
|
|
2122 Bytebpos retval = real_charbpos_to_bytebpos (buf, x);
|
|
2123 ASSERT_VALID_BYTEBPOS_UNSAFE (buf, retval);
|
|
2124 return retval;
|
|
2125 }
|
|
2126
|
|
2127 Charbpos
|
|
2128 bytebpos_to_charbpos (struct buffer *buf, Bytebpos x)
|
|
2129 {
|
|
2130 ASSERT_VALID_BYTEBPOS_UNSAFE (buf, x);
|
|
2131 return real_bytebpos_to_charbpos (buf, x);
|
|
2132 }
|
|
2133
|
800
|
2134 #endif /* ERROR_CHECK_TEXT */
|
771
|
2135
|
|
2136
|
|
2137 /************************************************************************/
|
|
2138 /* verifying buffer and string positions */
|
|
2139 /************************************************************************/
|
|
2140
|
|
2141 /* Functions below are tagged with either _byte or _char indicating
|
|
2142 whether they return byte or character positions. For a buffer,
|
|
2143 a character position is a "Charbpos" and a byte position is a "Bytebpos".
|
|
2144 For strings, these are sometimes typed using "Charcount" and
|
|
2145 "Bytecount". */
|
|
2146
|
|
2147 /* Flags for the functions below are:
|
|
2148
|
|
2149 GB_ALLOW_PAST_ACCESSIBLE
|
|
2150
|
|
2151 Allow positions to range over the entire buffer (BUF_BEG to BUF_Z),
|
|
2152 rather than just the accessible portion (BUF_BEGV to BUF_ZV).
|
|
2153 For strings, this flag has no effect.
|
|
2154
|
|
2155 GB_COERCE_RANGE
|
|
2156
|
|
2157 If the position is outside the allowable range, return the lower
|
|
2158 or upper bound of the range, whichever is closer to the specified
|
|
2159 position.
|
|
2160
|
|
2161 GB_NO_ERROR_IF_BAD
|
|
2162
|
|
2163 If the position is outside the allowable range, return -1.
|
|
2164
|
|
2165 GB_NEGATIVE_FROM_END
|
|
2166
|
|
2167 If a value is negative, treat it as an offset from the end.
|
|
2168 Only applies to strings.
|
|
2169
|
|
2170 The following additional flags apply only to the functions
|
|
2171 that return ranges:
|
|
2172
|
|
2173 GB_ALLOW_NIL
|
|
2174
|
|
2175 Either or both positions can be nil. If FROM is nil,
|
|
2176 FROM_OUT will contain the lower bound of the allowed range.
|
|
2177 If TO is nil, TO_OUT will contain the upper bound of the
|
|
2178 allowed range.
|
|
2179
|
|
2180 GB_CHECK_ORDER
|
|
2181
|
|
2182 FROM must contain the lower bound and TO the upper bound
|
|
2183 of the range. If the positions are reversed, an error is
|
|
2184 signalled.
|
|
2185
|
|
2186 The following is a combination flag:
|
|
2187
|
|
2188 GB_HISTORICAL_STRING_BEHAVIOR
|
|
2189
|
|
2190 Equivalent to (GB_NEGATIVE_FROM_END | GB_ALLOW_NIL).
|
|
2191 */
|
|
2192
|
|
2193 /* Return a buffer position stored in a Lisp_Object. Full
|
|
2194 error-checking is done on the position. Flags can be specified to
|
|
2195 control the behavior of out-of-range values. The default behavior
|
|
2196 is to require that the position is within the accessible part of
|
|
2197 the buffer (BEGV and ZV), and to signal an error if the position is
|
|
2198 out of range.
|
|
2199
|
|
2200 */
|
|
2201
|
|
2202 Charbpos
|
|
2203 get_buffer_pos_char (struct buffer *b, Lisp_Object pos, unsigned int flags)
|
|
2204 {
|
|
2205 /* Does not GC */
|
|
2206 Charbpos ind;
|
|
2207 Charbpos min_allowed, max_allowed;
|
|
2208
|
|
2209 CHECK_INT_COERCE_MARKER (pos);
|
|
2210 ind = XINT (pos);
|
|
2211 min_allowed = flags & GB_ALLOW_PAST_ACCESSIBLE ? BUF_BEG (b) : BUF_BEGV (b);
|
|
2212 max_allowed = flags & GB_ALLOW_PAST_ACCESSIBLE ? BUF_Z (b) : BUF_ZV (b);
|
|
2213
|
|
2214 if (ind < min_allowed || ind > max_allowed)
|
|
2215 {
|
|
2216 if (flags & GB_COERCE_RANGE)
|
|
2217 ind = ind < min_allowed ? min_allowed : max_allowed;
|
|
2218 else if (flags & GB_NO_ERROR_IF_BAD)
|
|
2219 ind = -1;
|
|
2220 else
|
|
2221 {
|
793
|
2222 Lisp_Object buffer = wrap_buffer (b);
|
|
2223
|
771
|
2224 args_out_of_range (buffer, pos);
|
|
2225 }
|
|
2226 }
|
|
2227
|
|
2228 return ind;
|
|
2229 }
|
|
2230
|
|
2231 Bytebpos
|
|
2232 get_buffer_pos_byte (struct buffer *b, Lisp_Object pos, unsigned int flags)
|
|
2233 {
|
|
2234 Charbpos bpos = get_buffer_pos_char (b, pos, flags);
|
|
2235 if (bpos < 0) /* could happen with GB_NO_ERROR_IF_BAD */
|
|
2236 return -1;
|
|
2237 return charbpos_to_bytebpos (b, bpos);
|
|
2238 }
|
|
2239
|
|
2240 /* Return a pair of buffer positions representing a range of text,
|
|
2241 taken from a pair of Lisp_Objects. Full error-checking is
|
|
2242 done on the positions. Flags can be specified to control the
|
|
2243 behavior of out-of-range values. The default behavior is to
|
|
2244 allow the range bounds to be specified in either order
|
|
2245 (however, FROM_OUT will always be the lower bound of the range
|
|
2246 and TO_OUT the upper bound),to require that the positions
|
|
2247 are within the accessible part of the buffer (BEGV and ZV),
|
|
2248 and to signal an error if the positions are out of range.
|
|
2249 */
|
|
2250
|
|
2251 void
|
|
2252 get_buffer_range_char (struct buffer *b, Lisp_Object from, Lisp_Object to,
|
|
2253 Charbpos *from_out, Charbpos *to_out, unsigned int flags)
|
|
2254 {
|
|
2255 /* Does not GC */
|
|
2256 Charbpos min_allowed, max_allowed;
|
|
2257
|
|
2258 min_allowed = (flags & GB_ALLOW_PAST_ACCESSIBLE) ?
|
|
2259 BUF_BEG (b) : BUF_BEGV (b);
|
|
2260 max_allowed = (flags & GB_ALLOW_PAST_ACCESSIBLE) ?
|
|
2261 BUF_Z (b) : BUF_ZV (b);
|
|
2262
|
|
2263 if (NILP (from) && (flags & GB_ALLOW_NIL))
|
|
2264 *from_out = min_allowed;
|
|
2265 else
|
|
2266 *from_out = get_buffer_pos_char (b, from, flags | GB_NO_ERROR_IF_BAD);
|
|
2267
|
|
2268 if (NILP (to) && (flags & GB_ALLOW_NIL))
|
|
2269 *to_out = max_allowed;
|
|
2270 else
|
|
2271 *to_out = get_buffer_pos_char (b, to, flags | GB_NO_ERROR_IF_BAD);
|
|
2272
|
|
2273 if ((*from_out < 0 || *to_out < 0) && !(flags & GB_NO_ERROR_IF_BAD))
|
|
2274 {
|
793
|
2275 Lisp_Object buffer = wrap_buffer (b);
|
|
2276
|
771
|
2277 args_out_of_range_3 (buffer, from, to);
|
|
2278 }
|
|
2279
|
|
2280 if (*from_out >= 0 && *to_out >= 0 && *from_out > *to_out)
|
|
2281 {
|
|
2282 if (flags & GB_CHECK_ORDER)
|
|
2283 invalid_argument_2 ("start greater than end", from, to);
|
|
2284 else
|
|
2285 {
|
|
2286 Charbpos temp = *from_out;
|
|
2287 *from_out = *to_out;
|
|
2288 *to_out = temp;
|
|
2289 }
|
|
2290 }
|
|
2291 }
|
|
2292
|
|
2293 void
|
|
2294 get_buffer_range_byte (struct buffer *b, Lisp_Object from, Lisp_Object to,
|
|
2295 Bytebpos *from_out, Bytebpos *to_out, unsigned int flags)
|
|
2296 {
|
|
2297 Charbpos s, e;
|
|
2298
|
|
2299 get_buffer_range_char (b, from, to, &s, &e, flags);
|
|
2300 if (s >= 0)
|
|
2301 *from_out = charbpos_to_bytebpos (b, s);
|
|
2302 else /* could happen with GB_NO_ERROR_IF_BAD */
|
|
2303 *from_out = -1;
|
|
2304 if (e >= 0)
|
|
2305 *to_out = charbpos_to_bytebpos (b, e);
|
|
2306 else
|
|
2307 *to_out = -1;
|
|
2308 }
|
|
2309
|
|
2310 static Charcount
|
|
2311 get_string_pos_char_1 (Lisp_Object string, Lisp_Object pos, unsigned int flags,
|
|
2312 Charcount known_length)
|
|
2313 {
|
|
2314 Charcount ccpos;
|
|
2315 Charcount min_allowed = 0;
|
|
2316 Charcount max_allowed = known_length;
|
|
2317
|
|
2318 /* Computation of KNOWN_LENGTH is potentially expensive so we pass
|
|
2319 it in. */
|
|
2320 CHECK_INT (pos);
|
|
2321 ccpos = XINT (pos);
|
|
2322 if (ccpos < 0 && flags & GB_NEGATIVE_FROM_END)
|
|
2323 ccpos += max_allowed;
|
|
2324
|
|
2325 if (ccpos < min_allowed || ccpos > max_allowed)
|
|
2326 {
|
|
2327 if (flags & GB_COERCE_RANGE)
|
|
2328 ccpos = ccpos < min_allowed ? min_allowed : max_allowed;
|
|
2329 else if (flags & GB_NO_ERROR_IF_BAD)
|
|
2330 ccpos = -1;
|
|
2331 else
|
|
2332 args_out_of_range (string, pos);
|
|
2333 }
|
|
2334
|
|
2335 return ccpos;
|
|
2336 }
|
|
2337
|
|
2338 Charcount
|
|
2339 get_string_pos_char (Lisp_Object string, Lisp_Object pos, unsigned int flags)
|
|
2340 {
|
|
2341 return get_string_pos_char_1 (string, pos, flags,
|
|
2342 XSTRING_CHAR_LENGTH (string));
|
|
2343 }
|
|
2344
|
|
2345 Bytecount
|
|
2346 get_string_pos_byte (Lisp_Object string, Lisp_Object pos, unsigned int flags)
|
|
2347 {
|
|
2348 Charcount ccpos = get_string_pos_char (string, pos, flags);
|
|
2349 if (ccpos < 0) /* could happen with GB_NO_ERROR_IF_BAD */
|
|
2350 return -1;
|
793
|
2351 return string_index_char_to_byte (string, ccpos);
|
771
|
2352 }
|
|
2353
|
|
2354 void
|
|
2355 get_string_range_char (Lisp_Object string, Lisp_Object from, Lisp_Object to,
|
|
2356 Charcount *from_out, Charcount *to_out,
|
|
2357 unsigned int flags)
|
|
2358 {
|
|
2359 Charcount min_allowed = 0;
|
|
2360 Charcount max_allowed = XSTRING_CHAR_LENGTH (string);
|
|
2361
|
|
2362 if (NILP (from) && (flags & GB_ALLOW_NIL))
|
|
2363 *from_out = min_allowed;
|
|
2364 else
|
|
2365 *from_out = get_string_pos_char_1 (string, from,
|
|
2366 flags | GB_NO_ERROR_IF_BAD,
|
|
2367 max_allowed);
|
|
2368
|
|
2369 if (NILP (to) && (flags & GB_ALLOW_NIL))
|
|
2370 *to_out = max_allowed;
|
|
2371 else
|
|
2372 *to_out = get_string_pos_char_1 (string, to,
|
|
2373 flags | GB_NO_ERROR_IF_BAD,
|
|
2374 max_allowed);
|
|
2375
|
|
2376 if ((*from_out < 0 || *to_out < 0) && !(flags & GB_NO_ERROR_IF_BAD))
|
|
2377 args_out_of_range_3 (string, from, to);
|
|
2378
|
|
2379 if (*from_out >= 0 && *to_out >= 0 && *from_out > *to_out)
|
|
2380 {
|
|
2381 if (flags & GB_CHECK_ORDER)
|
|
2382 invalid_argument_2 ("start greater than end", from, to);
|
|
2383 else
|
|
2384 {
|
|
2385 Charbpos temp = *from_out;
|
|
2386 *from_out = *to_out;
|
|
2387 *to_out = temp;
|
|
2388 }
|
|
2389 }
|
|
2390 }
|
|
2391
|
|
2392 void
|
|
2393 get_string_range_byte (Lisp_Object string, Lisp_Object from, Lisp_Object to,
|
|
2394 Bytecount *from_out, Bytecount *to_out,
|
|
2395 unsigned int flags)
|
|
2396 {
|
|
2397 Charcount s, e;
|
|
2398
|
|
2399 get_string_range_char (string, from, to, &s, &e, flags);
|
|
2400 if (s >= 0)
|
793
|
2401 *from_out = string_index_char_to_byte (string, s);
|
771
|
2402 else /* could happen with GB_NO_ERROR_IF_BAD */
|
|
2403 *from_out = -1;
|
|
2404 if (e >= 0)
|
793
|
2405 *to_out = string_index_char_to_byte (string, e);
|
771
|
2406 else
|
|
2407 *to_out = -1;
|
|
2408
|
|
2409 }
|
|
2410
|
|
2411 Charbpos
|
|
2412 get_buffer_or_string_pos_char (Lisp_Object object, Lisp_Object pos,
|
|
2413 unsigned int flags)
|
|
2414 {
|
|
2415 return STRINGP (object) ?
|
|
2416 get_string_pos_char (object, pos, flags) :
|
|
2417 get_buffer_pos_char (XBUFFER (object), pos, flags);
|
|
2418 }
|
|
2419
|
|
2420 Bytebpos
|
|
2421 get_buffer_or_string_pos_byte (Lisp_Object object, Lisp_Object pos,
|
|
2422 unsigned int flags)
|
|
2423 {
|
|
2424 return STRINGP (object) ?
|
|
2425 get_string_pos_byte (object, pos, flags) :
|
|
2426 get_buffer_pos_byte (XBUFFER (object), pos, flags);
|
|
2427 }
|
|
2428
|
|
2429 void
|
|
2430 get_buffer_or_string_range_char (Lisp_Object object, Lisp_Object from,
|
|
2431 Lisp_Object to, Charbpos *from_out,
|
|
2432 Charbpos *to_out, unsigned int flags)
|
|
2433 {
|
|
2434 if (STRINGP (object))
|
|
2435 get_string_range_char (object, from, to, from_out, to_out, flags);
|
|
2436 else
|
|
2437 get_buffer_range_char (XBUFFER (object), from, to, from_out, to_out, flags);
|
|
2438 }
|
|
2439
|
|
2440 void
|
|
2441 get_buffer_or_string_range_byte (Lisp_Object object, Lisp_Object from,
|
|
2442 Lisp_Object to, Bytebpos *from_out,
|
|
2443 Bytebpos *to_out, unsigned int flags)
|
|
2444 {
|
|
2445 if (STRINGP (object))
|
|
2446 get_string_range_byte (object, from, to, from_out, to_out, flags);
|
|
2447 else
|
|
2448 get_buffer_range_byte (XBUFFER (object), from, to, from_out, to_out, flags);
|
|
2449 }
|
|
2450
|
|
2451 Charbpos
|
|
2452 buffer_or_string_accessible_begin_char (Lisp_Object object)
|
|
2453 {
|
|
2454 return STRINGP (object) ? 0 : BUF_BEGV (XBUFFER (object));
|
|
2455 }
|
|
2456
|
|
2457 Charbpos
|
|
2458 buffer_or_string_accessible_end_char (Lisp_Object object)
|
|
2459 {
|
|
2460 return STRINGP (object) ?
|
|
2461 XSTRING_CHAR_LENGTH (object) : BUF_ZV (XBUFFER (object));
|
|
2462 }
|
|
2463
|
|
2464 Bytebpos
|
|
2465 buffer_or_string_accessible_begin_byte (Lisp_Object object)
|
|
2466 {
|
|
2467 return STRINGP (object) ? 0 : BI_BUF_BEGV (XBUFFER (object));
|
|
2468 }
|
|
2469
|
|
2470 Bytebpos
|
|
2471 buffer_or_string_accessible_end_byte (Lisp_Object object)
|
|
2472 {
|
|
2473 return STRINGP (object) ?
|
|
2474 XSTRING_LENGTH (object) : BI_BUF_ZV (XBUFFER (object));
|
|
2475 }
|
|
2476
|
|
2477 Charbpos
|
|
2478 buffer_or_string_absolute_begin_char (Lisp_Object object)
|
|
2479 {
|
|
2480 return STRINGP (object) ? 0 : BUF_BEG (XBUFFER (object));
|
|
2481 }
|
|
2482
|
|
2483 Charbpos
|
|
2484 buffer_or_string_absolute_end_char (Lisp_Object object)
|
|
2485 {
|
|
2486 return STRINGP (object) ?
|
|
2487 XSTRING_CHAR_LENGTH (object) : BUF_Z (XBUFFER (object));
|
|
2488 }
|
|
2489
|
|
2490 Bytebpos
|
|
2491 buffer_or_string_absolute_begin_byte (Lisp_Object object)
|
|
2492 {
|
|
2493 return STRINGP (object) ? 0 : BI_BUF_BEG (XBUFFER (object));
|
|
2494 }
|
|
2495
|
|
2496 Bytebpos
|
|
2497 buffer_or_string_absolute_end_byte (Lisp_Object object)
|
|
2498 {
|
|
2499 return STRINGP (object) ?
|
|
2500 XSTRING_LENGTH (object) : BI_BUF_Z (XBUFFER (object));
|
|
2501 }
|
|
2502
|
|
2503
|
|
2504 /************************************************************************/
|
|
2505 /* Implement TO_EXTERNAL_FORMAT, TO_INTERNAL_FORMAT */
|
|
2506 /************************************************************************/
|
|
2507
|
|
2508 typedef struct
|
|
2509 {
|
|
2510 Dynarr_declare (Intbyte_dynarr *);
|
|
2511 } Intbyte_dynarr_dynarr;
|
|
2512
|
|
2513 typedef struct
|
|
2514 {
|
|
2515 Dynarr_declare (Extbyte_dynarr *);
|
|
2516 } Extbyte_dynarr_dynarr;
|
|
2517
|
|
2518 static Extbyte_dynarr_dynarr *conversion_out_dynarr_list;
|
|
2519 static Intbyte_dynarr_dynarr *conversion_in_dynarr_list;
|
|
2520
|
|
2521 static int dfc_convert_to_external_format_in_use;
|
|
2522 static int dfc_convert_to_internal_format_in_use;
|
|
2523
|
|
2524 static Lisp_Object
|
|
2525 dfc_convert_to_external_format_reset_in_use (Lisp_Object value)
|
|
2526 {
|
|
2527 dfc_convert_to_external_format_in_use = XINT (value);
|
|
2528 return Qnil;
|
|
2529 }
|
|
2530
|
|
2531 static Lisp_Object
|
|
2532 dfc_convert_to_internal_format_reset_in_use (Lisp_Object value)
|
|
2533 {
|
|
2534 dfc_convert_to_internal_format_in_use = XINT (value);
|
|
2535 return Qnil;
|
|
2536 }
|
|
2537
|
|
2538 void
|
|
2539 dfc_convert_to_external_format (dfc_conversion_type source_type,
|
|
2540 dfc_conversion_data *source,
|
|
2541 Lisp_Object coding_system,
|
|
2542 dfc_conversion_type sink_type,
|
|
2543 dfc_conversion_data *sink)
|
|
2544 {
|
|
2545 /* It's guaranteed that many callers are not prepared for GC here,
|
|
2546 esp. given that this code conversion occurs in many very hidden
|
|
2547 places. */
|
|
2548 int count = begin_gc_forbidden ();
|
|
2549 Extbyte_dynarr *conversion_out_dynarr;
|
|
2550
|
|
2551 type_checking_assert
|
|
2552 (((source_type == DFC_TYPE_DATA) ||
|
|
2553 (source_type == DFC_TYPE_LISP_LSTREAM && LSTREAMP (source->lisp_object)) ||
|
|
2554 (source_type == DFC_TYPE_LISP_STRING && STRINGP (source->lisp_object)))
|
|
2555 &&
|
|
2556 ((sink_type == DFC_TYPE_DATA) ||
|
|
2557 (sink_type == DFC_TYPE_LISP_LSTREAM && LSTREAMP (source->lisp_object))));
|
|
2558
|
|
2559 record_unwind_protect (dfc_convert_to_external_format_reset_in_use,
|
|
2560 make_int (dfc_convert_to_external_format_in_use));
|
|
2561 if (Dynarr_length (conversion_out_dynarr_list) <=
|
|
2562 dfc_convert_to_external_format_in_use)
|
|
2563 Dynarr_add (conversion_out_dynarr_list, Dynarr_new (Extbyte));
|
|
2564 conversion_out_dynarr = Dynarr_at (conversion_out_dynarr_list,
|
|
2565 dfc_convert_to_external_format_in_use);
|
|
2566 dfc_convert_to_external_format_in_use++;
|
|
2567 Dynarr_reset (conversion_out_dynarr);
|
|
2568
|
|
2569 coding_system = get_coding_system_for_text_file (coding_system, 0);
|
|
2570
|
|
2571 /* Here we optimize in the case where the coding system does no
|
|
2572 conversion. However, we don't want to optimize in case the source
|
|
2573 or sink is an lstream, since writing to an lstream can cause a
|
|
2574 garbage collection, and this could be problematic if the source
|
|
2575 is a lisp string. */
|
|
2576 if (source_type != DFC_TYPE_LISP_LSTREAM &&
|
|
2577 sink_type != DFC_TYPE_LISP_LSTREAM &&
|
|
2578 coding_system_is_binary (coding_system))
|
|
2579 {
|
|
2580 const Intbyte *ptr;
|
|
2581 Bytecount len;
|
|
2582
|
|
2583 if (source_type == DFC_TYPE_LISP_STRING)
|
|
2584 {
|
|
2585 ptr = XSTRING_DATA (source->lisp_object);
|
|
2586 len = XSTRING_LENGTH (source->lisp_object);
|
|
2587 }
|
|
2588 else
|
|
2589 {
|
|
2590 ptr = (Intbyte *) source->data.ptr;
|
|
2591 len = source->data.len;
|
|
2592 }
|
|
2593
|
|
2594 #ifdef MULE
|
|
2595 {
|
|
2596 const Intbyte *end;
|
|
2597 for (end = ptr + len; ptr < end;)
|
|
2598 {
|
|
2599 Intbyte c =
|
|
2600 (BYTE_ASCII_P (*ptr)) ? *ptr :
|
|
2601 (*ptr == LEADING_BYTE_CONTROL_1) ? (*(ptr+1) - 0x20) :
|
|
2602 (*ptr == LEADING_BYTE_LATIN_ISO8859_1) ? (*(ptr+1)) :
|
|
2603 '~';
|
|
2604
|
|
2605 Dynarr_add (conversion_out_dynarr, (Extbyte) c);
|
|
2606 INC_CHARPTR (ptr);
|
|
2607 }
|
800
|
2608 text_checking_assert (ptr == end);
|
771
|
2609 }
|
|
2610 #else
|
|
2611 Dynarr_add_many (conversion_out_dynarr, ptr, len);
|
|
2612 #endif
|
|
2613
|
|
2614 }
|
|
2615 #ifdef HAVE_WIN32_CODING_SYSTEMS
|
|
2616 /* Optimize the common case involving Unicode where only ASCII is involved */
|
|
2617 else if (source_type != DFC_TYPE_LISP_LSTREAM &&
|
|
2618 sink_type != DFC_TYPE_LISP_LSTREAM &&
|
|
2619 dfc_coding_system_is_unicode (coding_system))
|
|
2620 {
|
|
2621 const Intbyte *ptr, *p;
|
|
2622 Bytecount len;
|
|
2623 const Intbyte *end;
|
|
2624
|
|
2625 if (source_type == DFC_TYPE_LISP_STRING)
|
|
2626 {
|
|
2627 ptr = XSTRING_DATA (source->lisp_object);
|
|
2628 len = XSTRING_LENGTH (source->lisp_object);
|
|
2629 }
|
|
2630 else
|
|
2631 {
|
|
2632 ptr = (Intbyte *) source->data.ptr;
|
|
2633 len = source->data.len;
|
|
2634 }
|
|
2635 end = ptr + len;
|
|
2636
|
|
2637 for (p = ptr; p < end; p++)
|
|
2638 {
|
|
2639 if (!BYTE_ASCII_P (*p))
|
|
2640 goto the_hard_way;
|
|
2641 }
|
|
2642
|
|
2643 for (p = ptr; p < end; p++)
|
|
2644 {
|
|
2645 Dynarr_add (conversion_out_dynarr, (Extbyte) (*p));
|
|
2646 Dynarr_add (conversion_out_dynarr, (Extbyte) '\0');
|
|
2647 }
|
|
2648 }
|
|
2649 #endif /* HAVE_WIN32_CODING_SYSTEMS */
|
|
2650 else
|
|
2651 {
|
|
2652 Lisp_Object streams_to_delete[3];
|
|
2653 int delete_count;
|
|
2654 Lisp_Object instream, outstream;
|
|
2655 Lstream *reader, *writer;
|
|
2656 struct gcpro gcpro1, gcpro2;
|
|
2657
|
|
2658 #ifdef HAVE_WIN32_CODING_SYSTEMS
|
|
2659 the_hard_way:
|
|
2660 #endif /* HAVE_WIN32_CODING_SYSTEMS */
|
|
2661 delete_count = 0;
|
|
2662 if (source_type == DFC_TYPE_LISP_LSTREAM)
|
|
2663 instream = source->lisp_object;
|
|
2664 else if (source_type == DFC_TYPE_DATA)
|
|
2665 streams_to_delete[delete_count++] = instream =
|
|
2666 make_fixed_buffer_input_stream (source->data.ptr, source->data.len);
|
|
2667 else
|
|
2668 {
|
|
2669 type_checking_assert (source_type == DFC_TYPE_LISP_STRING);
|
|
2670 streams_to_delete[delete_count++] = instream =
|
|
2671 /* This will GCPRO the Lisp string */
|
|
2672 make_lisp_string_input_stream (source->lisp_object, 0, -1);
|
|
2673 }
|
|
2674
|
|
2675 if (sink_type == DFC_TYPE_LISP_LSTREAM)
|
|
2676 outstream = sink->lisp_object;
|
|
2677 else
|
|
2678 {
|
|
2679 type_checking_assert (sink_type == DFC_TYPE_DATA);
|
|
2680 streams_to_delete[delete_count++] = outstream =
|
|
2681 make_dynarr_output_stream
|
|
2682 ((unsigned_char_dynarr *) conversion_out_dynarr);
|
|
2683 }
|
|
2684
|
|
2685 streams_to_delete[delete_count++] = outstream =
|
800
|
2686 make_coding_output_stream (XLSTREAM (outstream), coding_system,
|
|
2687 CODING_ENCODE, 0);
|
771
|
2688
|
|
2689 reader = XLSTREAM (instream);
|
|
2690 writer = XLSTREAM (outstream);
|
|
2691 /* decoding_stream will gc-protect outstream */
|
|
2692 GCPRO2 (instream, outstream);
|
|
2693
|
|
2694 while (1)
|
|
2695 {
|
|
2696 Bytecount size_in_bytes;
|
|
2697 char tempbuf[1024]; /* some random amount */
|
|
2698
|
|
2699 size_in_bytes = Lstream_read (reader, tempbuf, sizeof (tempbuf));
|
|
2700
|
|
2701 if (size_in_bytes == 0)
|
|
2702 break;
|
|
2703 else if (size_in_bytes < 0)
|
|
2704 signal_error (Qtext_conversion_error,
|
|
2705 "Error converting to external format", Qunbound);
|
|
2706
|
|
2707 if (Lstream_write (writer, tempbuf, size_in_bytes) < 0)
|
|
2708 signal_error (Qtext_conversion_error,
|
|
2709 "Error converting to external format", Qunbound);
|
|
2710 }
|
|
2711
|
|
2712 /* Closing writer will close any stream at the other end of writer. */
|
|
2713 Lstream_close (writer);
|
|
2714 Lstream_close (reader);
|
|
2715 UNGCPRO;
|
|
2716
|
|
2717 /* The idea is that this function will create no garbage. */
|
|
2718 while (delete_count)
|
|
2719 Lstream_delete (XLSTREAM (streams_to_delete [--delete_count]));
|
|
2720 }
|
|
2721
|
|
2722 unbind_to (count);
|
|
2723
|
|
2724 if (sink_type != DFC_TYPE_LISP_LSTREAM)
|
|
2725 {
|
|
2726 sink->data.len = Dynarr_length (conversion_out_dynarr);
|
|
2727 /* double zero-extend because we may be dealing with Unicode data */
|
|
2728 Dynarr_add (conversion_out_dynarr, '\0');
|
|
2729 Dynarr_add (conversion_out_dynarr, '\0');
|
|
2730 sink->data.ptr = Dynarr_atp (conversion_out_dynarr, 0);
|
|
2731 }
|
|
2732 }
|
|
2733
|
|
2734 void
|
|
2735 dfc_convert_to_internal_format (dfc_conversion_type source_type,
|
|
2736 dfc_conversion_data *source,
|
|
2737 Lisp_Object coding_system,
|
|
2738 dfc_conversion_type sink_type,
|
|
2739 dfc_conversion_data *sink)
|
|
2740 {
|
|
2741 /* It's guaranteed that many callers are not prepared for GC here,
|
|
2742 esp. given that this code conversion occurs in many very hidden
|
|
2743 places. */
|
|
2744 int count = begin_gc_forbidden ();
|
|
2745 Intbyte_dynarr *conversion_in_dynarr;
|
|
2746
|
|
2747 type_checking_assert
|
|
2748 ((source_type == DFC_TYPE_DATA ||
|
|
2749 source_type == DFC_TYPE_LISP_LSTREAM)
|
|
2750 &&
|
|
2751 (sink_type == DFC_TYPE_DATA ||
|
|
2752 sink_type == DFC_TYPE_LISP_LSTREAM));
|
|
2753
|
|
2754 record_unwind_protect (dfc_convert_to_internal_format_reset_in_use,
|
|
2755 make_int (dfc_convert_to_internal_format_in_use));
|
|
2756 if (Dynarr_length (conversion_in_dynarr_list) <=
|
|
2757 dfc_convert_to_internal_format_in_use)
|
|
2758 Dynarr_add (conversion_in_dynarr_list, Dynarr_new (Intbyte));
|
|
2759 conversion_in_dynarr = Dynarr_at (conversion_in_dynarr_list,
|
|
2760 dfc_convert_to_internal_format_in_use);
|
|
2761 dfc_convert_to_internal_format_in_use++;
|
|
2762 Dynarr_reset (conversion_in_dynarr);
|
|
2763
|
|
2764 coding_system = get_coding_system_for_text_file (coding_system, 1);
|
|
2765
|
|
2766 if (source_type != DFC_TYPE_LISP_LSTREAM &&
|
|
2767 sink_type != DFC_TYPE_LISP_LSTREAM &&
|
|
2768 coding_system_is_binary (coding_system))
|
|
2769 {
|
|
2770 #ifdef MULE
|
|
2771 const Intbyte *ptr = (const Intbyte *) source->data.ptr;
|
|
2772 Bytecount len = source->data.len;
|
|
2773 const Intbyte *end = ptr + len;
|
|
2774
|
|
2775 for (; ptr < end; ptr++)
|
|
2776 {
|
|
2777 Intbyte c = *ptr;
|
|
2778
|
|
2779 if (BYTE_ASCII_P (c))
|
|
2780 Dynarr_add (conversion_in_dynarr, c);
|
|
2781 else if (BYTE_C1_P (c))
|
|
2782 {
|
|
2783 Dynarr_add (conversion_in_dynarr, LEADING_BYTE_CONTROL_1);
|
|
2784 Dynarr_add (conversion_in_dynarr, c + 0x20);
|
|
2785 }
|
|
2786 else
|
|
2787 {
|
|
2788 Dynarr_add (conversion_in_dynarr, LEADING_BYTE_LATIN_ISO8859_1);
|
|
2789 Dynarr_add (conversion_in_dynarr, c);
|
|
2790 }
|
|
2791 }
|
|
2792 #else
|
|
2793 Dynarr_add_many (conversion_in_dynarr, source->data.ptr, source->data.len);
|
|
2794 #endif
|
|
2795 }
|
|
2796 #ifdef HAVE_WIN32_CODING_SYSTEMS
|
|
2797 /* Optimize the common case involving Unicode where only ASCII/Latin-1 is involved */
|
|
2798 else if (source_type != DFC_TYPE_LISP_LSTREAM &&
|
|
2799 sink_type != DFC_TYPE_LISP_LSTREAM &&
|
|
2800 dfc_coding_system_is_unicode (coding_system))
|
|
2801 {
|
|
2802 const Intbyte *ptr = (const Intbyte *) source->data.ptr + 1;
|
|
2803 Bytecount len = source->data.len;
|
|
2804 const Intbyte *end = ptr + len;
|
|
2805
|
|
2806 if (len & 1)
|
|
2807 goto the_hard_way;
|
|
2808
|
|
2809 for (; ptr < end; ptr += 2)
|
|
2810 {
|
|
2811 if (*ptr)
|
|
2812 goto the_hard_way;
|
|
2813 }
|
|
2814
|
|
2815 ptr = (const Intbyte *) source->data.ptr;
|
|
2816 end = ptr + len;
|
|
2817
|
|
2818 for (; ptr < end; ptr += 2)
|
|
2819 {
|
|
2820 Intbyte c = *ptr;
|
|
2821
|
|
2822 if (BYTE_ASCII_P (c))
|
|
2823 Dynarr_add (conversion_in_dynarr, c);
|
|
2824 #ifdef MULE
|
|
2825 else if (BYTE_C1_P (c))
|
|
2826 {
|
|
2827 Dynarr_add (conversion_in_dynarr, LEADING_BYTE_CONTROL_1);
|
|
2828 Dynarr_add (conversion_in_dynarr, c + 0x20);
|
|
2829 }
|
|
2830 else
|
|
2831 {
|
|
2832 Dynarr_add (conversion_in_dynarr, LEADING_BYTE_LATIN_ISO8859_1);
|
|
2833 Dynarr_add (conversion_in_dynarr, c);
|
|
2834 }
|
|
2835 #endif /* MULE */
|
|
2836 }
|
|
2837 }
|
|
2838 #endif /* HAVE_WIN32_CODING_SYSTEMS */
|
|
2839 else
|
|
2840 {
|
|
2841 Lisp_Object streams_to_delete[3];
|
|
2842 int delete_count;
|
|
2843 Lisp_Object instream, outstream;
|
|
2844 Lstream *reader, *writer;
|
|
2845 struct gcpro gcpro1, gcpro2;
|
|
2846
|
|
2847 #ifdef HAVE_WIN32_CODING_SYSTEMS
|
|
2848 the_hard_way:
|
|
2849 #endif /* HAVE_WIN32_CODING_SYSTEMS */
|
|
2850 delete_count = 0;
|
|
2851 if (source_type == DFC_TYPE_LISP_LSTREAM)
|
|
2852 instream = source->lisp_object;
|
|
2853 else
|
|
2854 {
|
|
2855 type_checking_assert (source_type == DFC_TYPE_DATA);
|
|
2856 streams_to_delete[delete_count++] = instream =
|
|
2857 make_fixed_buffer_input_stream (source->data.ptr, source->data.len);
|
|
2858 }
|
|
2859
|
|
2860 if (sink_type == DFC_TYPE_LISP_LSTREAM)
|
|
2861 outstream = sink->lisp_object;
|
|
2862 else
|
|
2863 {
|
|
2864 type_checking_assert (sink_type == DFC_TYPE_DATA);
|
|
2865 streams_to_delete[delete_count++] = outstream =
|
|
2866 make_dynarr_output_stream
|
|
2867 ((unsigned_char_dynarr *) conversion_in_dynarr);
|
|
2868 }
|
|
2869
|
|
2870 streams_to_delete[delete_count++] = outstream =
|
800
|
2871 make_coding_output_stream (XLSTREAM (outstream), coding_system,
|
|
2872 CODING_DECODE, 0);
|
771
|
2873
|
|
2874 reader = XLSTREAM (instream);
|
|
2875 writer = XLSTREAM (outstream);
|
|
2876 /* outstream will gc-protect its sink stream, if necessary */
|
|
2877 GCPRO2 (instream, outstream);
|
|
2878
|
|
2879 while (1)
|
|
2880 {
|
|
2881 Bytecount size_in_bytes;
|
|
2882 char tempbuf[1024]; /* some random amount */
|
|
2883
|
|
2884 size_in_bytes = Lstream_read (reader, tempbuf, sizeof (tempbuf));
|
|
2885
|
|
2886 if (size_in_bytes == 0)
|
|
2887 break;
|
|
2888 else if (size_in_bytes < 0)
|
|
2889 signal_error (Qtext_conversion_error,
|
|
2890 "Error converting to internal format", Qunbound);
|
|
2891
|
|
2892 if (Lstream_write (writer, tempbuf, size_in_bytes) < 0)
|
|
2893 signal_error (Qtext_conversion_error,
|
|
2894 "Error converting to internal format", Qunbound);
|
|
2895 }
|
|
2896
|
|
2897 /* Closing writer will close any stream at the other end of writer. */
|
|
2898 Lstream_close (writer);
|
|
2899 Lstream_close (reader);
|
|
2900 UNGCPRO;
|
|
2901
|
|
2902 /* The idea is that this function will create no garbage. */
|
|
2903 while (delete_count)
|
|
2904 Lstream_delete (XLSTREAM (streams_to_delete [--delete_count]));
|
|
2905 }
|
|
2906
|
|
2907 unbind_to (count);
|
|
2908
|
|
2909 if (sink_type != DFC_TYPE_LISP_LSTREAM)
|
|
2910 {
|
|
2911 sink->data.len = Dynarr_length (conversion_in_dynarr);
|
|
2912 Dynarr_add (conversion_in_dynarr, '\0'); /* remember to NUL-terminate! */
|
|
2913 /* The macros don't currently distinguish between internal and
|
|
2914 external sinks, and allocate and copy two extra bytes in both
|
|
2915 cases. So we add a second zero, just like for external data
|
|
2916 (in that case, because we may be converting to Unicode). */
|
|
2917 Dynarr_add (conversion_in_dynarr, '\0');
|
|
2918 sink->data.ptr = Dynarr_atp (conversion_in_dynarr, 0);
|
|
2919 }
|
|
2920 }
|
|
2921
|
|
2922
|
|
2923 /************************************************************************/
|
|
2924 /* Basic Emchar functions */
|
|
2925 /************************************************************************/
|
|
2926
|
|
2927 #ifdef MULE
|
|
2928
|
|
2929 /* Convert a non-ASCII Mule character C into a one-character Mule-encoded
|
|
2930 string in STR. Returns the number of bytes stored.
|
|
2931 Do not call this directly. Use the macro set_charptr_emchar() instead.
|
|
2932 */
|
|
2933
|
|
2934 Bytecount
|
|
2935 non_ascii_set_charptr_emchar (Intbyte *str, Emchar c)
|
|
2936 {
|
|
2937 Intbyte *p;
|
|
2938 Intbyte lb;
|
|
2939 int c1, c2;
|
|
2940 Lisp_Object charset;
|
|
2941
|
|
2942 p = str;
|
|
2943 BREAKUP_CHAR (c, charset, c1, c2);
|
|
2944 lb = CHAR_LEADING_BYTE (c);
|
|
2945 if (LEADING_BYTE_PRIVATE_P (lb))
|
|
2946 *p++ = PRIVATE_LEADING_BYTE_PREFIX (lb);
|
|
2947 *p++ = lb;
|
|
2948 if (EQ (charset, Vcharset_control_1))
|
|
2949 c1 += 0x20;
|
|
2950 *p++ = c1 | 0x80;
|
|
2951 if (c2)
|
|
2952 *p++ = c2 | 0x80;
|
|
2953
|
|
2954 return (p - str);
|
|
2955 }
|
|
2956
|
|
2957 /* Return the first character from a Mule-encoded string in STR,
|
|
2958 assuming it's non-ASCII. Do not call this directly.
|
|
2959 Use the macro charptr_emchar() instead. */
|
|
2960
|
|
2961 Emchar
|
|
2962 non_ascii_charptr_emchar (const Intbyte *str)
|
|
2963 {
|
|
2964 Intbyte i0 = *str, i1, i2 = 0;
|
|
2965 Lisp_Object charset;
|
|
2966
|
|
2967 if (i0 == LEADING_BYTE_CONTROL_1)
|
|
2968 return (Emchar) (*++str - 0x20);
|
|
2969
|
|
2970 if (LEADING_BYTE_PREFIX_P (i0))
|
|
2971 i0 = *++str;
|
|
2972
|
|
2973 i1 = *++str & 0x7F;
|
|
2974
|
|
2975 charset = CHARSET_BY_LEADING_BYTE (i0);
|
|
2976 if (XCHARSET_DIMENSION (charset) == 2)
|
|
2977 i2 = *++str & 0x7F;
|
|
2978
|
|
2979 return MAKE_CHAR (charset, i1, i2);
|
|
2980 }
|
|
2981
|
|
2982 /* Return whether CH is a valid Emchar, assuming it's non-ASCII.
|
|
2983 Do not call this directly. Use the macro valid_char_p() instead. */
|
|
2984
|
|
2985 int
|
|
2986 non_ascii_valid_char_p (Emchar ch)
|
|
2987 {
|
|
2988 int f1, f2, f3;
|
|
2989
|
|
2990 /* Must have only lowest 19 bits set */
|
|
2991 if (ch & ~0x7FFFF)
|
|
2992 return 0;
|
|
2993
|
|
2994 f1 = CHAR_FIELD1 (ch);
|
|
2995 f2 = CHAR_FIELD2 (ch);
|
|
2996 f3 = CHAR_FIELD3 (ch);
|
|
2997
|
|
2998 if (f1 == 0)
|
|
2999 {
|
|
3000 /* dimension-1 char */
|
|
3001 Lisp_Object charset;
|
|
3002
|
|
3003 /* leading byte must be correct */
|
|
3004 if (f2 < MIN_CHAR_FIELD2_OFFICIAL ||
|
|
3005 (f2 > MAX_CHAR_FIELD2_OFFICIAL && f2 < MIN_CHAR_FIELD2_PRIVATE) ||
|
|
3006 f2 > MAX_CHAR_FIELD2_PRIVATE)
|
|
3007 return 0;
|
|
3008 /* octet not out of range */
|
|
3009 if (f3 < 0x20)
|
|
3010 return 0;
|
|
3011 /* charset exists */
|
|
3012 /*
|
|
3013 NOTE: This takes advantage of the fact that
|
|
3014 FIELD2_TO_OFFICIAL_LEADING_BYTE and
|
|
3015 FIELD2_TO_PRIVATE_LEADING_BYTE are the same.
|
|
3016 */
|
|
3017 charset = CHARSET_BY_LEADING_BYTE (f2 + FIELD2_TO_OFFICIAL_LEADING_BYTE);
|
|
3018 if (EQ (charset, Qnil))
|
|
3019 return 0;
|
|
3020 /* check range as per size (94 or 96) of charset */
|
|
3021 return ((f3 > 0x20 && f3 < 0x7f) || XCHARSET_CHARS (charset) == 96);
|
|
3022 }
|
|
3023 else
|
|
3024 {
|
|
3025 /* dimension-2 char */
|
|
3026 Lisp_Object charset;
|
|
3027
|
|
3028 /* leading byte must be correct */
|
|
3029 if (f1 < MIN_CHAR_FIELD1_OFFICIAL ||
|
|
3030 (f1 > MAX_CHAR_FIELD1_OFFICIAL && f1 < MIN_CHAR_FIELD1_PRIVATE) ||
|
|
3031 f1 > MAX_CHAR_FIELD1_PRIVATE)
|
|
3032 return 0;
|
|
3033 /* octets not out of range */
|
|
3034 if (f2 < 0x20 || f3 < 0x20)
|
|
3035 return 0;
|
|
3036
|
|
3037 #ifdef ENABLE_COMPOSITE_CHARS
|
|
3038 if (f1 + FIELD1_TO_OFFICIAL_LEADING_BYTE == LEADING_BYTE_COMPOSITE)
|
|
3039 {
|
|
3040 if (UNBOUNDP (Fgethash (make_int (ch),
|
|
3041 Vcomposite_char_char2string_hash_table,
|
|
3042 Qunbound)))
|
|
3043 return 0;
|
|
3044 return 1;
|
|
3045 }
|
|
3046 #endif /* ENABLE_COMPOSITE_CHARS */
|
|
3047
|
|
3048 /* charset exists */
|
|
3049 if (f1 <= MAX_CHAR_FIELD1_OFFICIAL)
|
|
3050 charset =
|
|
3051 CHARSET_BY_LEADING_BYTE (f1 + FIELD1_TO_OFFICIAL_LEADING_BYTE);
|
|
3052 else
|
|
3053 charset =
|
|
3054 CHARSET_BY_LEADING_BYTE (f1 + FIELD1_TO_PRIVATE_LEADING_BYTE);
|
|
3055
|
|
3056 if (EQ (charset, Qnil))
|
|
3057 return 0;
|
|
3058 /* check range as per size (94x94 or 96x96) of charset */
|
|
3059 return ((f2 != 0x20 && f2 != 0x7F && f3 != 0x20 && f3 != 0x7F) ||
|
|
3060 XCHARSET_CHARS (charset) == 96);
|
|
3061 }
|
|
3062 }
|
|
3063
|
|
3064 /* Copy the character pointed to by SRC into DST. Do not call this
|
|
3065 directly. Use the macro charptr_copy_char() instead.
|
|
3066 Return the number of bytes copied. */
|
|
3067
|
|
3068 Bytecount
|
|
3069 non_ascii_charptr_copy_char (const Intbyte *src, Intbyte *dst)
|
|
3070 {
|
|
3071 Bytecount bytes = REP_BYTES_BY_FIRST_BYTE (*src);
|
|
3072 Bytecount i;
|
|
3073 for (i = bytes; i; i--, dst++, src++)
|
|
3074 *dst = *src;
|
|
3075 return bytes;
|
|
3076 }
|
|
3077
|
|
3078 #endif /* MULE */
|
|
3079
|
|
3080
|
|
3081 /************************************************************************/
|
|
3082 /* streams of Emchars */
|
|
3083 /************************************************************************/
|
|
3084
|
|
3085 #ifdef MULE
|
|
3086
|
|
3087 /* Treat a stream as a stream of Emchar's rather than a stream of bytes.
|
|
3088 The functions below are not meant to be called directly; use
|
|
3089 the macros in insdel.h. */
|
|
3090
|
|
3091 Emchar
|
|
3092 Lstream_get_emchar_1 (Lstream *stream, int ch)
|
|
3093 {
|
|
3094 Intbyte str[MAX_EMCHAR_LEN];
|
|
3095 Intbyte *strptr = str;
|
|
3096 Bytecount bytes;
|
|
3097
|
|
3098 str[0] = (Intbyte) ch;
|
|
3099
|
|
3100 for (bytes = REP_BYTES_BY_FIRST_BYTE (ch) - 1; bytes; bytes--)
|
|
3101 {
|
|
3102 int c = Lstream_getc (stream);
|
800
|
3103 text_checking_assert (c >= 0);
|
771
|
3104 *++strptr = (Intbyte) c;
|
|
3105 }
|
|
3106 return charptr_emchar (str);
|
|
3107 }
|
|
3108
|
|
3109 int
|
|
3110 Lstream_fput_emchar (Lstream *stream, Emchar ch)
|
|
3111 {
|
|
3112 Intbyte str[MAX_EMCHAR_LEN];
|
|
3113 Bytecount len = set_charptr_emchar (str, ch);
|
|
3114 return Lstream_write (stream, str, len);
|
|
3115 }
|
|
3116
|
|
3117 void
|
|
3118 Lstream_funget_emchar (Lstream *stream, Emchar ch)
|
|
3119 {
|
|
3120 Intbyte str[MAX_EMCHAR_LEN];
|
|
3121 Bytecount len = set_charptr_emchar (str, ch);
|
|
3122 Lstream_unread (stream, str, len);
|
|
3123 }
|
|
3124
|
|
3125 #endif /* MULE */
|
|
3126
|
|
3127
|
|
3128 /************************************************************************/
|
|
3129 /* Lisp primitives for working with characters */
|
|
3130 /************************************************************************/
|
|
3131
|
|
3132 DEFUN ("make-char", Fmake_char, 2, 3, 0, /*
|
|
3133 Make a character from CHARSET and octets ARG1 and ARG2.
|
|
3134 ARG2 is required only for characters from two-dimensional charsets.
|
|
3135
|
|
3136 Each octet should be in the range 32 through 127 for a 96 or 96x96
|
|
3137 charset and 33 through 126 for a 94 or 94x94 charset. (Most charsets
|
|
3138 are either 96 or 94x94.) Note that this is 32 more than the values
|
|
3139 typically given for 94x94 charsets. When two octets are required, the
|
|
3140 order is "standard" -- the same as appears in ISO-2022 encodings,
|
|
3141 reference tables, etc.
|
|
3142
|
|
3143 \(Note the following non-obvious result: Computerized translation
|
|
3144 tables often encode the two octets as the high and low bytes,
|
|
3145 respectively, of a hex short, while when there's only one octet, it
|
|
3146 goes in the low byte. When decoding such a value, you need to treat
|
|
3147 the two cases differently when calling make-char: One is (make-char
|
|
3148 CHARSET HIGH LOW), the other is (make-char CHARSET LOW).)
|
|
3149
|
|
3150 For example, (make-char 'latin-iso8859-2 185) or (make-char
|
|
3151 'latin-iso8859-2 57) will return the Latin 2 character s with caron.
|
|
3152
|
|
3153 As another example, the Japanese character for "kawa" (stream), which
|
|
3154 looks something like this:
|
|
3155
|
|
3156 | |
|
|
3157 | | |
|
|
3158 | | |
|
|
3159 | | |
|
|
3160 / |
|
|
3161
|
|
3162 appears in the Unicode Standard (version 2.0) on page 7-287 with the
|
|
3163 following values (see also page 7-4):
|
|
3164
|
|
3165 U 5DDD (Unicode)
|
|
3166 G 0-2008 (GB 2312-80)
|
|
3167 J 0-3278 (JIS X 0208-1990)
|
|
3168 K 0-8425 (KS C 5601-1987)
|
|
3169 B A474 (Big Five)
|
|
3170 C 1-4455 (CNS 11643-1986 (1st plane))
|
|
3171 A 213C34 (ANSI Z39.64-1989)
|
|
3172
|
|
3173 These are equivalent to:
|
|
3174
|
|
3175 \(make-char 'chinese-gb2312 52 40)
|
|
3176 \(make-char 'japanese-jisx0208 64 110)
|
|
3177 \(make-char 'korean-ksc5601 116 57)
|
|
3178 \(make-char 'chinese-cns11643-1 76 87)
|
|
3179 \(decode-big5-char '(164 . 116))
|
|
3180
|
|
3181 \(All codes above are two decimal numbers except for Big Five and ANSI
|
|
3182 Z39.64, which we don't support. We add 32 to each of the decimal
|
|
3183 numbers. Big Five is split in a rather hackish fashion into two
|
|
3184 charsets, `big5-1' and `big5-2', due to its excessive size -- 94x157,
|
|
3185 with the first codepoint in the range 0xA1 to 0xFE and the second in
|
|
3186 the range 0x40 to 0x7E or 0xA1 to 0xFE. `decode-big5-char' is used to
|
|
3187 generate the char from its codes, and `encode-big5-char' extracts the
|
|
3188 codes.)
|
|
3189
|
|
3190 When compiled without MULE, this function does not do much, but it's
|
|
3191 provided for compatibility. In this case, the following CHARSET symbols
|
|
3192 are allowed:
|
|
3193
|
|
3194 `ascii' -- ARG1 should be in the range 0 through 127.
|
|
3195 `control-1' -- ARG1 should be in the range 128 through 159.
|
|
3196 else -- ARG1 is coerced to be between 0 and 255, and then the high
|
|
3197 bit is set.
|
|
3198
|
|
3199 `int-to-char of the resulting ARG1' is returned, and ARG2 is always ignored.
|
|
3200 */
|
|
3201 (charset, arg1, arg2))
|
|
3202 {
|
|
3203 #ifdef MULE
|
|
3204 Lisp_Charset *cs;
|
|
3205 int a1, a2;
|
|
3206 int lowlim, highlim;
|
|
3207
|
|
3208 charset = Fget_charset (charset);
|
|
3209 cs = XCHARSET (charset);
|
|
3210
|
788
|
3211 get_charset_limits (charset, &lowlim, &highlim);
|
771
|
3212
|
|
3213 CHECK_INT (arg1);
|
|
3214 /* It is useful (and safe, according to Olivier Galibert) to strip
|
|
3215 the 8th bit off ARG1 and ARG2 because it allows programmers to
|
|
3216 write (make-char 'latin-iso8859-2 CODE) where code is the actual
|
|
3217 Latin 2 code of the character. */
|
|
3218 a1 = XINT (arg1) & 0x7f;
|
|
3219 if (a1 < lowlim || a1 > highlim)
|
|
3220 args_out_of_range_3 (arg1, make_int (lowlim), make_int (highlim));
|
|
3221
|
|
3222 if (CHARSET_DIMENSION (cs) == 1)
|
|
3223 {
|
|
3224 if (!NILP (arg2))
|
|
3225 invalid_argument
|
|
3226 ("Charset is of dimension one; second octet must be nil", arg2);
|
|
3227 return make_char (MAKE_CHAR (charset, a1, 0));
|
|
3228 }
|
|
3229
|
|
3230 CHECK_INT (arg2);
|
|
3231 a2 = XINT (arg2) & 0x7f;
|
|
3232 if (a2 < lowlim || a2 > highlim)
|
|
3233 args_out_of_range_3 (arg2, make_int (lowlim), make_int (highlim));
|
|
3234
|
|
3235 return make_char (MAKE_CHAR (charset, a1, a2));
|
|
3236 #else
|
|
3237 int a1;
|
|
3238 int lowlim, highlim;
|
|
3239
|
|
3240 if (EQ (charset, Qascii)) lowlim = 0, highlim = 127;
|
|
3241 else if (EQ (charset, Qcontrol_1)) lowlim = 0, highlim = 31;
|
|
3242 else lowlim = 0, highlim = 127;
|
|
3243
|
|
3244 CHECK_INT (arg1);
|
|
3245 /* It is useful (and safe, according to Olivier Galibert) to strip
|
|
3246 the 8th bit off ARG1 and ARG2 because it allows programmers to
|
|
3247 write (make-char 'latin-iso8859-2 CODE) where code is the actual
|
|
3248 Latin 2 code of the character. */
|
|
3249 a1 = XINT (arg1) & 0x7f;
|
|
3250 if (a1 < lowlim || a1 > highlim)
|
|
3251 args_out_of_range_3 (arg1, make_int (lowlim), make_int (highlim));
|
|
3252
|
|
3253 if (EQ (charset, Qascii))
|
|
3254 return make_char (a1);
|
|
3255 return make_char (a1 + 128);
|
|
3256 #endif /* MULE */
|
|
3257 }
|
|
3258
|
|
3259 #ifdef MULE
|
|
3260
|
|
3261 DEFUN ("char-charset", Fchar_charset, 1, 1, 0, /*
|
|
3262 Return the character set of char CH.
|
|
3263 */
|
|
3264 (ch))
|
|
3265 {
|
|
3266 CHECK_CHAR_COERCE_INT (ch);
|
|
3267
|
|
3268 return XCHARSET_NAME (CHARSET_BY_LEADING_BYTE
|
|
3269 (CHAR_LEADING_BYTE (XCHAR (ch))));
|
|
3270 }
|
|
3271
|
|
3272 DEFUN ("char-octet", Fchar_octet, 1, 2, 0, /*
|
|
3273 Return the octet numbered N (should be 0 or 1) of char CH.
|
|
3274 N defaults to 0 if omitted.
|
|
3275 */
|
|
3276 (ch, n))
|
|
3277 {
|
|
3278 Lisp_Object charset;
|
|
3279 int octet0, octet1;
|
|
3280
|
|
3281 CHECK_CHAR_COERCE_INT (ch);
|
|
3282
|
|
3283 BREAKUP_CHAR (XCHAR (ch), charset, octet0, octet1);
|
|
3284
|
|
3285 if (NILP (n) || EQ (n, Qzero))
|
|
3286 return make_int (octet0);
|
|
3287 else if (EQ (n, make_int (1)))
|
|
3288 return make_int (octet1);
|
|
3289 else
|
|
3290 invalid_constant ("Octet number must be 0 or 1", n);
|
|
3291 }
|
|
3292
|
|
3293 DEFUN ("split-char", Fsplit_char, 1, 1, 0, /*
|
|
3294 Return list of charset and one or two position-codes of CHAR.
|
|
3295 */
|
|
3296 (character))
|
|
3297 {
|
|
3298 /* This function can GC */
|
|
3299 struct gcpro gcpro1, gcpro2;
|
|
3300 Lisp_Object charset = Qnil;
|
|
3301 Lisp_Object rc = Qnil;
|
|
3302 int c1, c2;
|
|
3303
|
|
3304 GCPRO2 (charset, rc);
|
|
3305 CHECK_CHAR_COERCE_INT (character);
|
|
3306
|
|
3307 BREAKUP_CHAR (XCHAR (character), charset, c1, c2);
|
|
3308
|
|
3309 if (XCHARSET_DIMENSION (Fget_charset (charset)) == 2)
|
|
3310 {
|
|
3311 rc = list3 (XCHARSET_NAME (charset), make_int (c1), make_int (c2));
|
|
3312 }
|
|
3313 else
|
|
3314 {
|
|
3315 rc = list2 (XCHARSET_NAME (charset), make_int (c1));
|
|
3316 }
|
|
3317 UNGCPRO;
|
|
3318
|
|
3319 return rc;
|
|
3320 }
|
|
3321
|
|
3322 #endif /* MULE */
|
|
3323
|
|
3324
|
|
3325 /************************************************************************/
|
|
3326 /* composite character functions */
|
|
3327 /************************************************************************/
|
|
3328
|
|
3329 #ifdef ENABLE_COMPOSITE_CHARS
|
|
3330
|
|
3331 Emchar
|
|
3332 lookup_composite_char (Intbyte *str, int len)
|
|
3333 {
|
|
3334 Lisp_Object lispstr = make_string (str, len);
|
|
3335 Lisp_Object ch = Fgethash (lispstr,
|
|
3336 Vcomposite_char_string2char_hash_table,
|
|
3337 Qunbound);
|
|
3338 Emchar emch;
|
|
3339
|
|
3340 if (UNBOUNDP (ch))
|
|
3341 {
|
|
3342 if (composite_char_row_next >= 128)
|
|
3343 invalid_operation ("No more composite chars available", lispstr);
|
|
3344 emch = MAKE_CHAR (Vcharset_composite, composite_char_row_next,
|
|
3345 composite_char_col_next);
|
|
3346 Fputhash (make_char (emch), lispstr,
|
|
3347 Vcomposite_char_char2string_hash_table);
|
|
3348 Fputhash (lispstr, make_char (emch),
|
|
3349 Vcomposite_char_string2char_hash_table);
|
|
3350 composite_char_col_next++;
|
|
3351 if (composite_char_col_next >= 128)
|
|
3352 {
|
|
3353 composite_char_col_next = 32;
|
|
3354 composite_char_row_next++;
|
|
3355 }
|
|
3356 }
|
|
3357 else
|
|
3358 emch = XCHAR (ch);
|
|
3359 return emch;
|
|
3360 }
|
|
3361
|
|
3362 Lisp_Object
|
|
3363 composite_char_string (Emchar ch)
|
|
3364 {
|
|
3365 Lisp_Object str = Fgethash (make_char (ch),
|
|
3366 Vcomposite_char_char2string_hash_table,
|
|
3367 Qunbound);
|
|
3368 assert (!UNBOUNDP (str));
|
|
3369 return str;
|
|
3370 }
|
|
3371
|
|
3372 xxDEFUN ("make-composite-char", Fmake_composite_char, 1, 1, 0, /*
|
|
3373 Convert a string into a single composite character.
|
|
3374 The character is the result of overstriking all the characters in
|
|
3375 the string.
|
|
3376 */
|
|
3377 (string))
|
|
3378 {
|
|
3379 CHECK_STRING (string);
|
|
3380 return make_char (lookup_composite_char (XSTRING_DATA (string),
|
|
3381 XSTRING_LENGTH (string)));
|
|
3382 }
|
|
3383
|
|
3384 xxDEFUN ("composite-char-string", Fcomposite_char_string, 1, 1, 0, /*
|
|
3385 Return a string of the characters comprising a composite character.
|
|
3386 */
|
|
3387 (ch))
|
|
3388 {
|
|
3389 Emchar emch;
|
|
3390
|
|
3391 CHECK_CHAR (ch);
|
|
3392 emch = XCHAR (ch);
|
|
3393 if (CHAR_LEADING_BYTE (emch) != LEADING_BYTE_COMPOSITE)
|
|
3394 invalid_argument ("Must be composite char", ch);
|
|
3395 return composite_char_string (emch);
|
|
3396 }
|
|
3397 #endif /* ENABLE_COMPOSITE_CHARS */
|
|
3398
|
|
3399
|
|
3400 /************************************************************************/
|
|
3401 /* initialization */
|
|
3402 /************************************************************************/
|
|
3403
|
|
3404 void
|
814
|
3405 reinit_eistring_once_early (void)
|
771
|
3406 {
|
|
3407 the_eistring_malloc_zero_init = the_eistring_zero_init;
|
|
3408 the_eistring_malloc_zero_init.mallocp_ = 1;
|
|
3409 }
|
|
3410
|
|
3411 void
|
814
|
3412 init_eistring_once_early (void)
|
|
3413 {
|
|
3414 reinit_eistring_once_early ();
|
|
3415 }
|
|
3416
|
|
3417 void
|
771
|
3418 syms_of_text (void)
|
|
3419 {
|
|
3420 DEFSUBR (Fmake_char);
|
|
3421
|
|
3422 #ifdef MULE
|
|
3423 DEFSUBR (Fchar_charset);
|
|
3424 DEFSUBR (Fchar_octet);
|
|
3425 DEFSUBR (Fsplit_char);
|
|
3426
|
|
3427 #ifdef ENABLE_COMPOSITE_CHARS
|
|
3428 DEFSUBR (Fmake_composite_char);
|
|
3429 DEFSUBR (Fcomposite_char_string);
|
|
3430 #endif
|
|
3431 #endif /* MULE */
|
|
3432 }
|
|
3433
|
|
3434 void
|
|
3435 reinit_vars_of_text (void)
|
|
3436 {
|
|
3437 int i;
|
|
3438
|
|
3439 conversion_in_dynarr_list = Dynarr_new2 (Intbyte_dynarr_dynarr,
|
|
3440 Intbyte_dynarr *);
|
|
3441 conversion_out_dynarr_list = Dynarr_new2 (Extbyte_dynarr_dynarr,
|
|
3442 Extbyte_dynarr *);
|
|
3443
|
|
3444 /* #### Olivier, why does this need to be reinitted? */
|
|
3445 for (i = 0; i <= MAX_BYTEBPOS_GAP_SIZE_3; i++)
|
|
3446 three_to_one_table[i] = i / 3;
|
|
3447 }
|
|
3448
|
|
3449 void
|
|
3450 vars_of_text (void)
|
|
3451 {
|
|
3452 reinit_vars_of_text ();
|
|
3453
|
|
3454 #ifdef ENABLE_COMPOSITE_CHARS
|
|
3455 /* #### not dumped properly */
|
|
3456 composite_char_row_next = 32;
|
|
3457 composite_char_col_next = 32;
|
|
3458
|
|
3459 Vcomposite_char_string2char_hash_table =
|
|
3460 make_lisp_hash_table (500, HASH_TABLE_NON_WEAK, HASH_TABLE_EQUAL);
|
|
3461 Vcomposite_char_char2string_hash_table =
|
|
3462 make_lisp_hash_table (500, HASH_TABLE_NON_WEAK, HASH_TABLE_EQ);
|
|
3463 staticpro (&Vcomposite_char_string2char_hash_table);
|
|
3464 staticpro (&Vcomposite_char_char2string_hash_table);
|
|
3465 #endif /* ENABLE_COMPOSITE_CHARS */
|
|
3466 }
|