Thursday, 18 July 2013

replaceWithCallback() UDF for CFLib and suggested ER for ColdFusion

I was looking up the docs for Javascript's String replace() function the other day (because, no, I could not remember the vagaries of its syntax!). And whilst being relieved I had remembered it correctly for my immediate requirements, I also noticed it can take a callback instead of a string for its replacement argument. How cool is that?

I figured this could be handy for CFML's reReplace() function too, so decided to look at how it would work before raising the enhancement request, and in the process wrote a reasonable UDF which does the business, by way of proof of concept.

I'll submit replaceWithCallback() to CFLib, but I am wary of  self-approving stuff, so I'll get you lot to cast yer eyes over it too, by way of community driven code review.

I've put the code on, basically to encourage that site's use, but I'll replicate it here as well for the sake of completeness. Pls add any code-review-type comments on the code review site though, eh?

    @hint Analogous to reReplace()/reReplaceNoCase(), except the replacement is the result of a callback, not a hard-coded string
    @string The string to process
    @regex The regular expression to match
    @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)
    @scope Number of replacements to make: either ONE or ALL
    @caseSensitive Whether the regex is handled case-sensitively
string function replaceWithCallback(required string string, required string regex, required any callback, string scope="ONE", boolean caseSensitive=true){
    if (!isCustomFunction(callback)){ // for CF10 we could specify a type of "function", but not in CF9
        throw(type="Application", message="Invalid callback argument value", detail="The callback argument of the replaceWithCallback() function must itself be a function reference.");
    if (!isValid("regex", scope, "(?i)ONE|ALL")){
        throw(type="Application", message="The scope argument of the replaceWithCallback() function has an invalid value #scope#.", detail="Allowed values are ONE, ALL.");
    var startAt    = 1;

    while (true){    // there's multiple exit conditions in multiple places in the loop, so deal with exit conditions when appropriate rather than here
        if (caseSensitive){
            var found = reFind(regex, string, startAt, true);
            var found = reFindNoCase(regex, string, startAt, true);
        if (!found.pos[1]){ // ie: it didn't find anything
        found.substring=[];    // as well as the usual pos and len that CF gives us, we're gonna pass the actual substrings too
        for (var i=1; i <= arrayLen(found.pos); i++){
            found.substring[i] = mid(string, found.pos[i], found.len[i]);
        var match = mid(string, found.pos[1], found.len[1]);
        var offset = found.pos[1];

        var replacement = callback(match, found, offset, string);

        string = removeChars(string, found.pos[1], found.len[1]);
        string = insert(replacement, string, found.pos[1]-1);

        if (scope=="ONE"){
        startAt = found.pos[1] + len(replacement);
    return string;

Example usage:

function reverseEm(required string match, required struct found, required numeric offset, required string string){
    return reverse(match);

input = "abCDefGHij";
result = replaceWithCallback(input, "[a-z]{2}", reverseEm, "ALL", true);
writeOutput(input & "<br>" & result & "<br><hr>");

function oddOrEven(required string match, required struct found, required numeric offset, required string string){
    var oddOrEven = match MOD 2 ? "odd" : "even";
    return match & " (#oddOrEven#)";

input = "1, 6, 12, 17, 20";
result = replaceWithCallback(input, "\d+", oddOrEven, "ALL", true);
writeOutput(input & "<br>" & result & "<br><hr>");

function messWithUuid(required string match, required struct found, required numeric offset, required string string){
    var firstEight            = reverse(found.substring[2]);
    var nextFour            = lcase(found.substring[3]);
    var secondSetOfFour        = "<strong>" & found.substring[4] & "</strong>";
    var lastBit                = reReplace(found.substring[5], "\d", "x", "all");

    return "#firstEight#-#nextFour#-#secondSetOfFour#-#lastBit#";

input = "#createUuid()#,XXXXXXXXX-XXXX-XXXX-XXXXXXXXXXXXXXXX,#createUuid()#";
result = replaceWithCallback(input, "([0-9A-F]{8})-([0-9A-F]{4})-([0-9A-F]{4})-([0-9A-F]{16})", messWithUuid, "ALL", true);
writeOutput(input & "<br>" & result & "<br><hr>");



1, 6, 12, 17, 20
1 (odd), 6 (even), 12 (even), 17 (odd), 20 (even)


The enhancement for CF is 3597432; and Railo: RAILO-3138.

I look forward to your scrutiny.