character.h 15.3 KB
Newer Older
Kenichi Handa's avatar
Kenichi Handa committed
1 2
/* Header for multibyte character handler.
   Copyright (C) 1995, 1997, 1998 Electrotechnical Laboratory, JAPAN.
Kenichi Handa's avatar
Kenichi Handa committed
3
     Licensed to the Free Software Foundation.
4
   Copyright (C) 2003, 2004, 2005, 2006, 2007, 2008, 2009, 2010, 2011
Kenichi Handa's avatar
Kenichi Handa committed
5 6 7 8 9
     National Institute of Advanced Industrial Science and Technology (AIST)
     Registration Number H13PRO009

This file is part of GNU Emacs.

10
GNU Emacs is free software: you can redistribute it and/or modify
Kenichi Handa's avatar
Kenichi Handa committed
11
it under the terms of the GNU General Public License as published by
12 13
the Free Software Foundation, either version 3 of the License, or (at
your option) any later version.
Kenichi Handa's avatar
Kenichi Handa committed
14 15 16 17 18 19 20

GNU Emacs is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
21
along with GNU Emacs.  If not, see <https://www.gnu.org/licenses/>.  */
Kenichi Handa's avatar
Kenichi Handa committed
22 23 24 25

#ifndef EMACS_CHARACTER_H
#define EMACS_CHARACTER_H

26
#include <verify.h>
27
#include "lisp.h"
28

29 30
INLINE_HEADER_BEGIN

31 32 33
/* character code	1st byte   byte sequence
   --------------	--------   -------------
        0-7F		00..7F	   0xxxxxxx
34 35 36 37
       80-7FF		C2..DF	   110yyyyx 10xxxxxx
      800-FFFF		E0..EF	   1110yyyy 10yxxxxx 10xxxxxx
    10000-1FFFFF	F0..F7	   11110yyy 10yyxxxx 10xxxxxx 10xxxxxx
   200000-3FFF7F	F8	   11111000 1000yxxx 10xxxxxx 10xxxxxx 10xxxxxx
Kenichi Handa's avatar
Kenichi Handa committed
38 39
   3FFF80-3FFFFF	C0..C1	   1100000x 10xxxxxx (for eight-bit-char)
   400000-...		invalid
Kenichi Handa's avatar
Kenichi Handa committed
40

Kenichi Handa's avatar
Kenichi Handa committed
41
   invalid 1st byte	80..BF	   10xxxxxx
42 43 44 45 46
			F9..FF	   11111yyy

   In each bit pattern, 'x' and 'y' each represent a single bit of the
   character code payload, and least one 'y' must be a 1 bit.
   In the 5-byte sequence, the 22-bit payload cannot exceed 3FFF7F.
Kenichi Handa's avatar
Kenichi Handa committed
47 48
*/

49
/* Maximum character code ((1 << CHARACTERBITS) - 1).  */
50
enum { MAX_CHAR = 0x3FFFFF };
Kenichi Handa's avatar
Kenichi Handa committed
51

52
/* Maximum Unicode character code.  */
53
enum { MAX_UNICODE_CHAR = 0x10FFFF };
Kenichi Handa's avatar
Kenichi Handa committed
54

55
/* Maximum N-byte character codes.  */
56 57 58 59 60
enum { MAX_1_BYTE_CHAR = 0x7F };
enum { MAX_2_BYTE_CHAR = 0x7FF };
enum { MAX_3_BYTE_CHAR = 0xFFFF };
enum { MAX_4_BYTE_CHAR = 0x1FFFFF };
enum { MAX_5_BYTE_CHAR = 0x3FFF7F };
Kenichi Handa's avatar
Kenichi Handa committed
61

Kenichi Handa's avatar
Kenichi Handa committed
62
/* Minimum leading code of multibyte characters.  */
63
enum { MIN_MULTIBYTE_LEADING_CODE = 0xC0 };
64 65
/* Maximum leading code of multibyte characters.  Note: this must be
   updated if we ever increase MAX_CHAR above.  */
66
enum { MAX_MULTIBYTE_LEADING_CODE = 0xF8 };
Kenichi Handa's avatar
Kenichi Handa committed
67

