If you need the allocation there is get_substring is fine. However the pattern here seems to be get_substring (new alloc), followed by strdup (another alloc). So this is one too many. At least for key_ptr and val_ptr. For op you could use a static buffer with copy_substring.  Same for str_ptr I think, I only see it being used for the negation check.