logo

blog

My website can't be that messy, right? git clone https://hacktivis.me/git/blog.git
commit: dc2ce561190c97d7cc77d7c74f26884877878fa0
parent ac907b005a91168c9cab426b06f213a461773fe7
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>