string-copy(3scm)

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

πŸ’– string-copy β€” copy a string πŸ’–

okay so real talk, this page lives in the Scheme Programmer's Manual (that's the 3scm energy, iykyk) and it's about the single most useful little string move ever. let's get into it bestie πŸŽ€


✨ NAME ✨

string-copy β€” copy a string. that's it. that's the whole vibe. literally just copying a string and slaying while doing it πŸ’…


πŸ¦‹ LIBRARY πŸ¦‹

here's where you import it from, depending on your whole scheme situation:

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

pick your era, bestie, they all got you 🌸


⭐ SYNOPSIS ⭐

(string-copy string)
(string-copy string start)          ;R7RS
(string-copy string start end)      ;R7RS

so you can call it with just a string, or, in R7RS, with start and optionally end. the glow-up is real πŸ‘‘


πŸ’œ DESCRIPTION πŸ’œ

it returns a newly allocated copy of the part of the given string between start and end. brand new object, fresh out the box, not sharing a vibe with the original. 🫢


🧚 IMPLEMENTATION NOTES 🧚

heads up: some implementations have a single empty string object which is returned if start = end, see make-string(3scm). so like... don't be weird about empty copies, they might all be the same little guy 😭


πŸŽ€ RETURN VALUES πŸŽ€

returns a single value which is a string. one value. a string. no cap. ✨


🌈 EXAMPLES 🌈

the classic:

(string-copy "foo")
        => "foo"

and the R7RS slice moment:

;; R7RS
(string-copy "Hello" 1)
        => "ello"

ate. no crumbs. πŸ”₯


πŸ’€ APPLICATION USAGE πŸ’€

okay so why does this even exist? this procedure makes sense in the presence of mutating string procedures like string-set!(3scm). it's commonly used to create copies of strings where there is an original copy of a string that should not be mutated by the receiver of the copy.

like β€” you're handing someone a string and you do NOT want them messing with your original. so you hand them a copy. boundaries, bestie. we love them πŸ’…


πŸ™ˆ COMPATIBILITY πŸ™ˆ

this procedure is missing in IEEE Scheme. missing! absent! not invited to the function party πŸ˜”


🚨 ERRORS 🚨

this procedure can raise exceptions with the following condition types:

  • &assertion (R6RS) β€” the wrong number of arguments was passed or an argument was outside its domain.
  • 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.
ngl that "fail catastrophically" line is highkey sending me πŸ’€ like okay drama queen, pop off

πŸ“ SEE ALSO πŸ“

go read those next, they're the friends of this function fr fr 🫢


🌸 STANDARDS 🌸

R4RS, R5RS, R6RS, R7RS. all four. certified. periodt πŸ’–


⭐ HISTORY ⭐

the string-copy procedure first appeared in R2RS. a true original. main character energy from day one πŸ‘‘


πŸ’œ 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/.


and that's the whole page bestie πŸŽ€ this cutie was dated 2022-04-05 and it's brought to you by the scheme-manpages gang β€” go forth and copy those strings, but like, responsibly πŸ’…βœ¨

πŸ“¦ where this page comes from