1. --!movie
  2. --!encoding=utf-8
  3.  
  4. --****************************************************************************
  5. -- @file      Regular Expression Wrapper based on QtWidgets Xtra (+OOP framework) - UNTESTED CODE!!!
  6. -- @author    Valentin Schmidt
  7. -- @version   0.2
  8. --****************************************************************************
  9.  
  10. global $
  11.  
  12. ----------------------------------------
  13. -- Counts the pattern matches in string.
  14. -- Optional parameter matches (list) is filled with matches (indexed by back ref number)
  15. --
  16. -- @param {string} patt - The search pattern
  17. -- @param {string} str - The input string
  18. -- @param {list} [matches=void] - Optional, if specified, filled with matches
  19. -- @param {bool} [caseInsensitive=false] - Optional, default = false = case-sensitive
  20. -- @returns {number} Number of matches
  21. ----------------------------------------
  22. on regex_search (patt, str, matches, caseInsensitive)
  23.     if voidP(caseInsensitive) then caseInsensitive=0
  24.     rx = $.Qt.RegExp.new(patt, caseInsensitive=0)
  25.     rx.indexIn(str)
  26.     res = rx.capturedTexts()
  27.     if listP(matches) then
  28.         repeat with str in res
  29.             matches.add(str)
  30.         end repeat
  31.     end if
  32.   return res.count
  33. end
  34.  
  35. ----------------------------------------
  36. -- Replaces all pattern matches with replacement string
  37. --
  38. -- @param {string} patt - The search pattern
  39. -- @param {string} replStr - The replacement string
  40. -- @param {string} str - The input string
  41. -- @param {bool} [caseInsensitive=false] - Optional, default = false = case-sensitive
  42. -- @returns {string} The new string
  43. ----------------------------------------
  44. on regex_replace (patt, replStr, str, caseInsensitive)
  45.     if voidP(caseInsensitive) then caseInsensitive=0
  46.     rx = $.Qt.RegExp.new(patt, caseInsensitive=0)
  47.     return rx.replace(str, replStr)
  48. end
  49.  
  50. ----------------------------------------
  51. -- Replaces all pattern matches with replacement string according to "hash" (propertyList or Object)
  52. -- Usage: put regexp_replace_hash("#(\d+)#", ["1":"X","2":"Y"], "aaa#1#bbb#2#ccc") -> "aaaXbbbYccc"
  53. --
  54. -- @param {string} patt - The search pattern
  55. -- @param {propList|Object} hash
  56. -- @param {string} str - The input string
  57. -- @param {number} [num=0] - Index of back reference containing the index string
  58. -- @param {bool} [caseInsensitive=false] - Optional, default = false = case-sensitive
  59. -- @returns {string} The new string
  60. ----------------------------------------
  61. on regexp_replace_hash (patt, hash, str, num, caseInsensitive)
  62.   if voidP(num) then num=0
  63.     if voidP(caseInsensitive) then caseInsensitive=0
  64.        
  65.     rx = $.Qt.RegExp.new(patt, caseInsensitive=0)
  66.    
  67.     pos = 0
  68.     matches = []
  69.     repeat while true
  70.         rx.indexIn(str, pos)
  71.         pos = rx.pos(num)
  72.         if pos<0 then exit repeat
  73.         len = rx.matchedLength()
  74.         matches.add([pos, len])
  75.         pos = pos + len
  76.     end repeat
  77.    
  78.     res = str
  79.     cnt = count(matches)
  80.     repeat with i = cnt down to 1
  81.         pos = matches[i][1]
  82.         len = matches[i][2]
  83.         s = str.char[pos+1..pos+len]
  84.         n = hash.findPos(s)
  85.         if n>0 then
  86.             put hash[n] into char (pos+1) to (pos+len) of res
  87.         end if
  88.     end repeat
  89.     return res
  90. end
  91.  
  92. ----------------------------------------
  93. -- Replaces pattern matches in string with callback results based on match
  94. -- Usage: put regexp_replace_callback("&#([0-9]+);", str, #_ord_to_utf8, me, 1) ->
  95. --
  96. -- @param {string} patt - The search pattern
  97. -- @param {string} str - The input string
  98. -- @param {symbol} cbHandler
  99. -- @param {Object} [cbObject=_movie]
  100. -- @param {number} [num=0] - Index of argument passed to callback
  101. -- @param {bool} [caseInsensitive=false] - Optional, default = false = case-sensitive
  102. -- @returns {string} The new string
  103. ----------------------------------------
  104. on regexp_replace_callback (patt, str, cbHandler, cbObject, num, caseInsensitive)
  105.   if voidP(cbObject) then cbObject=_movie
  106.   if voidP(num) then num=0
  107.     if voidP(caseInsensitive) then caseInsensitive=0
  108.        
  109.     rx = $.Qt.RegExp.new(patt, caseInsensitive=0)
  110.    
  111.     pos = 0
  112.     matches = []
  113.     repeat while true
  114.         rx.indexIn(str, pos)
  115.         pos = rx.pos(num)
  116.         if pos<0 then exit repeat
  117.         len = rx.matchedLength()
  118.         matches.add([pos, len])
  119.         pos = pos + len
  120.     end repeat
  121.    
  122.     res = str
  123.     cnt = count(matches)
  124.     repeat with i = cnt down to 1
  125.         pos = matches[i][1]
  126.         len = matches[i][2]
  127.         s = str.char[pos+1..pos.len]
  128.         repl = _movie.call(cbHandler, [cbObject], s)
  129.         if not voidP(repl) then
  130.             put  repl into char (pos+1) to (pos+len) of res
  131.         end if
  132.     end repeat
  133.     return res
  134. end
  135.  
  136. ----------------------------------------
  137. -- Splits string based on RegExp pattern, returns list
  138. --
  139. -- @param {string} patt - The pattern used for splitting
  140. -- @param {string} str - The input string
  141. -- @param {bool} [caseInsensitive=false] - Optional, default = false = case-sensitive
  142. -- @returns {list}
  143. ----------------------------------------
  144. on regex_split (patt, str, caseInsensitive)
  145.     if voidP(caseInsensitive) then caseInsensitive=0
  146.     rx = $.Qt.RegExp.new(patt, caseInsensitive=0)
  147.     return rx.split(str)
  148. end
  149.  
  150. --**************************************
  151. -- String functions implemented via RegExp
  152. --**************************************
  153.  
  154. ----------------------------------------
  155. -- Replaces all occurences of stringToFind with stringToInsert, returns new string
  156. --
  157. -- @param {string} stringToFind
  158. -- @param {string} stringToInsert
  159. -- @param {string} str - The input string
  160. -- @param {bool} [caseInsensitive=false] - Optional, default = false = case-sensitive
  161. -- @returns {string} The new string
  162. ----------------------------------------
  163. on str_replace (stringToFind, stringToInsert, str, caseInsensitive)
  164.     stringToFind = $.Qt.RegExp.escape(stringToFind)
  165.     stringToInsert = $.Qt.RegExp.escape(stringToInsert) -- ??? only escape \1 etc?
  166.     return regex_replace (stringToFind, stringToInsert, str, caseInsensitive)
  167. end
  168.  
  169. ----------------------------------------
  170. -- Splits string based on delimter string, returns list
  171. --
  172. -- @param {string} delim - The string delimiter used for splitting
  173. -- @param {string} str - The input string
  174. -- @param {bool} [caseInsensitive=false] - Optional, default = false = case-sensitive
  175. -- @returns {list}
  176. ----------------------------------------
  177. on explode (delim, str, caseInsensitive)
  178.     delim = $.Qt.RegExp.escape(delim)
  179.     return regex_split (delim, str, caseInsensitive)
  180. end
  181.  
  182. ----------------------------------------
  183. -- Returns number of occurences of string needle in string haystack
  184. --
  185. -- @param {string} haystack
  186. -- @param {string} needle
  187. -- @param {bool} [caseInsensitive=false] - Optional, default = false = case-sensitive
  188. -- @returns {number}
  189. ----------------------------------------
  190. on substr_count (haystack, needle, caseInsensitive)
  191.     needle = $.Qt.RegExp.escape(needle)
  192.     return regex_search (needle, haystack, void, caseInsensitive)
  193. end
  194.  
  195. ----------------------------------------
  196. -- lower to upper case
  197. ----------------------------------------
  198. on strtoupper (str)
  199.     strFrom = "abcdefghijklmnopqrstuvwxyzäöü"
  200.     strTo = "ABCDEFGHIJKLMNOPQRSTUVWXYZÄÖÜ"
  201.     patt = ""
  202.     hash = [:]
  203.     repeat with i = 1 to 29
  204.         hash[strFrom.char[i]] = strTo.char[i]
  205.         put strFrom.char[i]&"|" after patt
  206.     end repeat
  207.     delete the last char of patt
  208.   return regexp_replace_hash (patt, hash, str)
  209. end
  210.  
  211. ----------------------------------------
  212. -- upper to lower case
  213. ----------------------------------------
  214. on strtolower (str)
  215.     strFrom = "ABCDEFGHIJKLMNOPQRSTUVWXYZÄÖÜ"
  216.     strTo = "abcdefghijklmnopqrstuvwxyzäöü"
  217.     patt = ""
  218.     hash = [:]
  219.     repeat with i = 1 to 29
  220.         hash[strFrom.char[i]] = strTo.char[i]
  221.         put strFrom.char[i]&"|" after patt
  222.     end repeat
  223.     delete the last char of patt
  224.   return regexp_replace_hash (patt, hash, str)
  225. end
  226.  
[raw code]