Re: Attribute String...

Subject: Re: Attribute String...
From: Norman Walsh <norm@xxxxxxxxxxxxx>
Date: Fri, 3 Oct 1997 14:45:14 -0400
>      This might be too easy to see but could any one help me to see why
> when I print out attribute-string of a certain element using (literal
> (attribute-string "string")), it sometimes displays exactly as in source
> SGML document but sometimes it's in all-uppercase??? Any specific thing

My guess would be that some of the attributes are CDATA and some
are...something else (lists, names, nmtokens, what have you).

> that I should be aware of here??? Is there any function that could
> translate to upper/lower case of a certain string???

With apologies to Jon, the other day I was hacking at his code
From the DocBook style sheet to do case conversions.  What I've
got now is attached below.

In addition to the functions, you have to supply two lists:

(define uppercase-list
  '(#\A #\B #\C #\D #\E #\F #\G #\H #\I #\J #\K #\L #\M
    #\N #\O #\P #\Q #\R #\S #\T #\U #\V #\W #\X #\Y #\Z))

(define lowercase-list
  '(#\a #\b #\c #\d #\e #\f #\g #\h #\i #\j #\k #\l #\m
    #\n #\o #\p #\q #\r #\s #\t #\u #\v #\w #\x #\y #\z))

These two lists form the basis for a simple mapping from
uppercase to lowercase (and vice-versa) by ordinal position.  I
know that there are complex language issues that require more
than this simple approach, but I don't think I fully understand
that complexity yet.

Any characters not listed are not translated.

With the functions below,

  (case-fold-down str) 

returns "str" translated to lower case and 

  (case-fold-up str)

returns the opposite.  All the other functions just support
these two routines.

By the way, comments from scheme experts on the efficiency and
style of my code wouldn't be unwelcome.  I'm just feeling my way
about.

;; ======================================================================

(define (list-member-find element elementlist)
  ;; Finds element in elementlist and returns the index of its location.
  ;; The first element in a list has index 0.
  (let loop ((elemlist elementlist) (count 0))
    (if (null? elemlist)
	-1
	(if (equal? element (car elemlist))
	    count
	    (loop (cdr elemlist) (+ count 1))))))

(define (list-member-get elementlist count)
  ;; Returns the count'th element from elementlist.
  ;; The first element in a list has index 0.
  (let loop ((elemlist elementlist) (idx count))
    (if (null? elemlist)
	#f
	(if (= idx 0)
	    (car elemlist)
	    (loop (cdr elemlist) (- idx 1))))))

;; ======================================================================

(define (case-fold-down-char ch)
  ;; Returns the lowercase form of ch or ch if ch is not an uppercase form
  (let ((idx (list-member-find ch uppercase-list)))
    (if (> idx 0)
	(list-member-get lowercase-list idx)
	ch)))

(define (case-fold-up-char ch)
  ;; Returns the uppercase form of ch or ch if ch is not a lowercase form
  (let ((idx (list-member-find ch lowercase-list)))
    (if (> idx 0)
	(list-member-get uppercase-list idx)
	ch)))

(define (case-fold-down-charlist charlist)
  ;; Shifts all characters in charlist to lowercase
  (if (null? charlist)
      '()
      (cons (case-fold-down-char (car charlist)) 
	    (case-fold-down-charlist (cdr charlist)))))

(define (case-fold-up-charlist charlist)
  ;; Shifts all characters in charlist to uppercase
  (if (null? charlist)
      '()
      (cons (case-fold-up-char (car charlist)) 
	    (case-fold-up-charlist (cdr charlist)))))

(define (case-fold-down str)
  ;; Returns str shifted to lowercase
  (apply string (case-fold-down-charlist (string-to-list str))))

(define (case-fold-up str)
  ;; Returns str shifted to uppercase
  (apply string (case-fold-up-charlist (string-to-list str))))

;; ======================================================================



 DSSSList info and archive:  http://www.mulberrytech.com/dsssl/dssslist


Current Thread