-
Notifications
You must be signed in to change notification settings - Fork 0
/
strings.lisp
1997 lines (1863 loc) · 85.2 KB
/
strings.lisp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
;;; :FILE-CREATED <Timestamp: #{2010-07-06T19:04:44-04:00Z}#{10272} - by MON>
;;; :FILE mon-systems/strings.lisp
;;; ==============================
(in-package #:mon)
(defun string-delimited-to-list (string &optional (separator #\space)
skip-terminal)
(declare
(type string string)
(type character separator))
(do* ((len (length string))
(output '())
(pos 0)
;; (end (mon::position-char separator string pos len)
;; (mon::position-char separator string pos len)))
(end (char-position separator string pos len)
(char-position separator string pos len)))
((null end)
(if (< pos len)
(push (subseq string pos) output)
(when (or (not skip-terminal) (zerop len))
(push (make-string 0) output)))
(nreverse output))
(declare (type fixnum pos len)
(type (or null fixnum) end))
(push (subseq string pos end) output)
(setq pos (1+ end))))
(defun string-from-delimited-list (list &optional (separator " "))
(format nil (concatenate 'string "~{~A~^" (string separator) "~}") list))
(defun simple-string-ensure (thing)
;; Signals an error if THING cannot be coerced with `cl:string'.
(let ((s (string thing)))
(make-array (length s)
:element-type 'character
:initial-contents s)))
(defun string-explode (str)
(declare (type string str))
(loop
for i across str ;(the string str)
collect (format nil "~A" i)))
;;; :SOURCE clocc/src/onlisp-util.lisp
;; (defun symbol-name-explode-string (explode-sym)
;; (declare (type symbol explode-sym))
;; (map 'list #'(lambda (c)
;; ;;(intern
;; (make-string 1 :initial-element c));))
;; (symbol-name explode-sym)))
(defun symbol-name-explode (explode-sym &optional as-chars)
(declare (type symbol explode-sym))
(or
(and (booleanp explode-sym) explode-sym)
(loop
with expld-sym = (symbol-name explode-sym)
for expld across (the string expld-sym)
if as-chars
collect expld
else
collect (make-string 1 :initial-element expld))))
;;; :COURTESY Drew Mcdermott's ytools/nilscompat.lisp
(defun string-elt (str idx)
;; (string-elt "bubba" 2)
(elt (the string str) (the index idx)))
;;; :COURTESY Drew Mcdermott's ytools/base.lisp
(defun string-length (str)
(declare (string str))
(the array-length (length str)))
;;; :COURTESY Drew Mcdermott's ytools/misc.lisp
(defun string-begins (str putative-start)
(let ((n (mismatch str putative-start)))
(or (not n)
(= n (string-length putative-start)))))
;;; :SOURCE xit/cl-utilities/cl-utilities.lisp :WAS `convert-to-string'
;; (defun convert-to-string (value)
;; (if (stringp value)
;; value
;; (let ((new-string (format nil "~A" value)))
;; (string-downcase new-string))))
;; :COURTESY Drew Mcdermott ytools/base.lisp :WAS `coerce-to-string'
(defun string-coerce-from (x)
;; :WAS (cond ((stringp x) x)
;; ((characterp x) (string x))
;; ((numberp x)
;; (princ-to-string x))
;; ((symbolp x)
;; (symbol-name x))
;; (t (format nil "~a" x)))
(typecase x
(string x)
(character (string x))
(number (princ-to-string x))
(symbol (symbol-name x))
(t (format nil "~A" x))))
(defun string-to-char (string &key (w-code-char nil))
(declare (type string string))
(with-input-from-string (inpt-str string :start 0 :end 1)
(if w-code-char
(read-char inpt-str)
(char-code (read-char inpt-str)))))
(defun string-first-char (string)
(declare (type string-or-null string))
(unless (or (null string)
(string-empty-p (the string string)))
(char string 0)))
(defun string-replace-all (string from-string to-string &key (test #'char=)) ;(w-case 'preserve))
;; (declare (optimize (speed 0) (safety 3) (compilation-speed 0) (debug 3)))
(with-output-to-string (out)
(loop
with part-length = (length from-string)
for old-pos = 0 then (+ pos part-length)
for pos = (search from-string string :start2 old-pos :test test)
do (write-string string out :start old-pos :end (or pos (length string)))
when pos
do ;; (break "POS was ~S" pos)
(write-string to-string out)
while pos)))
;;; ==============================
;;; :SOURCE texinfo-docstrings/colorize.lisp
;; (defun strcat (&rest strings)
;; (apply #'concatenate 'string strings))
;;
;;; :SOURCE s-sql.lisp :WAS strcat
(defun string-cat (string-sequence)
(declare (sequence string-sequence)
(optimize (speed 3)))
(etypecase string-sequence
(null (make-string 0))
(string string-sequence)
((or vector cons)
(if (not (or (vectorp string-sequence)
(listp (cdr string-sequence))))
(error ":FUNCTION `string-cat' ~
-- arg STRING-SEQUENCE is `cl:consp' with cdr not `listp'~% got: ~S" string-sequence)
(let* ((maybe-coerce-string-sequence
(locally
(declare (type (or list vector) string-sequence))
(if (some #'characterp string-sequence)
(flet ((%reduce-chars (string-or-char)
(declare ((or string character) string-or-char))
(etypecase string-or-char
(string string-or-char)
(character (string string-or-char)))))
(declare (type (or list vector) string-sequence))
(map 'vector
#'%reduce-chars
(or (and (vectorp string-sequence)
(the vector string-sequence))
(the list string-sequence))))
(if (vectorp string-sequence)
string-sequence
(make-array (length (the list string-sequence))
:initial-contents (the list string-sequence))))))
(result (make-string (reduce #'+ maybe-coerce-string-sequence :initial-value 0 :key #'length))))
(declare (vector maybe-coerce-string-sequence)
(string result))
(loop
for pos = 0 then (+ pos (length arg))
for arg across maybe-coerce-string-sequence
do (replace result arg :start1 pos))
result)))))
;;; ==============================
;;; :SOURCE s-sql.lisp :WAS `implode'
(defun string-implode (sep list)
(declare (type string sep))
(string-cat (loop
for element on list
collect (car element)
if (cdr element)
collect sep)))
;;; :SOURCE s-sql.lisp :WAS reduce-strings
(defun string-reduce (string-or-list)
(declare ((or null cons string) string-or-list)
(optimize (speed 3)))
(etypecase string-or-list
(null string-or-list)
(string (list string-or-list))
(cons
(unless (listp (cdr string-or-list))
(error ":FUNCTION `string-reduce' ~
-- arg STRING-OR-LIST is `cl:consp' with cdr not `listp'~% got: ~S" string-or-list))
(let ((accum ())
(span (make-string 0))
(cmpr-str (make-string 0)))
(declare (type (simple-array character (0)) cmpr-str))
(flet ((%cmpring-str (string)
(declare (string string))
(string/= cmpr-str string)))
(dolist (part string-or-list)
(cond ((stringp part)
(setf span (concatenate 'string span part)))
(t
(when (%cmpring-str span)
(push span accum)
(setf span (make-string 0)))
(push part accum))))
(and (%cmpring-str span)
(push span accum)))
(nreverse accum)))))
;;; :SOURCE xit/cl-utilities/cl-utilities.lisp :WAS `convert-to-readable-string'
(defun string-convert-to-readable (value)
(if (stringp value)
value
(let ((new-string (format nil "~S" value)))
(string-downcase new-string))))
;;; :SOURCE quicklisp/ql-util.lisp :WAS `split-spaces'
;;; Modified for #\tab, #\newline, etc.
(defun string-split-spaces (line &optional w-last-wspc)
;;(declare (optimize (speed 0) (safety 0) (compilation-speed 0) (debug 3)))
(declare (type string line))
(let ((words '())
(mark 0)
(pos 0))
(labels ((finish ()
(setf pos (length line))
(save)
(return-from string-split-spaces
(or (and w-last-wspc
(nreverse words))
(and words (or (pop words) t)
(nreverse words)))))
(save ()
(when (< mark pos)
(push (subseq line mark pos) words)))
(mark ()
(setf mark pos))
(in-word (char)
(or (and (whitespace-char-p char)
(progn
(save)
#'in-space))
#'in-word))
(in-space (char)
(or (and (whitespace-char-p char) #'in-space)
(progn
(mark)
#'in-word))))
(let ((state #'in-word))
(dotimes (i (length line) (finish))
(setf pos i)
(setf state (funcall state (char line i))))))))
;;; ==============================
;;; :NOTE Modified version from c.l.l source below.
;;; - Changed the multiplier semantics. It doesn't IMO make sense to have
;;; multipliers arguments occur in other than the car position of ARGS.
;;; e.g the old semantics:
;;; (make-string* 2 "foo" " " 2 "foo") ;=> "foo foo foo foo"
;;; new semanitcs:
;;; (make-string* 2 " foo " 2 " foo") ;=> " foo 2 foo foo 2 foo"
;;; - Now accepts characters and numbers (integers, floats, etc.)
;;; - Elides null elements occuring in other than the car position.
;;; - Defaults to 1x multiplier if car is null
;;; - Bails with return value "" when:
;;; -- arg is null;
;;; -- cdr is null
;;; -- car of args is 0
;;; -- args is length 2 and:
;;; --- car is null and cadr is null
;;; --- cadr is null or empty string
;;;
;;; :SOURCE (URL `http://groups.google.com/group/comp.lang.lisp/msg/2f64b48bcb0a0519')
;;; :NEWSGROUP comp.lang.lisp
;;; :FROM Szymon "tichy" [email protected]
;;; :DATE 2006-06-05 23:08:50 +0200
;;; :SUBJECT Re: With format, how to print a variable number of spaces
(defun make-string* (&rest args)
(or
(and (or
;; (make-string*) ;=> ""
(null args)
;; On second thought following form should be valid:
;; (make-string* nil "x" #\x 3) ;=> "xx3"
;; (null (car args))
;;
;; (make-string*) ;=> ""
(null (cdr args))
;; Macro/backquote can pass either of the following two forms:
;; (make-string* 8 nil) ;=> ""
;; (make-string* nil nil) ;=> ""
;; Also, check for empty string for good measure.
;; (make-string* 8 "") ;=> ""
(and (eql (length args) 2)
(or
(string-null-or-empty-p (cadr args))
(every #'null args)))
;; (make-string* 0 "x" "y" "z") ;=> ""
(and (typep (car args) '(integer 0 0))))
(make-string 0))
(let ((num (or (and (typep (car args) '(integer 1))
(or
;; Its irrelevant how many times we multiply
(and (null (cdr args))
(return-from make-string* (make-string 0)))
(prog1 (car args)
(setf args (cdr args)))))
1))
(stream (make-string-output-stream)))
(dolist (thing args)
(etypecase thing
(null nil)
;; :WAS ((integer) (setq num (* thing num)))
(number (write thing :stream stream))
(string (write-string thing stream))
;; :NOTE Reading symbols is prob a bad idea.
;; (symbol (write-string (string thing) stream))
(character (write-char thing stream))))
(let* ((string (get-output-stream-string stream))
(result-length (* (length string) num))
(result (make-string result-length)))
(loop
for index by (length string) below result-length
do (replace result string :start1 index))
result))))
(defun string-split-bag (string-or-null char-or-char-bag)
(declare (or (string null) string-or-null)
(or (character list) char-or-char-bag))
(labels ((map-trim-whitespace (bag)
(mapcar #'(lambda (thing)
(if (null thing)
thing
(string-trim-whitespace thing)))
bag))
(rmv-if (seq) (remove-if #'string-all-whitespace-p seq))
(char-or-bag-if (cobi) (if (characterp cobi)
(list cobi)
(if (or (null cobi)
(not (and (listp cobi)
(every #'characterp cobi))))
(error ":FUNCTION `split-bag', argument CHAR-OR-CHAR-BAG is not a character or list of characters'~%:GOT ~S~%" cobi)
cobi)))
(mem-split (y) (member y (char-or-bag-if char-or-char-bag)))
(split-if (son) (split-sequence:split-sequence-if #'mem-split
son
:remove-empty-subseqs t)))
(if (null string-or-null)
nil
(progn
(unless (stringp string-or-null)
(error ":FUNCTION `split-bag', argument STRING-OR-NULL does not satisfy `cl:stringp'~%:GOT ~S~%" string-or-null))
(map-trim-whitespace (rmv-if (split-if string-or-null)))))))
;;; :NOTE Has sb-rt test `string-split-on-chars-TEST'
(defun string-split-on-chars (string &optional separators white-on-white)
(declare (string string)
(optimize (speed 3)))
(let ((string (etypecase string
(string-empty (return-from string-split-on-chars string))
(simple-string string)
(string (copy-seq string))))
(separators (etypecase separators
(null (if (and (string-all-whitespace-p (the simple-string string))
(null white-on-white))
(return-from string-split-on-chars string)
(map 'string #'identity *whitespace-chars*)))
(mon:string-null-or-empty (return-from string-split-on-chars string))
(simple-string separators)
(string (copy-seq separators))
((or character mon:char-code-integer) (char-to-string separators :allow-integers t))
(mon:proper-list
(etypecase separators
(mon:each-a-string-of-length-1
(with-standard-io-syntax (format nil "~{~A~}" separators)))
(each-a-character-or-char-code-integer (char-list-to-string separators))))))
(chunks (make-array 0 :adjustable t :fill-pointer 0))
(position 0)
(nextpos 0)
(strlen (length string)))
(declare (type simple-string string separators)
(index-plus-1 position nextpos)
(array-index strlen)
(array chunks))
(loop
while (< position strlen)
do (loop
while (and (< nextpos strlen)
(not (position (char string nextpos) separators)))
;; :NOTE Don't change this to: :do (incf nextpos))
do (setq nextpos (1+ nextpos)))
(vector-push-extend (subseq string position nextpos) chunks)
;; :NOTE Don't change this to: (incf position (1+ nextpos))
(setq position (1+ nextpos))
(setq nextpos position)
;; :finally (return (coerce (the array chunks) 'list))
finally (return (map 'list #'identity (the array chunks))))))
;; (map 'list #'identity #(a b c))
;;; :SOURCE chunga-1.1.1/read.lisp :WAS `trim-whitespace'
(defun string-trim-whitespace (string &key (start 0) (end (length string)))
;; optimized version to replace STRING-TRIM, suggested by Jason Kantz
;; #+:lispworks (hcl:fixnum-safety 0)))
(declare (type string string)
(inline whitespace-char-p))
#-sbcl (assert (string-not-null-p arg)
(arg)
":FUNCTION `string-trim-whitespace' -- ~
arg STRING did not satisfy `string-not-null-p'~% ~
got: ~S ~% ~
type-of: ~S~%"
arg (type-of arg))
(if (or (string-empty-p string)
(and (= (the array-length (length string)) 1)
(whitespace-char-p (char (the string-not-null-or-empty string) 0))))
;; :NOTE Are the consequences/benefits to doing this instead:
;; (make-string 0 :element-type 'standard-char)
(make-string 0 :element-type 'character)
(locally
(declare (type string-not-null-or-empty string)
(array-index start)
(index-or-minus-1 end)
(optimize (speed 3)))
(let* ((str-ghost (copy-seq string)) ;; copy-seq returns a simple-string.
(start% (loop
;; :WAS :with str = (the string string)
:with str = (the simple-string str-ghost)
:for i :of-type index-or-minus-1 :from start :below end :by 1
;; :WAS :while (whitespace-char-p (char str i))
:while (whitespace-char-p (schar str i))
:finally (return i)))
(end% (loop
;; :WAS :with str = ;; :WAS (the string string)
:with str = (the simple-string str-ghost)
:for i :of-type index-or-minus-1 :downfrom (1- end) :downto start :by 1
;; :WAS :while (whitespace-char-p (char str i))
:while (whitespace-char-p (schar str i))
:finally (return (1+ i)))))
(declare (index-or-minus-1 start% end%))
(cond ((and (zerop start%) (= end% (the array-length (length string))))
;; (break "with string: ~S start was: 0 end% was: ~d" string end%)
(make-array end% :element-type (array-element-type string) :initial-contents string))
((> start% end%)
(make-string 0 :element-type 'character))
(t ;; (break "with string: ~S start was: ~d end% was: ~d" string start% end%)
(loop
with new-str = (make-array (- end% start%) :element-type (array-element-type string))
;; :WAS with str = (the string string)
with str = (the simple-string string)
for idx-new upfrom 0 to end% by 1
for idx-old from start% below end% by 1
;; :WAS do (setf (char new-str idx-new) (char str idx-old))
do (setf (char new-str idx-new) (schar str idx-old))
finally (return new-str)) ))))))
(defun string-split-newline (string)
(declare (type string string))
(loop
for i = 0 then (1+ j)
as j = (position #\newline string start i)
collect (subseq string i j)
while j))
;; :COURTESY PJB :SOURCE (URL `http://paste.lisp.org/display/120998')
(defun string-split-on-column (string column)
;; (last-elt (string-split-on-column (format nil "Hello World~%How do yo do?~%Comment ça va?~%") 8))
(declare (type string string)
(type index column))
(mapcan (lambda (line)
(loop
with i = 0
while (< (+ i column) (length line))
collect (subseq line i (min (length line) (incf i column))) into result
finally (return (nconc result (list (subseq line i))))))
;; :WAS (split-sequence:split-sequence #\newline string)
(string-split-newline string)))
;; :SOURCE clocc/src/string.lisp
(defun string-split-multi (str chars &rest opts)
(declare (type string str)
(type sequence chars))
;; (apply #'mon::split-seq str #'(lambda (ch)
(apply #'split-seq str #'(lambda (ch)
(declare (character ch))
(find ch chars))
opts))
(defun substring (string-seq from &optional (to (length (the string string-seq))))
(declare (type string string-seq)
(type fixnum from to))
(subseq string-seq from to))
;;; MY version (URL `http://paste.lisp.org/+2LC4/1')
;; (defun string-insert-char (string char index)
;; (declare (type string string)
;; (type character char)
;; ;; SBCL x86-32
;; ((mod 536870910) index)
;; (optimize speed))
;; (let* ((oldlen (length string))
;; (newlen (if (zerop oldlen)
;; (return-from string-insert-char (string char))
;; (1+ oldlen)))
;; (result (make-array newlen :element-type 'character)))
;; (declare ((simple-array character (*)) result))
;; (unless (< index newlen)
;; (error "arg INDEX must be less than ~D" newlen))
;; (if (or (zerop index) (= index oldlen))
;; (loop
;; initially (setf (char result index) char)
;; for x across string
;; for y upfrom (if (zerop index) 1 0) below (if (zerop index) newlen oldlen)
;; do (setf (char result y) x)
;; finally (return result))
;; (loop
;; for n from 0 below index
;; do (setf (char result n) (char string n))
;; finally (return
;; (loop
;; initially (setf (char result (1- index)) char)
;; for o upfrom index below newlen
;; do (setf (char result o) (char string (1- o)))
;; finally (return result)))))))
;;
;;
(defun string-insert-char (string insert-char index)
(declare (type string string)
(type character insert-char)
(index index) ; :NOTE on SBCL x86-32 this is (mod 536870910)
(optimize speed))
#-:SBCL (check-type string vector)
#-:SBCL (check-type index index)
(let* ((oldlen (length string))
(newlen (if (zerop oldlen)
(return-from string-insert-char (string insert-char))
(1+ oldlen)))
(result (make-array newlen :element-type 'character)))
(declare ((simple-array character (*)) result))
(unless (< index newlen)
(error "arg INDEX must be less than ~D" newlen))
(flet ((copy (str chr idx old new rslt)
(if (or (zerop idx) (= idx old))
(loop
initially (setf (char rslt idx) chr)
for x across str
for y upfrom (if (zerop idx) 1 0) below (if (zerop idx) new old)
do (setf (char rslt y) x)
finally (return rslt))
(loop
for n from 0 below idx
do (setf (char rslt n) (char str n))
finally (return
(loop
initially (setf (char rslt (1- idx)) chr)
for o upfrom idx below new
do (setf (char rslt o) (char string (1- o)))
finally (return rslt)))))))
(declare (inline copy))
(typecase string
((simple-array character (*))
(funcall #'copy string insert-char index oldlen newlen result))
((simple-base-string)
(funcall #'copy string insert-char index oldlen newlen result))
(t (funcall #'copy string insert-char index oldlen newlen result))))))
;; 3b's version
;; :PASTE-URL (URL `http://paste.lisp.org/+2LC4/5')
;; :WAS `push-in-place2'
(defun string-insert-char-3b (string insert-char index)
(declare (string string)
(character insert-char)
;; (fixnum index)
(index index)
(optimize speed))
#-:SBCL (check-type string vector)
#-:SBCL (check-type index index)
(let ((ret (make-string (1+ (length string)))))
(flet ((copy ()
(when (plusp index)
(replace ret string :end1 index))
(setf (char ret index) insert-char)
(when (< index (length string))
(replace ret string :start1 (1+ index) :start2 index))))
(declare (inline copy))
(typecase string
((simple-array character (*))
(copy))
((simple-base-string)
(copy))
(t (copy)))
ret)))
;;; ==============================
;; pjb's version which mutates.
;; :PASTE-TITLE what a programmer can do
;; :PASTE-NUMBER 127742
;; :PASTE-BY pjb
;; :PASTE-DATE 2012-01-13
;; :PASTE-URL (URL `http://paste.lisp.org/+2QKE')
;; :WAS `nstring-insert-char'
;; ,---- #lisp 2013-02-13 @ 09:18
;; | <francogrex> (defparameter *string* "ABCD1234") What is a *most straightforward
;; | way* to insert a character in that string without reconsitituting
;; | using concatenations... is it possible?
;; | <Xach> francogrex: use an adjustable string with a fill pointer.
;; | <pjb> francogrex: it's not possible because this is a literal string => immutable.
;; | <pjb> (defparameter *string* (make-array 8 :element-type 'character :adjustable t :fill-pointer 8 :initial-contents "ABCD1234"))
;; | <francogrex> and then use vector-push?
;; | <pjb> francogrex: not to insert in the middle. adjust-array, replace and (setf aref).
;; | <francogrex> ok
;; | <pjb> francogrex: adjust-array is O(n) unless you use a geometric series of
;; | size (in which case it's amortized O(1)). replace is O(n). so insert
;; | in the middle of an adjustable string is costly. Better use concatenate.
;; | <flip214> francogrex, pjb: but see http://common-lisp.net/project/cl-rope/
;; | <pjb> Sure, if you do that a lot on big strings, ropes are better.
;; | <pjb> francogrex: actually, better use make-array and replace.
;; | <francogrex> but replace in itself, just replaces , it doesn't insert
;; | <francogrex> suppose I want *string* to become "ABCDZ1234" I see no other way that subseq and coincatenate
;; | <pjb> francogrex: http://paste.lisp.org/+2QKE
;; `----
;;
(defun nstring-insert-char (string insert-char index)
(declare (character insert-char)
(index index)
;; (string string)
)
#-:SBCL (check-type string vector)
#-:SBCL (check-type index index)
(if (and (array-has-fill-pointer-p string)
(< (fill-pointer string) (array-dimension string 0)))
(progn
(incf (fill-pointer string))
(replace string string :start1 (1+ index) :start2 index
:end2 (fill-pointer string))
(setf (aref string index) insert-char)
string)
(let ((string (adjust-array string (1+ (length string))
:element-type (array-element-type string))))
(replace string string :start1 (1+ index) :start2 index
:end2 (length string))
(setf (aref string index) insert-char)
string)))
;;; ==============================
;;; :NOTE lice/fns.lisp had this which arnesi says is slow.
;;; (apply 'concatenate 'string strings))
;;; :COURTESY arnesi/string.lisp
;;; :NOTE Issue 155 REST-LIST-ALLOCATION CLTL2 p 77-78
;;; :SEE info node (info "(ansicl)Ordinary Lambda Lists")
;;; :SEE (URL `http://www.lispworks.com/documentation/HyperSpec/Issues/iss297_w.htm')
;; ,----
;; | the value of an &REST parameter is permitted, but not required,
;; | to share (top-level) structure with the last argument to APPLY.
;; `----
;; This means we must not: (setf (do thing to strings) strings)
;; without first copying the &rest list
;; :NOTE Has regression test `mon-test:concat-TEST'
(defun concat (&rest strings)
(declare (each-a-sequence-proper-or-character strings)
(optimize (speed 3)))
(let* ((strings-cln
(if (or (null strings) (every #'null strings))
(return-from concat (make-string 0))
(string-seqs-convert-chars-if
(if (some #'null strings)
(remove-if #'null strings)
(copy-seq strings)))))
(length (reduce #'+ strings-cln :key #'length))
(result (make-string length)))
(declare ((integer 0 *) length)
((simple-array character (*)) result)
(each-a-sequence strings-cln))
(loop
for string in strings-cln
for start = 0 then end
for end = (+ start (the array-length (length string)))
while string
do (replace result string :start1 start :end1 end)
finally (return result))))
;;; ==============================
;; Initial version used a stepping iterator over each elt
;; (defun string-seqs-convert-chars-if (string-seq)
;; (declare (list string-seq))
;; (if (notany #'characterp string-seq)
;; string-seq
;; (loop
;; for char-psn in string-seq
;; for cnt from 0
;; when (characterp char-psn)
;; do (setf (elt string-seq cnt) (string char-psn))
;; finally (return string-seq))))
;;
(defun string-seqs-convert-chars-if (string-seq)
(declare (each-a-sequence-proper-or-character string-seq)
(optimize (speed 3)))
(if (notany #'characterp string-seq)
string-seq
(loop
for char-psn = (position-if #'characterp string-seq)
then (position-if #'characterp string-seq :start char-psn)
for char = (and char-psn (string (elt string-seq char-psn)))
while char
do (setf (elt string-seq char-psn) char)
finally (return string-seq))))
;;; ==============================
;;; :COURTESY PJB
;;; :SEE (URL `http://groups.google.com/group/comp.lang.lisp/browse_frm/thread/2d71b553b62e20b5#')
;;; :SEE :FILE com/informatigo/tools/script.lisp
;;; :SOURCE comp.lang.lisp :DATE 2009-06-13 :SUBJECT Re: Emacs Lisp's "mapconcat" in Common Lisp?
;; (defun mapconcat-old (map-fun sequence separator) ;; :WAS (function sequence separator)
;; (etypecase sequence
;; (list
;; (if sequence
;; (let* ((items (mapcar (lambda (item)
;; (let ((sitem (funcall map-fun item)))
;; ;; I've found an implementation issue where the function differs from
;; ;; the emacs lisp equivalent:
;; ;; elisp> (mapconcat #'identity (list "a" nil "b" nil "c") "") => "abc"
;; ;; cl-user> (mapconcat #'identity (list "a" nil "b" nil "c") "") => "aNILbNILc"
;; ;; Following `and' form fixes a bug in PJB's implementation and omits nulls
;; (and sitem (if (stringp sitem) sitem (princ-to-string sitem)))))
;; sequence))
;; (ssepa (if (stringp separator)
;; separator
;; (princ-to-string separator)))
;; (size (+ (reduce (function +) items :key (function length))
;; (* (length ssepa) (1- (length items)))))
;; (result (make-array size :element-type 'character))
;; (start 0))
;; ;; :WAS (replace result (first items) :start1 start)
;; (replace result (car items) :start1 start)
;; ;; :WAS (incf start (length (first items)))
;; (incf start (length (car items)))
;; ;; :WAS (dolist (item (rest items)))
;; (dolist (item (cdr items) result)
;; (replace result ssepa :start1 start) (incf start (length ssepa))
;; (replace result item :start1 start) (incf start (length item)))
;; ) ;;result)
;; ""))
;; (vector
;; (if (plusp (length sequence))
;; (let* ((items (map 'vector #'(lambda (item)
;; (let ((sitem (funcall map-fun item)))
;; (and sitem
;; (if (stringp sitem)
;; sitem
;; (princ-to-string sitem)))))
;; sequence))
;; (ssepa (if (stringp separator)
;; separator
;; (princ-to-string separator)))
;; (size (+ (reduce (function +) items :key (function length))
;; (* (length ssepa) (1- (length items)))))
;; (result (make-array size :element-type 'character))
;; (start 0))
;; (replace result (aref items 0) :start1 start)
;; (incf start (length (aref items 0)))
;; (loop
;; for i from 1 below (length items)
;; do (replace result ssepa :start1 start)
;; (incf start (length ssepa))
;; (replace result (aref items i) :start1 start)
;; (incf start (length (aref items i))))
;; result)
;; ""))))
;;
;; (defun test/mapconcat-old ()
;; (loop :for (expression expected)
;; :in '(((mapconcat-old (lambda (x) (and x (string-downcase x))) '("one" two three nil "five") "-")
;; "one-two-three--five")
;; ((mapconcat-old (lambda (x) (and x (string-downcase x))) '("one") "-")
;; "one")
;; ((mapconcat-old (lambda (x) (and x (string-downcase x))) '(nil) "-")
;; "")
;; ((mapconcat-old (lambda (x) (and x (string-downcase x))) '() "-")
;; "")
;; ((mapconcat-old (lambda (x) (and x (string-downcase x))) #("one" two three nil "five") "-")
;; "one-two-three--five")
;; ((mapconcat-old (lambda (x) (and x (string-downcase x))) #("one") "-")
;; "one")
;; ((mapconcat-old (lambda (x) (and x (string-downcase x))) #(nil) "-")
;; "")
;; ((mapconcat-old (lambda (x) (and x (string-downcase x))) #() "-")
;; ""))
;; :do (assert (equal (eval expression) expected)
;; ()
;; "~%Expression: ~S~%Expected: ~S~%Got: ~S~%"
;; expression expected (eval expression)))
;; :success)
;;
;; (test/mapconcat-old)
;;; ==============================
;;; :COURTESY PJB
;;; :SEE (URL `http://groups.google.com/group/comp.lang.lisp/browse_frm/thread/2d71b553b62e20b5#')
;;; :SEE :FILE com/informatigo/tools/script.lisp
;;; :SOURCE comp.lang.lisp :DATE 2009-06-13 :SUBJECT Re: Emacs Lisp's "mapconcat" in Common Lisp?
;;;;;;;
;;; New version <Timestamp: #{2011-01-29T17:46:15-05:00Z}#{11046} - by MON>
;;; :SOURCE (URL `http://paste.lisp.org/display/119172')
;;; 1- mon_key on irc://irc.freenode.org/#lisp signaled that nil are
;;; substituted by empty strings in emacs mapconcat.
;;; 2- Factorized out the processing for both vectors and lists in JOIN.
;;
(defun mapconcat (function sequence separator &key (char-code-as-integer nil))
(declare (sequence sequence)
(type (or null string character char-code-integer) separator)
(boolean char-code-as-integer)
(optimize (speed 3)))
(if (null sequence)
(make-string 0)
(labels ((convert-sep-or-item (sep-or-item initial)
(etypecase sep-or-item
(null
(make-string 0))
(string
sep-or-item)
(character
(string sep-or-item))
(char-code-integer
(if char-code-as-integer
(string (code-char sep-or-item))
(princ-to-string sep-or-item)))
(t
(if initial
(error ":function `mapconcat' -- arg SEPARATOR not a reasoanble delimeter~% got: ~S~%"
sep-or-item)
(princ-to-string sep-or-item)))))
(process-item (item)
(let ((sitem (funcall function item)))
(convert-sep-or-item sitem nil)))
(joiner (items sep sep-len)
(declare (string sep)
(mon:array-index sep-len))
(let* ((size (+ (reduce (function +) items :key (function length))
(* sep-len (1- (length items)))))
(result (make-string size :element-type 'character))
(start 0))
(declare (simple-string result)
(unsigned-byte start size))
(replace result (first items) :start1 start)
(incf start (length (first items)))
(dolist (item (rest items) result)
(replace result sep :start1 start)
(incf start sep-len)
(replace result item :start1 start)
(incf start (length item)))))
(join (items)
(let* ((ssepa (convert-sep-or-item separator t))
(ssepa-len (length ssepa)))
(declare (string ssepa)
(mon:array-index ssepa-len))
(joiner items ssepa ssepa-len))))
(etypecase sequence
(cons
(if (listp (cdr sequence))
(join (mapcar (function process-item) sequence))
(error ":FUNCTION `mapconcat' -- arg SEQUENCE is `cl:consp' with cdr not `cl:listp'~% got: ~S"
sequence)))
(vector
(if (plusp (length sequence))
(join (map 'list (function process-item) sequence))
(make-string 0)))))))
;; :SOURCE cl-data-format-validation-20101006-git/validation.lisp :WAS `join-strings'
(defun string-join-strings (strings &optional (separator #\space))
(let ((writer (etypecase separator
(character #'write-char)
(sequence #'write-sequence))))
(with-output-to-string (os)
(let ((firstp t))
(map 'nil
#'(lambda (string)
(if firstp
(setf firstp nil)
(funcall writer separator os))
(write-string string os))
strings)))))
;;; :COURTESY mcclim/Tools/gilbert/clim-doc-convert.lis p:WAS `map-over-string'
;;; :NOTE `+ucs-escape+' (defconstant +ucs-escape+ (code-char 21))
;;
;; :EXAMPLE
;; (string-map #'(lambda (x) (princ (identity x))) "foo")
;; (let ((lvar ()))
;; (string-map #'(lambda (x) (push (char-code x) lvar)) "foo")
;; (map 'list #'code-char (nreverse lvar)))
(defun string-map (function string)
(assert (typep string 'string))
(locally
(declare (type string string)
(type function function) ;; :ADDED
(optimize (speed 3) (safety 1)))
(let ((str-len (length string)))
(declare (type fixnum-exclusive str-len))
(do ((i 0 (the fixnum-exclusive (+ i 1))))
((>= i str-len))
(declare (type fixnum-exclusive i))
(let ((idx-char (schar string i)))
(cond ((char= idx-char #\Nak) ;; (char= c +ucs-escape+)
(let ((idx-char (parse-integer string
:start (+ i 1)
:end (+ i 5)
:junk-allowed nil
;; Is this the radix we want? It is if we want HEX.
:radix 16)))
(incf i 4)
(funcall function idx-char)))
(t
(funcall function idx-char))))))))
(defun string-or-char-or-code-point-integer-if (obj)
(if (typep obj 'string-or-char-or-code-point-integer)
(typecase obj
(string (copy-seq obj))
(character obj)
(char-code-integer (char-code-integer-to-char obj)))
(simple-error-mon :w-sym 'string-or-char-or-code-point-integer-if
:w-type 'function
:w-spec "Arg OBJ not of type `string-or-char-or-code-point-integer'"
:w-got obj
:w-type-of t
:signal-or-only nil)))
(defun string-symbol-or-char-or-code-point-integer-if (obj)
(if (typep obj 'string-symbol-or-char-or-code-point-integer)
(typecase obj
(string (copy-seq obj))
(symbol (string obj))
(character obj)
(char-code-integer (char-code-integer-to-char obj)))
(simple-error-mon :w-sym 'string-symbol-or-char-or-code-point-integer-if
:w-type 'function
:w-spec "Arg OBJ not of type `mon:string-symbol-or-char-or-code-point-integer'"
:w-got obj
:w-type-of t
:signal-or-only nil)))
(defun string-symbol-or-char-if (obj)
(if (typep obj 'string-symbol-or-char)
;; :WAS (typecase obj
(etypecase obj
(symbol (string obj))
(string (copy-seq obj))
(character obj))
(simple-error-mon :w-sym 'string-symbol-or-char-if
:w-type 'function
:w-spec "Arg OBJ not of type `mon:string-symbol-or-char'"
:w-got obj
:w-type-of t
:signal-or-only nil)))
;;; ==============================
(defun downcase (obj)
(let ((chk-dwn (string-symbol-or-char-if obj)))
(declare (string-symbol-or-char chk-dwn))
(typecase chk-dwn
(character (char-downcase chk-dwn))
(string (string-downcase chk-dwn)))))
(defun upcase (obj)
(let ((chk-up (string-symbol-or-char-if obj)))
(declare (string-symbol-or-char chk-up))
(typecase chk-up
(character (char-upcase chk-up))
(string (string-upcase chk-up)))))
(defun capitalize (obj)
(let ((chk-cap (string-symbol-or-char-if obj)))
(declare (string-symbol-or-char chk-cap))
(typecase chk-cap
(character (if (upper-case-p chk-cap)
chk-cap
(char-downcase chk-cap)))
(string (string-capitalize chk-cap)))))
;;; ==============================
(defun capitalize-loosely (obj)
(let ((cptlz-if (string-symbol-or-char-or-code-point-integer-if obj)))
(declare (string-or-char-or-code-point-integer cptlz-if))
(typecase cptlz-if
(string
;; `nstring-capitalize' is fine b/c `%capitalize-if' `cl:copy-seq'd CPTLZ-IF
(nstring-capitalize cptlz-if))
(character
(char-code (if (upper-case-p cptlz-if)