1. --!movie
  2. --!encoding=utf-8
  3.  
  4. --****************************************************************************
  5. -- @file      Regular Expression Wrapper based on PRegEx Xtra
  6. -- @author    Valentin Schmidt
  7. -- @version   0.2
  8. --****************************************************************************
  9.  
  10. ----------------------------------------
  11. -- Counts the pattern matches in string.
  12. -- Optional parameter matches (list) is filled with matches (indexed by back ref number)
  13. --
  14. -- @param {string} patt - The search pattern
  15. -- @param {string} str - The input string
  16. -- @param {list} [matches=void] - Optional, if specified, filled with matches
  17. -- @param {bool} [caseInsensitive=false] - Optional, default = false = case-sensitive
  18. -- @returns {number} Number of matches
  19. ----------------------------------------
  20. on regex_search (patt, str, matches, caseInsensitive)
  21.   opts = "g"
  22.   if caseInsensitive then put "i" after opts
  23.   ret = PRegEx_Search([str], patt, opts)
  24.   if listP(matches) then
  25.     matches.add(PRegEx_GetMatchString())
  26.     n = PRegEx_GetMatchBRCount()
  27.     repeat with i = 1 to n
  28.       matches.add(PRegEx_GetMatchString(i))
  29.     end repeat
  30.   end if
  31.   return ret
  32. end
  33.  
  34. ----------------------------------------
  35. -- Replaces all pattern matches with replacement string
  36. --
  37. -- @param {string} findPatt - The search pattern
  38. -- @param {string} replStr - The replacement string
  39. -- @param {string} str - The input string
  40. -- @param {bool} [caseInsensitive=false] - Optional, default = false = case-sensitive
  41. -- @returns {string} The new string
  42. ----------------------------------------
  43. on regex_replace (findPatt, replStr, str, caseInsensitive)
  44.   opts = "g"
  45.   if caseInsensitive then put "i" after opts
  46.   tList = [str]
  47.   PRegEx_Replace(tList, findPatt, opts, replPatt)
  48.   return tList[1]
  49. end
  50.  
  51. ----------------------------------------
  52. -- Replaces all pattern matches with replacement string according to "hash" (propertyList or Object)
  53. -- Usage: put regexp_replace_hash("#(\d+)#", ["1":"X","2":"Y"], "aaa#1#bbb#2#ccc") -> "aaaXbbbYccc"
  54. --
  55. -- @param {string} patt - The search pattern
  56. -- @param {propList|Object} hash
  57. -- @param {string} str - The input string
  58. -- @param {number} [num=0] - Index of back reference containing the index string
  59. -- @param {bool} [caseInsensitive=false] - Optional, default = false = case-sensitive
  60. -- @returns {string} The new string
  61. ----------------------------------------
  62. on regexp_replace_hash (patt, hash, str, num, caseInsensitive)
  63.   if voidP(num) then num=0
  64.   opts = "g"
  65.   if caseInsensitive then put "i" after opts
  66.   tList = [str]
  67.   PRegEx_ReplaceExec(tList, patt, opts, #_replace_hash, [hash, num])
  68.   return tList[1]
  69. end
  70.  
  71. -- replace_multi callback
  72. on _replace_hash (hash, num)
  73.   s = PRegEx_GetMatchString(num)
  74.   n = hash.findPos(s)
  75.   if n>0 then return hash[n]
  76.   else return s
  77. end
  78.  
  79. ----------------------------------------
  80. -- Replaces pattern matches in string with callback results based on match
  81. -- Usage: put regexp_replace_callback("&#([0-9]+);", str, #_ord_to_utf8, me, 1) ->
  82. --
  83. -- @param {string} patt - The search pattern
  84. -- @param {string} str - The input string
  85. -- @param {symbol} cbHandler
  86. -- @param {Object} [cbObject=_movie]
  87. -- @param {number} [num=0] - Index of argument passed to callback
  88. -- @param {bool} [caseInsensitive=false] - Optional, default = false = case-sensitive
  89. -- @returns {string} The new string
  90. ----------------------------------------
  91. on regexp_replace_callback (patt, str, cbHandler, cbObject, num, caseInsensitive)
  92.   if voidP(cbObject) then cbObject=_movie
  93.   if voidP(num) then num=0
  94.   opts = "g"
  95.   if caseInsensitive then put "i" after opts
  96.   l = [str]
  97.   PRegEx_ReplaceExec(l, patt, opts, #_replace_callback, [cbHandler, cbObject, num]) -- ig
  98.   return l[1]
  99. end
  100.  
  101. -- replace callback
  102. on _replace_callback (cbHandler, cbObject, num)
  103.   s = PRegEx_GetMatchString(num)
  104.   return call(cbHandler, cbObject, s)
  105. end
  106.  
  107. ----------------------------------------
  108. -- Splits string based on RegExp pattern, returns list
  109. --
  110. -- @param {string} patt - The pattern used for splitting
  111. -- @param {string} str - The input string
  112. -- @param {bool} [caseInsensitive=false] - Optional, default = false = case-sensitive
  113. -- @returns {list}
  114. ----------------------------------------
  115. on regex_split (patt, str, caseInsensitive)
  116.   opts = "g"
  117.   if caseInsensitive then put "i" after opts
  118.   tList = [str]
  119.   return PRegEx_Split(tList, patt, opts)
  120. end
  121.  
  122. --**************************************
  123. -- String functions implemented via RegExp
  124. --**************************************
  125.  
  126. ----------------------------------------
  127. -- Replaces all occurences of stringToFind with stringToInsert, returns new string
  128. --
  129. -- @param {string} stringToFind
  130. -- @param {string} stringToInsert
  131. -- @param {string} str - The input string
  132. -- @param {bool} [caseInsensitive=false] - Optional, default = false = case-sensitive
  133. -- @returns {string} The new string
  134. ----------------------------------------
  135. on str_replace (stringToFind, stringToInsert, str, caseInsensitive)
  136.   opts = "g"
  137.   if caseInsensitive then put "i" after opts
  138.   tList = [str]
  139.   stringToFind = PRegEx_QuoteMeta(stringToFind)
  140.   stringToInsert = PRegEx_QuoteMeta(stringToInsert)
  141.   PRegEx_Replace(tList, stringToFind, opts, stringToInsert)
  142.   return tList[1]
  143. end
  144.  
  145. ----------------------------------------
  146. -- Splits string based on delimter string, returns list
  147. --
  148. -- @param {string} delim - The string delimiter used for splitting
  149. -- @param {string} str - The input string
  150. -- @param {bool} [caseInsensitive=false] - Optional, default = false = case-sensitive
  151. -- @returns {list}
  152. ----------------------------------------
  153. on explode (delim, str, caseInsensitive)
  154.   opts = "g"
  155.   if caseInsensitive then put "i" after opts
  156.   tList = [str]
  157.   return PRegEx_Split(tList, PRegEx_QuoteMeta(delim), opts)
  158. end
  159.  
  160. ----------------------------------------
  161. -- Returns number of occurences of string needle in string haystack
  162. --
  163. -- @param {string} haystack
  164. -- @param {string} needle
  165. -- @param {bool} [caseInsensitive=false] - Optional, default = false = case-sensitive
  166. -- @returns {number}
  167. ----------------------------------------
  168. on substr_count (haystack, needle, caseInsensitive)
  169.   opts = "g"
  170.   if caseInsensitive then put "i" after opts
  171.   cnt = PRegEx_Search([haystack], PRegEx_QuoteMeta(needle), opts)
  172.   if cnt>0 then return cnt
  173.   else return 0
  174. end
  175.  
  176. ----------------------------------------
  177. -- Lower to upper case (only for characters in Latin-1 (ISO 8859-1) range)
  178. --
  179. -- @param {string} str - Input string
  180. -- @returns {string} Result string
  181. ----------------------------------------
  182. on strtoupper (str)
  183.   tList = [str&""] -- thanks to Alex da Franca for this trick, otherwise unwanted call by reference would happen!
  184.   PRegEx_Translate(tList, "[a-zäöü]", "[A-ZÄÖÜ]")
  185.   return tList[1]
  186. end
  187.  
  188. ----------------------------------------
  189. -- Upper to lower case (only for characters in Latin-1 (ISO 8859-1) range)
  190. --
  191. -- @param {string} str - Input string
  192. -- @returns {string} Result string
  193. ----------------------------------------
  194. on strtolower (str)
  195.   tList = [str&""]
  196.   PRegEx_Translate(tList, "[A-ZÄÖÜ]", "[a-zäöü]")
  197.   return tList[1]
  198. end
  199.  
[raw code]