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*/
36 /* implements index if direction > 0 otherwise rindex. */
38 scm_i_index (SCM
*str
, SCM chr
, int direction
, SCM sub_start
,
39 SCM sub_end
, const char *why
)
47 SCM_ASSERT (SCM_NIMP (*str
) && SCM_ROSTRINGP (*str
), *str
, SCM_ARG1
, why
);
48 SCM_ASSERT (SCM_ICHRP (chr
), chr
, SCM_ARG2
, why
);
50 if (sub_start
== SCM_BOOL_F
)
51 sub_start
= SCM_MAKINUM (0);
53 SCM_ASSERT (SCM_INUMP (sub_start
), sub_start
, SCM_ARG3
, why
);
54 lower
= SCM_INUM (sub_start
);
56 || lower
> SCM_ROLENGTH (*str
))
57 scm_out_of_range (why
, sub_start
);
59 if (sub_end
== SCM_BOOL_F
)
60 sub_end
= SCM_MAKINUM (SCM_ROLENGTH (*str
));
62 SCM_ASSERT (SCM_INUMP (sub_end
), sub_end
, SCM_ARG4
, why
);
63 upper
= SCM_INUM (sub_end
);
64 if (upper
< SCM_INUM (sub_start
)
65 || upper
> SCM_ROLENGTH (*str
))
66 scm_out_of_range (why
, sub_end
);
70 p
= (unsigned char *)SCM_ROCHARS (*str
) + lower
;
73 for (x
= SCM_INUM (sub_start
); x
< upper
; ++x
, ++p
)
79 p
= upper
- 1 + (unsigned char *)SCM_ROCHARS (*str
);
81 for (x
= upper
- 1; x
>= lower
; --x
, --p
)
89 GUILE_PROC(scm_string_index
, "string-index", 2, 2, 0,
90 (SCM str
, SCM chr
, SCM frm
, SCM to
),
92 #define FUNC_NAME s_scm_string_index
96 if (frm
== SCM_UNDEFINED
)
98 if (to
== SCM_UNDEFINED
)
100 pos
= scm_i_index (&str
, chr
, 1, frm
, to
, FUNC_NAME
);
103 : SCM_MAKINUM (pos
));
107 GUILE_PROC(scm_string_rindex
, "string-rindex", 2, 2, 0,
108 (SCM str
, SCM chr
, SCM frm
, SCM to
),
110 #define FUNC_NAME s_scm_string_rindex
114 if (frm
== SCM_UNDEFINED
)
116 if (to
== SCM_UNDEFINED
)
118 pos
= scm_i_index (&str
, chr
, -1, frm
, to
, FUNC_NAME
);
121 : SCM_MAKINUM (pos
));
126 SCM_REGISTER_PROC(s_substring_move_left_x
, "substring-move-left!", 5, 0, 0, scm_substring_move_x
);
127 SCM_REGISTER_PROC(s_substring_move_right_x
, "substring-move-right!", 5, 0, 0, scm_substring_move_x
);
130 GUILE_PROC(scm_substring_move_x
, "substring-move!", 5, 0, 0,
131 (SCM str1
, SCM start1
, SCM end1
, SCM str2
, SCM start2
),
133 #define FUNC_NAME s_scm_substring_move_x
137 SCM_VALIDATE_STRING(1,str1
);
138 SCM_VALIDATE_INT_COPY(2,start1
,s1
);
139 SCM_VALIDATE_INT_COPY(3,end1
,e
);
140 SCM_VALIDATE_STRING(4,str2
);
141 SCM_VALIDATE_INT_COPY(5,start2
,s2
);
143 SCM_ASSERT_RANGE (3,end1
,len
>= 0);
144 SCM_ASSERT_RANGE (2,start1
,s1
<= SCM_LENGTH (str1
) && s1
>= 0);
145 SCM_ASSERT_RANGE (5,start2
,s2
<= SCM_LENGTH (str2
) && s2
>= 0);
146 SCM_ASSERT_RANGE (3,end1
,e
<= SCM_LENGTH (str1
) && e
>= 0);
147 SCM_ASSERT_RANGE (5,start2
,len
+s2
<= SCM_LENGTH (str2
));
149 SCM_SYSCALL(memmove((void *)(&(SCM_CHARS(str2
)[s2
])),
150 (void *)(&(SCM_CHARS(str1
)[s1
])),
153 return scm_return_first(SCM_UNSPECIFIED
, str1
, str2
);
158 GUILE_PROC(scm_substring_fill_x
, "substring-fill!", 4, 0, 0,
159 (SCM str
, SCM start
, SCM end
, SCM fill
),
161 #define FUNC_NAME s_scm_substring_fill_x
165 SCM_VALIDATE_STRING(1,str
);
166 SCM_VALIDATE_INT_COPY(2,start
,i
);
167 SCM_VALIDATE_INT_COPY(3,end
,e
);
168 SCM_VALIDATE_CHAR_COPY(4,fill
,c
);
169 SCM_ASSERT_RANGE (2,start
,i
<= SCM_LENGTH (str
) && i
>= 0);
170 SCM_ASSERT_RANGE (3,end
,e
<= SCM_LENGTH (str
) && e
>= 0);
171 while (i
<e
) SCM_CHARS (str
)[i
++] = c
;
172 return SCM_UNSPECIFIED
;
177 GUILE_PROC(scm_string_null_p
, "string-null?", 1, 0, 0,
180 #define FUNC_NAME s_scm_string_null_p
182 SCM_VALIDATE_ROSTRING(1,str
);
183 return SCM_NEGATE_BOOL(SCM_ROLENGTH (str
));
188 GUILE_PROC(scm_string_to_list
, "string->list", 1, 0, 0,
191 #define FUNC_NAME s_scm_string_to_list
196 SCM_VALIDATE_ROSTRING(1,str
);
197 src
= SCM_ROUCHARS (str
);
198 for (i
= SCM_ROLENGTH (str
)-1;i
>= 0;i
--) res
= scm_cons ((SCM
)SCM_MAKICHR (src
[i
]), res
);
205 GUILE_PROC(scm_string_copy
, "string-copy", 1, 0, 0,
208 #define FUNC_NAME s_scm_string_copy
210 SCM_VALIDATE_STRINGORSUBSTR(1,str
);
211 return scm_makfromstr (SCM_ROCHARS (str
), (scm_sizet
)SCM_ROLENGTH (str
), 0);
216 GUILE_PROC(scm_string_fill_x
, "string-fill!", 2, 0, 0,
219 #define FUNC_NAME s_scm_string_fill_x
221 register char *dst
, c
;
223 SCM_VALIDATE_STRING_COPY(1,str
,dst
);
224 SCM_VALIDATE_CHAR_COPY(2,chr
,c
);
225 for (k
= SCM_LENGTH (str
)-1;k
>= 0;k
--) dst
[k
] = c
;
226 return SCM_UNSPECIFIED
;
230 GUILE_PROC(scm_string_upcase_x
, "string-upcase!", 1, 0, 0,
233 #define FUNC_NAME s_scm_string_upcase_x
236 register unsigned char *cs
;
237 SCM_ASRTGO (SCM_NIMP (v
), badarg1
);
245 cs
[k
] = scm_upcase(cs
[k
]);
248 badarg1
:SCM_WTA (1,v
);
254 GUILE_PROC(scm_string_upcase
, "string-upcase", 1, 0, 0,
257 #define FUNC_NAME s_scm_string_upcase
259 return scm_string_upcase_x(scm_string_copy(str
));
263 GUILE_PROC(scm_string_downcase_x
, "string-downcase!", 1, 0, 0,
266 #define FUNC_NAME s_scm_string_downcase_x
269 register unsigned char *cs
;
270 SCM_ASRTGO (SCM_NIMP (v
), badarg1
);
277 cs
[k
] = scm_downcase(cs
[k
]);
280 badarg1
:SCM_WTA (1,v
);
286 GUILE_PROC(scm_string_downcase
, "string-downcase", 1, 0, 0,
289 #define FUNC_NAME s_scm_string_downcase
291 SCM_VALIDATE_STRING(1,str
);
292 return scm_string_downcase_x(scm_string_copy(str
));
296 GUILE_PROC(scm_string_capitalize_x
, "string-capitalize!", 1, 0, 0,
299 #define FUNC_NAME s_scm_string_capitalize_x
302 int i
, len
, in_word
=0;
303 SCM_VALIDATE_STRING(1,s
);
306 for(i
=0; i
<len
; i
++) {
307 if(SCM_NFALSEP(scm_char_alphabetic_p(SCM_MAKICHR(str
[i
])))) {
309 str
[i
] = scm_upcase(str
[i
]);
312 str
[i
] = scm_downcase(str
[i
]);
321 GUILE_PROC(scm_string_capitalize
, "string-capitalize", 1, 0, 0,
324 #define FUNC_NAME s_scm_string_capitalize
326 SCM_VALIDATE_STRING(1,s
);
327 return scm_string_capitalize_x(scm_string_copy(s
));
331 GUILE_PROC(scm_string_ci_to_symbol
, "string-ci->symbol", 1, 0, 0,
334 #define FUNC_NAME s_scm_string_ci_to_symbol
336 return scm_string_to_symbol (SCM_CASE_INSENSITIVE_P
337 ? scm_string_downcase(str
)