mirror of
https://git.savannah.gnu.org/git/guile.git
synced 2025-06-24 20:30:28 +02:00
Extend bytevector-fill! to handle a partial fill
* libguile/bytevectors.c (bytevector-fill!): As stated. (scm_bytevector_fill_x): Stub to avoid changing the C API. * doc/ref/api-data.texi: Documentation. * libguile/vectors.c (vector-fill!): Less confusing variable names. * test-suite/tests/bytevectors.test: Test partial fill cases for bytevector-fill!.
This commit is contained in:
parent
926f70f9b5
commit
9a62f7caca
4 changed files with 63 additions and 28 deletions
|
@ -565,32 +565,46 @@ SCM_DEFINE (scm_bytevector_eq_p, "bytevector=?", 2, 0, 0,
|
|||
}
|
||||
#undef FUNC_NAME
|
||||
|
||||
SCM_DEFINE (scm_bytevector_fill_x, "bytevector-fill!", 2, 0, 0,
|
||||
(SCM bv, SCM fill),
|
||||
"Fill bytevector @var{bv} with @var{fill}, a byte.")
|
||||
#define FUNC_NAME s_scm_bytevector_fill_x
|
||||
{
|
||||
size_t c_len, i;
|
||||
uint8_t *c_bv, c_fill;
|
||||
int value;
|
||||
static SCM scm_bytevector_fill_partial_x (SCM bv, SCM fill, SCM start, SCM end);
|
||||
|
||||
SCM_DEFINE (scm_bytevector_fill_partial_x, "bytevector-fill!", 2, 2, 0,
|
||||
(SCM bv, SCM fill, SCM start, SCM end),
|
||||
"Fill positions [@var{start} ... @var{end}) of bytevector "
|
||||
"@var{bv} with @var{fill}, a byte. @var{start} defaults to 0 "
|
||||
"and @var{end} defaults to the length of @var{bv}. "
|
||||
"The return value is unspecified.")
|
||||
#define FUNC_NAME s_scm_bytevector_fill_partial_x
|
||||
{
|
||||
SCM_VALIDATE_MUTABLE_BYTEVECTOR (1, bv);
|
||||
|
||||
value = scm_to_int (fill);
|
||||
int value = scm_to_int (fill);
|
||||
if (SCM_UNLIKELY ((value < -128) || (value > 255)))
|
||||
scm_out_of_range (FUNC_NAME, fill);
|
||||
c_fill = (uint8_t) value;
|
||||
|
||||
size_t i = 0;
|
||||
size_t c_end = SCM_BYTEVECTOR_LENGTH (bv);
|
||||
uint8_t *c_bv = (uint8_t *) SCM_BYTEVECTOR_CONTENTS (bv);
|
||||
|
||||
c_len = SCM_BYTEVECTOR_LENGTH (bv);
|
||||
c_bv = (uint8_t *) SCM_BYTEVECTOR_CONTENTS (bv);
|
||||
if (!SCM_UNBNDP (start))
|
||||
i = scm_to_unsigned_integer (start, 0, c_end);
|
||||
if (!SCM_UNBNDP (end))
|
||||
c_end = scm_to_unsigned_integer (end, i, c_end);
|
||||
|
||||
for (i = 0; i < c_len; i++)
|
||||
c_bv[i] = c_fill;
|
||||
memset (c_bv + i, value, c_end-i);
|
||||
|
||||
return SCM_UNSPECIFIED;
|
||||
}
|
||||
#undef FUNC_NAME
|
||||
|
||||
SCM
|
||||
scm_bytevector_fill_x (SCM bv, SCM fill)
|
||||
#define FUNC_NAME s_scm_bytevector_fill_x
|
||||
{
|
||||
return scm_bytevector_fill_partial_x (bv, fill, SCM_UNDEFINED, SCM_UNDEFINED);
|
||||
}
|
||||
#undef FUNC_NAME
|
||||
|
||||
|
||||
SCM_DEFINE (scm_bytevector_copy_x, "bytevector-copy!", 5, 0, 0,
|
||||
(SCM source, SCM source_start, SCM target, SCM target_start,
|
||||
SCM len),
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue