]> git.notmuchmail.org Git - notmuch/blobdiff - json.c
debian: bump gmime dependency to 2.6.7
[notmuch] / json.c
diff --git a/json.c b/json.c
index 96141433d578b037aa9dbd2217ccb6ff40ecad04..817fc83a3de060eb954575138a7c2c50471982ea 100644 (file)
--- a/json.c
+++ b/json.c
  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  * THE SOFTWARE.
  */
+
 char *
-json_quote_str(const void *ctx, const char *str)
+json_quote_chararray(const void *ctx, const char *str, const size_t len)
 {
     const char *ptr;
     char *ptr2;
     char *out;
-    int len = 0;
-
-    if (!str)
-       return NULL;
+    size_t loop;
+    size_t required;
 
-    for (ptr = str; *ptr; len++, ptr++) {
-       if (*ptr < 32 || *ptr == '\"' || *ptr == '\\')
-           len++;
+    for (loop = 0, required = 0, ptr = str;
+        loop < len;
+        loop++, required++, ptr++) {
+       if ((unsigned char)(*ptr) < 32 || *ptr == '\"' || *ptr == '\\')
+           required++;
     }
 
-    out = talloc_array (ctx, char, len + 3);
+    /*
+     * + 3 for:
+     * - leading quotation mark,
+     * - trailing quotation mark,
+     * - trailing NULL.
+     */
+    out = talloc_array (ctx, char, required + 3);
 
     ptr = str;
     ptr2 = out;
 
     *ptr2++ = '\"';
-    while (*ptr) {
-           if (*ptr > 31 && *ptr != '\"' && *ptr != '\\') {
+    for (loop = 0; loop < len; loop++) {
+       if ((unsigned char)(*ptr) > 31 && *ptr != '\"' && *ptr != '\\') {
                *ptr2++ = *ptr++;
            } else {
                *ptr2++ = '\\';
@@ -91,3 +98,12 @@ json_quote_str(const void *ctx, const char *str)
 
     return out;
 }
+
+char *
+json_quote_str(const void *ctx, const char *str)
+{
+    if (str == NULL)
+       str = "";
+
+    return (json_quote_chararray (ctx, str, strlen (str)));
+}