strings.h 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309
  1. #ifndef SCM_STRINGS_H
  2. #define SCM_STRINGS_H
  3. /* Copyright 1995-1998,2000-2001,2004-2006,2008-2011,2013,2015-2019,2022
  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/gc.h>
  18. #include <libguile/error.h>
  19. #include "libguile/inline.h"
  20. #include <libguile/snarf.h>
  21. /* String representation.
  22. A string is a piece of a stringbuf. A stringbuf can be used by
  23. more than one string. When a string is written to and the
  24. stringbuf of that string is used by more than one string, a new
  25. stringbuf is created. That is, strings are copy-on-write. This
  26. behavior can be used to make the substring operation quite
  27. efficient.
  28. The implementation is tuned so that mutating a string is costly,
  29. but just reading it is cheap and lock-free.
  30. There are also mutation-sharing strings. They refer to a part of
  31. an ordinary string. Writing to a mutation-sharing string just
  32. writes to the ordinary string.
  33. Internal, low level interface to the character arrays
  34. - Use scm_i_is_narrow_string to determine is the string is narrow or
  35. wide.
  36. - Use scm_i_string_chars or scm_i_string_wide_chars to get a
  37. pointer to the byte or scm_t_wchar array of a string for reading.
  38. Use scm_i_string_length to get the number of characters in that
  39. array. The array is not null-terminated.
  40. - The array is valid as long as the corresponding SCM object is
  41. protected but only until the next SCM_TICK. During such a 'safe
  42. point', strings might change their representation.
  43. - Use scm_i_string_start_writing to get a version of the string
  44. ready for reading and writing. This is a potentially costly
  45. operation since it implements the copy-on-write behavior. When
  46. done with the writing, call scm_i_string_stop_writing. You must
  47. do this before the next SCM_TICK. (This means, before calling
  48. almost any other scm_ function and you can't allow throws, of
  49. course.)
  50. - New strings can be created with scm_i_make_string or
  51. scm_i_make_wide_string. This gives access to a writable pointer
  52. that remains valid as long as nobody else makes a copy-on-write
  53. substring of the string. Do not call scm_i_string_stop_writing
  54. for this pointer.
  55. - Alternately, scm_i_string_ref and scm_i_string_set_x can be used
  56. to read and write strings without worrying about whether the
  57. string is narrow or wide. scm_i_string_set_x still needs to be
  58. bracketed by scm_i_string_start_writing and
  59. scm_i_string_stop_writing.
  60. Legacy interface
  61. - SCM_STRINGP is just scm_is_string.
  62. - SCM_STRING_CHARS uses scm_i_string_writable_chars and immediately
  63. calls scm_i_stop_writing, hoping for the best. SCM_STRING_LENGTH
  64. is the same as scm_i_string_length. SCM_STRING_CHARS will throw
  65. an error for strings that are not null-terminated. There is
  66. no wide version of this interface.
  67. */
  68. /* A type indicating what strategy to take when string locale
  69. conversion is unsuccessful. */
  70. typedef enum
  71. {
  72. SCM_FAILED_CONVERSION_ERROR = SCM_ICONVEH_ERROR,
  73. SCM_FAILED_CONVERSION_QUESTION_MARK = SCM_ICONVEH_QUESTION_MARK,
  74. SCM_FAILED_CONVERSION_ESCAPE_SEQUENCE = SCM_ICONVEH_ESCAPE_SEQUENCE
  75. } scm_t_string_failed_conversion_handler;
  76. SCM_INTERNAL SCM scm_nullstr;
  77. SCM_INTERNAL scm_t_string_failed_conversion_handler
  78. scm_i_default_string_failed_conversion_handler (void);
  79. SCM_INLINE int scm_is_string (SCM x);
  80. SCM_API SCM scm_string_p (SCM x);
  81. SCM_API SCM scm_string (SCM chrs);
  82. SCM_API SCM scm_make_string (SCM k, SCM chr);
  83. SCM_API SCM scm_string_length (SCM str);
  84. SCM_API SCM scm_string_utf8_length (SCM str);
  85. SCM_API SCM scm_string_bytes_per_char (SCM str);
  86. SCM_API SCM scm_string_ref (SCM str, SCM k);
  87. SCM_API SCM scm_string_set_x (SCM str, SCM k, SCM chr);
  88. SCM_API SCM scm_substring (SCM str, SCM start, SCM end);
  89. SCM_API SCM scm_substring_read_only (SCM str, SCM start, SCM end);
  90. SCM_API SCM scm_substring_shared (SCM str, SCM start, SCM end);
  91. SCM_API SCM scm_substring_copy (SCM str, SCM start, SCM end);
  92. SCM_API SCM scm_string_append (SCM args);
  93. SCM_API SCM scm_from_stringn (const char *str, size_t len, const char *encoding,
  94. scm_t_string_failed_conversion_handler handler);
  95. SCM_API SCM scm_c_make_string (size_t len, SCM chr);
  96. SCM_API size_t scm_c_string_length (SCM str);
  97. SCM_API size_t scm_c_string_utf8_length (SCM str);
  98. SCM_API size_t scm_c_symbol_length (SCM sym);
  99. SCM_API SCM scm_c_string_ref (SCM str, size_t pos);
  100. SCM_API void scm_c_string_set_x (SCM str, size_t pos, SCM chr);
  101. SCM_API SCM scm_c_substring (SCM str, size_t start, size_t end);
  102. SCM_API SCM scm_c_substring_read_only (SCM str, size_t start, size_t end);
  103. SCM_API SCM scm_c_substring_shared (SCM str, size_t start, size_t end);
  104. SCM_API SCM scm_c_substring_copy (SCM str, size_t start, size_t end);
  105. /* Use locale encoding for user input, user output, or interacting with
  106. the C library. Use latin1 for ASCII, and for literals in source
  107. code. Use utf8 for interaction with modern libraries which deal in
  108. UTF-8. Otherwise use scm_to_stringn or scm_from_stringn with a
  109. specific encoding. */
  110. SCM_API SCM scm_from_locale_string (const char *str);
  111. SCM_API SCM scm_from_locale_stringn (const char *str, size_t len);
  112. SCM_API SCM scm_take_locale_string (char *str);
  113. SCM_API SCM scm_take_locale_stringn (char *str, size_t len);
  114. SCM_API char *scm_to_locale_string (SCM str);
  115. SCM_API char *scm_to_locale_stringn (SCM str, size_t *lenp);
  116. SCM_API SCM scm_from_latin1_string (const char *str);
  117. SCM_API SCM scm_from_latin1_stringn (const char *str, size_t len);
  118. SCM_API char *scm_to_latin1_string (SCM str);
  119. SCM_API char *scm_to_latin1_stringn (SCM str, size_t *lenp);
  120. SCM_API char *scm_to_utf8_string (SCM str);
  121. SCM_API char *scm_to_utf8_stringn (SCM str, size_t *lenp);
  122. SCM_API SCM scm_from_utf8_string (const char *str);
  123. SCM_API SCM scm_from_utf8_stringn (const char *str, size_t len);
  124. SCM_API scm_t_wchar *scm_to_utf32_string (SCM str);
  125. SCM_API scm_t_wchar *scm_to_utf32_stringn (SCM str, size_t *lenp);
  126. SCM_API SCM scm_from_utf32_string (const scm_t_wchar *str);
  127. SCM_API SCM scm_from_utf32_stringn (const scm_t_wchar *str, size_t len);
  128. SCM_API char *scm_to_port_string (SCM str, SCM port);
  129. SCM_API char *scm_to_port_stringn (SCM str, size_t *lenp, SCM port);
  130. SCM_API SCM scm_from_port_string (const char *str, SCM port);
  131. SCM_API SCM scm_from_port_stringn (const char *str, size_t len, SCM port);
  132. SCM_API char *scm_to_stringn (SCM str, size_t *lenp, const char *encoding,
  133. scm_t_string_failed_conversion_handler handler);
  134. SCM_API size_t scm_to_locale_stringbuf (SCM str, char *buf, size_t max_len);
  135. SCM_API SCM scm_string_normalize_nfd (SCM str);
  136. SCM_API SCM scm_string_normalize_nfkd (SCM str);
  137. SCM_API SCM scm_string_normalize_nfc (SCM str);
  138. SCM_API SCM scm_string_normalize_nfkc (SCM str);
  139. SCM_API SCM scm_makfromstrs (int argc, char **argv);
  140. /* Snarfing support. See snarf.h. */
  141. #ifdef SCM_SUPPORT_STATIC_ALLOCATION
  142. #define SCM_IMMUTABLE_STRINGBUF(c_name, contents) \
  143. static SCM_UNUSED const \
  144. struct \
  145. { \
  146. scm_t_bits word_0; \
  147. scm_t_bits word_1; \
  148. const char buffer[sizeof (contents)]; \
  149. } \
  150. c_name = \
  151. { \
  152. scm_tc7_stringbuf, \
  153. sizeof (contents) - 1, \
  154. contents \
  155. }
  156. #define SCM_IMMUTABLE_STRING(c_name, contents) \
  157. SCM_IMMUTABLE_STRINGBUF (scm_i_paste (c_name, _stringbuf), contents); \
  158. SCM_IMMUTABLE_DOUBLE_CELL (c_name, \
  159. scm_tc7_ro_string, \
  160. (scm_t_bits) &scm_i_paste (c_name, \
  161. _stringbuf), \
  162. (scm_t_bits) 0, \
  163. (scm_t_bits) (sizeof (contents) - 1))
  164. #endif /* SCM_SUPPORT_STATIC_ALLOCATION */
  165. /* internal constants */
  166. /* Type tag for read-only strings. */
  167. #define scm_tc7_ro_string (scm_tc7_string + 0x200)
  168. /* Flags for shared and wide strings. */
  169. #define SCM_I_STRINGBUF_F_WIDE 0x400
  170. #define SCM_I_STRINGBUF_F_MUTABLE 0x800
  171. SCM_INTERNAL void scm_i_print_stringbuf (SCM exp, SCM port,
  172. scm_print_state *pstate);
  173. /* internal accessor functions. Arguments must be valid. */
  174. SCM_INTERNAL SCM scm_i_make_string (size_t len, char **datap,
  175. int read_only_p);
  176. SCM_INTERNAL SCM scm_i_make_wide_string (size_t len, scm_t_wchar **datap,
  177. int read_only_p);
  178. SCM_INTERNAL SCM scm_i_substring (SCM str, size_t start, size_t end);
  179. SCM_INTERNAL SCM scm_i_substring_read_only (SCM str, size_t start, size_t end);
  180. SCM_INTERNAL SCM scm_i_substring_shared (SCM str, size_t start, size_t end);
  181. SCM_INTERNAL SCM scm_i_substring_copy (SCM str, size_t start, size_t end);
  182. SCM_INTERNAL size_t scm_i_string_length (SCM str);
  183. SCM_INTERNAL int scm_i_string_is_mutable (SCM str);
  184. SCM_API /* FIXME: not internal */ const char *scm_i_string_chars (SCM str);
  185. SCM_API /* FIXME: not internal */ char *scm_i_string_writable_chars (SCM str);
  186. SCM_INTERNAL const scm_t_wchar *scm_i_string_wide_chars (SCM str);
  187. SCM_INTERNAL const void *scm_i_string_data (SCM str);
  188. SCM_INTERNAL SCM scm_i_string_start_writing (SCM str);
  189. SCM_INTERNAL void scm_i_string_stop_writing (void);
  190. SCM_INTERNAL int scm_i_is_narrow_string (SCM str);
  191. SCM_INTERNAL scm_t_wchar scm_i_string_ref (SCM str, size_t x);
  192. SCM_INTERNAL int scm_i_string_contains_char (SCM str, char c);
  193. SCM_INTERNAL int scm_i_string_strcmp (SCM sstr, size_t start_x, const char *cstr);
  194. SCM_INTERNAL void scm_i_string_set_x (SCM str, size_t p, scm_t_wchar chr);
  195. /* internal functions related to symbols. */
  196. SCM_INTERNAL SCM scm_i_make_symbol (SCM name, scm_t_bits flags,
  197. unsigned long hash);
  198. SCM_INTERNAL const char *scm_i_symbol_chars (SCM sym);
  199. SCM_INTERNAL const scm_t_wchar *scm_i_symbol_wide_chars (SCM sym);
  200. SCM_INTERNAL size_t scm_i_symbol_length (SCM sym);
  201. SCM_INTERNAL int scm_i_is_narrow_symbol (SCM str);
  202. SCM_INTERNAL int scm_i_try_narrow_string (SCM str);
  203. SCM_INTERNAL SCM scm_i_symbol_substring (SCM sym, size_t start, size_t end);
  204. SCM_INTERNAL scm_t_wchar scm_i_symbol_ref (SCM sym, size_t x);
  205. SCM_INTERNAL void scm_encoding_error (const char *subr, int err,
  206. const char *message, SCM port, SCM chr);
  207. SCM_INTERNAL void scm_decoding_error (const char *subr, int err,
  208. const char *message, SCM port);
  209. /* internal utility functions. */
  210. SCM_INTERNAL char **scm_i_allocate_string_pointers (SCM list);
  211. SCM_INTERNAL void scm_i_get_substring_spec (size_t len,
  212. SCM start, size_t *cstart,
  213. SCM end, size_t *cend);
  214. /* Debugging functions */
  215. SCM_API SCM scm_sys_string_dump (SCM);
  216. SCM_API SCM scm_sys_symbol_dump (SCM);
  217. #ifdef SCM_STRING_LENGTH_HISTOGRAM
  218. SCM_API SCM scm_sys_stringbuf_hist (void);
  219. #endif
  220. #if SCM_CAN_INLINE || defined SCM_INLINE_C_IMPLEMENTING_INLINES
  221. /* Either inlining, or being included from inline.c. */
  222. SCM_INLINE_IMPLEMENTATION int
  223. scm_is_string (SCM x)
  224. {
  225. return SCM_HAS_TYP7 (x, scm_tc7_string);
  226. }
  227. #endif
  228. #define SCM_VALIDATE_STRING(pos, str) \
  229. do { \
  230. SCM_ASSERT_TYPE (scm_is_string (str), str, pos, FUNC_NAME, "string"); \
  231. } while (0)
  232. SCM_INTERNAL void scm_init_strings (void);
  233. #endif /* SCM_STRINGS_H */