Many languages have powerful and useful (binary safe) string manipulation functions, while others don't, making it harder for these languages to accomplish some tasks.

This task is about creating functions to handle binary strings (strings made of arbitrary bytes, i.e. byte strings according to Wikipedia) for those languages that don't have built-in support for them.

If your language of choice does have this built-in support, show a possible alternative implementation for the functions or abilities already provided by the language.

In particular the functions you need to create are:

 String creation and destruction (when needed and if there's no garbage collection or similar mechanism)
 String assignment
 String comparison
 String cloning and copying
 Check if a string is empty
 Append a byte to a string
 Extract a substring from a string
 Replace every occurrence of a byte (or a string) in a string with another string
 Join strings


Possible contexts of use: compression algorithms (like LZW compression), L-systems (manipulation of symbols), many more.




 
#lang racket
 
;; Byte strings can be created either by a function (b1)
;; or as a literal string (b2). No operation is needed for
;; destruction due to garbage collection.
 
(define b1 (make-bytes 5 65))  ; b1 -> #"AAAAA"
(define b2 #"BBBBB")           ; b2 -> #"BBBBB"
 
;; String assignment. Note that b2 cannot be
;; mutated since literal byte strings are immutable.
 
(bytes-set! b1 0 66)           ; b1 -> #"BAAAA"
 
;; Comparison. Less than & greater than are
;; lexicographic comparison.
 
(bytes=? b1 b2)                ; -> #f
(bytes<? b1 b2)                ; -> #t
(bytes>? b1 b2)                ; -> #f
 
;; Byte strings can be cloned by copying to a
;; new one or by overwriting an existing one.
 
(define b3 (bytes-copy b1))    ; b3 -> #"BAAAA"
(bytes-copy! b1 0 b2)          ; b1 -> #"BBBBB"
 
;; Byte strings can be appended to one another. A
;; single byte is appended as a length 1 string.
 
(bytes-append b1 b2)           ; -> #"BBBBBBBBBB"
(bytes-append b3 #"B")         ; -> #"BAAAAB"
 
;; Substring
 
(subbytes b3 0)                ; -> #"BAAAA"
(subbytes b3 0 2)              ; -> #"BA"
 
;; Regular expressions can be used to do replacements
;; in a byte string (or ordinary strings)
 
(regexp-replace #"B" b1 #"A")  ; -> #"ABBBB" (only the first one)
(regexp-replace* #"B" b1 #"A") ; -> #"AAAAA"
 
;; Joining strings
 
(bytes-join (list b2 b3) #" ") ; -> #"BBBBB BAAAA"
 