emacs – 在elisp中寻找替换字符串中的函数

我正在寻找一个等同于replace-regexp-in-string的字符串,它只是使用文字字符串,没有正则表达式.

(replace-regexp-in-string "." "bar" "foo.buzz") => "barbarbarbarbarbarbarbar"

但我想要

(replace-in-string "." "bar" "foo.buzz") => "foobarbuzz"

我尝试过各种replace- *功能,但无法弄清楚.

编辑

作为回报,我决定对他们进行基准测试(是的,我知道所有的基准测试都是错误的,但仍然很有意思).

基准运行的输出是(时间,#个垃圾收集,GC时间):

(benchmark-run 10000
  (replace-regexp-in-string "." "bar" "foo.buzz"))

  => (0.5530160000000001 7 0.4121459999999999)

(benchmark-run 10000
  (haxe-replace-string "." "bar" "foo.buzz"))

  => (5.301392 68 3.851943000000009)

(benchmark-run 10000
  (replace-string-in-string "." "bar" "foo.buzz"))

  => (1.429293 5 0.29774799999999857)

replace-regexp-in-string与引用的regexp获胜.临时缓冲区做得非常好.

编辑2

现在汇编!必须做10x以上的迭代:

(benchmark-run 100000
  (haxe-replace-string "." "bar" "foo.buzz"))

  => (0.8736970000000001 14 0.47306700000000035)

(benchmark-run 100000
  (replace-in-string "." "bar" "foo.buzz"))

  => (1.25983 29 0.9721819999999983)

(benchmark-run 100000
  (replace-string-in-string "." "bar" "foo.buzz"))

  => (11.877136 86 3.1208540000000013)

haxe-replace-string看起来不错

最佳答案
尝试这个:

(defun replace-in-string (what with in)
  (replace-regexp-in-string (regexp-quote what) with in nil 'literal))

转载注明原文:emacs – 在elisp中寻找替换字符串中的函数 - 代码日志