68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86
/* Unicode character values.  */
enum
{
  NO_BREAK_SPACE = 0x00A0,
  SOFT_HYPHEN = 0x00AD,
  ZERO_WIDTH_NON_JOINER = 0x200C,
  ZERO_WIDTH_JOINER = 0x200D,
  HYPHEN = 0x2010,
  NON_BREAKING_HYPHEN = 0x2011,
  LEFT_SINGLE_QUOTATION_MARK = 0x2018,
  RIGHT_SINGLE_QUOTATION_MARK = 0x2019,
  PARAGRAPH_SEPARATOR = 0x2029,
  LEFT_POINTING_ANGLE_BRACKET = 0x2329,
  RIGHT_POINTING_ANGLE_BRACKET = 0x232A,
  LEFT_ANGLE_BRACKET = 0x3008,
  RIGHT_ANGLE_BRACKET = 0x3009,
  OBJECT_REPLACEMENT_CHARACTER = 0xFFFC,
};

Paul Eggert's avatar
Paul Eggert committed
87 88
extern int char_string (unsigned, unsigned char *);

89 90 91 92 93
/* UTF-8 encodings.  Use \x escapes, so they are portable to pre-C11
   compilers and can be concatenated with ordinary string literals.  */
#define uLSQM "\xE2\x80\x98" /* U+2018 LEFT SINGLE QUOTATION MARK */
#define uRSQM "\xE2\x80\x99" /* U+2019 RIGHT SINGLE QUOTATION MARK */

94 95 96 97 98 99 100
/* True iff C is a character of code less than 0x100.  */
INLINE bool
SINGLE_BYTE_CHAR_P (intmax_t c)
{
  return 0 <= c && c < 0x100;
}

101
/* True iff C is a character that corresponds to a raw 8-bit
102
   byte.  */
103 104 105 106 107
INLINE bool
CHAR_BYTE8_P (int c)
{
  return MAX_5_BYTE_CHAR < c;
}
108

109
/* Return the character code for raw 8-bit byte BYTE.  */
110 111 112 113 114
INLINE int
BYTE8_TO_CHAR (int byte)
{
  return byte + 0x3FFF00;
}
115

116 117 118 119 120
INLINE int
UNIBYTE_TO_CHAR (int byte)
{
  return ASCII_CHAR_P (byte) ? byte : BYTE8_TO_CHAR (byte);
}
121

122
/* Return the raw 8-bit byte for character C.  */
123 124 125 126 127
INLINE int
CHAR_TO_BYTE8 (int c)
{
  return CHAR_BYTE8_P (c) ? c - 0x3FFF00 : c & 0xFF;
}
128

129 130
/* Return the raw 8-bit byte for character C,
   or -1 if C doesn't correspond to a byte.  */
131 132 133 134 135
INLINE int
CHAR_TO_BYTE_SAFE (int c)
{
  return ASCII_CHAR_P (c) ? c : CHAR_BYTE8_P (c) ? c - 0x3FFF00 : -1;
}
136

137
/* True iff BYTE is the 1st byte of a multibyte form of a character
138
   that corresponds to a raw 8-bit byte.  */
139 140 141 142 143
INLINE bool
CHAR_BYTE8_HEAD_P (int byte)
{
  return byte == 0xC0 || byte == 0xC1;
}
Kenichi Handa's avatar
Kenichi Handa committed
144

145
/* If C is not ASCII, make it multibyte.  Assumes C < 256.  */
146 147 148 149 150 151
INLINE int
make_char_multibyte (int c)
{
  eassert (SINGLE_BYTE_CHAR_P (c));
  return UNIBYTE_TO_CHAR (c);
}
152

153
/* This is the maximum byte length of multibyte form.  */
154
enum { MAX_MULTIBYTE_LENGTH = 5 };
Kenichi Handa's avatar
Kenichi Handa committed
155

156
/* Nonzero iff C is valid as a character code.  */
Paul Eggert's avatar
Paul Eggert committed
157 158 159 160 161
INLINE bool
CHAR_VALID_P (intmax_t c)
{
  return 0 <= c && c <= MAX_CHAR;
}
Kenichi Handa's avatar
Kenichi Handa committed
162

163 164 165 166 167 168 169
/* Nonzero iff X is a character.  */
INLINE bool
CHARACTERP (Lisp_Object x)
{
  return FIXNUMP (x) && CHAR_VALID_P (XFIXNUM (x));
}

Kenichi Handa's avatar
Kenichi Handa committed
170
/* Check if Lisp object X is a character or not.  */
171 172 173 174 175
INLINE void
CHECK_CHARACTER (Lisp_Object x)
{
  CHECK_TYPE (CHARACTERP (x), Qcharacterp, x);
}
Kenichi Handa's avatar
Kenichi Handa committed
176

