Next / Previous / Contents / Shipman's homepage

22. Sox._attrEscape(): Escape forbidden characters in attribute values

sox.py
# - - -   S o x . _ a t t r E s c a p e

    def _attrEscape(self, s):
        '''Escape forbidden attribute characters.

          [ if s is unicode-okay ->
              return s as unicode, with any characters that are
              keys in ESCAPE_MAP replaced by the related values
            else -> raise SoxError ]
        '''

See Section 7.4, “ESCAPE_MAP for the list of forbidden characters and their equivalents. We use recursion because it's cleaner. First, the basis case, then the recursive case.

sox.py
        #-- 1
        # [ if s is empty ->
        #     return u''
        #   else if s[0] is unicode-okay ->
        #     first  :=  s[0] as unicode
        #     rest  :=  s[1:]
        #   else -> raise SoxError ]
        if len(s) == 0:
            return u''        
        else:
            first = self._unicodify(s[0])
            rest = s[1:]

        #-- 2
        if first in ESCAPE_MAP:
            return ESCAPE_MAP[first]+self._attrEscape(rest)
        else:
            return first+self._attrEscape(rest)