strings.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390
  1. /* Copyright (C) 1995,1996,1998,2000, 2002 Free Software Foundation, Inc.
  2. *
  3. * This program is free software; you can redistribute it and/or modify
  4. * it under the terms of the GNU General Public License as published by
  5. * the Free Software Foundation; either version 2, or (at your option)
  6. * any later version.
  7. *
  8. * This program is distributed in the hope that it will be useful,
  9. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. * GNU General Public License for more details.
  12. *
  13. * You should have received a copy of the GNU General Public License
  14. * along with this software; see the file COPYING. If not, write to
  15. * the Free Software Foundation, Inc., 59 Temple Place, Suite 330,
  16. * Boston, MA 02111-1307 USA
  17. *
  18. * As a special exception, the Free Software Foundation gives permission
  19. * for additional uses of the text contained in its release of GUILE.
  20. *
  21. * The exception is that, if you link the GUILE library with other files
  22. * to produce an executable, this does not by itself cause the
  23. * resulting executable to be covered by the GNU General Public License.
  24. * Your use of that executable is in no way restricted on account of
  25. * linking the GUILE library code into it.
  26. *
  27. * This exception does not however invalidate any other reasons why
  28. * the executable file might be covered by the GNU General Public License.
  29. *
  30. * This exception applies only to the code released by the
  31. * Free Software Foundation under the name GUILE. If you copy
  32. * code from other Free Software Foundation releases into a copy of
  33. * GUILE, as the General Public License permits, the exception does
  34. * not apply to the code that you add in this way. To avoid misleading
  35. * anyone as to the status of such modified files, you must delete
  36. * this exception notice from them.
  37. *
  38. * If you write modifications of your own for GUILE, it is your choice
  39. * whether to permit this exception to apply to your modifications.
  40. * If you do not wish that, delete this exception notice. */
  41. #include <stdio.h>
  42. #include <string.h>
  43. #include "libguile/_scm.h"
  44. #include "libguile/chars.h"
  45. #include "libguile/strings.h"
  46. #include "libguile/validate.h"
  47. /* {Strings}
  48. */
  49. SCM_DEFINE (scm_string_p, "string?", 1, 0, 0,
  50. (SCM obj),
  51. "Returns #t iff OBJ is a string, else returns #f.")
  52. #define FUNC_NAME s_scm_string_p
  53. {
  54. return SCM_BOOL(SCM_STRINGP (obj));
  55. }
  56. #undef FUNC_NAME
  57. SCM_DEFINE (scm_read_only_string_p, "read-only-string?", 1, 0, 0,
  58. (SCM x),
  59. "Return true if OBJ can be read as a string,\n\n"
  60. "This illustrates the difference between @code{string?} and\n"
  61. "@code{read-only-string?}:\n\n"
  62. "@example\n"
  63. "(string? \"a string\") @result{} #t\n"
  64. "(string? 'a-symbol) @result{} #f\n\n"
  65. "(read-only-string? \"a string\") @result{} #t\n"
  66. "(read-only-string? 'a-symbol) @result{} #t\n"
  67. "@end example")
  68. #define FUNC_NAME s_scm_read_only_string_p
  69. {
  70. return SCM_BOOL(SCM_ROSTRINGP (x));
  71. }
  72. #undef FUNC_NAME
  73. SCM_REGISTER_PROC (s_scm_list_to_string, "list->string", 1, 0, 0, scm_string);
  74. SCM_DEFINE (scm_string, "string", 0, 0, 1,
  75. (SCM chrs),
  76. "Returns a newly allocated string composed of the arguments, CHRS.")
  77. #define FUNC_NAME s_scm_string
  78. {
  79. SCM result;
  80. {
  81. long i = scm_ilength (chrs);
  82. SCM_ASSERT (i >= 0, chrs, SCM_ARGn, FUNC_NAME);
  83. result = scm_makstr (i, 0);
  84. }
  85. {
  86. unsigned char *data = SCM_UCHARS (result);
  87. while (SCM_NNULLP (chrs))
  88. {
  89. SCM elt = SCM_CAR (chrs);
  90. SCM_VALIDATE_CHAR (SCM_ARGn, elt);
  91. *data++ = SCM_CHAR (elt);
  92. chrs = SCM_CDR (chrs);
  93. }
  94. }
  95. return result;
  96. }
  97. #undef FUNC_NAME
  98. SCM
  99. scm_makstr (long len, int slots)
  100. {
  101. SCM s;
  102. scm_bits_t * mem;
  103. SCM_NEWCELL (s);
  104. --slots;
  105. SCM_REDEFER_INTS;
  106. mem = (scm_bits_t *) scm_must_malloc (sizeof (scm_bits_t) * (slots + 1)
  107. + len + 1, "scm_makstr");
  108. if (slots >= 0)
  109. {
  110. int x;
  111. mem[slots] = (scm_bits_t) mem;
  112. for (x = 0; x < slots; ++x)
  113. mem[x] = SCM_UNPACK (SCM_BOOL_F);
  114. }
  115. SCM_SETCHARS (s, (char *) (mem + slots + 1));
  116. SCM_SETLENGTH (s, len, scm_tc7_string);
  117. SCM_REALLOW_INTS;
  118. SCM_CHARS (s)[len] = 0;
  119. return s;
  120. }
  121. /* converts C scm_array of strings to SCM scm_list of strings. */
  122. /* If argc < 0, a null terminated scm_array is assumed. */
  123. SCM
  124. scm_makfromstrs (int argc, char **argv)
  125. {
  126. int i = argc;
  127. SCM lst = SCM_EOL;
  128. if (0 > i)
  129. for (i = 0; argv[i]; i++);
  130. while (i--)
  131. lst = scm_cons (scm_makfromstr (argv[i], (scm_sizet) strlen (argv[i]), 0), lst);
  132. return lst;
  133. }
  134. /* This function must only be applied to memory obtained via malloc,
  135. since the GC is going to apply `free' to it when the string is
  136. dropped.
  137. Also, s[len] must be `\0', since we promise that strings are
  138. null-terminated. Perhaps we could handle non-null-terminated
  139. strings by claiming they're shared substrings of a string we just
  140. made up. */
  141. SCM
  142. scm_take_str (char *s, int len)
  143. {
  144. SCM answer;
  145. SCM_NEWCELL (answer);
  146. SCM_DEFER_INTS;
  147. SCM_SETLENGTH (answer, len, scm_tc7_string);
  148. scm_done_malloc (len + 1);
  149. SCM_SETCHARS (answer, s);
  150. SCM_ALLOW_INTS;
  151. return answer;
  152. }
  153. /* `s' must be a malloc'd string. See scm_take_str. */
  154. SCM
  155. scm_take0str (char *s)
  156. {
  157. return scm_take_str (s, strlen (s));
  158. }
  159. SCM
  160. scm_makfromstr (const char *src, scm_sizet len, int slots)
  161. {
  162. SCM s = scm_makstr (len, slots);
  163. char *dst = SCM_CHARS (s);
  164. while (len--)
  165. *dst++ = *src++;
  166. return s;
  167. }
  168. SCM
  169. scm_makfrom0str (const char *src)
  170. {
  171. if (!src) return SCM_BOOL_F;
  172. return scm_makfromstr (src, (scm_sizet) strlen (src), 0);
  173. }
  174. SCM
  175. scm_makfrom0str_opt (const char *src)
  176. {
  177. return scm_makfrom0str (src);
  178. }
  179. SCM_DEFINE (scm_make_string, "make-string", 1, 1, 0,
  180. (SCM k, SCM chr),
  181. "Returns a newly allocated string of\n"
  182. "length K. If CHR is given, then all elements of the string\n"
  183. "are initialized to CHR, otherwise the contents of the\n"
  184. "STRING are unspecified.\n")
  185. #define FUNC_NAME s_scm_make_string
  186. {
  187. SCM res;
  188. register long i;
  189. SCM_VALIDATE_INUM_MIN_COPY (1,k,0,i);
  190. res = scm_makstr (i, 0);
  191. if (!SCM_UNBNDP (chr))
  192. {
  193. SCM_VALIDATE_CHAR (2,chr);
  194. {
  195. unsigned char *dst = SCM_UCHARS (res);
  196. char c = SCM_CHAR (chr);
  197. memset (dst, c, i);
  198. }
  199. }
  200. return res;
  201. }
  202. #undef FUNC_NAME
  203. SCM_DEFINE (scm_string_length, "string-length", 1, 0, 0,
  204. (SCM string),
  205. "Returns the number of characters in STRING")
  206. #define FUNC_NAME s_scm_string_length
  207. {
  208. SCM_VALIDATE_ROSTRING (1,string);
  209. return SCM_MAKINUM (SCM_ROLENGTH (string));
  210. }
  211. #undef FUNC_NAME
  212. SCM_DEFINE (scm_string_ref, "string-ref", 2, 0, 0,
  213. (SCM str, SCM k),
  214. "Returns character K of STR using zero-origin indexing.\n"
  215. "K must be a valid index of STR.")
  216. #define FUNC_NAME s_scm_string_ref
  217. {
  218. int idx;
  219. SCM_VALIDATE_ROSTRING (1, str);
  220. SCM_VALIDATE_INUM_COPY (2, k, idx);
  221. SCM_ASSERT_RANGE (2, k, idx >= 0 && idx < SCM_ROLENGTH (str));
  222. return SCM_MAKE_CHAR (SCM_ROUCHARS (str)[idx]);
  223. }
  224. #undef FUNC_NAME
  225. SCM_DEFINE (scm_string_set_x, "string-set!", 3, 0, 0,
  226. (SCM str, SCM k, SCM chr),
  227. "Stores CHR in element K of STRING and returns an unspecified value.\n"
  228. "K must be a valid index of STR.")
  229. #define FUNC_NAME s_scm_string_set_x
  230. {
  231. SCM_VALIDATE_RWSTRING (1,str);
  232. SCM_VALIDATE_INUM_RANGE (2,k,0,SCM_LENGTH(str));
  233. SCM_VALIDATE_CHAR (3,chr);
  234. SCM_UCHARS (str)[SCM_INUM (k)] = SCM_CHAR (chr);
  235. return SCM_UNSPECIFIED;
  236. }
  237. #undef FUNC_NAME
  238. SCM_DEFINE (scm_substring, "substring", 2, 1, 0,
  239. (SCM str, SCM start, SCM end),
  240. "Returns a newly allocated string formed from the characters\n"
  241. "of STR beginning with index START (inclusive) and ending with\n"
  242. "index END (exclusive).\n"
  243. "STR must be a string, START and END must be exact integers satisfying:\n\n"
  244. "0 <= START <= END <= (string-length STR).")
  245. #define FUNC_NAME s_scm_substring
  246. {
  247. long l;
  248. SCM_VALIDATE_ROSTRING (1,str);
  249. SCM_VALIDATE_INUM (2,start);
  250. SCM_VALIDATE_INUM_DEF (3,end,SCM_ROLENGTH(str));
  251. SCM_ASSERT_RANGE (2,start,SCM_INUM (start) <= SCM_ROLENGTH (str));
  252. SCM_ASSERT_RANGE (2,end,SCM_INUM (end) <= SCM_ROLENGTH (str));
  253. l = SCM_INUM (end)-SCM_INUM (start);
  254. SCM_ASSERT (l >= 0, SCM_MAKINUM (l), SCM_OUTOFRANGE, FUNC_NAME);
  255. return scm_makfromstr (&SCM_ROCHARS (str)[SCM_INUM (start)], (scm_sizet)l, 0);
  256. }
  257. #undef FUNC_NAME
  258. SCM_DEFINE (scm_string_append, "string-append", 0, 0, 1,
  259. (SCM args),
  260. "Returns a newly allocated string whose characters form the\n"
  261. "concatenation of the given strings, ARGS.")
  262. #define FUNC_NAME s_scm_string_append
  263. {
  264. SCM res;
  265. register long i = 0;
  266. register SCM l, s;
  267. register unsigned char *data;
  268. SCM_VALIDATE_REST_ARGUMENT (args);
  269. for (l = args; !SCM_NULLP (l); l = SCM_CDR (l)) {
  270. s = SCM_CAR (l);
  271. SCM_VALIDATE_ROSTRING (SCM_ARGn,s);
  272. i += SCM_ROLENGTH (s);
  273. }
  274. res = scm_makstr (i, 0);
  275. data = SCM_UCHARS (res);
  276. for (l = args;SCM_NIMP (l);l = SCM_CDR (l)) {
  277. s = SCM_CAR (l);
  278. for (i = 0;i<SCM_ROLENGTH (s);i++) *data++ = SCM_ROUCHARS (s)[i];
  279. }
  280. return res;
  281. }
  282. #undef FUNC_NAME
  283. SCM_DEFINE (scm_make_shared_substring, "make-shared-substring", 1, 2, 0,
  284. (SCM str, SCM frm, SCM to),
  285. "Return a shared substring of @var{str}. The semantics are the same as\n"
  286. "for the @code{substring} function: the shared substring returned\n"
  287. "includes all of the text from @var{str} between indexes @var{start}\n"
  288. "(inclusive) and @var{end} (exclusive). If @var{end} is omitted, it\n"
  289. "defaults to the end of @var{str}. The shared substring returned by\n"
  290. "@code{make-shared-substring} occupies the same storage space as\n"
  291. "@var{str}.")
  292. #define FUNC_NAME s_scm_make_shared_substring
  293. {
  294. long f;
  295. long t;
  296. SCM answer;
  297. SCM len_str;
  298. SCM_VALIDATE_ROSTRING (1,str);
  299. SCM_VALIDATE_INUM_DEF_COPY (2,frm,0,f);
  300. SCM_VALIDATE_INUM_DEF_COPY (3,to,SCM_ROLENGTH(str),t);
  301. SCM_ASSERT_RANGE (2,frm,(f >= 0));
  302. SCM_ASSERT_RANGE (3,to, (f <= t) && (t <= SCM_ROLENGTH (str)));
  303. SCM_NEWCELL (answer);
  304. SCM_NEWCELL (len_str);
  305. SCM_DEFER_INTS;
  306. if (SCM_SUBSTRP (str))
  307. {
  308. long offset;
  309. offset = SCM_INUM (SCM_SUBSTR_OFFSET (str));
  310. f += offset;
  311. t += offset;
  312. SCM_SETCAR (len_str, SCM_MAKINUM (f));
  313. SCM_SETCDR (len_str, SCM_SUBSTR_STR (str));
  314. SCM_SETCDR (answer, len_str);
  315. SCM_SETLENGTH (answer, t - f, scm_tc7_substring);
  316. }
  317. else
  318. {
  319. SCM_SETCAR (len_str, SCM_MAKINUM (f));
  320. SCM_SETCDR (len_str, str);
  321. SCM_SETCDR (answer, len_str);
  322. SCM_SETLENGTH (answer, t - f, scm_tc7_substring);
  323. }
  324. SCM_ALLOW_INTS;
  325. return answer;
  326. }
  327. #undef FUNC_NAME
  328. void
  329. scm_init_strings ()
  330. {
  331. #include "libguile/strings.x"
  332. }
  333. /*
  334. Local Variables:
  335. c-file-style: "gnu"
  336. End:
  337. */