summaryrefslogtreecommitdiffstats
path: root/src/util/staticstr.h
diff options
context:
space:
mode:
authorDaniel Baumann <daniel.baumann@progress-linux.org>2024-05-04 12:47:08 +0000
committerDaniel Baumann <daniel.baumann@progress-linux.org>2024-05-04 12:47:08 +0000
commit29b5ab554790bb57337a3b6ab9dcd963cf69d22e (patch)
treebe1456d2bc6c1fb078695fad7bc8f6b212062d3c /src/util/staticstr.h
parentInitial commit. (diff)
downloadlibgit2-29b5ab554790bb57337a3b6ab9dcd963cf69d22e.tar.xz
libgit2-29b5ab554790bb57337a3b6ab9dcd963cf69d22e.zip
Adding upstream version 1.7.2+ds.upstream/1.7.2+ds
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'src/util/staticstr.h')
-rw-r--r--src/util/staticstr.h66
1 files changed, 66 insertions, 0 deletions
diff --git a/src/util/staticstr.h b/src/util/staticstr.h
new file mode 100644
index 0000000..b7d0790
--- /dev/null
+++ b/src/util/staticstr.h
@@ -0,0 +1,66 @@
+/*
+ * Copyright (C) the libgit2 contributors. All rights reserved.
+ *
+ * This file is part of libgit2, distributed under the GNU GPL v2 with
+ * a Linking Exception. For full terms see the included COPYING file.
+ */
+#ifndef INCLUDE_stackstr_h__
+#define INCLUDE_stackstr_h__
+
+#include "git2_util.h"
+
+typedef struct {
+ /* Length of / number of bytes used by `data`. */
+ size_t len;
+
+ /* Size of the allocated `data` buffer. */
+ size_t size;
+
+ /* The actual string buffer data. */
+ char data[GIT_FLEX_ARRAY];
+} git_staticstr;
+
+#define git_staticstr_with_size(__size) \
+ struct { \
+ size_t len; \
+ size_t size; \
+ char data[__size]; \
+ }
+
+#define git_staticstr_init(__str, __size) \
+ do { \
+ (__str)->len = 0; \
+ (__str)->size = __size; \
+ (__str)->data[0] = '\0'; \
+ } while(0)
+
+#define git_staticstr_offset(__str) \
+ ((__str)->data + (__str)->len)
+
+#define git_staticstr_remain(__str) \
+ ((__str)->len > (__str)->size ? 0 : ((__str)->size - (__str)->len))
+
+#define git_staticstr_increase(__str, __len) \
+ do { ((__str)->len += __len); } while(0)
+
+#define git_staticstr_consume_bytes(__str, __len) \
+ do { git_staticstr_consume(__str, (__str)->data + __len); } while(0)
+
+#define git_staticstr_consume(__str, __end) \
+ do { \
+ if (__end > (__str)->data && \
+ __end <= (__str)->data + (__str)->len) { \
+ size_t __consumed = __end - (__str)->data; \
+ memmove((__str)->data, __end, (__str)->len - __consumed); \
+ (__str)->len -= __consumed; \
+ (__str)->data[(__str)->len] = '\0'; \
+ } \
+ } while(0)
+
+#define git_staticstr_clear(__str) \
+ do { \
+ (__str)->len = 0; \
+ (__str)->data[0] = 0; \
+ } while(0)
+
+#endif