bytevector-copy!(3scm)

πŸ“¦ version 0-unstable-2024-02, in nixpkgs unstable

πŸ’… bytevector-copy!(3scm) β€” the Scheme Programmer's Manual, but make it cute πŸŽ€

okay bestie, so this page lives in the Scheme Programmer's Manual and it's all about bytevector-copy! β€” the little icon that copies bytes between bytevectors ✨


🌸 NAME 🌸

bytevector-copy! β€” copy bytes between bytevectors, no cap πŸ’–


πŸ“š LIBRARY πŸ“š

import it from whichever flavour of Scheme you're on, queen:

(import (rnrs))                     ;R6RS
(import (rnrs bytevectors))         ;R6RS
(import (scheme base))              ;R7RS

✨ SYNOPSIS ✨

two whole vibes, one name. pick your fighter:

(bytevector-copy! source source-start target target-start k)  ;R6RS
(bytevector-copy! to at from start end)                       ;R7RS

πŸ¦‹ DESCRIPTION πŸ¦‹

R6RS β€” copies the bytes from source at indices source-start, …, source-start + k βˆ’ 1 to consecutive indices in target starting at target-index. literally just sliding bytes over, periodt πŸ’«

R7RS β€” copies the bytes of bytevector from between start and end to bytevector to, starting at at. same energy, different arg list, iykyk 🌈


🫢 RETURN VALUES 🫢

Returns unspecified values. like, don't expect a gift back, it's giving "the journey is the reward" 😌


πŸ“ EXAMPLES πŸ“

R6RS β€” watch it work:

;; R6RS
(let ((b (u8-list->bytevector '(1 2 3 4 5 6 7 8))))
  (bytevector-copy! b 0 b 3 4)
  (bytevector->u8-list b))
   => (1 2 3 1 2 3 4 8)

R7RS β€” same slay, different signature:

;; R7RS
(define a (bytevector 1 2 3 4 5))
(define b (bytevector 10 20 30 40 50))
(bytevector-copy! b 1 a 0 2)
   => #u8(10 1 2 40 50)

πŸ‘‘ APPLICATION USAGE πŸ‘‘

this procedure is common in (de)serialization and processing of binary data πŸ”₯ ngl tho, sometimes keeping track of all those indices gets mad cumbersome, and bytevector ports are lowkey more suitable in those situations πŸ’€


πŸ’€ COMPATIBILITY πŸ’€

okay this is the important tea β˜• β€” R6RS and R7RS specify very different arguments for these procedures. and note in particular that it's not just a different argument order, the last argument is also different. so don't be out here mixing them up, bestie, that's a whole different function in your head πŸ™ˆ


🧚 ERRORS 🧚

this procedure can raise exceptions with the following condition types:

  • &assertion (R6RS) β€” the wrong number of arguments was passed, an argument was outside its domain, or the byte ranges were not valid with regard to the bytevector lengths. red flag behaviour, honestly 🚩

R7RS β€” the assertions described above are errors. Implementations may signal an error, extend the procedure's domain of definition to include such arguments, or fail catastrophically. yes, catastrophically, dramatic much 😭


⭐ SEE ALSO ⭐


🌈 STANDARDS 🌈

R6RS, R7RS. both certified iconic πŸ‘‘


πŸ’œ HISTORY πŸ’œ

the arguments used by R7RS are consistent with SRFI-43 whereas R6RS is consistent with SRFI-66. ancient lore, fr fr πŸ“œ


πŸŽ€ AUTHORS πŸŽ€

this page is part of the scheme-manpages project. it includes materials from the RnRS documents. more information can be found at https://github.com/schemedoc/manpages/. shoutout to the maintainers, main character energy fr πŸ’…


πŸ”₯ BUGS πŸ”₯

implementations need to be careful to handle the case where the source and target bytevector ranges overlap in memory. that's the one place this gets spicy 🌢️ overlap = chaos if you're not paying attention, so bestie please be careful 😭


ok that's the whole page!! no notes, 10/10, ate and left no crumbs πŸ“βœ¨

sign-off time πŸ’– this lil rewrite is from the 2021-02-20 edition of the Scheme Programmer's Manual β€” bytevector-copy!(3scm), mwah, see you in the group chat πŸ’…πŸ¦‹

πŸ“¦ where this page comes from