浏览代码

Fix str_replace implementation

David Peter 1 年之前
父节点
当前提交
02e8faa39a
共有 2 个文件被更改,包括 2 次插入1 次删除
  1. 1 0
      examples/prelude_tests.nbt
  2. 1 1
      numbat/modules/core/strings.nbt

+ 1 - 0
examples/prelude_tests.nbt

@@ -70,6 +70,7 @@ assert(str_contains("hello world", "HELLO") == false)
 
 
 assert(str_replace("hello world", "hello", "HEY") == "HEY world")
 assert(str_replace("hello world", "hello", "HEY") == "HEY world")
 assert(str_replace("xxx", "x", "yY") == "yYyYyY")
 assert(str_replace("xxx", "x", "yY") == "yYyYyY")
+assert(str_replace("a b", " ", "   ") == "a   b")
 
 
 assert(str_repeat("xy", 3) == "xyxyxy")
 assert(str_repeat("xy", 3) == "xyxyxy")
 
 

+ 1 - 1
numbat/modules/core/strings.nbt

@@ -24,7 +24,7 @@ fn str_replace(s: String, pattern: String, replacement: String) -> String =
     then s
     then s
     else if str_contains(s, pattern)
     else if str_contains(s, pattern)
            then if str_slice(s, 0, str_length(pattern)) == pattern
            then if str_slice(s, 0, str_length(pattern)) == pattern
-               then str_replace(str_append(replacement, str_slice(s, str_length(pattern), str_length(s))), pattern, replacement)
+               then str_append(replacement, str_replace(str_slice(s, str_length(pattern), str_length(s)), pattern, replacement))
                else str_append(str_slice(s, 0, 1), str_replace(str_slice(s, 1, str_length(s)), pattern, replacement))
                else str_append(str_slice(s, 0, 1), str_replace(str_slice(s, 1, str_length(s)), pattern, replacement))
            else s
            else s