source: project/release/5/symbol-utils/trunk/symbol-name-utils.scm @ 37121

Last change on this file since 37121 was 37121, checked in by Kon Lovett, 10 months ago

rm unused

File size: 3.0 KB
Line 
1;;;; symbol-name-utils.scm  -*- Scheme -*-
2;;;; Kon Lovett, Jul '18
3
4(module symbol-name-utils
5
6(;export
7  keyword->symbol
8  keyword->uninterned-symbol
9  symbol->keyword
10  symbol-printname-details
11  symbol-printname=? symbol-printname<?
12  symbol-printname-length
13  max-symbol-printname-length)
14
15(import scheme
16  (chicken base)
17  (chicken keyword)
18  (chicken fixnum)
19  (chicken type)
20  (only type-checks check-symbol check-list))
21
22;;; Support
23
24;;
25
26(: exploded-qualified-symbol=? (string string string string --> boolean))
27;
28(define (exploded-qualified-symbol=? px sx py sy)
29  (and (string=? px py) (string=? sx sy)) )
30
31(: exploded-qualified-symbol<? (string string string string --> boolean))
32;
33(define (exploded-qualified-symbol<? px sx py sy)
34  (or
35    (and (string=? px py) (string<? sx sy))
36    (string<? px py)) )
37
38;;;
39
40;;
41
42(: keyword->symbol (symbol --> symbol))
43;
44(define (keyword->symbol kwd)
45  (string->symbol (keyword->string kwd)) )
46
47(: keyword->uninterned-symbol (symbol --> symbol))
48;
49(define (keyword->uninterned-symbol kwd)
50  (string->uninterned-symbol (keyword->string kwd)) )
51
52;;
53
54;symbol->string drops namespace qualification!
55;which means a keyword and a symbol of the same name have the same printname.
56
57(: symbol->keyword (symbol --> symbol))
58;
59(define (symbol->keyword sym)
60  (if (keyword? sym)
61    sym
62    (string->keyword (symbol->string (check-symbol 'symbol->keyword sym))) ) )
63
64;;
65
66(: *symbol-printname-details (symbol --> string string))
67;
68(define (*symbol-printname-details sym)
69  (values (symbol->string sym) (if (keyword? sym) ":" "")) )
70
71(: symbol-printname-details (symbol --> string string))
72;
73(define (symbol-printname-details sym)
74  (let-values (
75    ((s p) (*symbol-printname-details (check-symbol 'symbol-printname-details sym))))
76    ;do not expose the symbol's "raw" printname
77    (values (string-copy s) p) ) )
78
79(: symbol-printname=? (symbol symbol --> boolean))
80;
81(define (symbol-printname=? x y)
82  (let-values (
83    ((sx px) (*symbol-printname-details (check-symbol 'symbol-printname=? x)))
84    ((sy py) (*symbol-printname-details (check-symbol 'symbol-printname=? y))) )
85    (exploded-qualified-symbol=? px sx py sy) ) )
86
87(: symbol-printname<? (symbol symbol --> boolean))
88;
89(define (symbol-printname<? x y)
90  (let-values (
91    ((sx px) (*symbol-printname-details (check-symbol 'symbol-printname<? x)))
92    ((sy py) (*symbol-printname-details (check-symbol 'symbol-printname<? y))) )
93    (exploded-qualified-symbol<? px sx py sy) ) )
94
95;;
96
97(: symbol-printname-length (symbol --> fixnum))
98;
99(define (symbol-printname-length sym)
100  (let (
101    (len (string-length (symbol->string (check-symbol 'symbol-printname-length sym)))) )
102    (if (keyword? sym)
103      (fx- len 2) ;compensate for leading '###' when only a ':' is printed
104      len ) ) )
105
106(: max-symbol-printname-length ((list-of symbol) --> fixnum))
107;
108(define (max-symbol-printname-length syms)
109  (if (null? (check-list 'max-symbol-printname-length syms))
110    '()
111    (apply max (map symbol-printname-length syms)) ) )
112
113) ;module symbol-name-utils
Note: See TracBrowser for help on using the repository browser.