Skip to content

Commit bcf7371

Browse files
committed
Add Modifying A String With blit_string as a reason til
1 parent d6e1901 commit bcf7371

File tree

2 files changed

+27
-1
lines changed

2 files changed

+27
-1
lines changed

README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ smart people at [Hashrocket](http://hashrocket.com/).
1010
For a steady stream of TILs from a variety of rocketeers, checkout
1111
[til.hashrocket.com](https://til.hashrocket.com/).
1212

13-
_729 TILs and counting..._
13+
_730 TILs and counting..._
1414

1515
---
1616

@@ -534,6 +534,7 @@ _729 TILs and counting..._
534534
- [Inline Component Styles With Reason React](reason/inline-component-styles-with-reason-react.md)
535535
- [Is This A Directory Or A File?](reason/is-this-a-directory-or-a-file.md)
536536
- [Making Things Mutable](reason/making-things-mutable.md)
537+
- [Modifying A String With blit_string](reason/modifying-a-string-with-blit-string.md)
537538
- [Multi-Argument Functions As Syntactic Sugar](reason/multi-argument-functions-as-syntactic-sugar.md)
538539
- [Pattern Match On Exceptions](reason/pattern-match-on-exceptions.md)
539540
- [Quickly Bootstrap A React App Using Reason](reason/quickly-bootstrap-a-react-app-using-reason.md)
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
# Modifying A String With blit_string
2+
3+
[ReasonML's `Bytes` module](https://reasonml.github.io/api/Bytes.html) has a
4+
function called
5+
[`blit_string`](https://reasonml.github.io/api/Bytes.html#VALblit_string).
6+
This function allows you to copy portions of a string into a destination
7+
byte sequence. It is a fairly low-level operation, so you have to provide a
8+
source string and provide an offset of that source string to start copying
9+
from. You then have to provide a properly sized byte sequence as well as the
10+
destination's starting offset and length of bytes to be copied.
11+
12+
Here is an example of how we can use `blit_string` to create a copy of the
13+
string with the first character removed.
14+
15+
```reason
16+
let remove_first_char = (str: string): string => {
17+
let copy_len = String.length(str) - 1;
18+
let dst = Bytes.create(copy_len);
19+
Bytes.blit_string(str, 1, dst, 0, copy_len);
20+
Bytes.to_string(dst);
21+
};
22+
```
23+
24+
Notice that once the byte sequence has been copied over, we then need to
25+
convert it back into a string.

0 commit comments

Comments
 (0)