Debian Codegraph @

src/c/string.d

raw
1 /* -*- Mode: C; c-basic-offset: 2; indent-tabs-mode: nil -*- */
2 /* vim: set filetype=c tabstop=2 shiftwidth=2 expandtab: */
3
4 /*
5 * string.d - string routines
6 *
7 * Copyright (c) 1984 Taiichi Yuasa and Masami Hagiya
8 * Copyright (c) 1990 Giuseppe Attardi
9 * Copyright (c) 2001 Juan Jose Garcia Ripoll
10 *
11 * See file 'LICENSE' for the copyright details.
12 *
13 */
14
15 #include <ecl/ecl.h>
16 #include <string.h>
17 #include <ecl/internal.h>
18 #include <ecl/ecl-inl.h>
19
20 typedef ecl_character (*ecl_casefun)(ecl_character, bool *);
21
22 static cl_object
23 do_make_base_string(cl_index s, ecl_base_char code)
24 {
25 cl_object x = ecl_alloc_simple_base_string(s);
26 cl_index i;
27 for (i = 0; i < s; i++)
28 x->base_string.self[i] = code;
29 return x;
30 }
31
32 #ifdef ECL_UNICODE
33 static cl_object
34 do_make_string(cl_index s, ecl_character code)
35 {
36 cl_object x = ecl_alloc_simple_extended_string(s);
37 cl_index i;
38 for (i = 0; i < s; i++)
39 x->string.self[i] = code;
40 return x;
41 }
42 #else
43 #define do_make_string do_make_base_string
44 #endif
45
46 @(defun make_string (size &key (initial_element ECL_CODE_CHAR(' '))
47 (element_type @'character'))
48 cl_index s;
49 cl_object x;
50 @
51 s = ecl_to_index(size);
52 /* INV: ecl_[base_]char_code() checks the type of initial_element() */
53 if (element_type == @'base-char' || element_type == @'standard-char') {
54 int code = ecl_base_char_code(initial_element);
55 x = do_make_base_string(s, code);
56 } else if (element_type == @'character') {
57 cl_index code = ecl_char_code(initial_element);
58 x = do_make_string(s, code);
59 } else if (_ecl_funcall3(@'subtypep', element_type, @'base-char') == ECL_T) {
60 int code = ecl_base_char_code(initial_element);
61 x = do_make_base_string(s, code);
62 } else if (_ecl_funcall3(@'subtypep', element_type, @'character') == ECL_T) {
63 cl_index code = ecl_char_code(initial_element);
64 x = do_make_string(s, code);
65 } else {
66 FEerror("The type ~S is not a valid string char type.",
67 1, element_type);
68 }
69 @(return x);
70 @)
71
72 /*
73 Make a string of a certain size, with some leading zeros to
74 keep C happy. The string must be adjustable, to allow further
75 growth. (See unixfsys.c for its use).
76 */
77 cl_object
78 ecl_alloc_adjustable_base_string(cl_index l)
79 {
80 cl_object output = ecl_alloc_object(t_base_string);
81 output->base_string.self = (ecl_base_char *)ecl_alloc_atomic(l+1);
82 output->base_string.self[l] = 0;
83 output->base_string.flags = ECL_FLAG_HAS_FILL_POINTER | ECL_FLAG_ADJUSTABLE;
84 output->base_string.elttype = ecl_aet_bc;
85 output->base_string.displaced = ECL_NIL;
86 output->base_string.dim = l;
87 output->base_string.fillp = 0;
88 return output;
89 }
90
91 #ifdef ECL_UNICODE
92 cl_object
93 ecl_alloc_adjustable_extended_string(cl_index l)
94 {
95 cl_index bytes = sizeof(ecl_character) * l;
96 cl_object output = ecl_alloc_object(t_string);
97 output->string.self = (ecl_character *)ecl_alloc_atomic(bytes);
98 output->string.flags = ECL_FLAG_HAS_FILL_POINTER | ECL_FLAG_ADJUSTABLE;
99 output->string.elttype = ecl_aet_ch;
100 output->string.displaced = ECL_NIL;
101 output->string.dim = l;
102 output->string.fillp = 0;
103 return output;
104 }
105 #endif
106
107 /*
108 ecl_make_simple_base_string(s) creates a simple-base string from C string s.
109 ecl_make_constant_base_string(s) does the same, but without copying the C string.
110 */
111 cl_object
112 ecl_make_constant_base_string(const char *s, cl_fixnum l)
113 {
114 cl_object x = ecl_alloc_object(t_base_string);
115 x->base_string.elttype = ecl_aet_bc;
116 x->base_string.flags = 0; /* no fill pointer, not adjustable */
117 x->base_string.displaced = ECL_NIL;
118 if (l < 0) l = strlen(s);
119 x->base_string.dim = (x->base_string.fillp = l);
120 x->base_string.self = (ecl_base_char *)s;
121 return x;
122 }
123
124 cl_object
125 ecl_make_simple_base_string(const char *s, cl_fixnum l)
126 {
127 cl_object x;
128 if (l < 0) l = strlen(s);
129
130 x = ecl_alloc_simple_base_string(l);
131 memcpy(x->base_string.self, s, l);
132 return x;
133 }
134
135 cl_object
136 ecl_cstring_to_base_string_or_nil(const char *s)
137 {
138 if (s == NULL)
139 return ECL_NIL;
140 else
141 return ecl_make_simple_base_string(s,-1);
142 }
143
144 bool
145 ecl_fits_in_base_string(cl_object s)
146 {
147 switch (ecl_t_of(s)) {
148 #ifdef ECL_UNICODE
149 case t_string: {
150 cl_index i;
151 for (i = 0; i < s->string.fillp; i++) {
152 if (!ECL_BASE_CHAR_CODE_P(s->string.self[i]))
153 return 0;
154 }
155 return 1;
156 }
157 #endif
158 case t_base_string:
159 return 1;
160 default:
161 FEwrong_type_nth_arg(@[si::copy-to-simple-base-string],1,s,@[string]);
162 }
163 }
164
165 cl_object
166 si_copy_to_simple_base_string(cl_object x)
167 {
168 cl_object y;
169 AGAIN:
170 switch(ecl_t_of(x)) {
171 case t_symbol:
172 x = x->symbol.name;
173 goto AGAIN;
174 case t_character:
175 x = cl_string(x);
176 goto AGAIN;
177 #ifdef ECL_UNICODE
178 case t_string: {
179 cl_index index, length = x->string.fillp;
180 y = ecl_alloc_simple_base_string(length);
181 for (index=0; index < length; index++) {
182 ecl_character c = x->string.self[index];
183 if (!ECL_BASE_CHAR_CODE_P(c))
184 FEerror("Cannot coerce string ~A to a base-string", 1, x);
185 y->base_string.self[index] = c;
186 }
187 break;
188 }
189 #endif
190 case t_base_string: {
191 cl_index length = x->base_string.fillp;
192 y = ecl_alloc_simple_base_string(length);
193 memcpy(y->base_string.self, x->base_string.self, length);
194 break;
195 }
196 case t_list:
197 if (Null(x)) {
198 x = ECL_NIL_SYMBOL->symbol.name;
199 goto AGAIN;
200 }
201 default:
202 FEwrong_type_nth_arg(@[si::copy-to-simple-base-string],1,x,@[string]);
203 }
204 @(return y);
205 }
206
207 cl_object
208 cl_string(cl_object x)
209 {
210 switch (ecl_t_of(x)) {
211 case t_symbol:
212 x = x->symbol.name;
213 break;
214 case t_character: {
215 cl_object y;
216 ecl_character c = ECL_CHAR_CODE(x);
217 #ifdef ECL_UNICODE
218 if (ECL_BASE_CHAR_CODE_P(c)) {
219 y = ecl_alloc_simple_base_string(1);
220 y->base_string.self[0] = c;
221 x = y;
222 } else {
223 y = ecl_alloc_simple_extended_string(1);
224 y->string.self[0] = c;
225 x = y;
226 }
227 #else
228 y = ecl_alloc_simple_base_string(1);
229 y->base_string.self[0] = c;
230 x = y;
231 break;
232 #endif
233 }
234 #ifdef ECL_UNICODE
235 case t_string:
236 #endif
237 case t_base_string:
238 break;
239 case t_list:
240 if (Null(x)) {
241 x = ECL_NIL_SYMBOL->symbol.name;
242 break;
243 }
244 default:
245 FEwrong_type_only_arg(@[string],x,@[string]);
246 }
247 @(return x);
248 }
249
250 #ifdef ECL_UNICODE
251 cl_object
252 si_coerce_to_base_string(cl_object x)
253 {
254 if (!ECL_BASE_STRING_P(x)) {
255 x = si_copy_to_simple_base_string(x);
256 }
257 @(return x);
258 }
259
260 cl_object
261 si_coerce_to_extended_string(cl_object x)
262 {
263 cl_object y;
264 AGAIN:
265 switch (ecl_t_of(x)) {
266 case t_symbol:
267 x = x->symbol.name;
268 goto AGAIN;
269 case t_character:
270 y = ecl_alloc_simple_extended_string(1);
271 y->string.self[0] = ECL_CHAR_CODE(x);
272 break;
273 case t_base_string: {
274 cl_index index, len = x->base_string.dim;
275 y = ecl_alloc_simple_extended_string(x->base_string.fillp);
276 for(index=0; index < len; index++) {
277 y->string.self[index] = x->base_string.self[index];
278 }
279 y->string.fillp = x->base_string.fillp;
280 }
281 case t_string:
282 y = x;
283 break;
284 case t_list:
285 if (Null(x)) {
286 x = ECL_NIL_SYMBOL->symbol.name;
287 goto AGAIN;
288 }
289 default:
290 FEwrong_type_nth_arg(@[si::coerce-to-extended-string],1,x,@[string]);
291 }
292 @(return y);
293 }
294 #endif
295
296 cl_object
297 cl_char(cl_object object, cl_object index)
298 {
299 cl_index position = ecl_to_index(index);
300 @(return ECL_CODE_CHAR(ecl_char(object, position)));
301 }
302
303 ecl_character
304 ecl_char(cl_object object, cl_index index)
305 {
306 /* CHAR bypasses fill pointers when accessing strings */
307 switch(ecl_t_of(object)) {
308 #ifdef ECL_UNICODE
309 case t_string:
310 if (index >= object->string.dim)
311 FEtype_error_index(object, index);
312 return object->string.self[index];
313 #endif
314 case t_base_string:
315 if (index >= object->base_string.dim)
316 FEtype_error_index(object, index);
317 return object->base_string.self[index];
318 default:
319 FEwrong_type_nth_arg(@[char],1,object,@[string]);
320 }
321 }
322
323 cl_object
324 si_char_set(cl_object object, cl_object index, cl_object value)
325 {
326 cl_index position = ecl_to_index(index);
327 cl_index c = ecl_char_code(value);
328 ecl_char_set(object, position, c);
329 @(return value);
330 }
331
332 ecl_character
333 ecl_char_set(cl_object object, cl_index index, ecl_character value)
334 {
335 /* CHAR bypasses fill pointers when accessing strings */
336 switch(ecl_t_of(object)) {
337 #ifdef ECL_UNICODE
338 case t_string:
339 if (index >= object->string.dim)
340 FEtype_error_index(object, index);
341 return object->string.self[index] = value;
342 #endif
343 case t_base_string:
344 if (index >= object->base_string.dim)
345 FEtype_error_index(object, index);
346 return object->base_string.self[index] = value;
347 default:
348 FEwrong_type_nth_arg(@[si::char-set],1,object,@[string]);
349 }
350 }
351
352 #ifdef ECL_UNICODE
353 static int
354 compare_strings(cl_object string1, cl_index s1, cl_index e1,
355 cl_object string2, cl_index s2, cl_index e2,
356 int case_sensitive, cl_index *m)
357 {
358 cl_index c1, c2;
359 for (; s1 < e1; s1++, s2++) {
360 if (s2 >= e2) { /* s1 is longer than s2, therefore s2 < s1 */
361 *m = s1;
362 return +1;
363 }
364 c1 = ecl_char(string1, s1);
365 c2 = ecl_char(string2, s2);
366 if (!case_sensitive) {
367 c1 = ecl_char_upcase(c1);
368 c2 = ecl_char_upcase(c2);
369 }
370 if (c1 < c2) {
371 *m = s1;
372 return -1;
373 } else if (c1 > c2) {
374 *m = s1;
375 return +1;
376 }
377 }
378 *m = s1;
379 if (s2 >= e2) {
380 return 0;
381 } else { /* s1 is shorter than s2, hence s1 < s2 */
382 return -1;
383 }
384 }
385 #endif
386
387 static int
388 compare_base(unsigned char *s1, cl_index l1, unsigned char *s2, cl_index l2,
389 int case_sensitive, cl_index *m)
390 {
391 cl_index l, c1, c2;
392 for (l = 0; l < l1; l++, s1++, s2++) {
393 if (l == l2) { /* s1 is longer than s2, therefore s2 < s1 */
394 *m = l;
395 return +1;
396 }
397 c1 = *s1;
398 c2 = *s2;
399 if (!case_sensitive) {
400 c1 = ecl_char_upcase(c1);
401 c2 = ecl_char_upcase(c2);
402 }
403 if (c1 < c2) {
404 *m = l;
405 return -1;
406 } else if (c1 > c2) {
407 *m = l;
408 return +1;
409 }
410 }
411 *m = l;
412 if (l1 == l2)
413 return 0;
414 else { /* s1 is shorter than s2, hence s1 < s2 */
415 return -1;
416 }
417 }
418
419 @(defun string= (string1 string2 &key (start1 ecl_make_fixnum(0)) end1
420 (start2 ecl_make_fixnum(0)) end2)
421 cl_index_pair p;
422 cl_index s1, e1, s2, e2;
423 @
424 {
425 string1 = cl_string(string1);
426 string2 = cl_string(string2);
427 p = ecl_vector_start_end(@[string=], string1, start1, end1);
428 s1 = p.start; e1 = p.end;
429 p = ecl_vector_start_end(@[string=], string2, start2, end2);
430 s2 = p.start; e2 = p.end;
431 if (e1 - s1 != e2 - s2) {
432 @(return ECL_NIL);
433 }
434 #ifdef ECL_UNICODE
435 if (string1->string.t == t_string) {
436 if (string2->string.t == t_string) {
437 while (s1 < e1)
438 if (string1->string.self[s1++] != string2->string.self[s2++]) {
439 @(return ECL_NIL);
440 }
441 @(return ECL_T);
442 } else {
443 while (s1 < e1)
444 if (string1->string.self[s1++] != string2->base_string.self[s2++]) {
445 @(return ECL_NIL);
446 }
447 @(return ECL_T);
448 }
449 } else {
450 if (string2->string.t == t_string) {
451 while (s1 < e1)
452 if (string1->base_string.self[s1++] != string2->string.self[s2++]) {
453 @(return ECL_NIL);
454 }
455 @(return ECL_T);
456 } else {
457 while (s1 < e1)
458 if (string1->base_string.self[s1++] != string2->base_string.self[s2++]) {
459 @(return ECL_NIL);
460 }
461 @(return ECL_T);
462 }
463 }
464 #else
465 while (s1 < e1)
466 if (string1->base_string.self[s1++] != string2->base_string.self[s2++]) {
467 @(return ECL_NIL);
468 }
469 #endif
470 @(return ECL_T);
471 }
472 @)
473
474 /*
475 This correponds to string= (just the string equality).
476 */
477 bool
478 ecl_string_eq(cl_object x, cl_object y)
479 {
480 cl_index i, j;
481 i = x->base_string.fillp;
482 j = y->base_string.fillp;
483 if (i != j) return 0;
484 #ifdef ECL_UNICODE
485 switch(ecl_t_of(x)) {
486 case t_string:
487 switch(ecl_t_of(y)) {
488 case t_string:
489 return memcmp(x->string.self, y->string.self, i * sizeof *x->string.self) == 0;
490 case t_base_string: {
491 cl_index index;
492 for(index=0; index<i; index++)
493 if (x->string.self[index] != y->base_string.self[index])
494 return 0;
495 return 1;
496 }
497 default:
498 FEwrong_type_nth_arg(@[string=],2,y,@[string]);
499 }
500 break;
501 case t_base_string:
502 switch(ecl_t_of(y)) {
503 case t_string:
504 return ecl_string_eq(y, x);
505 case t_base_string:
506 return memcmp(x->base_string.self, y->base_string.self, i) == 0;
507 default:
508 FEwrong_type_nth_arg(@[string=],2,y,@[string]);
509 }
510 break;
511 default:
512 FEwrong_type_nth_arg(@[string=],2,x,@[string]);
513 }
514 #else
515 return memcmp(x->base_string.self, y->base_string.self, i) == 0;
516 #endif
517 }
518
519
520 @(defun string_equal (string1 string2 &key (start1 ecl_make_fixnum(0)) end1
521 (start2 ecl_make_fixnum(0)) end2)
522 cl_index s1, e1, s2, e2;
523 cl_index_pair p;
524 int output;
525 @
526 string1 = cl_string(string1);
527 string2 = cl_string(string2);
528 p = ecl_vector_start_end(@[string=], string1, start1, end1);
529 s1 = p.start; e1 = p.end;
530 p = ecl_vector_start_end(@[string=], string2, start2, end2);
531 s2 = p.start; e2 = p.end;
532 if (e1 - s1 != e2 - s2) {
533 @(return ECL_NIL);
534 }
535 #ifdef ECL_UNICODE
536 if (ECL_EXTENDED_STRING_P(string1) || ECL_EXTENDED_STRING_P(string2)) {
537 output = compare_strings(string1, s1, e1, string2, s2, e2, 0, &e1);
538 } else
539 #endif
540 output = compare_base(string1->base_string.self + s1, e1 - s1,
541 string2->base_string.self + s2, e2 - s2,
542 0, &e1);
543 @(return ((output == 0)? ECL_T : ECL_NIL));
544 @)
545
546 static cl_object
547 string_compare(cl_narg narg, int sign1, int sign2, int case_sensitive, ecl_va_list ARGS)
548 {
549 cl_object string1 = ecl_va_arg(ARGS);
550 cl_object string2 = ecl_va_arg(ARGS);
551 cl_index s1, e1, s2, e2;
552 cl_index_pair p;
553 int output;
554 cl_object result;
555 cl_object KEYS[4];
556 #define start1 KEY_VARS[0]
557 #define end1 KEY_VARS[1]
558 #define start2 KEY_VARS[2]
559 #define end2 KEY_VARS[3]
560 #define start1p KEY_VARS[4]
561 #define start2p KEY_VARS[6]
562 cl_object KEY_VARS[8];
563
564 if (narg < 2) FEwrong_num_arguments_anonym();
565 KEYS[0]=@':start1';
566 KEYS[1]=@':end1';
567 KEYS[2]=@':start2';
568 KEYS[3]=@':end2';
569 cl_parse_key(ARGS, 4, KEYS, KEY_VARS, NULL, FALSE);
570
571 string1 = cl_string(string1);
572 string2 = cl_string(string2);
573 if (start1p == ECL_NIL) start1 = ecl_make_fixnum(0);
574 if (start2p == ECL_NIL) start2 = ecl_make_fixnum(0);
575 p = ecl_vector_start_end(@[string=], string1, start1, end1);
576 s1 = p.start; e1 = p.end;
577 p = ecl_vector_start_end(@[string=], string2, start2, end2);
578 s2 = p.start; e2 = p.end;
579 #ifdef ECL_UNICODE
580 if (ECL_EXTENDED_STRING_P(string1) || ECL_EXTENDED_STRING_P(string2)) {
581 output = compare_strings(string1, s1, e1, string2, s2, e2,
582 case_sensitive, &e1);
583 } else
584 #endif
585 {
586 output = compare_base(string1->base_string.self + s1, e1 - s1,
587 string2->base_string.self + s2, e2 - s2,
588 case_sensitive, &e1);
589 e1 += s1;
590 }
591 if (output == sign1 || output == sign2) {
592 result = ecl_make_fixnum(e1);
593 } else {
594 result = ECL_NIL;
595 }
596 @(return result);
597 #undef start1p
598 #undef start2p
599 #undef start1
600 #undef end1
601 #undef start2
602 #undef end2
603 }
604
605 @(defun string< (&rest args)
606 @
607 @(return string_compare(narg, -1, -1, 1, args));
608 @)
609
610 @(defun string> (&rest args)
611 @
612 @(return string_compare(narg, +1, +1, 1, args));
613 @)
614
615 @(defun string<= (&rest args)
616 @
617 @(return string_compare(narg, -1, 0, 1, args));
618 @)
619
620 @(defun string>= (&rest args)
621 @
622 @(return string_compare(narg, 0, +1, 1, args));
623 @)
624
625 @(defun string/= (&rest args)
626 @
627 @(return string_compare(narg, -1, +1, 1, args));
628 @)
629
630 @(defun string-lessp (&rest args)
631 @
632 @(return string_compare(narg, -1, -1, 0, args));
633 @)
634
635 @(defun string-greaterp (&rest args)
636 @
637 @(return string_compare(narg, +1, +1, 0, args));
638 @)
639
640 @(defun string-not-greaterp (&rest args)
641 @
642 @(return string_compare(narg, -1, 0, 0, args));
643 @)
644
645 @(defun string-not-lessp (&rest args)
646 @
647 @(return string_compare(narg, 0, +1, 0, args));
648 @)
649
650 @(defun string-not-equal (&rest args)
651 @
652 @(return string_compare(narg, -1, +1, 0, args));
653 @)
654
655 bool
656 ecl_member_char(ecl_character c, cl_object char_bag)
657 {
658 cl_index i, f;
659 switch (ecl_t_of(char_bag)) {
660 case t_list:
661 loop_for_in(char_bag) {
662 cl_object other = CAR(char_bag);
663 if (ECL_CHARACTERP(other) && c == ECL_CHAR_CODE(other))
664 return(TRUE);
665 } end_loop_for_in;
666 return(FALSE);
667 case t_vector:
668 for (i = 0, f = char_bag->vector.fillp; i < f; i++) {
669 cl_object other = char_bag->vector.self.t[i];
670 if (ECL_CHARACTERP(other) && c == ECL_CHAR_CODE(other))
671 return(TRUE);
672 }
673 return(FALSE);
674 #ifdef ECL_UNICODE
675 case t_string:
676 for (i = 0, f = char_bag->string.fillp; i < f; i++) {
677 if (c == char_bag->string.self[i])
678 return(TRUE);
679 }
680 return(FALSE);
681 #endif
682 case t_base_string:
683 for (i = 0, f = char_bag->base_string.fillp; i < f; i++) {
684 if (c == char_bag->base_string.self[i])
685 return(TRUE);
686 }
687 return(FALSE);
688 case t_bitvector:
689 return(FALSE);
690 default:
691 FEwrong_type_nth_arg(@[member],2,char_bag,@[sequence]);
692 }
693 }
694
695 static cl_object
696 string_trim0(bool left_trim, bool right_trim, cl_object char_bag, cl_object strng)
697 {
698 cl_index i, j;
699
700 strng = cl_string(strng);
701 i = 0;
702 j = ecl_length(strng);
703 if (left_trim) {
704 for (; i < j; i++) {
705 cl_index c = ecl_char(strng, i);
706 if (!ecl_member_char(c, char_bag))
707 break;
708 }
709 }
710 if (right_trim) {
711 for (; j > i; j--) {
712 cl_index c = ecl_char(strng, j-1);
713 if (!ecl_member_char(c, char_bag)) {
714 break;
715 }
716 }
717 }
718 return cl_subseq(3, strng, ecl_make_fixnum(i), ecl_make_fixnum(j));
719 }
720
721 cl_object
722 cl_string_trim(cl_object char_bag, cl_object strng)
723 {
724 return string_trim0(TRUE, TRUE, char_bag, strng);
725 }
726
727 cl_object
728 cl_string_left_trim(cl_object char_bag, cl_object strng)
729 {
730 return string_trim0(TRUE, FALSE, char_bag, strng);
731 }
732
733 cl_object
734 cl_string_right_trim(cl_object char_bag, cl_object strng)
735 {
736 return string_trim0(FALSE, TRUE, char_bag, strng);
737 }
738
739 static cl_object
740 string_case(cl_narg narg, cl_object fun, ecl_casefun casefun, ecl_va_list ARGS)
741 {
742 cl_object strng = ecl_va_arg(ARGS);
743 cl_index_pair p;
744 cl_index i;
745 bool b;
746 cl_object KEYS[2];
747 #define kstart KEY_VARS[0]
748 #define kend KEY_VARS[1]
749 #define kstartp KEY_VARS[2]
750 cl_object KEY_VARS[4];
751
752 if (narg < 1) FEwrong_num_arguments_anonym();
753 KEYS[0]=@':start';
754 KEYS[1]=@':end';
755 cl_parse_key(ARGS, 2, KEYS, KEY_VARS, NULL, FALSE);
756
757 strng = cl_string(strng);
758 strng = cl_copy_seq(strng);
759 if (kstartp == ECL_NIL)
760 kstart = ecl_make_fixnum(0);
761 p = ecl_vector_start_end(fun, strng, kstart, kend);
762 b = TRUE;
763 #ifdef ECL_UNICODE
764 if (ECL_EXTENDED_STRING_P(strng)) {
765 for (i = p.start; i < p.end; i++)
766 strng->string.self[i] = (*casefun)(strng->string.self[i], &b);
767 } else
768 #endif
769 for (i = p.start; i < p.end; i++)
770 strng->base_string.self[i] = (*casefun)(strng->base_string.self[i], &b);
771 @(return strng);
772 #undef kstartp
773 #undef kstart
774 #undef kend
775 }
776
777 static ecl_character
778 char_upcase(ecl_character c, bool *bp)
779 {
780 return ecl_char_upcase(c);
781 }
782
783 @(defun string-upcase (&rest args)
784 @
785 @(return string_case(narg, @[string-upcase], char_upcase, args));
786 @)
787
788 static ecl_character
789 char_downcase(ecl_character c, bool *bp)
790 {
791 return ecl_char_downcase(c);
792 }
793
794 @(defun string-downcase (&rest args)
795 @
796 @(return string_case(narg, @[string-downcase], char_downcase, args));
797 @)
798
799 static ecl_character
800 char_capitalize(ecl_character c, bool *bp)
801 {
802 if (ecl_lower_case_p(c)) {
803 if (*bp)
804 c = ecl_char_upcase(c);
805 *bp = FALSE;
806 } else if (ecl_upper_case_p(c)) {
807 if (!*bp)
808 c = ecl_char_downcase(c);
809 *bp = FALSE;
810 } else {
811 *bp = !ecl_alphanumericp(c);
812 }
813 return c;
814 }
815
816 @(defun string-capitalize (&rest args)
817 @
818 @(return string_case(narg, @[string-capitalize], char_capitalize, args));
819 @)
820
821
822 static cl_object
823 nstring_case(cl_narg narg, cl_object fun, ecl_casefun casefun, ecl_va_list ARGS)
824 {
825 cl_object strng = ecl_va_arg(ARGS);
826 cl_index_pair p;
827 cl_index i;
828 bool b;
829 cl_object KEYS[2];
830 #define kstart KEY_VARS[0]
831 #define kend KEY_VARS[1]
832 #define kstartp KEY_VARS[2]
833 cl_object KEY_VARS[4];
834
835 if (narg < 1) FEwrong_num_arguments_anonym();
836 KEYS[0]=@':start';
837 KEYS[1]=@':end';
838 cl_parse_key(ARGS, 2, KEYS, KEY_VARS, NULL, FALSE);
839
840 if (ecl_unlikely(!ECL_STRINGP(strng)))
841 FEwrong_type_nth_arg(fun, 1, strng, @[string]);
842 if (kstartp == ECL_NIL)
843 kstart = ecl_make_fixnum(0);
844 p = ecl_vector_start_end(fun, strng, kstart, kend);
845 b = TRUE;
846 #ifdef ECL_UNICODE
847 if (ECL_EXTENDED_STRING_P(strng)) {
848 for (i = p.start; i < p.end; i++)
849 strng->string.self[i] = (*casefun)(strng->string.self[i], &b);
850 } else
851 #endif
852 for (i = p.start; i < p.end; i++)
853 strng->base_string.self[i] = (*casefun)(strng->base_string.self[i], &b);
854 @(return strng);
855 #undef kstartp
856 #undef kstart
857 #undef kend
858 }
859
860 @(defun nstring-upcase (&rest args)
861 @
862 @(return nstring_case(narg, @'nstring-upcase', char_upcase, args));
863 @)
864
865 @(defun nstring-downcase (&rest args)
866 @
867 @(return nstring_case(narg, @'nstring-downcase', char_downcase, args));
868 @)
869
870 @(defun nstring-capitalize (&rest args)
871 @
872 @(return nstring_case(narg, @'nstring-capitalize', char_capitalize, args));
873 @)
874
875 @(defun si::base-string-concatenate (&rest args)
876 cl_index l;
877 int i;
878 cl_object output;
879 @
880 /* Compute final size and store NONEMPTY coerced strings. */
881 for (i = 0, l = 0; i < narg; i++) {
882 cl_object s = si_coerce_to_base_string(ecl_va_arg(args));
883 if (s->base_string.fillp) {
884 ECL_STACK_PUSH(the_env, s);
885 l += s->base_string.fillp;
886 }
887 }
888 /* Do actual copying by recovering those strings */
889 output = ecl_alloc_simple_base_string(l);
890 while (l) {
891 cl_object s = ECL_STACK_POP_UNSAFE(the_env);
892 size_t bytes = s->base_string.fillp;
893 l -= bytes;
894 memcpy(output->base_string.self + l, s->base_string.self, bytes);
895 }
896 @(return output);
897 @)
898
899 @(defun ext::octets-to-string (input &key
900 (external_format @':default')
901 (start ecl_make_fixnum(0))
902 (end ECL_NIL))
903 cl_object output;
904 cl_index input_size;
905 cl_object input_stream;
906 cl_index output_size;
907 cl_object ret;
908 @
909 output = si_get_buffer_string();
910 input_stream = si_make_sequence_input_stream(7, input,
911 @':external-format', external_format,
912 @':start', start,
913 @':end', end);
914 /* INV: MAKE-SEQUENCE-INPUT-STREAM checks types of start and end indices */
915 input_size = (Null(end) ? ecl_length(input) : ecl_fixnum(end)) - ecl_fixnum(start);
916 output_size = 0;
917 do {
918 output->base_string.fillp = output->base_string.dim;
919 output_size += ecl_to_unsigned_integer(si_do_read_sequence(output, input_stream,
920 ecl_make_fixnum(output_size),
921 ecl_make_fixnum(output->base_string.dim)));
922 if (output_size < output->base_string.dim) {
923 break;
924 }
925 output = si_adjust_vector(output, ecl_make_fixnum(input_size > output_size
926 ? input_size
927 : output_size + 128));
928 } while (1);
929 output->base_string.fillp = output_size;
930 if (ecl_fits_in_base_string(output)) {
931 ret = si_copy_to_simple_base_string(output);
932 } else {
933 ret = cl_copy_seq(output);
934 }
935 si_put_buffer_string(output);
936 @(return ret);
937 @)
938
939 @(defun ext::string-to-octets (input &key
940 (external_format @':default')
941 (start ecl_make_fixnum(0))
942 (end ECL_NIL)
943 (null_terminate ECL_NIL)
944 (element_type @'ext::byte8'))
945 cl_object output;
946 cl_object output_stream;
947 cl_fixnum length;
948 @
949 length = (Null(end) ? ecl_length(input) : ecl_to_fixnum(end)) - ecl_to_fixnum(start) + 1;
950 output = si_make_vector(element_type, /* element-type */
951 ecl_make_fixnum(length), /* length */
952 ECL_T, /* adjustable */
953 ecl_make_fixnum(0), /* fillp */
954 ECL_NIL, /* displaced */
955 ECL_NIL); /* displaced-offset */
956 output_stream = si_make_sequence_output_stream(3, output,
957 @':external-format', external_format);
958 si_do_write_sequence(input, output_stream, start, end);
959 if (!Null(null_terminate)) {
960 ecl_write_char(0, output_stream);
961 }
962 @(return output);
963 @)
964
965 cl_object
966 ecl_decode_from_cstring(const char *s, cl_fixnum len, cl_object encoding)
967 {
968 volatile cl_object ret;
969 ECL_HANDLER_CASE_BEGIN(ecl_process_env(), ecl_list1(@'ext::character-decoding-error')) {
970 ret = si_octets_to_string(3, ecl_make_constant_base_string(s, len), @':external-format', encoding);
971 } ECL_HANDLER_CASE(1, c) {
972 ret = c; /* suppress "unused variable `c`" warning */
973 ret = OBJNULL;
974 } ECL_HANDLER_CASE_END;
975 return ret;
976 }
977
978 cl_fixnum
979 ecl_encode_to_cstring(char *output, cl_fixnum output_len, cl_object input, cl_object encoding)
980 {
981 volatile cl_fixnum ret;
982 ECL_HANDLER_CASE_BEGIN(ecl_process_env(), ecl_list1(@'ext::character-encoding-error')) {
983 cl_object output_vec = si_string_to_octets(3, input, @':external-format', encoding);
984 ret = output_vec->vector.fillp + 1;
985 if (ret <= output_len) {
986 memcpy(output, output_vec->vector.self.b8, (ret-1)*sizeof(char));
987 output[ret-1] = 0; /* null-terminator */
988 }
989 } ECL_HANDLER_CASE(1, c) {
990 input = c; /* suppress "unused variable `c`" warning */
991 ret = -1;
992 } ECL_HANDLER_CASE_END;
993 return ret;
994 }
995
996 #ifdef HAVE_WCHAR_H
997 cl_object
998 ecl_decode_from_unicode_wstring(const wchar_t *s, cl_fixnum len)
999 {
1000 cl_object input;
1001 cl_object elttype;
1002 cl_object encoding;
1003 volatile cl_object ret;
1004 if (len < 0) {
1005 len = wcslen(s);
1006 }
1007 switch (sizeof(wchar_t)) {
1008 case 1:
1009 elttype = @'ext::byte8';
1010 encoding = @':utf-8';
1011 break;
1012 case 2:
1013 elttype = @'ext::byte16';
1014 encoding = @':ucs-2';
1015 break;
1016 case 4:
1017 elttype = @'ext::byte32';
1018 encoding = @':ucs-4';
1019 break;
1020 default:
1021 ecl_internal_error("Unexpected sizeof(wchar_t)");
1022 }
1023 input = si_make_vector(elttype, ecl_make_fixnum(len), ECL_NIL, ECL_NIL, ECL_NIL, ECL_NIL);
1024 memcpy(input->vector.self.b8, s, len*sizeof(wchar_t));
1025 ECL_HANDLER_CASE_BEGIN(ecl_process_env(), ecl_list1(@'ext::character-decoding-error')) {
1026 ret = si_octets_to_string(3, input, @':external-format', encoding);
1027 } ECL_HANDLER_CASE(1, c) {
1028 ret = c; /* suppress "unused variable `c`" warning */
1029 ret = OBJNULL;
1030 } ECL_HANDLER_CASE_END;
1031 return ret;
1032 }
1033
1034 cl_fixnum
1035 ecl_encode_to_unicode_wstring(wchar_t *output, cl_fixnum output_len, cl_object input)
1036 {
1037 cl_object elttype;
1038 cl_object encoding;
1039 volatile cl_fixnum ret;
1040 switch (sizeof(wchar_t)) {
1041 case 1:
1042 elttype = @'ext::byte8';
1043 encoding = @':utf-8';
1044 break;
1045 case 2:
1046 elttype = @'ext::byte16';
1047 encoding = @':ucs-2';
1048 break;
1049 case 4:
1050 elttype = @'ext::byte32';
1051 encoding = @':ucs-4';
1052 break;
1053 default:
1054 ecl_internal_error("Unexpected sizeof(wchar_t)");
1055 }
1056 ECL_HANDLER_CASE_BEGIN(ecl_process_env(), ecl_list1(@'ext::character-encoding-error')) {
1057 cl_object output_vec = si_string_to_octets(5, input, @':external-format', encoding, @':element-type', elttype);
1058 ret = output_vec->vector.fillp + 1;
1059 if (ret <= output_len) {
1060 memcpy(output, output_vec->vector.self.b8, (ret-1)*sizeof(wchar_t));
1061 output[ret-1] = 0; /* null-terminator */
1062 }
1063 } ECL_HANDLER_CASE(1, c) {
1064 input = c; /* suppress "unused variable `c`" warning */
1065 ret = -1;
1066 } ECL_HANDLER_CASE_END;
1067 return ret;
1068 }
1069 #endif