diff --git a/README.md b/README.md index afa9e3b..1daf1ad 100644 --- a/README.md +++ b/README.md @@ -10,7 +10,7 @@ smart people at [Hashrocket](http://hashrocket.com/). For a steady stream of TILs from a variety of rocketeers, checkout [til.hashrocket.com](https://til.hashrocket.com/). -_729 TILs and counting..._ +_730 TILs and counting..._ --- @@ -534,6 +534,7 @@ _729 TILs and counting..._ - [Inline Component Styles With Reason React](reason/inline-component-styles-with-reason-react.md) - [Is This A Directory Or A File?](reason/is-this-a-directory-or-a-file.md) - [Making Things Mutable](reason/making-things-mutable.md) +- [Modifying A String With blit_string](reason/modifying-a-string-with-blit-string.md) - [Multi-Argument Functions As Syntactic Sugar](reason/multi-argument-functions-as-syntactic-sugar.md) - [Pattern Match On Exceptions](reason/pattern-match-on-exceptions.md) - [Quickly Bootstrap A React App Using Reason](reason/quickly-bootstrap-a-react-app-using-reason.md) diff --git a/reason/modifying-a-string-with-blit-string.md b/reason/modifying-a-string-with-blit-string.md new file mode 100644 index 0000000..fb98d13 --- /dev/null +++ b/reason/modifying-a-string-with-blit-string.md @@ -0,0 +1,25 @@ +# Modifying A String With blit_string + +[ReasonML's `Bytes` module](https://reasonml.github.io/api/Bytes.html) has a +function called +[`blit_string`](https://reasonml.github.io/api/Bytes.html#VALblit_string). +This function allows you to copy portions of a string into a destination +byte sequence. It is a fairly low-level operation, so you have to provide a +source string and provide an offset of that source string to start copying +from. You then have to provide a properly sized byte sequence as well as the +destination's starting offset and length of bytes to be copied. + +Here is an example of how we can use `blit_string` to create a copy of the +string with the first character removed. + +```reason +let remove_first_char = (str: string): string => { + let copy_len = String.length(str) - 1; + let dst = Bytes.create(copy_len); + Bytes.blit_string(str, 1, dst, 0, copy_len); + Bytes.to_string(dst); +}; +``` + +Notice that once the byte sequence has been copied over, we then need to +convert it back into a string.