Browse Source

Fix bug in dystring-append

If appending an empty string ("") the null terminator wouldn't be
removed, causing it to appear broken. Now, that condition is checked
for. I also made it get the length, then resize and strcpy, because I
think that will be faster anyways, especially on large strings.
master
Macoy Madson 11 months ago
parent
commit
720fce6d06
  1. 12
      src/DynamicArray.cake

12
src/DynamicArray.cake

@ -163,16 +163,14 @@
(assert (!= (deref str) append-str))
;; Remove the null terminator
(when (> (dynstring-strlen (deref str)) 0)
(when (or (> (dynstring-strlen (deref str)) 0)
(= 1 (dynarray-length (deref str)))) ;; Empty string
(dynarray-pop (deref str)))
;; using strcat may be faster if it is optimized. I won't worry about it for now
(each-char-in-string-const append-str current-char
;; Will resize if necessary
(dynarray-push (deref str) (deref current-char)))
(var length-before-append size_t (dynarray-length (deref str)))
(dynarray-set-length (deref str) (+ length-before-append (strlen append-str) 1))
(strcpy (+ (deref str) length-before-append) append-str)
;; Add the null terminator
(dynarray-push (deref str) 0)
(return (dynstring-strlen (deref str))))
;; dynarray-length includes the null terminator. This function removes it

Loading…
Cancel
Save