1 /* classes: src_files */
3 /* Copyright (C) 1994, 1996, 1997, 1999 Free Software Foundation, Inc.
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation; either version 2, or (at your option)
10 This program 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
13 GNU General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with this software; see the file COPYING. If not, write to the
17 Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
20 /* Software engineering face-lift by Greg J. Badros, 11-Dec-1999,
21 gjb@cs.washington.edu, http://www.cs.washington.edu/homes/gjb */
30 #include "scm_validate.h"
32 #include "read.h" /*For SCM_CASE_INSENSITIVE_P*/
41 xSCM_DEFINE (scm_i_index, "i-index", 2, 2, 0,
42 (SCM str, SCM chr, SCM frm, SCM to),
43 "@deftypefn {Internal C Function} {static int} scm_i_index (SCM *@var{str}, \n"
44 "SCM @var{chr}, int @var{direction}, SCM @var{sub_start}, SCM @var{sub_end}, char *@var{why})
45 "This is a workhorse function that performs either an @code{index} or\n"
46 "@code{rindex} function, depending on the value of @var{direction}. I'm\n"
47 "not at all clear on the usage of the pos arguments, though the purpose\n"
48 "seems to be correct reporting of which argument values are reporting\n"
49 "errors. Why you would do that, rather than just use @code{SCM_ARG[1234]}\n"
50 "explicitly is beyond me. Anyone holding any enlightenment?"
52 /* implements index if direction > 0 otherwise rindex. */
54 scm_i_index (SCM
*str
, SCM chr
, int direction
, SCM sub_start
,
55 SCM sub_end
, const char *why
)
63 SCM_ASSERT (SCM_ROSTRINGP (*str
), *str
, SCM_ARG1
, why
);
64 SCM_ASSERT (SCM_ICHRP (chr
), chr
, SCM_ARG2
, why
);
66 if (sub_start
== SCM_BOOL_F
)
67 sub_start
= SCM_MAKINUM (0);
69 SCM_ASSERT (SCM_INUMP (sub_start
), sub_start
, SCM_ARG3
, why
);
70 lower
= SCM_INUM (sub_start
);
72 || lower
> SCM_ROLENGTH (*str
))
73 scm_out_of_range (why
, sub_start
);
75 if (sub_end
== SCM_BOOL_F
)
76 sub_end
= SCM_MAKINUM (SCM_ROLENGTH (*str
));
78 SCM_ASSERT (SCM_INUMP (sub_end
), sub_end
, SCM_ARG4
, why
);
79 upper
= SCM_INUM (sub_end
);
80 if (upper
< SCM_INUM (sub_start
)
81 || upper
> SCM_ROLENGTH (*str
))
82 scm_out_of_range (why
, sub_end
);
86 p
= (unsigned char *)SCM_ROCHARS (*str
) + lower
;
89 for (x
= SCM_INUM (sub_start
); x
< upper
; ++x
, ++p
)
95 p
= upper
- 1 + (unsigned char *)SCM_ROCHARS (*str
);
97 for (x
= upper
- 1; x
>= lower
; --x
, --p
)
105 SCM_DEFINE (scm_string_index
, "string-index", 2, 2, 0,
106 (SCM str
, SCM chr
, SCM frm
, SCM to
),
107 "Return the index of the first occurrence of @var{chr} in @var{str}. The\n"
108 "optional integer arguments @var{frm} and @var{to} limit the search to\n"
109 "a portion of the string. This procedure essentially implements the\n"
110 "@code{index} or @code{strchr} functions from the C library.\n\n"
111 "(qdocs:) Returns the index of @var{char} in @var{str}, or @code{#f} if the\n"
112 "@var{char} isn't in @var{str}. If @var{frm} is given and not @code{#f},\n"
113 "it is used as the starting index; if @var{to} is given and not @var{#f},\n"
114 "it is used as the ending index (exclusive).\n\n"
116 "(string-index "weiner
" #\e)\n"
118 "(string-index "weiner
" #\e 2)\n"
120 "(string-index "weiner
" #\e 2 4)\n"
123 #define FUNC_NAME s_scm_string_index
127 if (frm
== SCM_UNDEFINED
)
129 if (to
== SCM_UNDEFINED
)
131 pos
= scm_i_index (&str
, chr
, 1, frm
, to
, FUNC_NAME
);
134 : SCM_MAKINUM (pos
));
138 SCM_DEFINE (scm_string_rindex
, "string-rindex", 2, 2, 0,
139 (SCM str
, SCM chr
, SCM frm
, SCM to
),
140 "Like @code{string-index}, but search from the right of the string rather\n"
141 "than from the left. This procedure essentially implements the\n"
142 "@code{rindex} or @code{strrchr} functions from the C library.\n\n"
143 "(qdocs:) The same as @code{string-index}, except it gives the rightmost occurance\n"
144 "of @var{char} in the range [@var{frm}, @var{to}-1], which defaults to\n"
145 "the entire string.\n\n"
147 "(string-rindex "weiner
" #\e)\n"
149 "(string-rindex "weiner
" #\e 2 4)\n"
151 "(string-rindex "weiner
" #\e 2 5)\n"
154 #define FUNC_NAME s_scm_string_rindex
158 if (frm
== SCM_UNDEFINED
)
160 if (to
== SCM_UNDEFINED
)
162 pos
= scm_i_index (&str
, chr
, -1, frm
, to
, FUNC_NAME
);
165 : SCM_MAKINUM (pos
));
170 SCM_REGISTER_PROC(s_substring_move_left_x
, "substring-move-left!", 5, 0, 0, scm_substring_move_x
);
171 SCM_REGISTER_PROC(s_substring_move_right_x
, "substring-move-right!", 5, 0, 0, scm_substring_move_x
);
174 @defun substring-move-left! str1 start1 end1 str2 start2
176 @deftypefn {C Function} SCM scm_substring_move_left_x (SCM @var{str1}, SCM @var{start1}, SCM @var{end1}, SCM @var{str2}, SCM @var{start2})
177 [@strong{Note:} this is only valid if you've applied the strop patch].
179 Moves a substring of @var{str1}, from @var{start1} to @var{end1}
180 (@var{end1} is exclusive), into @var{str2}, starting at
181 @var{start2}. Allows overlapping strings.
184 (define x (make-string 10 #\a))
186 (substring-move-left! x 2 5 y 0)
191 @result{} "aaaaaaaaaa"
194 (substring-move-left! x 2 5 y 0)
199 (substring-move-left! y 2 5 y 3)
206 @defun substring-move-right! str1 start1 end1 str2 start2
208 @deftypefn {C Function} SCM scm_substring_move_right_x (SCM @var{str1}, SCM @var{start1}, SCM @var{end1}, SCM @var{str2}, SCM @var{start2})
209 [@strong{Note:} this is only valid if you've applied the strop patch, if
210 it hasn't made it into the guile tree].
212 Does much the same thing as @code{substring-move-left!}, except it
213 starts moving at the end of the sequence, rather than the beginning.
216 (substring-move-right! y 2 5 y 0)
221 (substring-move-right! y 2 5 y 3)
227 SCM_DEFINE (scm_substring_move_x
, "substring-move!", 5, 0, 0,
228 (SCM str1
, SCM start1
, SCM end1
, SCM str2
, SCM start2
),
229 "Copy the substring of @var{str1} bounded by @var{start1} and @var{end1}\n"
230 "into @var{str2} beginning at position @var{end2}.\n"
231 "@code{substring-move-right!} begins copying from the rightmost character\n"
232 "and moves left, and @code{substring-move-left!} copies from the leftmost\n"
233 "character moving right.\n\n"
234 "It is useful to have two functions that copy in different directions so\n"
235 "that substrings can be copied back and forth within a single string. If\n"
236 "you wish to copy text from the left-hand side of a string to the\n"
237 "right-hand side of the same string, and the source and destination\n"
238 "overlap, you must be careful to copy the rightmost characters of the\n"
239 "text first, to avoid clobbering your data. Hence, when @var{str1} and\n"
240 "@var{str2} are the same string, you should use\n"
241 "@code{substring-move-right!} when moving text from left to right, and\n"
242 "@code{substring-move-left!} otherwise. If @code{str1} and @samp{str2}\n"
243 "are different strings, it does not matter which function you use.")
244 #define FUNC_NAME s_scm_substring_move_x
248 SCM_VALIDATE_STRING (1,str1
);
249 SCM_VALIDATE_INUM_COPY (2,start1
,s1
);
250 SCM_VALIDATE_INUM_COPY (3,end1
,e
);
251 SCM_VALIDATE_STRING (4,str2
);
252 SCM_VALIDATE_INUM_COPY (5,start2
,s2
);
254 SCM_ASSERT_RANGE (3,end1
,len
>= 0);
255 SCM_ASSERT_RANGE (2,start1
,s1
<= SCM_LENGTH (str1
) && s1
>= 0);
256 SCM_ASSERT_RANGE (5,start2
,s2
<= SCM_LENGTH (str2
) && s2
>= 0);
257 SCM_ASSERT_RANGE (3,end1
,e
<= SCM_LENGTH (str1
) && e
>= 0);
258 SCM_ASSERT_RANGE (5,start2
,len
+s2
<= SCM_LENGTH (str2
));
260 SCM_SYSCALL(memmove((void *)(&(SCM_CHARS(str2
)[s2
])),
261 (void *)(&(SCM_CHARS(str1
)[s1
])),
264 return scm_return_first(SCM_UNSPECIFIED
, str1
, str2
);
269 SCM_DEFINE (scm_substring_fill_x
, "substring-fill!", 4, 0, 0,
270 (SCM str
, SCM start
, SCM end
, SCM fill
),
271 "Change every character in @var{str} between @var{start} and @var{end} to\n"
272 "@var{fill-char}.\n\n"
273 "(qdocs:) Destructively fills @var{str}, from @var{start} to @var{end}, with @var{fill}.\n\n"
275 "(define y \"abcdefg\")\n"
276 "(substring-fill! y 1 3 #\r)\n"
278 "@result{} \"arrdefg\"\n"
280 #define FUNC_NAME s_scm_substring_fill_x
284 SCM_VALIDATE_STRING (1,str
);
285 SCM_VALIDATE_INUM_COPY (2,start
,i
);
286 SCM_VALIDATE_INUM_COPY (3,end
,e
);
287 SCM_VALIDATE_ICHR_COPY (4,fill
,c
);
288 SCM_ASSERT_RANGE (2,start
,i
<= SCM_LENGTH (str
) && i
>= 0);
289 SCM_ASSERT_RANGE (3,end
,e
<= SCM_LENGTH (str
) && e
>= 0);
290 while (i
<e
) SCM_CHARS (str
)[i
++] = c
;
291 return SCM_UNSPECIFIED
;
296 SCM_DEFINE (scm_string_null_p
, "string-null?", 1, 0, 0,
298 "Return @code{#t} if @var{str}'s length is nonzero, and @code{#f}\n"
300 "(qdocs:) Returns @code{#t} if @var{str} is empty, else returns @code{#f}.\n\n"
302 "(string-null? \"\")\n"
307 #define FUNC_NAME s_scm_string_null_p
309 SCM_VALIDATE_ROSTRING (1,str
);
310 return SCM_NEGATE_BOOL(SCM_ROLENGTH (str
));
315 SCM_DEFINE (scm_string_to_list
, "string->list", 1, 0, 0,
317 "@samp{String->list} returns a newly allocated list of the\n"
318 "characters that make up the given string. @samp{List->string}\n"
319 "returns a newly allocated string formed from the characters in the list\n"
320 "@var{list}, which must be a list of characters. @samp{String->list}\n"
321 "and @samp{list->string} are\n"
322 "inverses so far as @samp{equal?} is concerned. (r5rs)")
323 #define FUNC_NAME s_scm_string_to_list
328 SCM_VALIDATE_ROSTRING (1,str
);
329 src
= SCM_ROUCHARS (str
);
330 for (i
= SCM_ROLENGTH (str
)-1;i
>= 0;i
--) res
= scm_cons ((SCM
)SCM_MAKICHR (src
[i
]), res
);
337 SCM_DEFINE (scm_string_copy
, "string-copy", 1, 0, 0,
339 "Returns a newly allocated copy of the given @var{string}. (r5rs)")
340 #define FUNC_NAME s_scm_string_copy
342 SCM_VALIDATE_STRINGORSUBSTR (1,str
);
343 return scm_makfromstr (SCM_ROCHARS (str
), (scm_sizet
)SCM_ROLENGTH (str
), 0);
348 SCM_DEFINE (scm_string_fill_x
, "string-fill!", 2, 0, 0,
350 "Stores @var{char} in every element of the given @var{string} and returns an\n"
351 "unspecified value. (r5rs)")
352 #define FUNC_NAME s_scm_string_fill_x
354 register char *dst
, c
;
356 SCM_VALIDATE_STRING_COPY (1,str
,dst
);
357 SCM_VALIDATE_ICHR_COPY (2,chr
,c
);
358 for (k
= SCM_LENGTH (str
)-1;k
>= 0;k
--) dst
[k
] = c
;
359 return SCM_UNSPECIFIED
;
363 SCM_DEFINE (scm_string_upcase_x
, "string-upcase!", 1, 0, 0,
365 "Destructively upcase every character in @code{str}.\n\n"
366 "(qdocs:) Converts each element in @var{str} to upper case.\n\n"
368 "(string-upcase! y)\n"
369 "@result{} \"ARRDEFG\"\n\n"
371 "@result{} \"ARRDEFG\"\n"
373 #define FUNC_NAME s_scm_string_upcase_x
376 register unsigned char *cs
;
377 SCM_ASRTGO (SCM_NIMP (v
), badarg1
);
385 cs
[k
] = scm_upcase(cs
[k
]);
388 badarg1
:SCM_WTA (1,v
);
394 SCM_DEFINE (scm_string_upcase
, "string-upcase", 1, 0, 0,
396 "Upcase every character in @code{str}.")
397 #define FUNC_NAME s_scm_string_upcase
399 return scm_string_upcase_x(scm_string_copy(str
));
403 SCM_DEFINE (scm_string_downcase_x
, "string-downcase!", 1, 0, 0,
405 "Destructively downcase every character in @code{str}.\n\n"
406 "(qdocs:) Converts each element in @var{str} to lower case.\n\n"
409 "@result{} \"ARRDEFG\"\n\n"
410 "(string-downcase! y)\n"
411 "@result{} \"arrdefg\"\n\n"
413 "@result{} \"arrdefg\"\n"
415 #define FUNC_NAME s_scm_string_downcase_x
418 register unsigned char *cs
;
419 SCM_ASRTGO (SCM_NIMP (v
), badarg1
);
426 cs
[k
] = scm_downcase(cs
[k
]);
429 badarg1
:SCM_WTA (1,v
);
435 SCM_DEFINE (scm_string_downcase
, "string-downcase", 1, 0, 0,
437 "Downcase every character in @code{str}.")
438 #define FUNC_NAME s_scm_string_downcase
440 SCM_VALIDATE_STRING (1,str
);
441 return scm_string_downcase_x(scm_string_copy(str
));
445 SCM_DEFINE (scm_string_capitalize_x
, "string-capitalize!", 1, 0, 0,
447 "Destructively capitalize every character in @code{str}.")
448 #define FUNC_NAME s_scm_string_capitalize_x
451 int i
, len
, in_word
=0;
452 SCM_VALIDATE_STRING (1,str
);
453 len
= SCM_LENGTH(str
);
455 for(i
=0; i
<len
; i
++) {
456 if(SCM_NFALSEP(scm_char_alphabetic_p(SCM_MAKICHR(sz
[i
])))) {
458 sz
[i
] = scm_upcase(sz
[i
]);
461 sz
[i
] = scm_downcase(sz
[i
]);
470 SCM_DEFINE (scm_string_capitalize
, "string-capitalize", 1, 0, 0,
472 "Capitalize every character in @code{str}.")
473 #define FUNC_NAME s_scm_string_capitalize
475 SCM_VALIDATE_STRING (1,str
);
476 return scm_string_capitalize_x(scm_string_copy(str
));
480 SCM_DEFINE (scm_string_ci_to_symbol
, "string-ci->symbol", 1, 0, 0,
482 "Return the symbol whose name is @var{str}, downcased in necessary(???).")
483 #define FUNC_NAME s_scm_string_ci_to_symbol
485 return scm_string_to_symbol (SCM_CASE_INSENSITIVE_P
486 ? scm_string_downcase(str
)