strings.h 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  1. /* classes: h_files */
  2. #ifndef SCM_STRINGS_H
  3. #define SCM_STRINGS_H
  4. /* Copyright (C) 1995,1996,1997,1998,2000,2001, 2004, 2005, 2006 Free Software Foundation, Inc.
  5. *
  6. * This library is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU Lesser General Public
  8. * License as published by the Free Software Foundation; either
  9. * version 2.1 of the License, or (at your option) any later version.
  10. *
  11. * This library is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  14. * Lesser General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU Lesser General Public
  17. * License along with this library; if not, write to the Free Software
  18. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  19. */
  20. #include "libguile/__scm.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_string_chars to get a pointer to the byte array of a
  35. string for reading. Use scm_i_string_length to get the number of
  36. bytes in that array. The array is not null-terminated.
  37. - The array is valid as long as the corresponding SCM object is
  38. protected but only until the next SCM_TICK. During such a 'safe
  39. point', strings might change their representation.
  40. - Use scm_i_string_writable_chars to get the same pointer as with
  41. scm_i_string_chars, but for reading and writing. This is a
  42. potentially costly operation since it implements the
  43. copy-on-write behavior. When done with the writing, call
  44. scm_i_string_stop_writing. You must do this before the next
  45. SCM_TICK. (This means, before calling almost any other scm_
  46. function and you can't allow throws, of course.)
  47. - New strings can be created with scm_i_make_string. This gives
  48. access to a writable pointer that remains valid as long as nobody
  49. else makes a copy-on-write substring of the string. Do not call
  50. scm_i_string_stop_writing for this pointer.
  51. Legacy interface
  52. - SCM_STRINGP is just scm_is_string.
  53. - SCM_STRING_CHARS uses scm_i_string_writable_chars and immediately
  54. calls scm_i_stop_writing, hoping for the best. SCM_STRING_LENGTH
  55. is the same as scm_i_string_length. SCM_STRING_CHARS will throw
  56. an error for for strings that are not null-terminated.
  57. */
  58. SCM_API SCM scm_string_p (SCM x);
  59. SCM_API SCM scm_string (SCM chrs);
  60. SCM_API SCM scm_make_string (SCM k, SCM chr);
  61. SCM_API SCM scm_string_length (SCM str);
  62. SCM_API SCM scm_string_ref (SCM str, SCM k);
  63. SCM_API SCM scm_string_set_x (SCM str, SCM k, SCM chr);
  64. SCM_API SCM scm_substring (SCM str, SCM start, SCM end);
  65. SCM_API SCM scm_substring_read_only (SCM str, SCM start, SCM end);
  66. SCM_API SCM scm_substring_shared (SCM str, SCM start, SCM end);
  67. SCM_API SCM scm_substring_copy (SCM str, SCM start, SCM end);
  68. SCM_API SCM scm_string_append (SCM args);
  69. SCM_API SCM scm_c_make_string (size_t len, SCM chr);
  70. SCM_API size_t scm_c_string_length (SCM str);
  71. SCM_API size_t scm_c_symbol_length (SCM sym);
  72. SCM_API SCM scm_c_string_ref (SCM str, size_t pos);
  73. SCM_API void scm_c_string_set_x (SCM str, size_t pos, SCM chr);
  74. SCM_API SCM scm_c_substring (SCM str, size_t start, size_t end);
  75. SCM_API SCM scm_c_substring_read_only (SCM str, size_t start, size_t end);
  76. SCM_API SCM scm_c_substring_shared (SCM str, size_t start, size_t end);
  77. SCM_API SCM scm_c_substring_copy (SCM str, size_t start, size_t end);
  78. SCM_API int scm_is_string (SCM x);
  79. SCM_API SCM scm_from_locale_string (const char *str);
  80. SCM_API SCM scm_from_locale_stringn (const char *str, size_t len);
  81. SCM_API SCM scm_take_locale_string (char *str);
  82. SCM_API SCM scm_take_locale_stringn (char *str, size_t len);
  83. SCM_API char *scm_to_locale_string (SCM str);
  84. SCM_API char *scm_to_locale_stringn (SCM str, size_t *lenp);
  85. SCM_API size_t scm_to_locale_stringbuf (SCM str, char *buf, size_t max_len);
  86. SCM_API SCM scm_makfromstrs (int argc, char **argv);
  87. /* internal accessor functions. Arguments must be valid. */
  88. SCM_API SCM scm_i_make_string (size_t len, char **datap);
  89. SCM_API SCM scm_i_substring (SCM str, size_t start, size_t end);
  90. SCM_API SCM scm_i_substring_read_only (SCM str, size_t start, size_t end);
  91. SCM_API SCM scm_i_substring_shared (SCM str, size_t start, size_t end);
  92. SCM_API SCM scm_i_substring_copy (SCM str, size_t start, size_t end);
  93. SCM_API size_t scm_i_string_length (SCM str);
  94. SCM_API const char *scm_i_string_chars (SCM str);
  95. SCM_API char *scm_i_string_writable_chars (SCM str);
  96. SCM_API void scm_i_string_stop_writing (void);
  97. /* internal functions related to symbols. */
  98. SCM_API SCM scm_i_make_symbol (SCM name, scm_t_bits flags,
  99. unsigned long hash, SCM props);
  100. SCM_API SCM
  101. scm_i_c_make_symbol (const char *name, size_t len,
  102. scm_t_bits flags, unsigned long hash, SCM props);
  103. SCM_API SCM
  104. scm_i_c_take_symbol (char *name, size_t len,
  105. scm_t_bits flags, unsigned long hash, SCM props);
  106. SCM_API const char *scm_i_symbol_chars (SCM sym);
  107. SCM_API size_t scm_i_symbol_length (SCM sym);
  108. SCM_API SCM scm_i_symbol_substring (SCM sym, size_t start, size_t end);
  109. /* internal GC functions. */
  110. SCM_API SCM scm_i_string_mark (SCM str);
  111. SCM_API SCM scm_i_stringbuf_mark (SCM buf);
  112. SCM_API SCM scm_i_symbol_mark (SCM buf);
  113. SCM_API void scm_i_string_free (SCM str);
  114. SCM_API void scm_i_stringbuf_free (SCM buf);
  115. SCM_API void scm_i_symbol_free (SCM sym);
  116. /* internal utility functions. */
  117. SCM_API char **scm_i_allocate_string_pointers (SCM list);
  118. SCM_API void scm_i_free_string_pointers (char **pointers);
  119. SCM_API void scm_i_get_substring_spec (size_t len,
  120. SCM start, size_t *cstart,
  121. SCM end, size_t *cend);
  122. SCM_API SCM scm_i_take_stringbufn (char *str, size_t len);
  123. /* deprecated stuff */
  124. #if SCM_ENABLE_DEPRECATED
  125. SCM_API int scm_i_deprecated_stringp (SCM obj);
  126. SCM_API char *scm_i_deprecated_string_chars (SCM str);
  127. SCM_API size_t scm_i_deprecated_string_length (SCM str);
  128. #define SCM_STRINGP(x) scm_i_deprecated_stringp(x)
  129. #define SCM_STRING_CHARS(x) scm_i_deprecated_string_chars(x)
  130. #define SCM_STRING_LENGTH(x) scm_i_deprecated_string_length(x)
  131. #define SCM_STRING_UCHARS(str) ((unsigned char *)SCM_STRING_CHARS (str))
  132. #endif
  133. SCM_API void scm_init_strings (void);
  134. #endif /* SCM_STRINGS_H */
  135. /*
  136. Local Variables:
  137. c-file-style: "gnu"
  138. End:
  139. */