November 21, 2017, 03:55:37 PM

Author Topic: [bmx] Escape unsafe characters in strings by Pineapple [ 1+ years ago ]  (Read 188 times)

Offline BlitzBot

  • Newbie
  • *
  • Posts: 0
Title : Escape unsafe characters in strings
Author : Pineapple
Posted : 1+ years ago

Description : Turns things like " into " or ~q and back again

Code :
Code: BlitzMax
  1. '   --+-----------------------------------------------------------------------------------------+--
  2. '     |   This code was originally written by Sophie Kirschner (sophiek@pineapplemachine.com)   |  
  3. '     | It is released as public domain. Please don't interpret that as liberty to claim credit |  
  4. '     |   that isn't yours, or to sell this code when it could otherwise be obtained for free   |  
  5. '     |                because that would be a really shitty thing of you to do.                |
  6. '   --+-----------------------------------------------------------------------------------------+--
  7.  
  8. superstrict
  9.  
  10.  
  11.  
  12. ' Example code
  13.  
  14. rem
  15.  
  16. framework brl.standardio
  17.  
  18. local str$ = "tab: ~t~n double quote: ~q~n single quote: '~n tilde: ~~~n backslash: "
  19.  
  20. print "test string looks like this:"
  21. print str
  22.  
  23. print "c escapes:"
  24. local cescape:stringescape = new stringescape.c()
  25. local cresult$ = cescape.escape( str )
  26. print cresult
  27. assert cescape.unescape( cresult ) = str
  28.  
  29. print "json escapes:"
  30. local jsonescape:stringescape = new stringescape.json()
  31. local jsonresult$ = jsonescape.escape( str )
  32. print jsonresult
  33. assert jsonescape.unescape( jsonresult ) = str
  34.  
  35. print "blitzmax escapes:"
  36. local bmaxescape:stringescape = new stringescape.bmax()
  37. local bmaxresult$ = bmaxescape.escape( str )
  38. print bmaxresult
  39. assert bmaxescape.unescape( bmaxresult ) = str
  40.  
  41. endrem
  42.  
  43.  
  44.  
  45. type stringescape
  46.  
  47.     field escapechar$
  48.     field escapes$[][]
  49.    
  50.     ' intialize with C escapes
  51.     method c:stringescape()
  52.         escapechar = ""
  53.         escapes = [ ..
  54.             [ "~q", "~q" ], [ "/", "/" ], ..
  55.             [ "'", "'" ], [ "?", "?" ], ..
  56.             [ chr($08), "b" ], .. ' backspace
  57.             [ chr($0C), "f" ], .. ' form feed
  58.             [ chr($0A), "n" ], .. ' newline
  59.             [ chr($0D), "r" ], .. ' carriage return
  60.             [ chr($09), "t" ], .. ' tab
  61.             [ chr($0B), "v" ], .. ' vertical tab
  62.             [ chr($07), "a" ]  .. ' alarm
  63.         ]
  64.         return self
  65.     end method
  66.    
  67.     ' initialize with javascript escapes
  68.     method js:stringescape()
  69.         escapechar = ""
  70.         escapes = [ ..
  71.             [ "~q", "~q" ], [ "'", "'" ], ..
  72.             [ chr($08), "b" ], .. ' backspace
  73.             [ chr($0C), "f" ], .. ' form feed
  74.             [ chr($0A), "n" ], .. ' newline
  75.             [ chr($0D), "r" ], .. ' carriage return
  76.             [ chr($09), "t" ]  .. ' tab
  77.         ]
  78.         return self
  79.     end method
  80.    
  81.     ' initialize with json escapes
  82.     method json:stringescape()
  83.         escapechar = ""
  84.         escapes = [ ..
  85.             [ "~q", "~q" ], ..
  86.             [ chr($08), "b" ], .. ' backspace
  87.             [ chr($0C), "f" ], .. ' form feed
  88.             [ chr($0A), "n" ], .. ' newline
  89.             [ chr($0D), "r" ], .. ' carriage return
  90.             [ chr($09), "t" ], .. ' tab
  91.             [ chr($0B), "v" ]  .. ' vertical tab
  92.         ]
  93.         return self
  94.     end method
  95.    
  96.     ' initialize with blitzmax escapes
  97.     method bmax:stringescape()
  98.         escapechar = "~~"
  99.         escapes = [ ..
  100.             [ chr($00), "0" ], .. ' null
  101.             [ chr($09), "t" ], .. ' tab
  102.             [ chr($0D), "r" ], .. ' return
  103.             [ chr($0A), "n" ], .. ' newline
  104.             [ chr($22), "q" ]  .. ' quote
  105.         ]
  106.         return self
  107.     end method
  108.    
  109.     ' initialize with regex escapes
  110.     method regex:stringescape()
  111.         escapechar = ""
  112.         escapes = [ ..
  113.             [ "/", "/" ], [ "^", "^" ], ..
  114.             [ "$", "$" ], [ ".", "." ], ..
  115.             [ "|", "|" ], [ "?", "?" ], ..
  116.             [ "*", "*" ], [ "+", "+" ], ..
  117.             [ "(", "(" ], [ ")", ")" ], ..
  118.             [ "[", "[" ], [ "]", "]" ], ..
  119.             [ "{", "{" ], [ "}", "}" ]  ..
  120.         ]
  121.         return self
  122.     end method
  123.    
  124.     ' escape unsafe characters in input string
  125.     method escape$( str$ )
  126.         local out$ = ""
  127.         for local i% = 0 until str.length
  128.             local char$ = chr( str[i] )
  129.             local needesc% = (char = escapechar)
  130.             local escchar$
  131.             if not needesc
  132.                 for local j% = 0 until escapes.length
  133.                     if char = escapes[j][0]
  134.                         needesc = true
  135.                         escchar = escapes[j][1]
  136.                         exit
  137.                     endif
  138.                 next
  139.             endif
  140.             if needesc
  141.                 out :+ escapechar
  142.                 if escchar
  143.                     out :+ escchar
  144.                 else
  145.                     out :+ char
  146.                 endif
  147.             else
  148.                 out :+ char
  149.             endif
  150.         next
  151.         return out
  152.     end method
  153.     ' get escaped string with unsafe chars not escaped
  154.     method unescape$( str$ )
  155.         local out$ = ""
  156.         for local i% = 0 until str.length
  157.             local char$ = chr( str[i] )
  158.             if char = escapechar and i-1 < str.length
  159.                 local escchar$ = str[i+1]
  160.                 local subchar$ = escchar
  161.                 for local j% = 0 until escapes.length
  162.                     if char = escapes[j][1]
  163.                         subchar = escapes[j][0]
  164.                         exit
  165.                     endif
  166.                 next
  167.                 out :+ subchar
  168.                 i :+ 1
  169.             else
  170.                 out :+ char
  171.             endif
  172.         next
  173.         return out
  174.     end method
  175.    
  176. end type


Comments :


Pineapple(Posted 1+ years ago)

 23 Feb 2015: Added a default escape constructor for regex characters.