Commit | Line | Data |
---|---|---|
0f2d19dd JB |
1 | /* classes: src_files */ |
2 | ||
36284627 | 3 | /* Copyright (C) 1994,1996,1997,1999,2000,2001 Free Software Foundation, Inc. |
0f2d19dd | 4 | |
73be1d9e MV |
5 | * This library is free software; you can redistribute it and/or |
6 | * modify it under the terms of the GNU Lesser General Public | |
7 | * License as published by the Free Software Foundation; either | |
8 | * version 2.1 of the License, or (at your option) any later version. | |
9 | * | |
10 | * This library is distributed in the hope that it will be useful, | |
11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
13 | * Lesser General Public License for more details. | |
14 | * | |
15 | * You should have received a copy of the GNU Lesser General Public | |
16 | * License along with this library; if not, write to the Free Software | |
17 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA | |
18 | */ | |
0f2d19dd JB |
19 | |
20 | \f | |
6ada43c9 RB |
21 | #if HAVE_CONFIG_H |
22 | # include <config.h> | |
23 | #endif | |
0f2d19dd | 24 | |
e6e2e95a MD |
25 | #include <errno.h> |
26 | ||
a0599745 MD |
27 | #include "libguile/_scm.h" |
28 | #include "libguile/chars.h" | |
29 | #include "libguile/strings.h" | |
0f2d19dd | 30 | |
a0599745 MD |
31 | #include "libguile/validate.h" |
32 | #include "libguile/strop.h" | |
33 | #include "libguile/read.h" /*For SCM_CASE_INSENSITIVE_P*/ | |
bd9e24b3 GH |
34 | |
35 | #ifdef HAVE_STRING_H | |
36 | #include <string.h> | |
37 | #endif | |
38 | ||
0f2d19dd JB |
39 | \f |
40 | ||
6552dbf7 | 41 | /* |
5ad8ab0a | 42 | xSCM_DEFINE (scm_i_index, "i-index", 2, 2, 0, |
6552dbf7 | 43 | (SCM str, SCM chr, SCM frm, SCM to), |
9401323e | 44 | "@deftypefn {Internal C Function} {static int} scm_i_index (SCM *@var{str},\n" |
6552dbf7 GB |
45 | "SCM @var{chr}, int @var{direction}, SCM @var{sub_start}, SCM @var{sub_end}, char *@var{why}) |
46 | "This is a workhorse function that performs either an @code{index} or\n" | |
2b7b76d5 | 47 | "@code{rindex} function, depending on the value of @var{direction}." |
6552dbf7 | 48 | */ |
03bc4386 | 49 | /* implements index if direction > 0 otherwise rindex. */ |
c014a02e | 50 | static long |
7fd0a369 | 51 | scm_i_index (SCM str, SCM chr, int direction, SCM sub_start, |
99a9952d | 52 | SCM sub_end, const char *why) |
0f2d19dd JB |
53 | { |
54 | unsigned char * p; | |
c014a02e ML |
55 | long x; |
56 | long lower; | |
57 | long upper; | |
0f2d19dd JB |
58 | int ch; |
59 | ||
cc95e00a | 60 | SCM_ASSERT (scm_is_string (str), str, SCM_ARG1, why); |
7866a09b | 61 | SCM_ASSERT (SCM_CHARP (chr), chr, SCM_ARG2, why); |
0f2d19dd | 62 | |
7888309b | 63 | if (scm_is_false (sub_start)) |
e11e83f3 MV |
64 | lower = 0; |
65 | else | |
cc95e00a | 66 | lower = scm_to_signed_integer (sub_start, 0, scm_i_string_length(str)); |
0f2d19dd | 67 | |
7888309b | 68 | if (scm_is_false (sub_end)) |
cc95e00a | 69 | upper = scm_i_string_length (str); |
e11e83f3 | 70 | else |
cc95e00a | 71 | upper = scm_to_signed_integer (sub_end, lower, scm_i_string_length(str)); |
7fd0a369 MV |
72 | |
73 | x = -1; | |
03bc4386 GH |
74 | |
75 | if (direction > 0) | |
76 | { | |
cc95e00a | 77 | p = (unsigned char *) scm_i_string_chars (str) + lower; |
7866a09b | 78 | ch = SCM_CHAR (chr); |
03bc4386 | 79 | |
e11e83f3 | 80 | for (x = lower; x < upper; ++x, ++p) |
03bc4386 | 81 | if (*p == ch) |
7fd0a369 | 82 | goto found_it; |
03bc4386 | 83 | } |
0f2d19dd | 84 | else |
03bc4386 | 85 | { |
cc95e00a | 86 | p = upper - 1 + (unsigned char *)scm_i_string_chars (str); |
7866a09b | 87 | ch = SCM_CHAR (chr); |
03bc4386 GH |
88 | for (x = upper - 1; x >= lower; --x, --p) |
89 | if (*p == ch) | |
7fd0a369 | 90 | goto found_it; |
03bc4386 | 91 | } |
0f2d19dd | 92 | |
7fd0a369 MV |
93 | found_it: |
94 | scm_remember_upto_here_1 (str); | |
95 | return x; | |
0f2d19dd JB |
96 | } |
97 | ||
5ad8ab0a | 98 | SCM_DEFINE (scm_string_index, "string-index", 2, 2, 0, |
1bbd0b84 | 99 | (SCM str, SCM chr, SCM frm, SCM to), |
5352393c MG |
100 | "Return the index of the first occurrence of @var{chr} in\n" |
101 | "@var{str}. The optional integer arguments @var{frm} and\n" | |
102 | "@var{to} limit the search to a portion of the string. This\n" | |
103 | "procedure essentially implements the @code{index} or\n" | |
1e6808ea MG |
104 | "@code{strchr} functions from the C library.\n" |
105 | "\n" | |
106 | "@lisp\n" | |
1670bef9 | 107 | "(string-index \"weiner\" #\\e)\n" |
6552dbf7 | 108 | "@result{} 1\n\n" |
1670bef9 | 109 | "(string-index \"weiner\" #\\e 2)\n" |
6552dbf7 | 110 | "@result{} 4\n\n" |
1670bef9 | 111 | "(string-index \"weiner\" #\\e 2 4)\n" |
6552dbf7 | 112 | "@result{} #f\n" |
1e6808ea | 113 | "@end lisp") |
1bbd0b84 | 114 | #define FUNC_NAME s_scm_string_index |
0f2d19dd | 115 | { |
c014a02e | 116 | long pos; |
5ad8ab0a | 117 | |
54778cd3 | 118 | if (SCM_UNBNDP (frm)) |
0f2d19dd | 119 | frm = SCM_BOOL_F; |
54778cd3 | 120 | if (SCM_UNBNDP (to)) |
0f2d19dd | 121 | to = SCM_BOOL_F; |
7fd0a369 | 122 | pos = scm_i_index (str, chr, 1, frm, to, FUNC_NAME); |
0f2d19dd JB |
123 | return (pos < 0 |
124 | ? SCM_BOOL_F | |
e11e83f3 | 125 | : scm_from_long (pos)); |
0f2d19dd | 126 | } |
1bbd0b84 | 127 | #undef FUNC_NAME |
0f2d19dd | 128 | |
5ad8ab0a | 129 | SCM_DEFINE (scm_string_rindex, "string-rindex", 2, 2, 0, |
1bbd0b84 | 130 | (SCM str, SCM chr, SCM frm, SCM to), |
1e6808ea MG |
131 | "Like @code{string-index}, but search from the right of the\n" |
132 | "string rather than from the left. This procedure essentially\n" | |
133 | "implements the @code{rindex} or @code{strrchr} functions from\n" | |
134 | "the C library.\n" | |
135 | "\n" | |
136 | "@lisp\n" | |
1670bef9 | 137 | "(string-rindex \"weiner\" #\\e)\n" |
6552dbf7 | 138 | "@result{} 4\n\n" |
1670bef9 | 139 | "(string-rindex \"weiner\" #\\e 2 4)\n" |
6552dbf7 | 140 | "@result{} #f\n\n" |
1670bef9 | 141 | "(string-rindex \"weiner\" #\\e 2 5)\n" |
6552dbf7 | 142 | "@result{} 4\n" |
1e6808ea | 143 | "@end lisp") |
1bbd0b84 | 144 | #define FUNC_NAME s_scm_string_rindex |
0f2d19dd | 145 | { |
c014a02e | 146 | long pos; |
5ad8ab0a | 147 | |
54778cd3 | 148 | if (SCM_UNBNDP (frm)) |
0f2d19dd | 149 | frm = SCM_BOOL_F; |
54778cd3 | 150 | if (SCM_UNBNDP (to)) |
0f2d19dd | 151 | to = SCM_BOOL_F; |
7fd0a369 | 152 | pos = scm_i_index (str, chr, -1, frm, to, FUNC_NAME); |
0f2d19dd JB |
153 | return (pos < 0 |
154 | ? SCM_BOOL_F | |
e11e83f3 | 155 | : scm_from_long (pos)); |
0f2d19dd | 156 | } |
1bbd0b84 GB |
157 | #undef FUNC_NAME |
158 | ||
5ad8ab0a | 159 | SCM_DEFINE (scm_substring_move_x, "substring-move!", 5, 0, 0, |
1bbd0b84 | 160 | (SCM str1, SCM start1, SCM end1, SCM str2, SCM start2), |
b380b885 | 161 | "Copy the substring of @var{str1} bounded by @var{start1} and @var{end1}\n" |
5ad8ab0a | 162 | "into @var{str2} beginning at position @var{start2}.\n" |
0534158a | 163 | "@var{str1} and @var{str2} can be the same string.") |
1bbd0b84 | 164 | #define FUNC_NAME s_scm_substring_move_x |
0f2d19dd | 165 | { |
a55c2b68 | 166 | unsigned long s1, s2, e, len; |
cc95e00a MV |
167 | const char *src; |
168 | char *dst; | |
99a9952d | 169 | |
34d19ef6 | 170 | SCM_VALIDATE_STRING (1, str1); |
34d19ef6 | 171 | SCM_VALIDATE_STRING (4, str2); |
cc95e00a MV |
172 | s1 = scm_to_unsigned_integer (start1, 0, scm_i_string_length(str1)); |
173 | e = scm_to_unsigned_integer (end1, s1, scm_i_string_length(str1)); | |
99a9952d | 174 | len = e - s1; |
cc95e00a | 175 | s2 = scm_to_unsigned_integer (start2, 0, scm_i_string_length(str2)-len); |
0f2d19dd | 176 | |
cc95e00a MV |
177 | src = scm_i_string_chars (str2); |
178 | dst = scm_i_string_writable_chars (str1); | |
179 | SCM_SYSCALL (memmove (dst+s2, src+s1, len)); | |
180 | scm_i_string_stop_writing (); | |
5ad8ab0a | 181 | |
8824ac88 MV |
182 | scm_remember_upto_here_2 (str1, str2); |
183 | return SCM_UNSPECIFIED; | |
0f2d19dd | 184 | } |
1bbd0b84 | 185 | #undef FUNC_NAME |
0f2d19dd JB |
186 | |
187 | ||
5ad8ab0a | 188 | SCM_DEFINE (scm_substring_fill_x, "substring-fill!", 4, 0, 0, |
1bbd0b84 | 189 | (SCM str, SCM start, SCM end, SCM fill), |
1e6808ea MG |
190 | "Change every character in @var{str} between @var{start} and\n" |
191 | "@var{end} to @var{fill}.\n" | |
192 | "\n" | |
193 | "@lisp\n" | |
6552dbf7 | 194 | "(define y \"abcdefg\")\n" |
1670bef9 | 195 | "(substring-fill! y 1 3 #\\r)\n" |
6552dbf7 GB |
196 | "y\n" |
197 | "@result{} \"arrdefg\"\n" | |
1e6808ea | 198 | "@end lisp") |
1bbd0b84 | 199 | #define FUNC_NAME s_scm_substring_fill_x |
0f2d19dd | 200 | { |
a55c2b68 | 201 | size_t i, e; |
0f2d19dd | 202 | char c; |
cc95e00a MV |
203 | char *dst; |
204 | ||
34d19ef6 | 205 | SCM_VALIDATE_STRING (1, str); |
cc95e00a MV |
206 | i = scm_to_unsigned_integer (start, 0, scm_i_string_length (str)); |
207 | e = scm_to_unsigned_integer (end, i, scm_i_string_length (str)); | |
34d19ef6 | 208 | SCM_VALIDATE_CHAR_COPY (4, fill, c); |
cc95e00a | 209 | dst = scm_i_string_writable_chars (str); |
8824ac88 | 210 | while (i<e) |
cc95e00a MV |
211 | dst[i++] = c; |
212 | scm_i_string_stop_writing (); | |
213 | scm_remember_upto_here (str); | |
0f2d19dd JB |
214 | return SCM_UNSPECIFIED; |
215 | } | |
1bbd0b84 | 216 | #undef FUNC_NAME |
0f2d19dd JB |
217 | |
218 | ||
5ad8ab0a | 219 | SCM_DEFINE (scm_string_null_p, "string-null?", 1, 0, 0, |
1bbd0b84 | 220 | (SCM str), |
9c4c86c6 | 221 | "Return @code{#t} if @var{str}'s length is zero, and\n" |
1e6808ea MG |
222 | "@code{#f} otherwise.\n" |
223 | "@lisp\n" | |
224 | "(string-null? \"\") @result{} #t\n" | |
225 | "y @result{} \"foo\"\n" | |
226 | "(string-null? y) @result{} #f\n" | |
227 | "@end lisp") | |
1bbd0b84 | 228 | #define FUNC_NAME s_scm_string_null_p |
0f2d19dd | 229 | { |
34d19ef6 | 230 | SCM_VALIDATE_STRING (1, str); |
cc95e00a | 231 | return scm_from_bool (scm_i_string_length (str) == 0); |
0f2d19dd | 232 | } |
1bbd0b84 | 233 | #undef FUNC_NAME |
0f2d19dd JB |
234 | |
235 | ||
5ad8ab0a | 236 | SCM_DEFINE (scm_string_to_list, "string->list", 1, 0, 0, |
1bbd0b84 | 237 | (SCM str), |
91344ceb MG |
238 | "Return a newly allocated list of the characters that make up\n" |
239 | "the given string @var{str}. @code{string->list} and\n" | |
240 | "@code{list->string} are inverses as far as @samp{equal?} is\n" | |
241 | "concerned.") | |
1bbd0b84 | 242 | #define FUNC_NAME s_scm_string_to_list |
0f2d19dd | 243 | { |
c014a02e | 244 | long i; |
0f2d19dd | 245 | SCM res = SCM_EOL; |
cc95e00a | 246 | const unsigned char *src; |
34d19ef6 | 247 | SCM_VALIDATE_STRING (1, str); |
cc95e00a MV |
248 | src = scm_i_string_chars (str); |
249 | for (i = scm_i_string_length (str)-1;i >= 0;i--) | |
8824ac88 MV |
250 | res = scm_cons (SCM_MAKE_CHAR (src[i]), res); |
251 | scm_remember_upto_here_1 (src); | |
0f2d19dd JB |
252 | return res; |
253 | } | |
1bbd0b84 | 254 | #undef FUNC_NAME |
0f2d19dd JB |
255 | |
256 | ||
a49af0c0 DH |
257 | /* Helper function for the string copy and string conversion functions. |
258 | * No argument checking is performed. */ | |
259 | static SCM | |
260 | string_copy (SCM str) | |
261 | { | |
cc95e00a MV |
262 | const char* chars = scm_i_string_chars (str); |
263 | size_t length = scm_i_string_length (str); | |
264 | char *dst; | |
265 | SCM new_string = scm_i_make_string (length, &dst); | |
266 | memcpy (dst, chars, length); | |
36284627 DH |
267 | scm_remember_upto_here_1 (str); |
268 | return new_string; | |
a49af0c0 DH |
269 | } |
270 | ||
0f2d19dd | 271 | |
5ad8ab0a | 272 | SCM_DEFINE (scm_string_copy, "string-copy", 1, 0, 0, |
a49af0c0 | 273 | (SCM str), |
1e6808ea | 274 | "Return a newly allocated copy of the given @var{string}.") |
1bbd0b84 | 275 | #define FUNC_NAME s_scm_string_copy |
0f2d19dd | 276 | { |
d6a1cb3c | 277 | return scm_c_substring (str, 0, scm_c_string_length (str)); |
0f2d19dd | 278 | } |
1bbd0b84 | 279 | #undef FUNC_NAME |
0f2d19dd JB |
280 | |
281 | ||
3b3b36dd | 282 | SCM_DEFINE (scm_string_fill_x, "string-fill!", 2, 0, 0, |
1bbd0b84 | 283 | (SCM str, SCM chr), |
1e6808ea MG |
284 | "Store @var{char} in every element of the given @var{string} and\n" |
285 | "return an unspecified value.") | |
1bbd0b84 | 286 | #define FUNC_NAME s_scm_string_fill_x |
0f2d19dd | 287 | { |
8824ac88 MV |
288 | char *dst, c; |
289 | long k; | |
290 | SCM_VALIDATE_STRING (1, str); | |
34d19ef6 | 291 | SCM_VALIDATE_CHAR_COPY (2, chr, c); |
cc95e00a MV |
292 | dst = scm_i_string_writable_chars (str); |
293 | for (k = scm_i_string_length (str)-1;k >= 0;k--) | |
8824ac88 | 294 | dst[k] = c; |
cc95e00a | 295 | scm_i_string_stop_writing (); |
8824ac88 | 296 | scm_remember_upto_here_1 (str); |
0f2d19dd JB |
297 | return SCM_UNSPECIFIED; |
298 | } | |
1bbd0b84 | 299 | #undef FUNC_NAME |
0f2d19dd | 300 | |
a49af0c0 | 301 | |
5ad8ab0a | 302 | /* Helper function for the string uppercase conversion functions. |
a49af0c0 DH |
303 | * No argument checking is performed. */ |
304 | static SCM | |
305 | string_upcase_x (SCM v) | |
306 | { | |
cc95e00a MV |
307 | size_t k, len; |
308 | char *dst; | |
309 | ||
310 | len = scm_i_string_length (v); | |
311 | dst = scm_i_string_writable_chars (v); | |
312 | for (k = 0; k < len; ++k) | |
313 | dst[k] = scm_c_upcase (dst[k]); | |
314 | scm_i_string_stop_writing (); | |
a49af0c0 DH |
315 | return v; |
316 | } | |
317 | ||
318 | ||
5ad8ab0a | 319 | SCM_DEFINE (scm_string_upcase_x, "string-upcase!", 1, 0, 0, |
a49af0c0 | 320 | (SCM str), |
91344ceb MG |
321 | "Destructively upcase every character in @var{str} and return\n" |
322 | "@var{str}.\n" | |
323 | "@lisp\n" | |
324 | "y @result{} \"arrdefg\"\n" | |
325 | "(string-upcase! y) @result{} \"ARRDEFG\"\n" | |
326 | "y @result{} \"ARRDEFG\"\n" | |
327 | "@end lisp") | |
1bbd0b84 | 328 | #define FUNC_NAME s_scm_string_upcase_x |
c101e39e | 329 | { |
a49af0c0 | 330 | SCM_VALIDATE_STRING (1, str); |
322ac0c5 | 331 | |
a49af0c0 | 332 | return string_upcase_x (str); |
c101e39e | 333 | } |
1bbd0b84 | 334 | #undef FUNC_NAME |
c101e39e | 335 | |
a49af0c0 | 336 | |
5ad8ab0a | 337 | SCM_DEFINE (scm_string_upcase, "string-upcase", 1, 0, 0, |
a49af0c0 | 338 | (SCM str), |
91344ceb MG |
339 | "Return a freshly allocated string containing the characters of\n" |
340 | "@var{str} in upper case.") | |
1bbd0b84 | 341 | #define FUNC_NAME s_scm_string_upcase |
99a9952d | 342 | { |
a49af0c0 DH |
343 | SCM_VALIDATE_STRING (1, str); |
344 | ||
345 | return string_upcase_x (string_copy (str)); | |
99a9952d | 346 | } |
1bbd0b84 | 347 | #undef FUNC_NAME |
99a9952d | 348 | |
a49af0c0 | 349 | |
5ad8ab0a | 350 | /* Helper function for the string lowercase conversion functions. |
a49af0c0 DH |
351 | * No argument checking is performed. */ |
352 | static SCM | |
353 | string_downcase_x (SCM v) | |
354 | { | |
cc95e00a MV |
355 | size_t k, len; |
356 | char *dst; | |
a49af0c0 | 357 | |
cc95e00a MV |
358 | len = scm_i_string_length (v); |
359 | dst = scm_i_string_writable_chars (v); | |
360 | for (k = 0; k < len; ++k) | |
361 | dst[k] = scm_c_downcase (dst[k]); | |
362 | scm_i_string_stop_writing (); | |
a49af0c0 DH |
363 | |
364 | return v; | |
365 | } | |
366 | ||
367 | ||
5ad8ab0a | 368 | SCM_DEFINE (scm_string_downcase_x, "string-downcase!", 1, 0, 0, |
a49af0c0 | 369 | (SCM str), |
91344ceb MG |
370 | "Destructively downcase every character in @var{str} and return\n" |
371 | "@var{str}.\n" | |
372 | "@lisp\n" | |
373 | "y @result{} \"ARRDEFG\"\n" | |
374 | "(string-downcase! y) @result{} \"arrdefg\"\n" | |
375 | "y @result{} \"arrdefg\"\n" | |
376 | "@end lisp") | |
1bbd0b84 | 377 | #define FUNC_NAME s_scm_string_downcase_x |
c101e39e | 378 | { |
a49af0c0 | 379 | SCM_VALIDATE_STRING (1, str); |
322ac0c5 | 380 | |
a49af0c0 | 381 | return string_downcase_x (str); |
c101e39e | 382 | } |
1bbd0b84 | 383 | #undef FUNC_NAME |
0f2d19dd | 384 | |
a49af0c0 | 385 | |
5ad8ab0a | 386 | SCM_DEFINE (scm_string_downcase, "string-downcase", 1, 0, 0, |
a49af0c0 | 387 | (SCM str), |
91344ceb MG |
388 | "Return a freshly allocation string containing the characters in\n" |
389 | "@var{str} in lower case.") | |
1bbd0b84 | 390 | #define FUNC_NAME s_scm_string_downcase |
99a9952d | 391 | { |
a49af0c0 DH |
392 | SCM_VALIDATE_STRING (1, str); |
393 | ||
394 | return string_downcase_x (string_copy (str)); | |
99a9952d | 395 | } |
1bbd0b84 | 396 | #undef FUNC_NAME |
99a9952d | 397 | |
a49af0c0 | 398 | |
5ad8ab0a | 399 | /* Helper function for the string capitalization functions. |
a49af0c0 DH |
400 | * No argument checking is performed. */ |
401 | static SCM | |
402 | string_capitalize_x (SCM str) | |
99a9952d | 403 | { |
ff0a837c | 404 | unsigned char *sz; |
cc95e00a | 405 | size_t i, len; |
1be6b49c | 406 | int in_word=0; |
a49af0c0 | 407 | |
cc95e00a MV |
408 | len = scm_i_string_length (str); |
409 | sz = scm_i_string_writable_chars (str); | |
410 | for (i = 0; i < len; i++) | |
411 | { | |
412 | if (scm_is_true (scm_char_alphabetic_p (SCM_MAKE_CHAR (sz[i])))) | |
413 | { | |
414 | if (!in_word) | |
415 | { | |
416 | sz[i] = scm_c_upcase (sz[i]); | |
417 | in_word = 1; | |
418 | } | |
419 | else | |
420 | { | |
421 | sz[i] = scm_c_downcase (sz[i]); | |
422 | } | |
423 | } | |
424 | else | |
425 | in_word = 0; | |
99a9952d | 426 | } |
cc95e00a | 427 | scm_i_string_stop_writing (); |
6552dbf7 | 428 | return str; |
99a9952d | 429 | } |
a49af0c0 DH |
430 | |
431 | ||
5ad8ab0a | 432 | SCM_DEFINE (scm_string_capitalize_x, "string-capitalize!", 1, 0, 0, |
a49af0c0 | 433 | (SCM str), |
91344ceb MG |
434 | "Upcase the first character of every word in @var{str}\n" |
435 | "destructively and return @var{str}.\n" | |
436 | "\n" | |
437 | "@lisp\n" | |
dd85ce47 ML |
438 | "y @result{} \"hello world\"\n" |
439 | "(string-capitalize! y) @result{} \"Hello World\"\n" | |
440 | "y @result{} \"Hello World\"\n" | |
91344ceb | 441 | "@end lisp") |
a49af0c0 DH |
442 | #define FUNC_NAME s_scm_string_capitalize_x |
443 | { | |
444 | SCM_VALIDATE_STRING (1, str); | |
445 | ||
446 | return string_capitalize_x (str); | |
447 | } | |
1bbd0b84 | 448 | #undef FUNC_NAME |
99a9952d | 449 | |
a49af0c0 | 450 | |
5ad8ab0a | 451 | SCM_DEFINE (scm_string_capitalize, "string-capitalize", 1, 0, 0, |
a49af0c0 | 452 | (SCM str), |
91344ceb MG |
453 | "Return a freshly allocated string with the characters in\n" |
454 | "@var{str}, where the first character of every word is\n" | |
455 | "capitalized.") | |
1bbd0b84 | 456 | #define FUNC_NAME s_scm_string_capitalize |
99a9952d | 457 | { |
a49af0c0 DH |
458 | SCM_VALIDATE_STRING (1, str); |
459 | ||
460 | return string_capitalize_x (string_copy (str)); | |
99a9952d | 461 | } |
1bbd0b84 | 462 | #undef FUNC_NAME |
99a9952d | 463 | |
a49af0c0 | 464 | |
5ad8ab0a | 465 | SCM_DEFINE (scm_string_split, "string-split", 2, 0, 0, |
dd2a6f3a MG |
466 | (SCM str, SCM chr), |
467 | "Split the string @var{str} into the a list of the substrings delimited\n" | |
468 | "by appearances of the character @var{chr}. Note that an empty substring\n" | |
469 | "between separator characters will result in an empty string in the\n" | |
470 | "result list.\n" | |
471 | "\n" | |
472 | "@lisp\n" | |
8f85c0c6 | 473 | "(string-split \"root:x:0:0:root:/root:/bin/bash\" #\\:)\n" |
dd2a6f3a MG |
474 | "@result{}\n" |
475 | "(\"root\" \"x\" \"0\" \"0\" \"root\" \"/root\" \"/bin/bash\")\n" | |
476 | "\n" | |
8f85c0c6 | 477 | "(string-split \"::\" #\\:)\n" |
dd2a6f3a MG |
478 | "@result{}\n" |
479 | "(\"\" \"\" \"\")\n" | |
480 | "\n" | |
8f85c0c6 | 481 | "(string-split \"\" #\\:)\n" |
dd2a6f3a MG |
482 | "@result{}\n" |
483 | "(\"\")\n" | |
484 | "@end lisp") | |
485 | #define FUNC_NAME s_scm_string_split | |
486 | { | |
c014a02e | 487 | long idx, last_idx; |
cc95e00a | 488 | const char * p; |
dd2a6f3a MG |
489 | int ch; |
490 | SCM res = SCM_EOL; | |
491 | ||
492 | SCM_VALIDATE_STRING (1, str); | |
493 | SCM_VALIDATE_CHAR (2, chr); | |
494 | ||
cc95e00a MV |
495 | idx = scm_i_string_length (str); |
496 | p = scm_i_string_chars (str); | |
dd2a6f3a MG |
497 | ch = SCM_CHAR (chr); |
498 | while (idx >= 0) | |
499 | { | |
500 | last_idx = idx; | |
501 | while (idx > 0 && p[idx - 1] != ch) | |
502 | idx--; | |
503 | if (idx >= 0) | |
504 | { | |
cc95e00a MV |
505 | res = scm_cons (scm_c_substring (str, idx, last_idx), res); |
506 | p = scm_i_string_chars (str); | |
dd2a6f3a MG |
507 | idx--; |
508 | } | |
509 | } | |
36284627 | 510 | scm_remember_upto_here_1 (str); |
dd2a6f3a MG |
511 | return res; |
512 | } | |
513 | #undef FUNC_NAME | |
514 | ||
515 | ||
5ad8ab0a | 516 | SCM_DEFINE (scm_string_ci_to_symbol, "string-ci->symbol", 1, 0, 0, |
91344ceb MG |
517 | (SCM str), |
518 | "Return the symbol whose name is @var{str}. @var{str} is\n" | |
519 | "converted to lowercase before the conversion is done, if Guile\n" | |
8f85c0c6 | 520 | "is currently reading symbols case-insensitively.") |
1bbd0b84 | 521 | #define FUNC_NAME s_scm_string_ci_to_symbol |
99a9952d JB |
522 | { |
523 | return scm_string_to_symbol (SCM_CASE_INSENSITIVE_P | |
524 | ? scm_string_downcase(str) | |
525 | : str); | |
526 | } | |
1bbd0b84 | 527 | #undef FUNC_NAME |
1cc91f1b | 528 | |
0f2d19dd JB |
529 | void |
530 | scm_init_strop () | |
0f2d19dd | 531 | { |
a0599745 | 532 | #include "libguile/strop.x" |
0f2d19dd | 533 | } |
89e00824 ML |
534 | |
535 | /* | |
536 | Local Variables: | |
537 | c-file-style: "gnu" | |
538 | End: | |
539 | */ |