177 178 179 180 181
INLINE void
CHECK_CHARACTER_CAR (Lisp_Object x)
{
  CHECK_CHARACTER (XCAR (x));
}
Kenichi Handa's avatar
Kenichi Handa committed
182

183 184 185 186 187
INLINE void
CHECK_CHARACTER_CDR (Lisp_Object x)
{
  CHECK_CHARACTER (XCDR (x));
}
Kenichi Handa's avatar
Kenichi Handa committed
188

189 190 191 192 193 194 195
/* True if character C has a printable glyph.  */
INLINE bool
CHAR_PRINTABLE_P (int c)
{
  return ((32 <= c && c < 127)
	  || ! NILP (CHAR_TABLE_REF (Vprintable_chars, c)));
}
Kenichi Handa's avatar
Kenichi Handa committed
196

197
/* Return byte length of multibyte form for character C.  */
198 199 200 201 202 203 204 205 206
INLINE int
CHAR_BYTES (int c)
{
  return ((MAX_5_BYTE_CHAR < c ? -2 : 1)
	  + (MAX_1_BYTE_CHAR < c)
	  + (MAX_2_BYTE_CHAR < c)
	  + (MAX_3_BYTE_CHAR < c)
	  + (MAX_4_BYTE_CHAR < c));
}
Kenichi Handa's avatar
Kenichi Handa committed
207 208

/* Return the leading code of multibyte form of C.  */
209 210 211 212 213 214 215 216 217 218
INLINE int
CHAR_LEADING_CODE (int c)
{
  return (c <= MAX_1_BYTE_CHAR ? c
	  : c <= MAX_2_BYTE_CHAR ? 0xC0 | (c >> 6)
	  : c <= MAX_3_BYTE_CHAR ? 0xE0 | (c >> 12)
	  : c <= MAX_4_BYTE_CHAR ? 0xF0 | (c >> 18)
	  : c <= MAX_5_BYTE_CHAR ? 0xF8
	  : 0xC0 | ((c >> 6) & 0x01));
}
Kenichi Handa's avatar
Kenichi Handa committed
219 220


221 222 223
/* Store multibyte form of the character C in P.  The caller should
   allocate at least MAX_MULTIBYTE_LENGTH bytes area at P in advance.
   Returns the length of the multibyte form.  */
Kenichi Handa's avatar
Kenichi Handa committed
224

Paul Eggert's avatar
Paul Eggert committed
225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250
INLINE int
CHAR_STRING (int c, unsigned char *p)
{
  eassume (0 <= c);
  if (c <= MAX_1_BYTE_CHAR)
    {
      p[0] = c;
      return 1;
    }
  if (c <= MAX_2_BYTE_CHAR)
    {
      p[0] = 0xC0 | (c >> 6);
      p[1] = 0x80 | (c & 0x3F);
      return 2;
    }
  if (c <= MAX_3_BYTE_CHAR)
    {
      p[0] = 0xE0 | (c >> 12);
      p[1] = 0x80 | ((c >> 6) & 0x3F);
      p[2] = 0x80 | (c & 0x3F);
      return 3;
    }
  int len = char_string (c, p);
  eassume (0 < len && len <= MAX_MULTIBYTE_LENGTH);
  return len;
}
Kenichi Handa's avatar
Kenichi Handa committed
251

252 253 254
/* Store multibyte form of byte B in P.  The caller should allocate at
   least MAX_MULTIBYTE_LENGTH bytes area at P in advance.  Returns the
   length of the multibyte form.  */
Kenichi Handa's avatar
Kenichi Handa committed
255

256 257 258 259 260 261 262
INLINE int
BYTE8_STRING (int b, unsigned char *p)
{
  p[0] = 0xC0 | ((b >> 6) & 0x01);
  p[1] = 0x80 | (b & 0x3F);
  return 2;
}
Kenichi Handa's avatar
Kenichi Handa committed
263

Kenichi Handa's avatar
Kenichi Handa committed
264

265 266 267 268 269 270
/* True iff BYTE starts a non-ASCII character in a multibyte form.  */
INLINE bool
LEADING_CODE_P (int byte)
{
  return (byte & 0xC0) == 0xC0;
}
Kenichi Handa's avatar
Kenichi Handa committed
271

272
/* True iff BYTE is a trailing code of a non-ASCII character in a
Kenichi Handa's avatar
Kenichi Handa committed
273
   multibyte form.  */
274 275 276 277 278
INLINE bool
TRAILING_CODE_P (int byte)
{
  return (byte & 0xC0) == 0x80;
}
Kenichi Handa's avatar
Kenichi Handa committed
279

