summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorEmiel Bruijntjes <emiel.bruijntjes@copernica.com>2014-03-15 22:18:44 +0100
committerEmiel Bruijntjes <emiel.bruijntjes@copernica.com>2014-03-15 22:18:44 +0100
commita471f0f0c4c09c8f83197568af7becfbf28f2e37 (patch)
tree009a321b276f219cdd6a899b2df1cab874a032a9
parent895f6315eb306a677bddba86980de8f8ee824efe (diff)
renamed reserve to resize
-rw-r--r--include/value.h12
-rw-r--r--src/value.cpp22
2 files changed, 22 insertions, 12 deletions
diff --git a/include/value.h b/include/value.h
index ed0367c..8fea5a6 100644
--- a/include/value.h
+++ b/include/value.h
@@ -379,18 +379,18 @@ public:
* variables - other variables return nullptr.
*
* If you are going to write to the buffer, make sure that you first call
- * the reserve() method to ensure that the buffer is big enough.
+ * the resize() method to ensure that the buffer is big enough.
*
* @return char *
*/
char *buffer() const;
/**
- * Reserve enough space in the buffer. If you want to write directly to
- * the buffer (which is returned by the buffer() method), you should first
- * reserve enough space in it. This can be done with this reserve() method.
- * This will also turn the Value object into a string (if it was not
- * already a string). The writable buffer is returned.
+ * Resize buffer space. If you want to write directly to the buffer (which
+ * is returned by the buffer() method), you should first reserve enough
+ * space in it. This can be done with this resize() method. This will also
+ * turn the Value object into a string (if it was not already a string).
+ * The writable buffer is returned.
*
* @param size
* @return char*
diff --git a/src/value.cpp b/src/value.cpp
index 7787538..1579341 100644
--- a/src/value.cpp
+++ b/src/value.cpp
@@ -1449,14 +1449,24 @@ char *Value::reserve(size_t size)
// must be a string
setType(Type::String);
- // leap ouf it the size if already big enough
- if (Z_STRLEN_P(_val) >= (int)size) return Z_STRVAL_P(_val);
+ // is the current buffer too small?
+ if (Z_STRLEN_P(_val) < (int)size)
+ {
+ // is there already a buffer?
+ if (!Z_STRVAL_P(_val)) Z_STRVAL_P(_val) = (char *)emalloc(size+1);
+
+ // reallocate an existing buffer
+ else Z_STRVAL_P(_val) = (char *)erealloc(Z_STRVAL_P(_val), size+1);
+
+ // last byte should be zero
+ Z_STRVAL_P(_val)[size] = 0;
+ }
- // is there already a buffer?
- if (!Z_STRVAL_P(_val)) return Z_STRVAL_P(_val) = (char *)emalloc(size);
+ // store size
+ Z_STRLEN_P(_val) = size;
- // reallocate an existing buffer
- return Z_STRVAL_P(_val) = (char *)erealloc(Z_STRVAL_P(_val), size);
+ // done
+ return Z_STRVAL_P(_val);
}
/**