chars.h 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. #ifndef SCM_CHARS_H
  2. #define SCM_CHARS_H
  3. /* Copyright 1995-1996,2000-2001,2004,2006,2008-2009,2018-2019
  4. Free Software Foundation, Inc.
  5. This file is part of Guile.
  6. Guile is free software: you can redistribute it and/or modify it
  7. under the terms of the GNU Lesser General Public License as published
  8. by the Free Software Foundation, either version 3 of the License, or
  9. (at your option) any later version.
  10. Guile is distributed in the hope that it will be useful, but WITHOUT
  11. ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  12. FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
  13. License for more details.
  14. You should have received a copy of the GNU Lesser General Public
  15. License along with Guile. If not, see
  16. <https://www.gnu.org/licenses/>. */
  17. #include "libguile/error.h"
  18. #include "libguile/inline.h"
  19. /* Immediate Characters
  20. */
  21. #define SCM_CHARP(x) (SCM_ITAG8(x) == scm_tc8_char)
  22. #define SCM_CHAR(x) ((scm_t_wchar)SCM_ITAG8_DATA(x))
  23. /* SCM_MAKE_CHAR maps signed chars (-128 to 127) and unsigned chars
  24. (0 to 255) to Latin-1 codepoints (0 to 255) while allowing higher
  25. codepoints (256 to 1114111) to pass through unchanged.
  26. This macro evaluates X twice, which may lead to side effects if used
  27. incorrectly. It's also likely to be inefficient if X calls a
  28. procedure. Use 'scm_c_make_char' in those cases. */
  29. #define SCM_MAKE_CHAR(x) \
  30. ((x) <= 1 \
  31. ? SCM_MAKE_ITAG8 ((scm_t_bits) (unsigned char) (x), scm_tc8_char) \
  32. : SCM_MAKE_ITAG8 ((scm_t_bits) (x), scm_tc8_char))
  33. #define SCM_CODEPOINT_DOTTED_CIRCLE (0x25cc)
  34. #define SCM_CODEPOINT_SURROGATE_START (0xd800)
  35. #define SCM_CODEPOINT_SURROGATE_END (0xdfff)
  36. #define SCM_CODEPOINT_MAX (0x10ffff)
  37. #define SCM_IS_UNICODE_CHAR(c) \
  38. (((scm_t_wchar) (c) >= 0 \
  39. && (scm_t_wchar) (c) < SCM_CODEPOINT_SURROGATE_START) \
  40. || ((scm_t_wchar) (c) > SCM_CODEPOINT_SURROGATE_END \
  41. && (scm_t_wchar) (c) <= SCM_CODEPOINT_MAX))
  42. #define SCM_VALIDATE_CHAR(pos, scm) SCM_MAKE_VALIDATE_MSG (pos, scm, CHARP, "character")
  43. #define SCM_VALIDATE_CHAR_COPY(pos, scm, cvar) \
  44. do { \
  45. SCM_ASSERT (SCM_CHARP (scm), scm, pos, FUNC_NAME); \
  46. cvar = SCM_CHAR (scm); \
  47. } while (0)
  48. SCM_API SCM scm_char_p (SCM x);
  49. SCM_API SCM scm_char_eq_p (SCM x, SCM y);
  50. SCM_API SCM scm_char_less_p (SCM x, SCM y);
  51. SCM_API SCM scm_char_leq_p (SCM x, SCM y);
  52. SCM_API SCM scm_char_gr_p (SCM x, SCM y);
  53. SCM_API SCM scm_char_geq_p (SCM x, SCM y);
  54. SCM_API SCM scm_char_ci_eq_p (SCM x, SCM y);
  55. SCM_API SCM scm_char_ci_less_p (SCM x, SCM y);
  56. SCM_API SCM scm_char_ci_leq_p (SCM x, SCM y);
  57. SCM_API SCM scm_char_ci_gr_p (SCM x, SCM y);
  58. SCM_API SCM scm_char_ci_geq_p (SCM x, SCM y);
  59. SCM_API SCM scm_char_alphabetic_p (SCM chr);
  60. SCM_API SCM scm_char_numeric_p (SCM chr);
  61. SCM_API SCM scm_char_whitespace_p (SCM chr);
  62. SCM_API SCM scm_char_upper_case_p (SCM chr);
  63. SCM_API SCM scm_char_lower_case_p (SCM chr);
  64. SCM_API SCM scm_char_is_both_p (SCM chr);
  65. SCM_API SCM scm_char_to_integer (SCM chr);
  66. SCM_API SCM scm_integer_to_char (SCM n);
  67. SCM_API SCM scm_char_upcase (SCM chr);
  68. SCM_API SCM scm_char_downcase (SCM chr);
  69. SCM_API SCM scm_char_titlecase (SCM chr);
  70. SCM_API SCM scm_char_general_category (SCM chr);
  71. SCM_INLINE SCM scm_c_make_char (scm_t_wchar c);
  72. SCM_API scm_t_wchar scm_c_upcase (scm_t_wchar c);
  73. SCM_API scm_t_wchar scm_c_downcase (scm_t_wchar c);
  74. SCM_API scm_t_wchar scm_c_titlecase (scm_t_wchar c);
  75. SCM_INTERNAL const char *scm_i_charname (SCM chr);
  76. SCM_INTERNAL SCM scm_i_charname_to_char (const char *charname,
  77. size_t charname_len);
  78. SCM_INTERNAL void scm_init_chars (void);
  79. #if SCM_CAN_INLINE || defined SCM_INLINE_C_IMPLEMENTING_INLINES
  80. SCM_INLINE_IMPLEMENTATION SCM
  81. scm_c_make_char (scm_t_wchar c)
  82. {
  83. return SCM_MAKE_CHAR(c);
  84. }
  85. #endif
  86. #endif /* SCM_CHARS_H */