280
/* True iff BYTE starts a character in a multibyte form.
281
   This is equivalent to:
282
	(ASCII_CHAR_P (byte) || LEADING_CODE_P (byte))  */
283 284 285 286 287
INLINE bool
CHAR_HEAD_P (int byte)
{
  return (byte & 0xC0) != 0x80;
}
288

Kenichi Handa's avatar
Kenichi Handa committed
289
/* How many bytes a character that starts with BYTE occupies in a
290
   multibyte form.  Unlike multibyte_length, this function does not
291
   validate the multibyte form, but looks only at its first byte.  */
292 293 294 295 296 297 298 299 300
INLINE int
BYTES_BY_CHAR_HEAD (int byte)
{
  return (!(byte & 0x80) ? 1
	  : !(byte & 0x20) ? 2
	  : !(byte & 0x10) ? 3
	  : !(byte & 0x08) ? 4
	  : 5);
}
Kenichi Handa's avatar
Kenichi Handa committed
301 302


303 304 305 306 307
/* The byte length of the multibyte form at the unibyte string P,
   ending at PEND if CHECK, and without a length check if !CHECK.
   If ALLOW_8BIT, allow multibyte forms of eight-bit characters.
   If the string doesn't point to a valid multibyte form, return 0.
   Unlike BYTES_BY_CHAR_HEAD, this function validates the multibyte form.  */
Kenichi Handa's avatar
Kenichi Handa committed
308

309
INLINE int
310 311
multibyte_length (unsigned char const *p, unsigned char const *pend,
		  bool check, bool allow_8bit)
312
{
313 314 315 316 317 318 319
  if (!check || p < pend)
    {
      unsigned char c = p[0];
      if (c < 0x80)
	return 1;
      if (!check || p + 1 < pend)
	{
320 321 322 323 324
	  unsigned char d = p[1];
	  int w = ((d & 0xC0) << 2) + c;
	  if ((allow_8bit ? 0x2C0 : 0x2C2) <= w && w <= 0x2DF)
	    return 2;
	  if (!check || p + 2 < pend)
325
	    {
326 327 328 329 330 331
	      unsigned char e = p[2];
	      w += (e & 0xC0) << 4;
	      int w1 = w | ((d & 0x20) >> 2);
	      if (0xAE1 <= w1 && w1 <= 0xAEF)
		return 3;
	      if (!check || p + 3 < pend)
332
		{
333 334 335 336 337 338
		  unsigned char f = p[3];
		  w += (f & 0xC0) << 6;
		  int w2 = w | ((d & 0x30) >> 3);
		  if (0x2AF1 <= w2 && w2 <= 0x2AF7)
		    return 4;
		  if (!check || p + 4 < pend)
339
		    {
340 341 342 343
		      int_fast64_t lw = w + ((p[4] & 0xC0) << 8),
			w3 = (lw << 24) + (d << 16) + (e << 8) + f;
		      if (0xAAF8888080 <= w3 && w3 <= 0xAAF88FBFBD)
			return 5;
344 345 346 347 348
		    }
		}
	    }
	}
    }
Kenichi Handa's avatar
Kenichi Handa committed
349

350
  return 0;
351
}
Kenichi Handa's avatar
Kenichi Handa committed
352

353 354

/* Return number of bytes in the multibyte character just before P.
355
   Assumes that P is already at a character boundary of the same
356
   multibyte form, and is not at the start of that form.  */
Kenichi Handa's avatar
Kenichi Handa committed
357

358 359 360 361 362 363 364
INLINE int
raw_prev_char_len (unsigned char const *p)
{
  for (int len = 1; ; len++)
    if (CHAR_HEAD_P (p[-len]))
      return len;
}
Kenichi Handa's avatar
Kenichi Handa committed
365 366


367 368 369 370 371 372
/* Return the character code of character whose multibyte form is at P,
   and set *LENGTH to its length.  */

