###
# backslash_subst
#
# Given a string
#
# perform any backslash substitutions necessary.
#
# \n     Newline (0xa).
# \a     Audible alert (bell) (0x7).
# \b     Backspace (0x8).
# \f     Form feed (0xc).
# \r     Carriage-return (0xd).
# \t     Tab (0x9).
# \v     Vertical tab (0xb).
# Anything else that's \ escaped is just itself

# XXX - octal, hex, unicode
# \ooo   
# \xhh
# \uhhhh
#
# Copyright (c) 2002-2003 by William J. Coleda

.sub __backslash_subst

  .param string buffer
  
  .local string bs
  .local int marker
  .local int pos
  .local int retval
  .local int buffer_length
  .local int next_byte
  .local string replace_str
 
  chr bs, 92
  marker=0

loop_outer:
  length buffer_length, buffer
  if marker > buffer_length goto my_end
  index pos, buffer, bs, marker
  if pos == -1 goto my_end

  marker = pos
  dec buffer_length
  if marker > buffer_length goto my_end

  inc marker
  ord next_byte, buffer, marker

  # n
  if next_byte != 110 goto bs_a
  replace_str = "\n"
  goto bs_done

bs_a:
  if next_byte != 97 goto bs_b
  chr replace_str, 7
  goto bs_done

bs_b:
  if next_byte != 98 goto bs_f
  chr replace_str, 8
  goto bs_done

bs_f:
  if next_byte != 102 goto bs_r
  chr replace_str, 12
  goto bs_done

bs_r:
  if next_byte != 114 goto bs_t
  chr replace_str, 13
  goto bs_done

bs_t:
  if next_byte != 116 goto bs_v
  chr replace_str, 9
  goto bs_done

bs_v:
  if next_byte != 118 goto bs_leftover
  chr replace_str, 11 
  goto bs_done


bs_leftover:
  chr replace_str, next_byte

bs_done:
  dec marker
  #This will have to change on more complex  items
  substr buffer,marker,2,replace_str

  inc marker
  goto loop_outer

my_end:
  .return buffer
  ret

.end
