mirror of
https://github.com/justinethier/cyclone.git
synced 2025-07-12 23:37:38 +02:00
Partial fixes to string-set!
This commit is contained in:
parent
02014322b7
commit
d584cf059e
1 changed files with 14 additions and 8 deletions
22
runtime.c
22
runtime.c
|
@ -2150,27 +2150,26 @@ fprintf(stderr, "DEBUG %s, num_cp = %d, len = %d\n", raw, string_num_cp(str), le
|
||||||
// or don't allocate if chr uses as many or fewer bytes
|
// or don't allocate if chr uses as many or fewer bytes
|
||||||
// than the codepoint it is replacing
|
// than the codepoint it is replacing
|
||||||
|
|
||||||
char *tmp = raw;
|
char *tmp = raw, *this_cp = raw;
|
||||||
char_type codepoint;
|
char_type codepoint;
|
||||||
uint32_t state = 0;
|
uint32_t state = 0;
|
||||||
int i = 0, count, start_len = 0, start_cp = 0, bytes = 0;
|
int i = 0, count, bytes = 0;
|
||||||
|
|
||||||
for (count = 0; *tmp; ++tmp){
|
for (count = 0; *tmp; ++tmp){
|
||||||
bytes++;
|
bytes++;
|
||||||
if (!Cyc_utf8_decode(&state, &codepoint, (uint8_t)*tmp)){
|
if (!Cyc_utf8_decode(&state, &codepoint, (uint8_t)*tmp)){
|
||||||
if (count < idx) {
|
if (count == idx) {
|
||||||
start_len = i;
|
|
||||||
start_cp = count;
|
|
||||||
} else if (count == idx) {
|
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
this_cp = tmp + 1;
|
||||||
count += 1;
|
count += 1;
|
||||||
bytes = 0;
|
bytes = 0;
|
||||||
}
|
}
|
||||||
i++;
|
i++;
|
||||||
}
|
}
|
||||||
if (state != CYC_UTF8_ACCEPT)
|
if (state != CYC_UTF8_ACCEPT) {
|
||||||
Cyc_rt_raise2(data, "string-set! - invalid character at index", k);
|
Cyc_rt_raise2(data, "string-set! - invalid character at index", k);
|
||||||
|
}
|
||||||
|
|
||||||
// TODO: perform actual mutation
|
// TODO: perform actual mutation
|
||||||
//
|
//
|
||||||
|
@ -2181,11 +2180,18 @@ fprintf(stderr, "DEBUG %s, num_cp = %d, len = %d\n", raw, string_num_cp(str), le
|
||||||
//
|
//
|
||||||
// 3 cases:
|
// 3 cases:
|
||||||
// - buf_len = bytes, just straight replace
|
// - buf_len = bytes, just straight replace
|
||||||
|
if (buf_len == bytes) {
|
||||||
|
for (i = 0; i < buf_len; i++) {
|
||||||
|
this_cp[i] = buf[i];
|
||||||
|
}
|
||||||
|
}
|
||||||
// - buf_len > bytes, will need to allocate more memory (!!)
|
// - buf_len > bytes, will need to allocate more memory (!!)
|
||||||
// - buf_len < bytes, just replace, but pad with NULL chars.
|
// - buf_len < bytes, just replace, but pad with NULL chars.
|
||||||
// in this case need to ensure string_len is not
|
// in this case need to ensure string_len is not
|
||||||
// reduced because original value still matters for GC purposes
|
// reduced because original value still matters for GC purposes
|
||||||
|
else {
|
||||||
|
Cyc_rt_raise2(data, "string-set! - unable to modify character", chr);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
return str;
|
return str;
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Reference in a new issue