INLINE int
string_char_and_length (unsigned char const *p, int *length)
{
373 374 375 376 377 378 379
  int c = p[0];
  if (! (c & 0x80))
    {
      *length = 1;
      return c;
    }
  eassume (0xC0 <= c);
380

381 382
  int d = (c << 6) + p[1] - ((0xC0 << 6) + 0x80);
  if (! (c & 0x20))
383
    {
384 385
      *length = 2;
      return d + (c < 0xC2 ? 0x3FFF80 : 0);
386
    }
387 388 389

  d = (d << 6) + p[2] - ((0x20 << 12) + 0x80);
  if (! (c & 0x10))
390
    {
391 392 393
      *length = 3;
      eassume (MAX_2_BYTE_CHAR < d && d <= MAX_3_BYTE_CHAR);
      return d;
394
    }
395 396 397

  d = (d << 6) + p[3] - ((0x10 << 18) + 0x80);
  if (! (c & 0x08))
398
    {
399 400 401
      *length = 4;
      eassume (MAX_3_BYTE_CHAR < d && d <= MAX_4_BYTE_CHAR);
      return d;
402 403
    }

404 405 406 407
  d = (d << 6) + p[4] - ((0x08 << 24) + 0x80);
  *length = 5;
  eassume (MAX_4_BYTE_CHAR < d && d <= MAX_5_BYTE_CHAR);
  return d;
408
}
Kenichi Handa's avatar
Kenichi Handa committed
409

410
/* Return the character code of character whose multibyte form is at P.  */
Kenichi Handa's avatar
Kenichi Handa committed
411

412 413 414
INLINE int
STRING_CHAR (unsigned char const *p)
{
415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459
  int len;
  return string_char_and_length (p, &len);
}


/* Like STRING_CHAR (*PP), but advance *PP to the end of multibyte form.  */

INLINE int
string_char_advance (unsigned char const **pp)
{
  unsigned char const *p = *pp;
  int len, c = string_char_and_length (p, &len);
  *pp = p + len;
  return c;
}


/* Return the next character from Lisp string STRING at byte position
   *BYTEIDX, character position *CHARIDX.  Update *BYTEIDX and
   *CHARIDX past the character fetched.  */

INLINE int
fetch_string_char_advance (Lisp_Object string,
			   ptrdiff_t *charidx, ptrdiff_t *byteidx)
{
  int output;
  ptrdiff_t b = *byteidx;
  unsigned char *chp = SDATA (string) + b;
  if (STRING_MULTIBYTE (string))
    {
      int chlen;
      output = string_char_and_length (chp, &chlen);
      b += chlen;
    }
  else
    {
      output = *chp;
      b++;
    }
  (*charidx)++;
  *byteidx = b;
  return output;
}

/* Like fetch_string_char_advance, but return a multibyte character
460
   even if STRING is unibyte.  */
Kenichi Handa's avatar
Kenichi Handa committed
461

462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498
INLINE int
fetch_string_char_as_multibyte_advance (Lisp_Object string,
					ptrdiff_t *charidx, ptrdiff_t *byteidx)
{
  int output;
  ptrdiff_t b = *byteidx;
  unsigned char *chp = SDATA (string) + b;
  if (STRING_MULTIBYTE (string))
    {
      int chlen;
      output = string_char_and_length (chp, &chlen);
      b += chlen;
    }
  else
    {
      output = make_char_multibyte (*chp);
      b++;
    }
  (*charidx)++;
  *byteidx = b;
  return output;
}


/* Like fetch_string_char_advance, but assumes STRING is multibyte.  */

INLINE int
fetch_string_char_advance_no_check (Lisp_Object string,
				    ptrdiff_t *charidx, ptrdiff_t *byteidx)
{
  ptrdiff_t b = *byteidx;
  unsigned char *chp = SDATA (string) + b;
  int chlen, output = string_char_and_length (chp, &chlen);
  (*charidx)++;
  *byteidx = b + chlen;
  return output;
}
Kenichi Handa's avatar
Kenichi Handa committed
499 500


501 502
/* If C is a variation selector, return the index of the
   variation selector (1..256).  Otherwise, return 0.  */
503

Paul Eggert's avatar
Paul Eggert committed
504
INLINE int
505
CHAR_VARIATION_SELECTOR_P (int c)
506
{
507 508 509 510 511
  return (c < 0xFE00 ? 0
	  : c <= 0xFE0F ? c - 0xFE00 + 1
	  : c < 0xE0100 ? 0
	  : c <= 0xE01EF ? c - 0xE0100 + 17
	  : 0);
512 513
}

Paul Eggert's avatar
Paul Eggert committed
514
/* Return true if C is a surrogate.  */
515

Paul Eggert's avatar
Paul Eggert committed
516 517 518 519 520
INLINE bool
char_surrogate_p (int c)
{
  return 0xD800 <= c && c <= 0xDFFF;
}
521

522 523 524
/* Data type for Unicode general category.

   The order of members must be in sync with the 8th element of the
525
   member of unidata-prop-alist (in admin/unidata/unidata-gen.el) for
526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560
   Unicode character property `general-category'.  */

