Check-in [0e61d1648b]
Not logged in

Many hyperlinks are disabled.
Use anonymous login to enable hyperlinks.

Overview
Comment:added print-stack-trace and read-string to thunder-utils.sls
Downloads: Tarball | ZIP archive | SQL archive
Timelines: family | ancestors | descendants | both | trunk
Files: files | file ages | folders
SHA1: 0e61d1648b52ed5bb6c9f7143393075aba430a34
User & Date: aldo 2017-01-11 17:46:07
Original Comment: added print-stack-trace
Context
2017-01-11
17:49
small fix to print-stack-trace check-in: 927509fadb user: aldo tags: trunk
17:46
added print-stack-trace and read-string to thunder-utils.sls check-in: 0e61d1648b user: aldo tags: trunk
16:37
added srfi 113 sets&bags, srfi 128 comparators check-in: 07be5132aa user: aldo tags: trunk
Changes
Hide Diffs Unified Diffs Ignore Whitespace Patch

Changes to thunder-utils.sls.

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

;; Unless required by applicable law or agreed to in writing, software
;; distributed under the License is distributed on an "AS IS" BASIS,
;; WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
;; See the License for the specific language governing permissions and
;; limitations under the License.

(library (thunder-utils)
	 (export string-split string-replace bytevector-copy*) 

	 (import (scheme) (srfi s14 char-sets))

	 ;; POSSIBLE THAT NOT EXISTS THIS FUNCTION???
	 ;; s is a string , c is a character-set
	 ;; null strings are discarded from result by default unless #f is specified as third argument
	 (define string-split
	   (case-lambda
	    [(s c)
	     (string-split s c #t)]
	    [(s c discard-null?)
	     (define res '())
	     (let loop ([l (string->list s)] [t '()])
	       (if (null? l) 
		   (if (and (null? t) discard-null?)
		       res (append res (list (list->string t))))
		 (if (char-set-contains? c (car l))
		     (begin 
		       (unless (and (null? t) discard-null?)
			       (set! res (append res (list (list->string t)))))
		       (loop (cdr l) '()))
		     (loop (cdr l) (append t (list (car l)))))))]))
	    
	 ;; POSSIBLE THAT THIS NOT EXIST?
	 ;; if x is a character: (eqv?  s[i] x) => s[i] = y
	 ;; if x is a list:      (memq s[i] x) => s[i] = y

	 (define (string-replace s x y)
	   (list->string  
	    (let ([cmp (if (list? x) memq eqv?)])
	      (map (lambda (z) (if (cmp z x) y z)) (string->list s)))))

	 ;; WHY THERE NOT EXISTS BYTEVECTOR-COPY WITH src-start and n? F*** YOU
	 (define bytevector-copy*
	   (case-lambda
	    [(bv) (bytevector-copy bv)]
	    [(bv start)
	     (bytevector-copy* start (- (bytevector-length bv) start))]
	    [(bv start n)
	     (let ([dst (make-bytevector n)])
	       (bytevector-copy! bv start dst 0 n) dst)]))






































);library








|
>
|

|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|

|
|
|
|

|
|
|
|
|
|
|
|
|

>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
|
>
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
;; Unless required by applicable law or agreed to in writing, software
;; distributed under the License is distributed on an "AS IS" BASIS,
;; WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
;; See the License for the specific language governing permissions and
;; limitations under the License.

(library (thunder-utils)
  (export string-split string-replace bytevector-copy* read-string
	  print-stack-trace) 
  (import (scheme) (srfi s14 char-sets))

  ;; POSSIBLE THAT NOT EXISTS THIS FUNCTION???
  ;; s is a string , c is a character-set
  ;; null strings are discarded from result by default unless #f is specified as third argument
  (define string-split
    (case-lambda
     [(s c)
      (string-split s c #t)]
     [(s c discard-null?)
      (define res '())
      (let loop ([l (string->list s)] [t '()])
	(if (null? l) 
	    (if (and (null? t) discard-null?)
		res (append res (list (list->string t))))
	    (if (char-set-contains? c (car l))
		(begin 
		  (unless (and (null? t) discard-null?)
			  (set! res (append res (list (list->string t)))))
		  (loop (cdr l) '()))
		(loop (cdr l) (append t (list (car l)))))))]))
  
  ;; POSSIBLE THAT THIS NOT EXIST?
  ;; if x is a character: (eqv?  s[i] x) => s[i] = y
  ;; if x is a list:      (memq s[i] x) => s[i] = y

  (define (string-replace s x y)
    (list->string  
     (let ([cmp (if (list? x) memq eqv?)])
       (map (lambda (z) (if (cmp z x) y z)) (string->list s)))))

  ;; WHY THERE NOT EXISTS BYTEVECTOR-COPY WITH src-start and n? F*** YOU
  (define bytevector-copy*
    (case-lambda
     [(bv) (bytevector-copy bv)]
     [(bv start)
      (bytevector-copy* start (- (bytevector-length bv) start))]
     [(bv start n)
      (let ([dst (make-bytevector n)])
	(bytevector-copy! bv start dst 0 n) dst)]))

  (define read-string
    (case-lambda
     [() (read-string #f)]
     [(n) (read-string n (current-input-port))]
     [(n port)
      (if n
	  (get-string-n port n)
	  (get-string-all port))]))

  (define (print-stack-trace depth)
    (printf "stack-trace:\n")
    (call/cc 
     (lambda (k)
       (let loop ((cur (inspect/object k))
		  (i 0))
	 (if (and (< i depth)
		  (> (cur 'depth) 1))
	     (let* ([name (cond [((cur 'code) 'name) => (lambda (x) x)]
				[else "*"])]
		    [source ((cur 'code) 'source)]
		    [source-txt (if source
				    (let ([ss (with-output-to-string
						(lambda ()
						  (source 'write (current-output-port))))])
					  (if (> (string-length ss) 50)
					      (string-truncate! ss 50)
					      ss))
				    "*")])
	       (call-with-values
		   (lambda () (cur 'source-path))
		 (case-lambda
		  [() (printf "[no source] [~a]: ~a\n" name source-txt)]
		  [(fn bfp) (printf "~a char ~a [~a]: ~a\n" fn bfp name source-txt)]
		  [(fn line char) (printf "~a:~a:~a [~a]: ~a\n" fn line char name source-txt)]))
	       (loop (cur 'link) (+ i 1)))))))
    (printf "stack-trace end.\n"))

  );library