Mercurial > emacs
comparison src/charset.c @ 38394:245ae371bd4d
(char_to_string_1): Extracted from char_to_string.
Return -1 instead of signaling an error.
(char_to_string): Use it.
author | Gerd Moellmann <gerd@gnu.org> |
---|---|
date | Fri, 13 Jul 2001 09:30:42 +0000 |
parents | 4c211a545642 |
children | 5f60884970a8 |
comparison
equal
deleted
inserted
replaced
38393:2f9707a444e4 | 38394:245ae371bd4d |
---|---|
1 /* Basic multilingual character support. | 1 /* Basic multilingual character support. |
2 Copyright (C) 1995, 1997, 1998 Electrotechnical Laboratory, JAPAN. | 2 Copyright (C) 1995, 1997, 1998 Electrotechnical Laboratory, JAPAN. |
3 Licensed to the Free Software Foundation. | 3 Licensed to the Free Software Foundation. |
4 Copyright (C) 2001 Free Software Foundation, Inc. | |
4 | 5 |
5 This file is part of GNU Emacs. | 6 This file is part of GNU Emacs. |
6 | 7 |
7 GNU Emacs is free software; you can redistribute it and/or modify | 8 GNU Emacs is free software; you can redistribute it and/or modify |
8 it under the terms of the GNU General Public License as published by | 9 it under the terms of the GNU General Public License as published by |
158 && (c2) >= 0x20 && (c2) <= 0x7F))))) | 159 && (c2) >= 0x20 && (c2) <= 0x7F))))) |
159 | 160 |
160 /* Store multi-byte form of the character C in STR. The caller should | 161 /* Store multi-byte form of the character C in STR. The caller should |
161 allocate at least 4-byte area at STR in advance. Returns the | 162 allocate at least 4-byte area at STR in advance. Returns the |
162 length of the multi-byte form. If C is an invalid character code, | 163 length of the multi-byte form. If C is an invalid character code, |
163 signal an error. | 164 return -1. */ |
164 | 165 |
165 Use macro `CHAR_STRING (C, STR)' instead of calling this function | 166 int |
166 directly if C can be an ASCII character. */ | 167 char_to_string_1 (c, str) |
167 | |
168 int | |
169 char_to_string (c, str) | |
170 int c; | 168 int c; |
171 unsigned char *str; | 169 unsigned char *str; |
172 { | 170 { |
173 unsigned char *p = str; | 171 unsigned char *p = str; |
174 | 172 |
175 if (c & CHAR_MODIFIER_MASK) /* This includes the case C is negative. */ | 173 if (c & CHAR_MODIFIER_MASK) /* This includes the case C is negative. */ |
176 { | 174 { |
177 /* Multibyte character can't have a modifier bit. */ | 175 /* Multibyte character can't have a modifier bit. */ |
178 if (! SINGLE_BYTE_CHAR_P ((c & ~CHAR_MODIFIER_MASK))) | 176 if (! SINGLE_BYTE_CHAR_P ((c & ~CHAR_MODIFIER_MASK))) |
179 invalid_character (c); | 177 return -1; |
180 | 178 |
181 /* For Meta, Shift, and Control modifiers, we need special care. */ | 179 /* For Meta, Shift, and Control modifiers, we need special care. */ |
182 if (c & CHAR_META) | 180 if (c & CHAR_META) |
183 { | 181 { |
184 /* Move the meta bit to the right place for a string. */ | 182 /* Move the meta bit to the right place for a string. */ |
209 } | 207 } |
210 | 208 |
211 /* If C still has any modifier bits, just ignore it. */ | 209 /* If C still has any modifier bits, just ignore it. */ |
212 c &= ~CHAR_MODIFIER_MASK; | 210 c &= ~CHAR_MODIFIER_MASK; |
213 } | 211 } |
212 | |
214 if (SINGLE_BYTE_CHAR_P (c)) | 213 if (SINGLE_BYTE_CHAR_P (c)) |
215 { | 214 { |
216 if (ASCII_BYTE_P (c) || c >= 0xA0) | 215 if (ASCII_BYTE_P (c) || c >= 0xA0) |
217 *p++ = c; | 216 *p++ = c; |
218 else | 217 else |
235 : (charset < LEADING_CODE_EXT_22 | 234 : (charset < LEADING_CODE_EXT_22 |
236 ? LEADING_CODE_PRIVATE_21 | 235 ? LEADING_CODE_PRIVATE_21 |
237 : LEADING_CODE_PRIVATE_22))); | 236 : LEADING_CODE_PRIVATE_22))); |
238 *p++ = charset; | 237 *p++ = charset; |
239 if (c1 > 0 && c1 < 32 || c2 > 0 && c2 < 32) | 238 if (c1 > 0 && c1 < 32 || c2 > 0 && c2 < 32) |
240 invalid_character (c); | 239 return -1; |
241 if (c1) | 240 if (c1) |
242 { | 241 { |
243 *p++ = c1 | 0x80; | 242 *p++ = c1 | 0x80; |
244 if (c2 > 0) | 243 if (c2 > 0) |
245 *p++ = c2 | 0x80; | 244 *p++ = c2 | 0x80; |
246 } | 245 } |
247 } | 246 } |
248 else | 247 else |
248 return -1; | |
249 | |
250 return (p - str); | |
251 } | |
252 | |
253 | |
254 /* Store multi-byte form of the character C in STR. The caller should | |
255 allocate at least 4-byte area at STR in advance. Returns the | |
256 length of the multi-byte form. If C is an invalid character code, | |
257 signal an error. | |
258 | |
259 Use macro `CHAR_STRING (C, STR)' instead of calling this function | |
260 directly if C can be an ASCII character. */ | |
261 | |
262 int | |
263 char_to_string (c, str) | |
264 int c; | |
265 unsigned char *str; | |
266 { | |
267 int len; | |
268 len = char_to_string_1 (c, str); | |
269 if (len == -1) | |
249 invalid_character (c); | 270 invalid_character (c); |
250 | 271 return len; |
251 return (p - str); | 272 } |
252 } | 273 |
253 | 274 |
254 /* Return the non-ASCII character corresponding to multi-byte form at | 275 /* Return the non-ASCII character corresponding to multi-byte form at |
255 STR of length LEN. If ACTUAL_LEN is not NULL, store the byte | 276 STR of length LEN. If ACTUAL_LEN is not NULL, store the byte |
256 length of the multibyte form in *ACTUAL_LEN. | 277 length of the multibyte form in *ACTUAL_LEN. |
257 | 278 |