typedef enum {
  UNICODE_CATEGORY_UNKNOWN = 0,
  UNICODE_CATEGORY_Lu,
  UNICODE_CATEGORY_Ll,
  UNICODE_CATEGORY_Lt,
  UNICODE_CATEGORY_Lm,
  UNICODE_CATEGORY_Lo,
  UNICODE_CATEGORY_Mn,
  UNICODE_CATEGORY_Mc,
  UNICODE_CATEGORY_Me,
  UNICODE_CATEGORY_Nd,
  UNICODE_CATEGORY_Nl,
  UNICODE_CATEGORY_No,
  UNICODE_CATEGORY_Pc,
  UNICODE_CATEGORY_Pd,
  UNICODE_CATEGORY_Ps,
  UNICODE_CATEGORY_Pe,
  UNICODE_CATEGORY_Pi,
  UNICODE_CATEGORY_Pf,
  UNICODE_CATEGORY_Po,
  UNICODE_CATEGORY_Sm,
  UNICODE_CATEGORY_Sc,
  UNICODE_CATEGORY_Sk,
  UNICODE_CATEGORY_So,
  UNICODE_CATEGORY_Zs,
  UNICODE_CATEGORY_Zl,
  UNICODE_CATEGORY_Zp,
  UNICODE_CATEGORY_Cc,
  UNICODE_CATEGORY_Cf,
  UNICODE_CATEGORY_Cs,
  UNICODE_CATEGORY_Co,
  UNICODE_CATEGORY_Cn
} unicode_category_t;
561

562
extern EMACS_INT char_resolve_modifier_mask (EMACS_INT) ATTRIBUTE_CONST;
Jan D's avatar
Jan D committed
563 564

extern int translate_char (Lisp_Object, int c);
565 566 567 568 569 570
extern ptrdiff_t count_size_as_multibyte (const unsigned char *, ptrdiff_t);
extern ptrdiff_t str_as_multibyte (unsigned char *, ptrdiff_t, ptrdiff_t,
				   ptrdiff_t *);
extern ptrdiff_t str_to_multibyte (unsigned char *, ptrdiff_t, ptrdiff_t);
extern ptrdiff_t str_as_unibyte (unsigned char *, ptrdiff_t);
extern ptrdiff_t str_to_unibyte (const unsigned char *, unsigned char *,
571
                                 ptrdiff_t);
572 573 574 575 576
extern ptrdiff_t strwidth (const char *, ptrdiff_t);
extern ptrdiff_t c_string_width (const unsigned char *, ptrdiff_t, int,
				 ptrdiff_t *, ptrdiff_t *);
extern ptrdiff_t lisp_string_width (Lisp_Object, ptrdiff_t,
				    ptrdiff_t *, ptrdiff_t *);
Kenichi Handa's avatar
Kenichi Handa committed
577 578

extern Lisp_Object Vchar_unify_table;
Jan D's avatar
Jan D committed
579
extern Lisp_Object string_escape_byte8 (Lisp_Object);
Dave Love's avatar
Dave Love committed
580

581
extern bool alphabeticp (int);
582
extern bool alphanumericp (int);
583
extern bool graphicp (int);
584
extern bool printablep (int);
585
extern bool blankp (int);
586

587 588 589 590 591 592 593
/* Look up the element in char table OBJ at index CH, and return it as
   an integer.  If the element is not a character, return CH itself.  */

INLINE int
char_table_translate (Lisp_Object obj, int ch)
{
  /* This internal function is expected to be called with valid arguments,
594
     so there is an eassert instead of CHECK_xxx for the sake of speed.  */
595 596 597
  eassert (CHAR_VALID_P (ch));
  eassert (CHAR_TABLE_P (obj));
  obj = CHAR_TABLE_REF (obj, ch);
Tom Tromey's avatar
Tom Tromey committed
598
  return CHARACTERP (obj) ? XFIXNUM (obj) : ch;
599 600
}

601
extern signed char const hexdigit[];
602 603 604 605 606 607 608

/* If C is a hexadecimal digit ('0'-'9', 'a'-'f', 'A'-'F'), return its
   value (0-15).  Otherwise return -1.  */

INLINE int
char_hexdigit (int c)
{
609
  return 0 <= c && c <= UCHAR_MAX ? hexdigit[c] - 1 : -1;
610 611
}

612 613
INLINE_HEADER_END

Kenichi Handa's avatar
Kenichi Handa committed
614
#endif /* EMACS_CHARACTER_H */