Re: sufreplace

Bram Moolenaar <[email protected]>
Newsgroups gmane.comp.tools.aap.devel
Message-ID <[email protected]>
Adriaan de Groot wrote:

> I see that current CVS' sufreplace requires that the source and target
> suffix begin with a ".". Why was this restriction introduced? It makes
> sufreplace() useless for my purposes [1].

The check for the "from" suffix starting with a dot is desirable,
otherwise strange things might happen.  If "from" doesn't start with a
dot this is probably a mistake by the user, e.g.:

	sufreplace("c", "o", source)

This would change "foo.roc" to "foo.roo".  But that mistake goes
unnoticed so long as you are feeding the function "*.c" files.  It
should be:

	sufreplace(".c", ".o", source)

I think it's essential that sufreplace() only replaces the suffix of a
file, not any tail that matches.  Otherwise it is too easy to make a
mistake and be puzzled about why it sometimes doesn't work.

I can understand you would want to change a suffix from ".h" to
"_skel.cc".  The question is whether sufreplace() should be used for
this, since you are not really replacing one suffix with another but
more than that.

Would a user mistakingly do:

	sufreplace(".c", "o", source)

Not very likely to make this mistake, is it?  So, perhaps we should
define sufreplace() to replace the suffix of the input into "anything".
That would work for your "fake sources", right?  And still give an error
for the most common mistake a user can make.

> FWIW, here's a version of sufreplace() (based on AAP 1.006's
> sufreplace()) that uses var2dictlist for the dirty work (like my
> earlier suffixmangle). The RE's become a little simpler, and
> attributes are preserved.

Yes, that looks better than before.  Including the check for the "from"
suffix and adding a few spaces to make it more readable.  Oh, and now
that the regexp is used several times it's a good idea to use
re.compile().  I end up with this, please test it:

-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
def sufreplace(suffrom, sufto, var):
    """
    Replace suffixes in "var" from "suffrom" to "sufto".
    When "suffrom" is empty accept any suffix.
    When "sufto" is empty the suffix is removed.
    """
    # Check that the from suffix starts with a dot.
    if suffrom and suffrom[0] != ".":
        recipe_error([], _('first sufreplace() argument must start with a dot'))

    try:
        if suffrom:
            rex = re.compile(string.replace(suffrom, ".", "\\.") + "$")
        else:
            rex = re.compile("\\.[^. \t/\\\\]+$")

        dl = var2dictlist(var)
        for n in dl:
            n["name"] = rex.sub(sufto, n["name"])
        return dictlist2str(dl)

    except StandardError, e:
        recipe_error([], _('sufreplace() failed: ') + str(e))
        return None     # not reached
-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-

> I guess the only possibility left is to use {var_OBJSUF=_skel.o} now,
> as long as src2obj() doesn't use sufreplace().

AAAarrrrggghhh!  :-)

-- 
hundred-and-one symptoms of being an internet addict:
174. You know what a listserv is.

 /// Bram Moolenaar -- [email protected] -- http://www.Moolenaar.net   \\\
///          Creator of Vim - Vi IMproved -- http://www.Vim.org          \\\
\\\              Project leader for A-A-P -- http://www.A-A-P.org        ///
 \\\  Help AIDS victims, buy here: http://ICCF-Holland.org/click1.html  ///


-------------------------------------------------------
This SF.net email is sponsored by Dice.com.
Did you know that Dice has over 25,000 tech jobs available today? From
careers in IT to Engineering to Tech Sales, Dice has tech jobs from the
best hiring companies. http://www.dice.com/index.epl?rel_code=104
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.