View Issue Details

IDProjectCategoryView StatusLast Update
0000170LDMudEfunspublic2009-10-06 04:02
ReporterlarsAssigned To 
PrioritynormalSeverityfeatureReproducibilityN/A
Status newResolutionopen 
Summary0000170: New efun: substitute_string()
DescriptionShort: new efun substitute_string()
From: Dave Setty <garpoz@locallink.net>
Date: Thu, 10 Dec 1998 02:03:04 -0500
Type: Feature
State: Unclassified
See also: f-020404

I've also got some various patches around that I'd like to see in there.
Lars, if you're interested in any of this let me know...
1) subst(), which is similar to MudOS's replace_string() which in turn
   is a more refined version of the old implode(explode()) trick. Mine
   can take two arrays or a mapping of search/replace pairs though.

--- Doomdark:
If I'm not mistaken, this is similar to 'substitute_string()'-efun
I made, which takes 2 string arrays to define the strings to replace.
It can be found from 'bugs/doomdark/strutil.c'-file from the ldmud-3.2.5
if you are interested. The extra arguments (min and max length
of the aliases to substitute) are just for optimizing the speed; also,
the first array is expeceted to be sorted to allow binary search of
the substitutions. Finally, the substitution is done by scanning the
whole string just once, and by using a static replace buffer (no mallocs
except for one used for mallocing room for the returned string, _if_
any substitutions were made). All in all, I tried to do a somewhat
optimized version which could be used for alias subsitution in command
strings; since this is called for every command players enter, it
should be made as fast as possible.
----

Suggestion by Matthew Julius:
varargs string strsubst(string, string|mapping|string*, string|string*)
  String replacement/substitution...
  Examples,
    strsubst("abc", "ab", "1") == "1c"
    strsubst("abc", ({ "a", "b" }), ({ "1", "2" })) == "12c"
    strsubst("abc", ([ "a": "1" ])) == "1bc"
    strsubst("abc", ({ "d" }), ({ "1" })) == "abc"
  The Timewarp mudlib has historically called this subst().

Or call it strreplace():

strreplace("abcdef", "cd", "x") -> "abxef"
TagsNo tags attached.
External Data (URL)

Relationships

duplicate of 0000452 new replace() efun like in Pike 

Activities

2004-11-26 22:09

 

lars

2004-11-27 01:21

reporter   ~0000224

p-990417-1: Daniel Sloan's take on this.

Short: New efun replace_string()
From: Daniel Sloan
Date: 990410
Type: Patch
State Unclassified

diff-replace : A diff for the efun 'replace_string()', which does the same as
               implode(explode(X, Y), Z);, but does it faster and without
               creating an array, of course :-)

MudOS uses a very efficient algorithm for it.

2004-11-27 01:21

 

p-990417-1.gz (1,076 bytes)

Bardioc

2004-12-11 19:49

reporter   ~0000244

Hmm, i'm not sure, but cant this be done by regreplace() in a very efficient manner too? Actually replace_string() as known from MudOS allows to replace several strings at once in a easy way. This can be done via regreplace() to, but with regular expression syntax which is not that obvious.

menaures

2004-12-12 05:51

reporter   ~0000248

We use regreplace to convert umlauts and other characters to ascii. All player input goes through this conversion. It's really fast since it's just a single call to regreplace. (A replacepattern closure gets the replacement characters out of a mapping). No need for a new EFun there. But in this scenario, the regular expression is static (and most likely cached by the driver) and replacement mapping/closure are static too, so there really isn't much to do.

Making strsubst()/substitute_string() a simul efun which uses regreplace would be much more difficult to do, because you have to both escape and (in the case of mappings) sort the input strings.

Of course you can just use regreplace directly, but that requires knowledge of regexp. It's like saying we don't need inline closures, we have lambda. ;-)

Issue History

Date Modified Username Field Change
2004-11-26 22:09 lars New Issue
2004-11-26 22:09 lars File Added: doomdark-strutil.tar.gz
2004-11-27 01:21 lars Note Added: 0000224
2004-11-27 01:21 lars File Added: p-990417-1.gz
2004-12-11 19:49 Bardioc Note Added: 0000244
2004-12-12 05:51 menaures Note Added: 0000248
2009-10-06 04:02 zesstra Relationship added duplicate of 0000452