logo

blog

My little blog can’t be this cute! git clone https://hacktivis.me/git/blog.git
commit: 006d6a5dd42a1e4554ec4bcea09608a6506fafb7
parent 3a391cbe8ada9e7f6b10a3ae2ef11fd4f0a9c44f
Author: Haelwenn (lanodan) Monnier <contact@hacktivis.me>
Date:   Mon,  6 Mar 2023 06:58:51 +0100

notes/unix-defects: cstr vs. string slices

Diffstat:

Mnotes/unix-defects.xhtml3++-
1 file changed, 2 insertions(+), 1 deletion(-)

diff --git a/notes/unix-defects.xhtml b/notes/unix-defects.xhtml @@ -13,7 +13,8 @@ <h3 id="cstr"><code>NULL</code>-Terminated strings</h3> <dd> - <dt>Slow</dt><dd>Time taken to obtain the length increases with each <em>byte</em> aka <code class="math">O(n)</code> while length prefix is constant-time aka <code class="math">O(1)</code>.</dd> + <dt>Slow to parse</dt><dd>Time taken to obtain the length increases with each <em>byte</em> aka <code class="math">O(n)</code> while length prefix is constant-time aka <code class="math">O(1)</code>.</dd> + <dt>Inefficient &amp; Unsafe string slices</dt><dd>For a slice without modifying the source, you need to copy the wanted part and terminate it with <code>NULL</code>. While for length prefix you can reuse the source string as-is via an offset (or pointer), followed by a byte length.</dd> <dt>Unsafe</dt><dd>How do you handle <code>NULL</code> being present in the middle of a string? Or <code>NULL</code> being absent?</dd> </dd>