1
0
mirror of https://github.com/janet-lang/janet synced 2024-12-25 16:00:27 +00:00

Fix error in string.replace-all

This commit is contained in:
Calvin Rose 2018-09-29 20:01:57 -04:00
parent e963672977
commit f41dab8f6c
5 changed files with 33 additions and 4 deletions

View File

@ -245,10 +245,7 @@ static void janet_deinit_block(JanetGCMemoryHeader *block) {
break;
case JANET_MEMORY_ABSTRACT:
if (h->type->gc) {
if (h->type->gc((void *)(h + 1), h->size)) {
/* finalizer failed. try again later? Panic? For now do nothing. */
;
}
janet_assert(!h->type->gc((void *)(h + 1), h->size), "finalizer failed");
}
break;
case JANET_MEMORY_FUNCENV:

View File

@ -605,6 +605,11 @@ static void kmp_deinit(struct kmp_state *state) {
free(state->lookup);
}
static void kmp_seti(struct kmp_state *state, int32_t i) {
state->i = i;
state->j = 0;
}
static int32_t kmp_next(struct kmp_state *state) {
int32_t i = state->i;
int32_t j = state->j;
@ -880,6 +885,7 @@ static int cfun_replaceall(JanetArgs args) {
janet_buffer_push_bytes(&b, s.kmp.text + lastindex, result - lastindex);
janet_buffer_push_bytes(&b, s.subst, s.substlen);
lastindex = result + s.kmp.patlen;
kmp_seti(&s.kmp, lastindex);
}
janet_buffer_push_bytes(&b, s.kmp.text + lastindex, s.kmp.textlen - lastindex);
ret = janet_string(b.data, b.count);

View File

@ -167,10 +167,12 @@ extern "C" {
* To turn of nanboxing, for debugging purposes or for certain
* architectures (Nanboxing only tested on x86 and x64), comment out
* the JANET_NANBOX define.*/
#ifndef JANET_NO_NANBOX
#define JANET_NANBOX
/* Further refines the type of nanboxing to use. */
#define JANET_NANBOX_47
#endif
/* Alignment for pointers */
#ifdef JANET_32

View File

@ -34,6 +34,8 @@
(assert (<= 1.0 2.0 3.0 3.0 4.0 5.0 6.0) "less than or equal to reals")
(assert (>= 6 5 4 4 3 2 1) "greater than or equal to integers")
(assert (>= 6.0 5.0 4.0 4.0 3.0 2.0 1.0) "greater than or equal to reals")
(assert (= 7 (% 20 13)) "modulo 1")
(assert (= -7 (% -20 13)) "modulo 2")
(assert (order< nil false true
(fiber.new (fn [x] x))
@ -60,6 +62,8 @@
(assert (not nil) "nil literal")
(assert (= 7 (| 3 4)) "bit or")
(assert (= 0 (& 3 4)) "bit and")
(assert (= 0xFF (^ 0x0F 0xF0)) "bit xor")
(assert (= 0xF0 (^ 0xFF 0x0F)) "bit xor 2")
# Set global variables to prevent some possible compiler optimizations that defeat point of the test
(var zero 0)

View File

@ -52,5 +52,25 @@
(def X1 100)
(assert (= X1 100) "X1 as symbol")
# String functions
(assert (= 3 (string.find "abc" " abcdefghijklmnop")) "string.find 1")
(assert (= nil (string.find "" "")) "string.find 2")
(assert (= 0 (string.find "A" "A")) "string.find 3")
(assert (= (string.replace "X" "." "XXX...XXX...XXX") ".XX...XXX...XXX") "string.replace 1")
(assert (= (string.replace-all "X" "." "XXX...XXX...XXX") "...............") "string.replace-all 1")
(assert (= (string.replace-all "XX" "." "XXX...XXX...XXX") ".X....X....X") "string.replace-all 2")
(assert (= (string.ascii-lower "ABCabc&^%!@:;.") "abcabc&^%!@:;.") "string.ascii-lower")
(assert (= (string.ascii-upper "ABCabc&^%!@:;.") "ABCABC&^%!@:;.") "string.ascii-lower")
(assert (= (string.reverse "") "") "string.reverse 1")
(assert (= (string.reverse "a") "a") "string.reverse 2")
(assert (= (string.reverse "abc") "cba") "string.reverse 3")
(assert (= (string.reverse "abcd") "dcba") "string.reverse 4")
(assert (= (string.join @["one" "two" "three"] ",") "one,two,three") "string.join 1")
(assert (= (string.join @["one" "two" "three"] ", ") "one, two, three") "string.join 2")
(assert (= (string.join @["one" "two" "three"]) "onetwothree") "string.join 3")
(assert (= (string.join @[] "hi") "") "string.join 4")
(assert (deep= (string.split "," "one,two,three") @["one" "two" "three"]) "string.split 1")
(assert (deep= (string.split "," "onetwothree") @["onetwothree"]) "string.split 2")
(end-suite)