Advertisement
Guest User

Untitled

a guest
Nov 28th, 2014
172
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.70 KB | None | 0 0
  1. /**
  2. @hint Analogous to reReplace()/reReplaceNoCase(), except the replacement is the result of a callback, not a hard-coded string
  3. @string The string to process
  4. @regex The regular expression to match
  5. @callback A UDF which takes arguments match (substring matched), found (a struct of keys pos,len,substring, which is subexpression breakdown of the match), offset (where in the string the match was found), string (the string the match was found within)
  6. @scope Number of replacements to make: either ONE or ALL
  7. @caseSensitive Whether the regex is handled case-sensitively
  8. */
  9. string function replaceWithCallback(required string string, required string regex, required any callback, string scope="ONE", boolean caseSensitive=true){
  10. if (!isCustomFunction(callback)){ // for CF10 we could specify a type of "function", but not in CF9
  11. throw(type="Application", message="Invalid callback argument value", detail="The callback argument of the replaceWithCallback() function must itself be a function reference.");
  12. }
  13. if (!isValid("regex", scope, "(?i)ONE|ALL")){
  14. throw(type="Application", message="The scope argument of the replaceWithCallback() function has an invalid value #scope#.", detail="Allowed values are ONE, ALL.");
  15. }
  16. var startAt = 1;
  17.  
  18. while (true){ // there's multiple exit conditions in multiple places in the loop, so deal with exit conditions when appropriate rather than here
  19. if (caseSensitive){
  20. var found = reFind(regex, string, startAt, true);
  21. }else{
  22. var found = reFindNoCase(regex, string, startAt, true);
  23. }
  24. if (!found.pos[1]){ // ie: it didn't find anything
  25. break;
  26. }
  27. found.substring=[]; // as well as the usual pos and len that CF gives us, we're gonna pass the actual substrings too
  28. for (var i=1; i <= arrayLen(found.pos); i++){
  29. found.substring[i] = mid(string, found.pos[i], found.len[i]);
  30. }
  31. var match = mid(string, found.pos[1], found.len[1]);
  32. var offset = found.pos[1];
  33.  
  34. var replacement = callback(match, found, offset, string);
  35.  
  36. string = removeChars(string, found.pos[1], found.len[1]);
  37. string = insert(replacement, string, found.pos[1]-1);
  38.  
  39. if (scope=="ONE"){
  40. break;
  41. }
  42. startAt = found.pos[1] + len(replacement);
  43. }
  44. return string;
  45. }
  46.  
  47. function reverseEm(required string match, required struct found, required numeric offset, required string string){
  48. return reverse(match);
  49. }
  50.  
  51. input = "abCDefGHij";
  52. result = replaceWithCallback(input, "[a-z]{2}", reverseEm, "ALL", true);
  53. writeOutput(input & "<br>" & result & "<br><hr>");
  54.  
  55. function oddOrEven(required string match, required struct found, required numeric offset, required string string){
  56. var oddOrEven = match MOD 2 ? "odd" : "even";
  57. return match & " (#oddOrEven#)";
  58. }
  59.  
  60. input = "1, 6, 12, 17, 20";
  61. result = replaceWithCallback(input, "d+", oddOrEven, "ALL", true);
  62. writeOutput(input & "<br>" & result & "<br><hr>");
  63.  
  64.  
  65. function messWithUuid(required string match, required struct found, required numeric offset, required string string){
  66. var firstEight = reverse(found.substring[2]);
  67. var nextFour = lcase(found.substring[3]);
  68. var secondSetOfFour = "<strong>" & found.substring[4] & "</strong>";
  69. var lastBit = reReplace(found.substring[5], "d", "x", "all");
  70.  
  71. return "#firstEight#-#nextFour#-#secondSetOfFour#-#lastBit#";
  72. }
  73.  
  74. input = "#createUuid()#,XXXXXXXXX-XXXX-XXXX-XXXXXXXXXXXXXXXX,#createUuid()#";
  75. result = replaceWithCallback(input, "([0-9A-F]{8})-([0-9A-F]{4})-([0-9A-F]{4})-([0-9A-F]{16})", messWithUuid, "ALL", true);
  76. writeOutput(input & "<br>" & result & "<br><hr>");
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement